mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Julian Stecklina via B4 Relay <devnull+julian.stecklina.cyberus-technology.de@kernel.org>
To: Christoph Hellwig <hch@lst.de>, Al Viro <viro@zeniv.linux.org.uk>,
	 Christian Brauner <brauner@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 "Rafael J. Wysocki" <rafael@kernel.org>,
	Gao Xiang <xiang@kernel.org>,
	 linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-erofs@lists.ozlabs.org,
	 Julian Stecklina <julian.stecklina@cyberus-technology.de>
Subject: [PATCH v2 8/9] fs: ext2, ext4: register an initrd fs detector
Date: Sat, 22 Mar 2025 21:34:20 +0100	[thread overview]
Message-ID: <20250322-initrd-erofs-v2-8-d66ee4a2c756@cyberus-technology.de> (raw)
In-Reply-To: <20250322-initrd-erofs-v2-0-d66ee4a2c756@cyberus-technology.de>

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



  parent reply	other threads:[~2025-03-22 20:35 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-22 20:34 [PATCH v2 0/9] initrd: cleanup and erofs support Julian Stecklina via B4 Relay
2025-03-22 20:34 ` [PATCH v2 1/9] initrd: remove ASCII spinner Julian Stecklina via B4 Relay
2025-03-25  6:42   ` David Disseldorp
2025-03-22 20:34 ` [PATCH v2 2/9] initrd: fix double fput for truncated ramdisks Julian Stecklina via B4 Relay
2025-03-22 20:34 ` [PATCH v2 3/9] initrd: add a generic mechanism to add fs detectors Julian Stecklina via B4 Relay
2025-03-24  9:35   ` Julian Stecklina
2025-03-22 20:34 ` [PATCH v2 4/9] fs: minix: register an initrd fs detector Julian Stecklina via B4 Relay
2025-03-22 23:26   ` kernel test robot
2025-03-22 20:34 ` [PATCH v2 5/9] fs: cramfs: " Julian Stecklina via B4 Relay
2025-03-22 20:34 ` [PATCH v2 6/9] fs: romfs: " Julian Stecklina via B4 Relay
2025-03-22 23:26   ` kernel test robot
2025-03-22 20:34 ` [PATCH v2 7/9] fs: squashfs: " Julian Stecklina via B4 Relay
2025-03-22 20:34 ` Julian Stecklina via B4 Relay [this message]
2025-03-22 23:36   ` [PATCH v2 8/9] fs: ext2, ext4: " kernel test robot
2025-03-22 20:34 ` [PATCH v2 9/9] fs: erofs: " Julian Stecklina via B4 Relay

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=20250322-initrd-erofs-v2-8-d66ee4a2c756@cyberus-technology.de \
    --to=devnull+julian.stecklina.cyberus-technology.de@kernel.org \
    --cc=brauner@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=julian.stecklina@cyberus-technology.de \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=xiang@kernel.org \
    /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

all inboxes | Powered by JetHome®