Skip to content

Commit db9ee22

Browse files
djwongtytso
authored andcommitted
jbd2: fix descriptor block size handling errors with journal_csum
It turns out that there are some serious problems with the on-disk format of journal checksum v2. The foremost is that the function to calculate descriptor tag size returns sizes that are too big. This causes alignment issues on some architectures and is compounded by the fact that some parts of jbd2 use the structure size (incorrectly) to determine the presence of a 64bit journal instead of checking the feature flags. Therefore, introduce journal checksum v3, which enlarges the descriptor block tag format to allow for full 32-bit checksums of journal blocks, fix the journal tag function to return the correct sizes, and fix the jbd2 recovery code to use feature flags to determine 64bitness. Add a few function helpers so we don't have to open-code quite so many pieces. Switching to a 16-byte block size was found to increase journal size overhead by a maximum of 0.1%, to convert a 32-bit journal with no checksumming to a 32-bit journal with checksum v3 enabled. Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reported-by: TR Reardon <thomas_reardon@hotmail.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@vger.kernel.org
1 parent 022eaa7 commit db9ee22

6 files changed

Lines changed: 95 additions & 49 deletions

File tree

‎fs/ext4/super.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,9 +3181,9 @@ static int set_journal_csum_feature_set(struct super_block *sb)
31813181

31823182
if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
31833183
EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3184-
/* journal checksum v2 */
3184+
/* journal checksum v3 */
31853185
compat = 0;
3186-
incompat = JBD2_FEATURE_INCOMPAT_CSUM_V2;
3186+
incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
31873187
} else {
31883188
/* journal checksum v1 */
31893189
compat = JBD2_FEATURE_COMPAT_CHECKSUM;
@@ -3205,6 +3205,7 @@ static int set_journal_csum_feature_set(struct super_block *sb)
32053205
jbd2_journal_clear_features(sbi->s_journal,
32063206
JBD2_FEATURE_COMPAT_CHECKSUM, 0,
32073207
JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3208+
JBD2_FEATURE_INCOMPAT_CSUM_V3 |
32083209
JBD2_FEATURE_INCOMPAT_CSUM_V2);
32093210
}
32103211

‎fs/jbd2/commit.c‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
9797
struct commit_header *h;
9898
__u32 csum;
9999

100-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
100+
if (!jbd2_journal_has_csum_v2or3(j))
101101
return;
102102

103103
h = (struct commit_header *)(bh->b_data);
@@ -313,11 +313,11 @@ static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
313313
return checksum;
314314
}
315315

316-
static void write_tag_block(int tag_bytes, journal_block_tag_t *tag,
316+
static void write_tag_block(journal_t *j, journal_block_tag_t *tag,
317317
unsigned long long block)
318318
{
319319
tag->t_blocknr = cpu_to_be32(block & (u32)~0);
320-
if (tag_bytes > JBD2_TAG_SIZE32)
320+
if (JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_64BIT))
321321
tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
322322
}
323323

@@ -327,7 +327,7 @@ static void jbd2_descr_block_csum_set(journal_t *j,
327327
struct jbd2_journal_block_tail *tail;
328328
__u32 csum;
329329

330-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
330+
if (!jbd2_journal_has_csum_v2or3(j))
331331
return;
332332

333333
tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
@@ -340,12 +340,13 @@ static void jbd2_descr_block_csum_set(journal_t *j,
340340
static void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
341341
struct buffer_head *bh, __u32 sequence)
342342
{
343+
journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
343344
struct page *page = bh->b_page;
344345
__u8 *addr;
345346
__u32 csum32;
346347
__be32 seq;
347348

348-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
349+
if (!jbd2_journal_has_csum_v2or3(j))
349350
return;
350351

351352
seq = cpu_to_be32(sequence);
@@ -355,8 +356,10 @@ static void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
355356
bh->b_size);
356357
kunmap_atomic(addr);
357358

358-
/* We only have space to store the lower 16 bits of the crc32c. */
359-
tag->t_checksum = cpu_to_be16(csum32);
359+
if (JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V3))
360+
tag3->t_checksum = cpu_to_be32(csum32);
361+
else
362+
tag->t_checksum = cpu_to_be16(csum32);
360363
}
361364
/*
362365
* jbd2_journal_commit_transaction
@@ -396,7 +399,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
396399
LIST_HEAD(io_bufs);
397400
LIST_HEAD(log_bufs);
398401

399-
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
402+
if (jbd2_journal_has_csum_v2or3(journal))
400403
csum_size = sizeof(struct jbd2_journal_block_tail);
401404

402405
/*
@@ -690,7 +693,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
690693
tag_flag |= JBD2_FLAG_SAME_UUID;
691694

692695
tag = (journal_block_tag_t *) tagp;
693-
write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
696+
write_tag_block(journal, tag, jh2bh(jh)->b_blocknr);
694697
tag->t_flags = cpu_to_be16(tag_flag);
695698
jbd2_block_tag_csum_set(journal, tag, wbuf[bufs],
696699
commit_transaction->t_tid);

‎fs/jbd2/journal.c‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ EXPORT_SYMBOL(__jbd2_debug);
124124
/* Checksumming functions */
125125
static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
126126
{
127-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
127+
if (!jbd2_journal_has_csum_v2or3(j))
128128
return 1;
129129

130130
return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
@@ -145,15 +145,15 @@ static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
145145

146146
static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
147147
{
148-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
148+
if (!jbd2_journal_has_csum_v2or3(j))
149149
return 1;
150150

151151
return sb->s_checksum == jbd2_superblock_csum(j, sb);
152152
}
153153

154154
static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
155155
{
156-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
156+
if (!jbd2_journal_has_csum_v2or3(j))
157157
return;
158158

159159
sb->s_checksum = jbd2_superblock_csum(j, sb);
@@ -1522,21 +1522,29 @@ static int journal_get_superblock(journal_t *journal)
15221522
goto out;
15231523
}
15241524

1525-
if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1526-
JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1525+
if (jbd2_journal_has_csum_v2or3(journal) &&
1526+
JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM)) {
15271527
/* Can't have checksum v1 and v2 on at the same time! */
15281528
printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2 "
15291529
"at the same time!\n");
15301530
goto out;
15311531
}
15321532

1533+
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2) &&
1534+
JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1535+
/* Can't have checksum v2 and v3 at the same time! */
1536+
printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1537+
"at the same time!\n");
1538+
goto out;
1539+
}
1540+
15331541
if (!jbd2_verify_csum_type(journal, sb)) {
15341542
printk(KERN_ERR "JBD2: Unknown checksum type\n");
15351543
goto out;
15361544
}
15371545

15381546
/* Load the checksum driver */
1539-
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1547+
if (jbd2_journal_has_csum_v2or3(journal)) {
15401548
journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
15411549
if (IS_ERR(journal->j_chksum_driver)) {
15421550
printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
@@ -1553,7 +1561,7 @@ static int journal_get_superblock(journal_t *journal)
15531561
}
15541562

15551563
/* Precompute checksum seed for all metadata */
1556-
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1564+
if (jbd2_journal_has_csum_v2or3(journal))
15571565
journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
15581566
sizeof(sb->s_uuid));
15591567

@@ -1813,8 +1821,14 @@ int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
18131821
if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
18141822
return 0;
18151823

1816-
/* Asking for checksumming v2 and v1? Only give them v2. */
1817-
if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1824+
/* If enabling v2 checksums, turn on v3 instead */
1825+
if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
1826+
incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
1827+
incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
1828+
}
1829+
1830+
/* Asking for checksumming v3 and v1? Only give them v3. */
1831+
if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
18181832
compat & JBD2_FEATURE_COMPAT_CHECKSUM)
18191833
compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
18201834

@@ -1823,8 +1837,8 @@ int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
18231837

18241838
sb = journal->j_superblock;
18251839

1826-
/* If enabling v2 checksums, update superblock */
1827-
if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1840+
/* If enabling v3 checksums, update superblock */
1841+
if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
18281842
sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
18291843
sb->s_feature_compat &=
18301844
~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
@@ -1842,8 +1856,7 @@ int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
18421856
}
18431857

18441858
/* Precompute checksum seed for all metadata */
1845-
if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1846-
JBD2_FEATURE_INCOMPAT_CSUM_V2))
1859+
if (jbd2_journal_has_csum_v2or3(journal))
18471860
journal->j_csum_seed = jbd2_chksum(journal, ~0,
18481861
sb->s_uuid,
18491862
sizeof(sb->s_uuid));
@@ -1852,7 +1865,8 @@ int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
18521865
/* If enabling v1 checksums, downgrade superblock */
18531866
if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
18541867
sb->s_feature_incompat &=
1855-
~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1868+
~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
1869+
JBD2_FEATURE_INCOMPAT_CSUM_V3);
18561870

18571871
sb->s_feature_compat |= cpu_to_be32(compat);
18581872
sb->s_feature_ro_compat |= cpu_to_be32(ro);
@@ -2165,16 +2179,20 @@ int jbd2_journal_blocks_per_page(struct inode *inode)
21652179
*/
21662180
size_t journal_tag_bytes(journal_t *journal)
21672181
{
2168-
journal_block_tag_t tag;
2169-
size_t x = 0;
2182+
size_t sz;
2183+
2184+
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3))
2185+
return sizeof(journal_block_tag3_t);
2186+
2187+
sz = sizeof(journal_block_tag_t);
21702188

21712189
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2172-
x += sizeof(tag.t_checksum);
2190+
sz += sizeof(__u16);
21732191

21742192
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2175-
return x + JBD2_TAG_SIZE64;
2193+
return sz;
21762194
else
2177-
return x + JBD2_TAG_SIZE32;
2195+
return sz - sizeof(__u32);
21782196
}
21792197

21802198
/*

‎fs/jbd2/recovery.c‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int jbd2_descr_block_csum_verify(journal_t *j,
181181
__be32 provided;
182182
__u32 calculated;
183183

184-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
184+
if (!jbd2_journal_has_csum_v2or3(j))
185185
return 1;
186186

187187
tail = (struct jbd2_journal_block_tail *)(buf + j->j_blocksize -
@@ -205,7 +205,7 @@ static int count_tags(journal_t *journal, struct buffer_head *bh)
205205
int nr = 0, size = journal->j_blocksize;
206206
int tag_bytes = journal_tag_bytes(journal);
207207

208-
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
208+
if (jbd2_journal_has_csum_v2or3(journal))
209209
size -= sizeof(struct jbd2_journal_block_tail);
210210

211211
tagp = &bh->b_data[sizeof(journal_header_t)];
@@ -338,10 +338,11 @@ int jbd2_journal_skip_recovery(journal_t *journal)
338338
return err;
339339
}
340340

341-
static inline unsigned long long read_tag_block(int tag_bytes, journal_block_tag_t *tag)
341+
static inline unsigned long long read_tag_block(journal_t *journal,
342+
journal_block_tag_t *tag)
342343
{
343344
unsigned long long block = be32_to_cpu(tag->t_blocknr);
344-
if (tag_bytes > JBD2_TAG_SIZE32)
345+
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
345346
block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
346347
return block;
347348
}
@@ -384,7 +385,7 @@ static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
384385
__be32 provided;
385386
__u32 calculated;
386387

387-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
388+
if (!jbd2_journal_has_csum_v2or3(j))
388389
return 1;
389390

390391
h = buf;
@@ -399,17 +400,21 @@ static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
399400
static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
400401
void *buf, __u32 sequence)
401402
{
403+
journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
402404
__u32 csum32;
403405
__be32 seq;
404406

405-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
407+
if (!jbd2_journal_has_csum_v2or3(j))
406408
return 1;
407409

408410
seq = cpu_to_be32(sequence);
409411
csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
410412
csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
411413

412-
return tag->t_checksum == cpu_to_be16(csum32);
414+
if (JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V3))
415+
return tag3->t_checksum == cpu_to_be32(csum32);
416+
else
417+
return tag->t_checksum == cpu_to_be16(csum32);
413418
}
414419

415420
static int do_one_pass(journal_t *journal,
@@ -513,8 +518,7 @@ static int do_one_pass(journal_t *journal,
513518
switch(blocktype) {
514519
case JBD2_DESCRIPTOR_BLOCK:
515520
/* Verify checksum first */
516-
if (JBD2_HAS_INCOMPAT_FEATURE(journal,
517-
JBD2_FEATURE_INCOMPAT_CSUM_V2))
521+
if (jbd2_journal_has_csum_v2or3(journal))
518522
descr_csum_size =
519523
sizeof(struct jbd2_journal_block_tail);
520524
if (descr_csum_size > 0 &&
@@ -575,7 +579,7 @@ static int do_one_pass(journal_t *journal,
575579
unsigned long long blocknr;
576580

577581
J_ASSERT(obh != NULL);
578-
blocknr = read_tag_block(tag_bytes,
582+
blocknr = read_tag_block(journal,
579583
tag);
580584

581585
/* If the block has been
@@ -814,7 +818,7 @@ static int jbd2_revoke_block_csum_verify(journal_t *j,
814818
__be32 provided;
815819
__u32 calculated;
816820

817-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
821+
if (!jbd2_journal_has_csum_v2or3(j))
818822
return 1;
819823

820824
tail = (struct jbd2_journal_revoke_tail *)(buf + j->j_blocksize -

‎fs/jbd2/revoke.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
#include <linux/list.h>
9292
#include <linux/init.h>
9393
#include <linux/bio.h>
94-
#endif
9594
#include <linux/log2.h>
95+
#endif
9696

9797
static struct kmem_cache *jbd2_revoke_record_cache;
9898
static struct kmem_cache *jbd2_revoke_table_cache;
@@ -597,7 +597,7 @@ static void write_one_revoke_record(journal_t *journal,
597597
offset = *offsetp;
598598

599599
/* Do we need to leave space at the end for a checksum? */
600-
if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
600+
if (jbd2_journal_has_csum_v2or3(journal))
601601
csum_size = sizeof(struct jbd2_journal_revoke_tail);
602602

603603
/* Make sure we have a descriptor with space left for the record */
@@ -644,7 +644,7 @@ static void jbd2_revoke_csum_set(journal_t *j, struct buffer_head *bh)
644644
struct jbd2_journal_revoke_tail *tail;
645645
__u32 csum;
646646

647-
if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
647+
if (!jbd2_journal_has_csum_v2or3(j))
648648
return;
649649

650650
tail = (struct jbd2_journal_revoke_tail *)(bh->b_data + j->j_blocksize -

0 commit comments

Comments
 (0)