ext4: fix quota inconsistency during orphan cleanup for read-only mounts
authorzhangyi (F) <yi.zhang@huawei.com>
Thu, 24 Aug 2017 19:21:50 +0000 (15:21 -0400)
committerRaspbian kernel package updater <root@raspbian.org>
Sun, 8 Oct 2017 01:09:21 +0000 (01:09 +0000)
commit 95f1fda47c9d8738f858c3861add7bf0a36a7c0b upstream.

Quota does not get enabled for read-only mounts if filesystem
has quota feature, so that quotas cannot updated during orphan
cleanup, which will lead to quota inconsistency.

This patch turn on quotas during orphan cleanup for this case,
make sure quotas can be updated correctly.

Reported-by: Jan Kara <jack@suse.cz>
Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext4/super.c

index 8087ec79a070880b3f3b26043debb318281711a8..de0d7ac9eddc5ffee6d83a2e685cb0729303b77b 100644 (file)
@@ -2334,6 +2334,7 @@ static void ext4_orphan_cleanup(struct super_block *sb,
        unsigned int s_flags = sb->s_flags;
        int nr_orphans = 0, nr_truncates = 0;
 #ifdef CONFIG_QUOTA
+       int quota_update = 0;
        int i;
 #endif
        if (!es->s_last_orphan) {
@@ -2372,14 +2373,32 @@ static void ext4_orphan_cleanup(struct super_block *sb,
 #ifdef CONFIG_QUOTA
        /* Needed for iput() to work correctly and not trash data */
        sb->s_flags |= MS_ACTIVE;
-       /* Turn on journaled quotas so that they are updated correctly */
+
+       /*
+        * Turn on quotas which were not enabled for read-only mounts if
+        * filesystem has quota feature, so that they are updated correctly.
+        */
+       if (ext4_has_feature_quota(sb) && (s_flags & MS_RDONLY)) {
+               int ret = ext4_enable_quotas(sb);
+
+               if (!ret)
+                       quota_update = 1;
+               else
+                       ext4_msg(sb, KERN_ERR,
+                               "Cannot turn on quotas: error %d", ret);
+       }
+
+       /* Turn on journaled quotas used for old sytle */
        for (i = 0; i < EXT4_MAXQUOTAS; i++) {
                if (EXT4_SB(sb)->s_qf_names[i]) {
                        int ret = ext4_quota_on_mount(sb, i);
-                       if (ret < 0)
+
+                       if (!ret)
+                               quota_update = 1;
+                       else
                                ext4_msg(sb, KERN_ERR,
                                        "Cannot turn on journaled "
-                                       "quota: error %d", ret);
+                                       "quota: type %d: error %d", i, ret);
                }
        }
 #endif
@@ -2438,10 +2457,12 @@ static void ext4_orphan_cleanup(struct super_block *sb,
                ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
                       PLURAL(nr_truncates));
 #ifdef CONFIG_QUOTA
-       /* Turn off journaled quotas if they were enabled for orphan cleanup */
-       for (i = 0; i < EXT4_MAXQUOTAS; i++) {
-               if (EXT4_SB(sb)->s_qf_names[i] && sb_dqopt(sb)->files[i])
-                       dquot_quota_off(sb, i);
+       /* Turn off quotas if they were enabled for orphan cleanup */
+       if (quota_update) {
+               for (i = 0; i < EXT4_MAXQUOTAS; i++) {
+                       if (sb_dqopt(sb)->files[i])
+                               dquot_quota_off(sb, i);
+               }
        }
 #endif
        sb->s_flags = s_flags; /* Restore MS_RDONLY status */
@@ -5365,6 +5386,9 @@ static int ext4_enable_quotas(struct super_block *sb)
                                DQUOT_USAGE_ENABLED |
                                (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
                        if (err) {
+                               for (type--; type >= 0; type--)
+                                       dquot_quota_off(sb, type);
+
                                ext4_warning(sb,
                                        "Failed to enable quota tracking "
                                        "(type=%d, err=%d). Please run "