[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250322-initrd-erofs-v2-8-d66ee4a2c756@cyberus-technology.de>
Date: Sat, 22 Mar 2025 21:34:20 +0100
From: Julian Stecklina via B4 Relay <devnull+julian.stecklina.cyberus-technology.de@...nel.org>
To: Christoph Hellwig <hch@....de>, Al Viro <viro@...iv.linux.org.uk>,
Christian Brauner <brauner@...nel.org>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>, Gao Xiang <xiang@...nel.org>,
linux-fsdevel@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-erofs@...ts.ozlabs.org,
Julian Stecklina <julian.stecklina@...erus-technology.de>
Subject: [PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector
From: Julian Stecklina <julian.stecklina@...erus-technology.de>
Port ext2fs to the new initrd_fs_detect API. There are minor
functional changes, because I thought that relying on a 16-bit magic
number alone is too error-prone. I also removed ext2_image_size from
linux/ext2_fs.h, because the initrd code is the only user.
Given that both the ext2 and ext4 module can handle ext2 filesystems,
we have to add the code to either module depending on the
configuration options.
Signed-off-by: Julian Stecklina <julian.stecklina@...erus-technology.de>
---
fs/ext2/Makefile | 5 +++++
fs/ext2/initrd.c | 27 +++++++++++++++++++++++++++
fs/ext4/Makefile | 4 ++++
include/linux/ext2_fs.h | 9 ---------
init/do_mounts_rd.c | 19 -------------------
5 files changed, 36 insertions(+), 28 deletions(-)
diff --git a/fs/ext2/Makefile b/fs/ext2/Makefile
index 8860948ef9ca4e0a9c3f90311c3cecf0c5b70c63..c38a5b209023f93c84e8a6b8995d1db0214bb01a 100644
--- a/fs/ext2/Makefile
+++ b/fs/ext2/Makefile
@@ -14,3 +14,8 @@ CFLAGS_trace.o := -I$(src)
ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
ext2-$(CONFIG_EXT2_FS_POSIX_ACL) += acl.o
ext2-$(CONFIG_EXT2_FS_SECURITY) += xattr_security.o
+
+# If we are built-in, we provide support for ext2 on initrds.
+ifeq ($(CONFIG_EXT2_FS),y)
+ext2-y += initrd.o
+endif
diff --git a/fs/ext2/initrd.c b/fs/ext2/initrd.c
new file mode 100644
index 0000000000000000000000000000000000000000..572930512b8b3bee0d733553117a026af6e2f833
--- /dev/null
+++ b/fs/ext2/initrd.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/initrd.h>
+#include <linux/fs.h>
+
+#include "ext2.h"
+
+static size_t __init detect_ext2fs(void *block_data)
+{
+ struct ext2_super_block *ext2sb
+ = (struct ext2_super_block *)block_data;
+ BUILD_BUG_ON(sizeof(*ext2sb) > BLOCK_SIZE);
+
+ /*
+ * The 16-bit magic number is not a lot to reliably detect the
+ * filesystem. We check the revision as well to decrease the
+ * chance of false positives.
+ */
+ if (le16_to_cpu(ext2sb->s_magic) != EXT2_SUPER_MAGIC ||
+ le32_to_cpu(ext2sb->s_rev_level) > EXT2_MAX_SUPP_REV)
+ return 0;
+
+ return le32_to_cpu(ext2sb->s_blocks_count)
+ << (le32_to_cpu(ext2sb->s_log_block_size) + BLOCK_SIZE_BITS);
+}
+
+initrd_fs_detect(detect_ext2fs, BLOCK_SIZE);
diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
index 72206a2926765feba6fc59332ffeca7c03c8677b..907c80c33c8fc1e5dee85f8e862c8b27615f1a04 100644
--- a/fs/ext4/Makefile
+++ b/fs/ext4/Makefile
@@ -18,3 +18,7 @@ ext4-inode-test-objs += inode-test.o
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o
ext4-$(CONFIG_FS_VERITY) += verity.o
ext4-$(CONFIG_FS_ENCRYPTION) += crypto.o
+
+ifeq ($(CONFIG_EXT4_FS),y)
+ext4-$(CONFIG_EXT4_USE_FOR_EXT2) += ../ext2/initrd.o
+endif
diff --git a/include/linux/ext2_fs.h b/include/linux/ext2_fs.h
index 1fef885690370e5c039871ac8dd99d649d72aa64..0662827c0c69c3b77fb74850e1b0f3626c14c713 100644
--- a/include/linux/ext2_fs.h
+++ b/include/linux/ext2_fs.h
@@ -31,13 +31,4 @@
#define EXT2_SB_BLOCKS_OFFSET 0x04
#define EXT2_SB_BSIZE_OFFSET 0x18
-static inline u64 ext2_image_size(void *ext2_sb)
-{
- __u8 *p = ext2_sb;
- if (*(__le16 *)(p + EXT2_SB_MAGIC_OFFSET) != cpu_to_le16(EXT2_SUPER_MAGIC))
- return 0;
- return (u64)le32_to_cpup((__le32 *)(p + EXT2_SB_BLOCKS_OFFSET)) <<
- le32_to_cpup((__le32 *)(p + EXT2_SB_BSIZE_OFFSET));
-}
-
#endif /* _LINUX_EXT2_FS_H */
diff --git a/init/do_mounts_rd.c b/init/do_mounts_rd.c
index 2a6cb08d0b4872ef8e861a813ef89dc1e9a150af..45d2c5f7da044166524bef808bb97bee46c3324b 100644
--- a/init/do_mounts_rd.c
+++ b/init/do_mounts_rd.c
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/kernel.h>
#include <linux/fs.h>
-#include <linux/ext2_fs.h>
#include <linux/initrd.h>
#include <linux/string.h>
@@ -39,7 +38,6 @@ static int __init crd_load(decompress_fn deco);
* numbers could not be found.
*
* We currently check for the following magic numbers:
- * ext2
* gzip
* bzip2
* lzma
@@ -56,7 +54,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
int nblocks = -1;
unsigned char *buf;
const char *compress_name;
- unsigned long n;
int start_block = rd_image_start;
struct initrd_detect_fs *detect_fs;
@@ -84,22 +81,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
goto done;
}
- /*
- * Read block 1 to test for ext2 superblock
- */
- pos = (start_block + 1) * BLOCK_SIZE;
- kernel_read(file, buf, size, &pos);
-
- /* Try ext2 */
- n = ext2_image_size(buf);
- if (n) {
- printk(KERN_NOTICE
- "RAMDISK: ext2 filesystem found at block %d\n",
- start_block);
- nblocks = n;
- goto done;
- }
-
/* Try to find a filesystem in the initrd */
for (detect_fs = __start_initrd_fs_detect;
detect_fs < __stop_initrd_fs_detect;
--
2.47.0
Powered by blists - more mailing lists