History

jbd2: checksum journal superblock

Calculate and verify a checksum covering the journal superblock.

Darrick J. Wong committed 14 years ago

Commit 4fd5ea41 parent 01b5adc2 files · +50 −0Browse files

Unified · Split

2 files
  1. fs/jbd2/journal.c +47 −0
  2. include/linux/jbd2.h +3 −0
fs/jbd2/journal.c+47−0
@@ -106,6 +106,34 @@ int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb) · 105 unmodified lines
106106 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
107107 }
108108
109+static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
110+{
111+ __u32 csum, old_csum;
112+
113+ old_csum = sb->s_checksum;
114+ sb->s_checksum = 0;
115+ csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
116+ sb->s_checksum = old_csum;
117+
118+ return cpu_to_be32(csum);
119+}
120+
121+int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
122+{
123+ if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
124+ return 1;
125+
126+ return sb->s_checksum == jbd2_superblock_csum(j, sb);
127+}
128+
129+void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
130+{
131+ if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
132+ return;
133+
134+ sb->s_checksum = jbd2_superblock_csum(j, sb);
135+}
136+
109137 /*
110138 * Helper function used to manage commit timeouts
111139 */
@@ -1357,6 +1385,7 @@ static void jbd2_journal_update_sb_errno(journal_t *journal) · 1245 unmodified lines
13571385 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
13581386 journal->j_errno);
13591387 sb->s_errno = cpu_to_be32(journal->j_errno);
1388+ jbd2_superblock_csum_set(journal, sb);
13601389 read_unlock(&journal->j_state_lock);
13611390
13621391 jbd2_write_superblock(journal, WRITE_SYNC);
@@ -1449,6 +1478,17 @@ static int journal_get_superblock(journal_t *journal) · 86 unmodified lines
14491478 }
14501479 }
14511480
1481+ /* Check superblock checksum */
1482+ if (!jbd2_superblock_csum_verify(journal, sb)) {
1483+ printk(KERN_ERR "JBD: journal checksum error\n");
1484+ goto out;
1485+ }
1486+
1487+ /* Precompute checksum seed for all metadata */
1488+ if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1489+ journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1490+ sizeof(sb->s_uuid));
1491+
14521492 set_buffer_verified(bh);
14531493
14541494 return 0;
@@ -1732,6 +1772,13 @@ int jbd2_journal_set_features (journal_t *journal, unsigned long compat, · 277 unmodified lines
17321772 return 0;
17331773 }
17341774 }
1775+
1776+ /* Precompute checksum seed for all metadata */
1777+ if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1778+ JBD2_FEATURE_INCOMPAT_CSUM_V2))
1779+ journal->j_csum_seed = jbd2_chksum(journal, ~0,
1780+ sb->s_uuid,
1781+ sizeof(sb->s_uuid));
17351782 }
17361783
17371784 /* If enabling v1 checksums, downgrade superblock */
include/linux/jbd2.h+3−0
@@ -969,6 +969,9 @@ struct journal_s · 968 unmodified lines
969969
970970 /* Reference to checksum algorithm driver via cryptoapi */
971971 struct crypto_shash *j_chksum_driver;
972+
973+ /* Precomputed journal UUID checksum for seeding other checksums */
974+ __u32 j_csum_seed;
972975 };
973976
974977 /*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t