Skip to content

Commit a9c4731

Browse files
Darrick J. Wongtytso
authored andcommitted
ext4: calculate and verify superblock checksum
Calculate and verify the superblock checksum. Since the UUID and block group number are embedded in each copy of the superblock, we need only checksum the entire block. Refactor some of the code to eliminate open-coding of the checksum update call. Signed-off-by: Darrick J. Wong <djwong@us.ibm.com> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 0441984 commit a9c4731

7 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,9 @@ struct ext4_sb_info {
12801280

12811281
/* Reference to checksum algorithm driver via cryptoapi */
12821282
struct crypto_shash *s_chksum_driver;
1283+
1284+
/* Precomputed FS UUID checksum for seeding other checksums */
1285+
__u32 s_csum_seed;
12831286
};
12841287

12851288
static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -2004,6 +2007,10 @@ extern int ext4_group_extend(struct super_block *sb,
20042007
extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
20052008

20062009
/* super.c */
2010+
extern int ext4_superblock_csum_verify(struct super_block *sb,
2011+
struct ext4_super_block *es);
2012+
extern void ext4_superblock_csum_set(struct super_block *sb,
2013+
struct ext4_super_block *es);
20072014
extern void *ext4_kvmalloc(size_t size, gfp_t flags);
20082015
extern void *ext4_kvzalloc(size_t size, gfp_t flags);
20092016
extern void ext4_kvfree(void *ptr);
@@ -2279,6 +2286,9 @@ static inline void ext4_unlock_group(struct super_block *sb,
22792286

22802287
static inline void ext4_mark_super_dirty(struct super_block *sb)
22812288
{
2289+
struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2290+
2291+
ext4_superblock_csum_set(sb, es);
22822292
if (EXT4_SB(sb)->s_journal == NULL)
22832293
sb->s_dirt =1;
22842294
}

‎fs/ext4/ext4_jbd2.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,23 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
138138
}
139139

140140
int __ext4_handle_dirty_super(const char *where, unsigned int line,
141-
handle_t *handle, struct super_block *sb)
141+
handle_t *handle, struct super_block *sb,
142+
int now)
142143
{
143144
struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
144145
int err = 0;
145146

146147
if (ext4_handle_valid(handle)) {
148+
ext4_superblock_csum_set(sb,
149+
(struct ext4_super_block *)bh->b_data);
147150
err = jbd2_journal_dirty_metadata(handle, bh);
148151
if (err)
149152
ext4_journal_abort_handle(where, line, __func__,
150153
bh, handle, err);
154+
} else if (now) {
155+
ext4_superblock_csum_set(sb,
156+
(struct ext4_super_block *)bh->b_data);
157+
mark_buffer_dirty(bh);
151158
} else
152159
sb->s_dirt = 1;
153160
return err;

‎fs/ext4/ext4_jbd2.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
213213
struct buffer_head *bh);
214214

215215
int __ext4_handle_dirty_super(const char *where, unsigned int line,
216-
handle_t *handle, struct super_block *sb);
216+
handle_t *handle, struct super_block *sb,
217+
int now);
217218

218219
#define ext4_journal_get_write_access(handle, bh) \
219220
__ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh))
@@ -225,8 +226,10 @@ int __ext4_handle_dirty_super(const char *where, unsigned int line,
225226
#define ext4_handle_dirty_metadata(handle, inode, bh) \
226227
__ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \
227228
(bh))
229+
#define ext4_handle_dirty_super_now(handle, sb) \
230+
__ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 1)
228231
#define ext4_handle_dirty_super(handle, sb) \
229-
__ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb))
232+
__ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 0)
230233

231234
handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks);
232235
int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle);

‎fs/ext4/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3936,7 +3936,7 @@ static int ext4_do_update_inode(handle_t *handle,
39363936
EXT4_SET_RO_COMPAT_FEATURE(sb,
39373937
EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
39383938
ext4_handle_sync(handle);
3939-
err = ext4_handle_dirty_super(handle, sb);
3939+
err = ext4_handle_dirty_super_now(handle, sb);
39403940
}
39413941
}
39423942
raw_inode->i_generation = cpu_to_le32(inode->i_generation);

‎fs/ext4/namei.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
20212021
/* Insert this inode at the head of the on-disk orphan list... */
20222022
NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
20232023
EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2024-
err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
2024+
err = ext4_handle_dirty_super_now(handle, sb);
20252025
rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
20262026
if (!err)
20272027
err = rc;
@@ -2094,7 +2094,7 @@ int ext4_orphan_del(handle_t *handle, struct inode *inode)
20942094
if (err)
20952095
goto out_brelse;
20962096
sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2097-
err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
2097+
err = ext4_handle_dirty_super_now(handle, inode->i_sb);
20982098
} else {
20992099
struct ext4_iloc iloc2;
21002100
struct inode *i_prev =

‎fs/ext4/resize.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
796796
ext4_kvfree(o_group_desc);
797797

798798
le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
799-
err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
799+
err = ext4_handle_dirty_super_now(handle, sb);
800800
if (err)
801801
ext4_std_error(sb, err);
802802

@@ -968,6 +968,8 @@ static void update_backups(struct super_block *sb,
968968
goto exit_err;
969969
}
970970

971+
ext4_superblock_csum_set(sb, (struct ext4_super_block *)data);
972+
971973
while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
972974
struct buffer_head *bh;
973975

‎fs/ext4/super.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,38 @@ static int ext4_verify_csum_type(struct super_block *sb,
122122
return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
123123
}
124124

125+
static __le32 ext4_superblock_csum(struct super_block *sb,
126+
struct ext4_super_block *es)
127+
{
128+
struct ext4_sb_info *sbi = EXT4_SB(sb);
129+
int offset = offsetof(struct ext4_super_block, s_checksum);
130+
__u32 csum;
131+
132+
csum = ext4_chksum(sbi, ~0, (char *)es, offset);
133+
134+
return cpu_to_le32(csum);
135+
}
136+
137+
int ext4_superblock_csum_verify(struct super_block *sb,
138+
struct ext4_super_block *es)
139+
{
140+
if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
141+
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
142+
return 1;
143+
144+
return es->s_checksum == ext4_superblock_csum(sb, es);
145+
}
146+
147+
void ext4_superblock_csum_set(struct super_block *sb,
148+
struct ext4_super_block *es)
149+
{
150+
if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
151+
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
152+
return;
153+
154+
es->s_checksum = ext4_superblock_csum(sb, es);
155+
}
156+
125157
void *ext4_kvmalloc(size_t size, gfp_t flags)
126158
{
127159
void *ret;
@@ -3057,6 +3089,20 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
30573089
}
30583090
}
30593091

3092+
/* Check superblock checksum */
3093+
if (!ext4_superblock_csum_verify(sb, es)) {
3094+
ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3095+
"invalid superblock checksum. Run e2fsck?");
3096+
silent = 1;
3097+
goto cantfind_ext4;
3098+
}
3099+
3100+
/* Precompute checksum seed for all metadata */
3101+
if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3102+
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3103+
sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3104+
sizeof(es->s_uuid));
3105+
30603106
/* Set defaults before we parse the mount options */
30613107
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
30623108
set_opt(sb, INIT_INODE_TABLE);
@@ -4059,6 +4105,7 @@ static int ext4_commit_super(struct super_block *sb, int sync)
40594105
&EXT4_SB(sb)->s_freeinodes_counter));
40604106
sb->s_dirt = 0;
40614107
BUFFER_TRACE(sbh, "marking dirty");
4108+
ext4_superblock_csum_set(sb, es);
40624109
mark_buffer_dirty(sbh);
40634110
if (sync) {
40644111
error = sync_dirty_buffer(sbh);

0 commit comments

Comments
 (0)