Fix "journal superblock changes" so that needs_recovery flag is
[ext4-patch-queue.git] / journal-superblock-changes
blob472a5b30213a5d8f2acbafb2136d7a9c62770c0e
1 ext4: journal superblock changes
3 There are a number of changes to the ext4 superblock during the mount
4 process which are done without using the journal, but instead via the
5 brute-force call to ext4_commit_super().  Concentrate these changes to
6 ext4_setup_super(), and make them using the journalling mechanism.
8 Not only is this more efficient, but it also avoids some cases where
9 the ext4 superblock's checksum was not properly set.
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
12 ---
13  fs/ext4/super.c | 47 +++++++++++++++++++++++++++--------------------
14  1 file changed, 27 insertions(+), 20 deletions(-)
16 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
17 index a673558fe5f8..67cb11f15aba 100644
18 --- a/fs/ext4/super.c
19 +++ b/fs/ext4/super.c
20 @@ -2064,9 +2064,10 @@ int ext4_seq_options_show(struct seq_file *seq, void *offset)
21  }
23  static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
24 -                           int read_only)
25 +                           unsigned long journal_devnum, int read_only)
26  {
27         struct ext4_sb_info *sbi = EXT4_SB(sb);
28 +       handle_t *handle;
29         int res = 0;
31         if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
32 @@ -2074,7 +2075,7 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
33                          "forcing read-only mode");
34                 res = MS_RDONLY;
35         }
36 -       if (read_only)
37 +       if (read_only || res)
38                 goto done;
39         if (!(sbi->s_mount_state & EXT4_VALID_FS))
40                 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
41 @@ -2095,6 +2096,15 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
42                 ext4_msg(sb, KERN_WARNING,
43                          "warning: checktime reached, "
44                          "running e2fsck is recommended");
45 +       handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
46 +       if (IS_ERR(handle))
47 +               return PTR_ERR(handle);
48 +       res = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
49 +       if (res) {
50 +       stop_journal:
51 +               ext4_journal_stop(handle);
52 +               return res;
53 +       }
54         if (!sbi->s_journal)
55                 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
56         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
57 @@ -2104,7 +2114,16 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
58         ext4_update_dynamic_rev(sb);
59         if (sbi->s_journal)
60                 ext4_set_feature_journal_needs_recovery(sb);
62 +       if (journal_devnum)
63 +               es->s_journal_dev = cpu_to_le32(journal_devnum);
64 +       if (DUMMY_ENCRYPTION_ENABLED(sbi))
65 +               ext4_set_feature_encrypt(sb);
66 +       res = ext4_handle_dirty_super(handle, sb);
67 +       if (res)
68 +               goto stop_journal;
69 +       res = ext4_journal_stop(handle);
70 +       if (res)
71 +               return res;
72         ext4_commit_super(sb, 1);
73  done:
74         if (test_opt(sb, DEBUG))
75 @@ -4048,12 +4067,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
76                 goto failed_mount_wq;
77         }
79 -       if (DUMMY_ENCRYPTION_ENABLED(sbi) && !(sb->s_flags & MS_RDONLY) &&
80 -           !ext4_has_feature_encrypt(sb)) {
81 -               ext4_set_feature_encrypt(sb);
82 -               ext4_commit_super(sb, 1);
83 -       }
85         /*
86          * Get the # of file system overhead blocks from the
87          * superblock if present.
88 @@ -4102,7 +4115,10 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
89                 goto failed_mount4;
90         }
92 -       if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
93 +       err = ext4_setup_super(sb, es, journal_devnum, sb->s_flags & MS_RDONLY);
94 +       if (err < 0)
95 +               goto failed_mount4a;
96 +       if (err)
97                 sb->s_flags |= MS_RDONLY;
99         /* determine the minimum size of new large inodes, if present */
100 @@ -4561,15 +4577,6 @@ static int ext4_load_journal(struct super_block *sb,
102         EXT4_SB(sb)->s_journal = journal;
103         ext4_clear_journal_err(sb, es);
105 -       if (!really_read_only && journal_devnum &&
106 -           journal_devnum != le32_to_cpu(es->s_journal_dev)) {
107 -               es->s_journal_dev = cpu_to_le32(journal_devnum);
109 -               /* Make sure we flush the recovery flag to disk. */
110 -               ext4_commit_super(sb, 1);
111 -       }
113         return 0;
116 @@ -5032,7 +5039,7 @@ static int ext4_remount(struct super_block *sb, int *flags, char *data)
117                         if (sbi->s_journal)
118                                 ext4_clear_journal_err(sb, es);
119                         sbi->s_mount_state = le16_to_cpu(es->s_state);
120 -                       if (!ext4_setup_super(sb, es, 0))
121 +                       if (!ext4_setup_super(sb, es, 0, 0))
122                                 sb->s_flags &= ~MS_RDONLY;
123                         if (ext4_has_feature_mmp(sb))
124                                 if (ext4_multi_mount_protect(sb,