[PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector

Julian Stecklina via B4 Relay posted 9 patches 10 months, 3 weeks ago
[PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector
Posted by Julian Stecklina via B4 Relay 10 months, 3 weeks ago
From: Julian Stecklina <julian.stecklina@cyberus-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@cyberus-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
Re: [PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector
Posted by kernel test robot 10 months, 3 weeks ago
Hi Julian,

kernel test robot noticed the following build errors:

[auto build test ERROR on 88d324e69ea9f3ae1c1905ea75d717c08bdb8e15]

url:    https://github.com/intel-lab-lkp/linux/commits/Julian-Stecklina-via-B4-Relay/initrd-remove-ASCII-spinner/20250323-043649
base:   88d324e69ea9f3ae1c1905ea75d717c08bdb8e15
patch link:    https://lore.kernel.org/r/20250322-initrd-erofs-v2-8-d66ee4a2c756%40cyberus-technology.de
patch subject: [PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector
config: i386-buildonly-randconfig-001-20250323 (https://download.01.org/0day-ci/archive/20250323/202503230718.5DYAbNZO-lkp@intel.com/config)
compiler: clang version 20.1.1 (https://github.com/llvm/llvm-project 424c2d9b7e4de40d0804dd374721e6411c27d1d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250323/202503230718.5DYAbNZO-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503230718.5DYAbNZO-lkp@intel.com/

All errors (new ones prefixed by >>):

>> fs/ext2/initrd.c:27:33: error: too many arguments provided to function-like macro invocation
      27 | initrd_fs_detect(detect_ext2fs, BLOCK_SIZE);
         |                                 ^
   include/linux/initrd.h:63:9: note: macro 'initrd_fs_detect' defined here
      63 | #define initrd_fs_detect(detectfn)
         |         ^
>> fs/ext2/initrd.c:27:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
      27 | initrd_fs_detect(detect_ext2fs, BLOCK_SIZE);
         | ^
         | int
   2 errors generated.


vim +27 fs/ext2/initrd.c

    26	
  > 27	initrd_fs_detect(detect_ext2fs, BLOCK_SIZE);

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki