Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[linux-2.6-microblaze.git] / fs / erofs / super.c
index 0e36949..057e6d7 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/statfs.h>
 #include <linux/parser.h>
 #include <linux/seq_file.h>
+#include <linux/crc32c.h>
 #include "xattr.h"
 
 #define CREATE_TRACE_POINTS
@@ -46,6 +47,30 @@ void _erofs_info(struct super_block *sb, const char *function,
        va_end(args);
 }
 
+static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
+{
+       struct erofs_super_block *dsb;
+       u32 expected_crc, crc;
+
+       dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
+                     EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
+       if (!dsb)
+               return -ENOMEM;
+
+       expected_crc = le32_to_cpu(dsb->checksum);
+       dsb->checksum = 0;
+       /* to allow for x86 boot sectors and other oddities. */
+       crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
+       kfree(dsb);
+
+       if (crc != expected_crc) {
+               erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
+                         crc, expected_crc);
+               return -EBADMSG;
+       }
+       return 0;
+}
+
 static void erofs_inode_init_once(void *ptr)
 {
        struct erofs_inode *vi = ptr;
@@ -112,7 +137,7 @@ static int erofs_read_superblock(struct super_block *sb)
 
        sbi = EROFS_SB(sb);
 
-       data = kmap_atomic(page);
+       data = kmap(page);
        dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
 
        ret = -EINVAL;
@@ -121,6 +146,13 @@ static int erofs_read_superblock(struct super_block *sb)
                goto out;
        }
 
+       sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
+       if (sbi->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) {
+               ret = erofs_superblock_csum_verify(sb, data);
+               if (ret)
+                       goto out;
+       }
+
        blkszbits = dsb->blkszbits;
        /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
        if (blkszbits != LOG_BLOCK_SIZE) {
@@ -155,7 +187,7 @@ static int erofs_read_superblock(struct super_block *sb)
        }
        ret = 0;
 out:
-       kunmap_atomic(data);
+       kunmap(page);
        put_page(page);
        return ret;
 }
@@ -566,9 +598,6 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)
                seq_puts(seq, ",cache_strategy=readahead");
        } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
                seq_puts(seq, ",cache_strategy=readaround");
-       } else {
-               seq_puts(seq, ",cache_strategy=(unknown)");
-               DBG_BUGON(1);
        }
 #endif
        return 0;