From: "Darrick J. Wong" <djwong@us.ibm.com>
To: Andreas Dilger <adilger.kernel@dilger.ca>,
Theodore Tso <tytso@mit.edu>,
"Darrick J. Wong" <djwong@us.ibm.com>
Cc: Sunil Mushran <sunil.mushran@oracle.com>,
Martin K Petersen <martin.petersen@oracle.com>,
Greg Freemyer <greg.freemyer@gmail.com>,
Amir Goldstein <amir73il@gmail.com>,
linux-kernel <linux-kernel@vger.kernel.org>,
Andi Kleen <andi@firstfloor.org>, Mingming Cao <cmm@us.ibm.com>,
Joel Becker <jlbec@evilplan.org>,
linux-fsdevel <linux-fsdevel@vger.kernel.org>,
linux-ext4@vger.kernel.org, Coly Li <colyli@gmail.com>
Subject: [PATCH 10/28] ext4: Calculate and verify superblock checksum
Date: Sat, 08 Oct 2011 00:54:49 -0700 [thread overview]
Message-ID: <20111008075449.20506.10158.stgit@elm3c44.beaverton.ibm.com> (raw)
In-Reply-To: <20111008075343.20506.23155.stgit@elm3c44.beaverton.ibm.com>
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>
---
fs/ext4/ext4.h | 11 ++++++++++-
fs/ext4/ext4_jbd2.c | 9 ++++++++-
fs/ext4/ext4_jbd2.h | 7 +++++--
fs/ext4/inode.c | 3 +--
fs/ext4/namei.c | 4 ++--
fs/ext4/resize.c | 6 +++++-
fs/ext4/super.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 78 insertions(+), 9 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 9012911..c99e44c 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1070,7 +1070,9 @@ struct ext4_super_block {
__u8 s_last_error_func[32]; /* function where the error happened */
#define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)
__u8 s_mount_opts[64];
- __le32 s_reserved[112]; /* Padding to the end of the block */
+ __u32 s_reserved1[3]; /* Padding */
+ __le32 s_reserved2[108]; /* Padding to the end of the block */
+ __le32 s_checksum; /* crc32c(superblock) */
};
#define EXT4_S_ERR_LEN (EXT4_S_ERR_END - EXT4_S_ERR_START)
@@ -1906,6 +1908,10 @@ extern int ext4_group_extend(struct super_block *sb,
ext4_fsblk_t n_blocks_count);
/* super.c */
+extern int ext4_superblock_csum_verify(struct super_block *sb,
+ struct ext4_super_block *es);
+extern void ext4_superblock_csum_set(struct super_block *sb,
+ struct ext4_super_block *es);
extern void *ext4_kvmalloc(size_t size, gfp_t flags);
extern void *ext4_kvzalloc(size_t size, gfp_t flags);
extern void ext4_kvfree(void *ptr);
@@ -2180,6 +2186,9 @@ static inline void ext4_unlock_group(struct super_block *sb,
static inline void ext4_mark_super_dirty(struct super_block *sb)
{
+ struct ext4_super_block *es = EXT4_SB(sb)->s_es;
+
+ ext4_superblock_csum_set(sb, es);
if (EXT4_SB(sb)->s_journal == NULL)
sb->s_dirt =1;
}
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index f5240aa..04ddc97 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -136,16 +136,23 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
}
int __ext4_handle_dirty_super(const char *where, unsigned int line,
- handle_t *handle, struct super_block *sb)
+ handle_t *handle, struct super_block *sb,
+ int now)
{
struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
int err = 0;
if (ext4_handle_valid(handle)) {
+ ext4_superblock_csum_set(sb,
+ (struct ext4_super_block *)bh->b_data);
err = jbd2_journal_dirty_metadata(handle, bh);
if (err)
ext4_journal_abort_handle(where, line, __func__,
bh, handle, err);
+ } else if (now) {
+ ext4_superblock_csum_set(sb,
+ (struct ext4_super_block *)bh->b_data);
+ mark_buffer_dirty(bh);
} else
sb->s_dirt = 1;
return err;
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 5802fa1..ed9b78d 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -141,7 +141,8 @@ int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
struct buffer_head *bh);
int __ext4_handle_dirty_super(const char *where, unsigned int line,
- handle_t *handle, struct super_block *sb);
+ handle_t *handle, struct super_block *sb,
+ int now);
#define ext4_journal_get_write_access(handle, bh) \
__ext4_journal_get_write_access(__func__, __LINE__, (handle), (bh))
@@ -153,8 +154,10 @@ int __ext4_handle_dirty_super(const char *where, unsigned int line,
#define ext4_handle_dirty_metadata(handle, inode, bh) \
__ext4_handle_dirty_metadata(__func__, __LINE__, (handle), (inode), \
(bh))
+#define ext4_handle_dirty_super_now(handle, sb) \
+ __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 1)
#define ext4_handle_dirty_super(handle, sb) \
- __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb))
+ __ext4_handle_dirty_super(__func__, __LINE__, (handle), (sb), 0)
handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks);
int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 986e238..6e64e0b 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3700,8 +3700,7 @@ static int ext4_do_update_inode(handle_t *handle,
EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
sb->s_dirt = 1;
ext4_handle_sync(handle);
- err = ext4_handle_dirty_metadata(handle, NULL,
- EXT4_SB(sb)->s_sbh);
+ err = ext4_handle_dirty_super_now(handle, sb);
}
}
raw_inode->i_generation = cpu_to_le32(inode->i_generation);
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 50c7294..d0158f2 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2014,7 +2014,7 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
/* Insert this inode at the head of the on-disk orphan list... */
NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
- err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
+ err = ext4_handle_dirty_super_now(handle, sb);
rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
if (!err)
err = rc;
@@ -2087,7 +2087,7 @@ int ext4_orphan_del(handle_t *handle, struct inode *inode)
if (err)
goto out_brelse;
sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
- err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
+ err = ext4_handle_dirty_super_now(handle, inode->i_sb);
} else {
struct ext4_iloc iloc2;
struct inode *i_prev =
diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c
index 707d3f1..2ad7008 100644
--- a/fs/ext4/resize.c
+++ b/fs/ext4/resize.c
@@ -511,7 +511,7 @@ static int add_new_gdb(handle_t *handle, struct inode *inode,
ext4_kvfree(o_group_desc);
le16_add_cpu(&es->s_reserved_gdt_blocks, -1);
- err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
+ err = ext4_handle_dirty_super_now(handle, sb);
if (err)
ext4_std_error(sb, err);
@@ -682,6 +682,8 @@ static void update_backups(struct super_block *sb,
goto exit_err;
}
+ ext4_superblock_csum_set(sb, (struct ext4_super_block *)data);
+
while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
struct buffer_head *bh;
@@ -925,6 +927,8 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
/* Update the global fs size fields */
sbi->s_groups_count++;
+ ext4_superblock_csum_set(sb,
+ (struct ext4_super_block *)primary->b_data);
err = ext4_handle_dirty_metadata(handle, NULL, primary);
if (unlikely(err)) {
ext4_std_error(sb, err);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index f02de28..58157ec 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -120,6 +120,38 @@ static int ext4_verify_csum_type(struct super_block *sb,
return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
}
+static __le32 ext4_superblock_csum(struct super_block *sb,
+ struct ext4_super_block *es)
+{
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ int offset = offsetof(struct ext4_super_block, s_checksum);
+ __u32 crc;
+
+ crc = ext4_chksum(sbi, ~0, (char *)es, offset);
+
+ return cpu_to_le32(crc);
+}
+
+int ext4_superblock_csum_verify(struct super_block *sb,
+ struct ext4_super_block *es)
+{
+ if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+ return 1;
+
+ return es->s_checksum == ext4_superblock_csum(sb, es);
+}
+
+void ext4_superblock_csum_set(struct super_block *sb,
+ struct ext4_super_block *es)
+{
+ if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+ return;
+
+ es->s_checksum = ext4_superblock_csum(sb, es);
+}
+
void *ext4_kvmalloc(size_t size, gfp_t flags)
{
void *ret;
@@ -3184,6 +3216,20 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
}
}
+ /* Check superblock checksum */
+ if (!ext4_superblock_csum_verify(sb, es)) {
+ ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
+ "invalid superblock checksum. Run e2fsck?");
+ silent = 1;
+ goto cantfind_ext4;
+ }
+
+ /* Precompute first piece of crc */
+ if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+ sbi->s_uuid_crc = ext4_chksum(sbi, ~0, es->s_uuid,
+ sizeof(es->s_uuid));
+
/* Set defaults before we parse the mount options */
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
set_opt(sb, INIT_INODE_TABLE);
@@ -4140,6 +4186,7 @@ static int ext4_commit_super(struct super_block *sb, int sync)
&EXT4_SB(sb)->s_freeinodes_counter));
sb->s_dirt = 0;
BUFFER_TRACE(sbh, "marking dirty");
+ ext4_superblock_csum_set(sb, es);
mark_buffer_dirty(sbh);
if (sync) {
error = sync_dirty_buffer(sbh);
next prev parent reply other threads:[~2011-10-08 7:57 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-08 7:53 [PATCH v2 00/28] ext4: Add metadata checksumming Darrick J. Wong
2011-10-08 7:53 ` [PATCH 01/28] ext4: ext4_dx_add_entry should dirty directory metadata with the directory inode Darrick J. Wong
2011-10-08 7:53 ` [PATCH 02/28] ext4: ext4_rename should dirty dir_bh with the correct directory Darrick J. Wong
2011-10-08 7:54 ` [PATCH 03/28] ext4: ext4_mkdir should dirty dir_block with the parent inode Darrick J. Wong
2011-10-08 7:54 ` [PATCH 04/28] ext4: Prevent stack overrun in ext4_file_open when recording last known mountpoint Darrick J. Wong
2011-10-08 7:54 ` [PATCH 05/28] ext4: Fix endian problem in MMP initialization Darrick J. Wong
2011-10-08 7:54 ` [PATCH 06/28] ext4: Create a new BH_Verified flag to avoid unnecessary metadata validation Darrick J. Wong
2011-10-08 7:54 ` [PATCH 07/28] ext4: Create a rocompat flag for extended metadata checksumming Darrick J. Wong
2011-10-08 7:54 ` [PATCH 08/28] ext4: Record the checksum algorithm in use in the superblock Darrick J. Wong
2011-10-08 7:54 ` [PATCH 09/28] ext4: Only call out to crc32c if necessary Darrick J. Wong
2011-10-08 7:54 ` Darrick J. Wong [this message]
2011-10-08 7:54 ` [PATCH 11/28] ext4: Calculate and verify inode checksums Darrick J. Wong
2011-10-12 19:45 ` Andreas Dilger
2011-10-12 21:03 ` Darrick J. Wong
2011-10-13 0:02 ` Andreas Dilger
2011-10-08 7:55 ` [PATCH 12/28] ext4: Use i_generation in inode-related metadata checksums Darrick J. Wong
2011-10-12 19:52 ` Andreas Dilger
2011-10-12 21:28 ` Darrick J. Wong
2011-10-13 0:06 ` Andreas Dilger
2011-10-08 7:55 ` [PATCH 13/28] ext4: Create bitmap checksum helper functions Darrick J. Wong
2011-10-08 7:55 ` [PATCH 14/28] ext4: Calculate and verify checksums for inode bitmaps Darrick J. Wong
2011-10-08 7:55 ` [PATCH 15/28] ext4: Calculate and verify block bitmap checksum Darrick J. Wong
[not found] ` <AE869D1A-2A06-4849-8752-74924B0C05BD@dilger.ca>
2011-10-13 7:16 ` Darrick J. Wong
2011-11-07 20:00 ` Darrick J. Wong
2011-11-07 21:44 ` Andreas Dilger
2011-11-10 0:57 ` Darrick J. Wong
2011-11-10 2:34 ` Andreas Dilger
2011-10-08 7:55 ` [PATCH 16/28] ext4: Verify and calculate checksums for extent tree blocks Darrick J. Wong
2011-10-08 7:55 ` [PATCH 17/28] ext4: Calculate and verify checksums for htree nodes Darrick J. Wong
[not found] ` <F6453844-06CC-4245-BB39-EBA4327D4C92@dilger.ca>
2011-10-13 7:21 ` Darrick J. Wong
2011-10-08 7:55 ` [PATCH 18/28] ext4: Calculate and verify checksums of directory leaf blocks Darrick J. Wong
2011-10-08 7:55 ` [PATCH 19/28] ext4: Calculate and verify checksums of extended attribute blocks Darrick J. Wong
2011-10-08 7:55 ` [PATCH 20/28] ext4: Add new feature to make block group checksums use metadata_csum algorithm Darrick J. Wong
2011-10-08 7:56 ` [PATCH 21/28] ext4: Add checksums to the MMP block Darrick J. Wong
2011-10-08 7:56 ` [PATCH 22/28] jbd2: Update structure definitions and flags to support extended checksumming Darrick J. Wong
2011-10-08 7:56 ` [PATCH 23/28] jbd2: Grab a reference to the crc32c driver only when necessary Darrick J. Wong
2011-10-08 7:56 ` [PATCH 24/28] jbd2: Update structure definitions and flags to support extended checksumming Darrick J. Wong
2011-10-08 7:56 ` [PATCH 25/28] jbd2: Checksum revocation blocks Darrick J. Wong
2011-10-08 7:56 ` [PATCH 26/28] jbd2: Checksum descriptor blocks Darrick J. Wong
2011-10-08 7:56 ` [PATCH 27/28] jbd2: Checksum commit blocks Darrick J. Wong
2011-10-08 7:56 ` [PATCH 28/28] jbd2: Checksum data blocks that are stored in the journal Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111008075449.20506.10158.stgit@elm3c44.beaverton.ibm.com \
--to=djwong@us.ibm.com \
--cc=adilger.kernel@dilger.ca \
--cc=amir73il@gmail.com \
--cc=andi@firstfloor.org \
--cc=cmm@us.ibm.com \
--cc=colyli@gmail.com \
--cc=greg.freemyer@gmail.com \
--cc=jlbec@evilplan.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=sunil.mushran@oracle.com \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).