Skip to contentSkip to conversation

initrd: cleanup and erofs support

PATCH In review Julian Stecklina 3 participants 15 messages Mar 22, 2025 – Mar 25, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v2 cover initrd: cleanup and erofs support

On my journey towards adding erofs support for initrd, Al Viro suggested to move the filesystem detection code into the respective filesystem modules. This patch series implements this, while also adding erofs support.

To achieve this, I added a macro initrd_fs_detect() that allows filesystem modules to add a filesystem detection hooks. I then moved all existing filesystem detection code to this new API. While I was there I also tried to clean up some of the code.

I've tested these changes with the following kinds of initrd images:

- ext2
- Minix v1
- cramfs (padded/unpadded)
- romfs
- squashfs
- erofs

initrds are still relevant, because they have some advantages over initramfs. They don't require unpacking all files before starting the init process and allows them to stay compressed in memory. They also allow using advanced file system features, such as extended attributes. In the NixOS community, we are heavy users of erofs, due to its sweet spot of compression, speed and features.

That being said, I'm totally in favor of cutting down the supported filesystems for initrd and further simplify the code. I would be surprised, if anyone is using ext2 or Minix v1 filesystems (64 MiB filesystem size limit!) or cramfs (16 MiB file size limit!) as an initrd these days! Squashfs and erofs seem genuinely useful, though.

---
Changes in v2:
- Remove more legacy code
- Introduce initrd_fs_detect
- Move all other initrd filesystems to the new API
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Julian Stecklina (9):
initrd: remove ASCII spinner
initrd: fix double fput for truncated ramdisks
initrd: add a generic mechanism to add fs detectors
fs: minix: register an initrd fs detector
fs: cramfs: register an initrd fs detector
fs: romfs: register an initrd fs detector
fs: squashfs: register an initrd fs detector
fs: ext2, ext4: register an initrd fs detector
fs: erofs: register an initrd fs detector

 fs/cramfs/Makefile                |   5 ++
 fs/cramfs/initrd.c                |  41 +++++++++++++
 fs/erofs/Makefile                 |   5 ++
 fs/erofs/initrd.c                 |  19 ++++++
 fs/ext2/Makefile                  |   5 ++
 fs/ext2/initrd.c                  |  27 +++++++++
 fs/ext4/Makefile                  |   4 ++
 fs/minix/Makefile                 |   5 ++
 fs/minix/initrd.c                 |  23 +++++++
 fs/romfs/Makefile                 |   4 ++
 fs/romfs/initrd.c                 |  22 +++++++
 fs/squashfs/Makefile              |   5 ++
 fs/squashfs/initrd.c              |  23 +++++++
 include/asm-generic/vmlinux.lds.h |   6 ++
 include/linux/ext2_fs.h           |   9 ---
 include/linux/initrd.h            |  37 ++++++++++++
 init/do_mounts_rd.c               | 122 ++++++++------------------------------
 17 files changed, 257 insertions(+), 105 deletions(-)

---
base-commit: 88d324e69ea9f3ae1c1905ea75d717c08bdb8e15
change-id: 20250320-initrd-erofs-76e925fdf68c

Best regards,

Signature · 1 line
Julian Stecklina <[email protected]>
#1 lore raw

PATCH v2 1/9 initrd: remove ASCII spinner

Writing the ASCII spinner probably costs more cycles than copying the block of data on some output devices if you output to serial and in all other cases it rotates at lightspeed in 2025.

---
 init/do_mounts_rd.c | 11 -----------
 1 file changed, 11 deletions(-)
init/do_mounts_rd.c +0 -11
index ac021ae6e6fa78c7b7828a78ab2fa3af3611bef3..473f4f9417e157118b9a6e582607435484d53d63 100644
@@ -189,11 +189,7 @@ int __init rd_load_image(char *from)
 	unsigned long rd_blocks, devblocks;
 	int nblocks, i;
 	char *buf = NULL;
-	unsigned short rotate = 0;
 	decompress_fn decompressor = NULL;
-#if !defined(CONFIG_S390)
-	char rotator[4] = { '|' , '/' , '-' , '\\' };
-#endif
 
 	out_file = filp_open("/dev/ram", O_RDWR, 0);
 	if (IS_ERR(out_file))
@@ -249,18 +245,11 @@ int __init rd_load_image(char *from)
 	for (i = 0; i < nblocks; i++) {
 		if (i && (i % devblocks == 0)) {
 			pr_cont("done disk #1.\n");
-			rotate = 0;
 			fput(in_file);
 			break;
 		}
 		kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
 		kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
-#if !defined(CONFIG_S390)
-		if (!(i % 16)) {
-			pr_cont("%c\b", rotator[rotate & 0x3]);
-			rotate++;
-		}
-#endif
 	}
 	pr_cont("done.\n");
 
Signature · 1 line
2.47.0
Reviewed-by on v2 1/9
#2 lore raw

Re: [PATCH v2 1/9] initrd: remove ASCII spinner

init/do_mounts_rd.c

Quoted 13 lines

From: Julian Stecklina <[email protected]>

Writing the ASCII spinner probably costs more cycles than copying the block of data on some output devices if you output to serial and in all other cases it rotates at lightspeed in 2025.

---
 init/do_mounts_rd.c | 11 -----------
 1 file changed, 11 deletions(-)
init/do_mounts_rd.c +0 -0
index ac021ae6e6fa78c7b7828a78ab2fa3af3611bef3..473f4f9417e157118b9a6e582607435484d53d63 100644

Looks good.

Will wait for v3 before looking at the rest of the series.

#3 lore raw

PATCH v2 2/9 initrd: fix double fput for truncated ramdisks

In reply to Julian Stecklina

When you actually take the exit via devblocks == 0, the code will do fput(in_file) twice. This was introduced when the APIs where switched to the file-based APIs.

Remove more of the multi-floppy support from the olden days.

Fixes bef173299613 ("initrd: switch initrd loading to struct file based APIs")
---
 init/do_mounts_rd.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
init/do_mounts_rd.c +7 -7
index 473f4f9417e157118b9a6e582607435484d53d63..d026df401afa0b7458ab1f266b21830aab974b92 100644
@@ -234,20 +234,20 @@ int __init rd_load_image(char *from)
 		goto done;
 	}
 
+	if (devblocks < nblocks) {
+		printk(KERN_ERR "RAMDISK: looks truncated: (%luKiB vs %dKiB) - continuing anyway\n",
+		       devblocks, nblocks);
+		nblocks = devblocks;
+	}
+
 	buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
 	if (!buf) {
 		printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
 		goto done;
 	}
 
-	printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
-		nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
+	printk(KERN_NOTICE "RAMDISK: Loading %dKiB into ram disk... ", nblocks);
 	for (i = 0; i < nblocks; i++) {
-		if (i && (i % devblocks == 0)) {
-			pr_cont("done disk #1.\n");
-			fput(in_file);
-			break;
-		}
 		kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
 		kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
 	}
Signature · 1 line
2.47.0
#4 lore raw

PATCH v2 3/9 initrd: add a generic mechanism to add fs detectors

In reply to Julian Stecklina

So far all filesystems that are supported as initrd have their filesystem detection code implemented in init/do_mounts_rd.c. A better approach is to let filesystem implementations register a detection hook. This allows the filesystem detection code to live with the rest of the filesystem implementation.

We could have done a more flexible mechanism than passing a block of data, but this simple solution works for all filesystems and keeps the boilerplate low.

The following patches will convert each of the filesystems.

---
 include/asm-generic/vmlinux.lds.h |  6 ++++++
 include/linux/initrd.h            | 37 +++++++++++++++++++++++++++++++++++++
 init/do_mounts_rd.c               | 28 +++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 1 deletion(-)
include/asm-generic/vmlinux.lds.h +6 -0
index 0d5b186abee86d27f3b02a49299155453a8c8e9e..d0816e6c41a9bbedf8f5a68c33b6f3e18014019a 100644
@@ -968,8 +968,13 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
 	KEEP(*(.init.ramfs))						\
 	. = ALIGN(8);							\
 	KEEP(*(.init.ramfs.info))
+
+#define INITRD_FS_DETECT()						\
+	. = ALIGN(16);							\
+	BOUNDED_SECTION(_initrd_fs_detect)
 #else
 #define INIT_RAM_FS
+#define INITRD_FS_DETECT()
 #endif
 
 /*
@@ -1170,6 +1175,7 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
 		INIT_CALLS						\
 		CON_INITCALL						\
 		INIT_RAM_FS						\
+		INITRD_FS_DETECT()					\
 	}
 
 #define BSS_SECTION(sbss_align, bss_align, stop_align)			\
include/linux/initrd.h +37 -0
index f1a1f4c92ded3921bf56d53bee3e20b549d851fb..25463ce9c26ad4e4e9d3b333aa9f5596585c1762 100644
@@ -3,6 +3,9 @@
 #ifndef __LINUX_INITRD_H
 #define __LINUX_INITRD_H
 
+#include <linux/init.h>
+#include <linux/types.h>
+
 #define INITRD_MINOR 250 /* shouldn't collide with /dev/ram* too soon ... */
 
 /* starting block # of image */
@@ -18,12 +21,46 @@ extern int initrd_below_start_ok;
 extern unsigned long initrd_start, initrd_end;
 extern void free_initrd_mem(unsigned long, unsigned long);
 
+struct file;
+
 #ifdef CONFIG_BLK_DEV_INITRD
 extern void __init reserve_initrd_mem(void);
 extern void wait_for_initramfs(void);
+
+/*
+ * Detect a filesystem on the initrd. You get 1 KiB (BLOCK_SIZE) of
+ * data to work with. The offset of the block is specified in
+ * initrd_fs_detect().
+ *
+ * @block_data: A pointer to BLOCK_SIZE of data
+ *
+ * Returns the size of the filesystem in bytes or 0, if the filesystem
+ * was not detected.
+ */
+typedef size_t initrd_fs_detect_fn(void * const block_data);
+
+struct initrd_detect_fs {
+	initrd_fs_detect_fn *detect_fn;
+	loff_t detect_byte_offset;
+};
+
+extern struct initrd_detect_fs __start_initrd_fs_detect[];
+extern struct initrd_detect_fs __stop_initrd_fs_detect[];
+
+/*
+ * Add a filesystem detector for initrds. See the documentation of
+ * initrd_fs_detect_fn above.
+ */
+#define initrd_fs_detect(fn, byte_offset)					\
+	static const struct initrd_detect_fs __initrd_fs_detect_ ## fn		\
+	__used __section("_initrd_fs_detect") =					\
+		{ .detect_fn = fn, .detect_byte_offset = byte_offset}
+
 #else
 static inline void __init reserve_initrd_mem(void) {}
 static inline void wait_for_initramfs(void) {}
+
+#define initrd_fs_detect(detectfn)
 #endif
 
 extern phys_addr_t phys_initrd_start;
init/do_mounts_rd.c +27 -1
index d026df401afa0b7458ab1f266b21830aab974b92..56c1fa935c7ee780870142923046a3d2fd2d6d96 100644
@@ -58,7 +58,7 @@ static int __init
 identify_ramdisk_image(struct file *file, loff_t pos,
 		decompress_fn *decompressor)
 {
-	const int size = 512;
+	const int size = BLOCK_SIZE;
 	struct minix_super_block *minixsb;
 	struct romfs_super_block *romfsb;
 	struct cramfs_super *cramfsb;
@@ -68,6 +68,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	const char *compress_name;
 	unsigned long n;
 	int start_block = rd_image_start;
+	struct initrd_detect_fs *detect_fs;
 
 	buf = kmalloc(size, GFP_KERNEL);
 	if (!buf)
@@ -165,6 +166,31 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		goto done;
 	}
 
+	/* Try to find a filesystem in the initrd */
+	for (detect_fs = __start_initrd_fs_detect;
+	     detect_fs < __stop_initrd_fs_detect;
+	     detect_fs++
+	     ) {
+		size_t fs_size;
+
+		pos = (start_block * BLOCK_SIZE) + detect_fs->detect_byte_offset;
+		kernel_read(file, buf, size, &pos);
+
+		fs_size = detect_fs->detect_fn(buf);
+
+		if (fs_size == 0)
+			continue;
+
+		nblocks = (fs_size + BLOCK_SIZE + 1)
+			>> BLOCK_SIZE_BITS;
+
+		printk(KERN_NOTICE
+		       "RAMDISK: filesystem found (%d blocks)\n",
+		       nblocks);
+
+		goto done;
+	}
+
 	printk(KERN_NOTICE
 	       "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
 	       start_block);
Signature · 1 line
2.47.0
#5 lore raw

Re: [PATCH v2 3/9] initrd: add a generic mechanism to add fs detectors

Quoted 37 lines · On Sat, 2025-03-22 at 21:34 +0100, Julian Stecklina via B4 Relay wrote:

 #ifdef CONFIG_BLK_DEV_INITRD
 extern void __init reserve_initrd_mem(void);
 extern void wait_for_initramfs(void);

include/linux/initrd.h +30 -0
+
+/*
+ * Detect a filesystem on the initrd. You get 1 KiB (BLOCK_SIZE) of
+ * data to work with. The offset of the block is specified in
+ * initrd_fs_detect().
+ *
+ * @block_data: A pointer to BLOCK_SIZE of data
+ *
+ * Returns the size of the filesystem in bytes or 0, if the filesystem
+ * was not detected.
+ */
+typedef size_t initrd_fs_detect_fn(void * const block_data);
+
+struct initrd_detect_fs {
+ initrd_fs_detect_fn *detect_fn;
+ loff_t detect_byte_offset;
+};
+
+extern struct initrd_detect_fs __start_initrd_fs_detect[];
+extern struct initrd_detect_fs __stop_initrd_fs_detect[];
+
+/*
+ * Add a filesystem detector for initrds. See the documentation of
+ * initrd_fs_detect_fn above.
+ */
+#define initrd_fs_detect(fn, byte_offset) \
+ static const struct initrd_detect_fs __initrd_fs_detect_ ## fn \
+ __used __section("_initrd_fs_detect") = \
+ { .detect_fn = fn, .detect_byte_offset = byte_offset}
+

 #else
 static inline void __init reserve_initrd_mem(void) {} static inline void wait_for_initramfs(void) {}

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define initrd_fs_detect(detectfn)

The !CONFIG_BLK_DEV_INITRD path is broken. Will fix in v3 and add it to my test plan.

Julian

#6 lore raw

PATCH v2 4/9 fs: minix: register an initrd fs detector

In reply to Julian Stecklina

Port minix to the new initrd_fs_detect API. There are no functional changes.

This code only supports the minix filesystem v1. This means 64 MiB filesystem size limit. This would be a good candidate to drop support for.

---
 fs/minix/Makefile   |  5 +++++
 fs/minix/initrd.c   | 23 +++++++++++++++++++++++
 init/do_mounts_rd.c | 16 +---------------
 3 files changed, 29 insertions(+), 15 deletions(-)
fs/minix/Makefile +5 -0
index a2d3ab58d1873eeada679a33a65b6cd0c421b3ad..cdd6a1a5b57a0205a1946faa676994c367380c97 100644
@@ -6,3 +6,8 @@
 obj-$(CONFIG_MINIX_FS) += minix.o
 
 minix-objs := bitmap.o itree_v1.o itree_v2.o namei.o inode.o file.o dir.o
+
+# If we are built-in, we provide support for minix filesystem on initrds.
+ifeq ($(CONFIG_MINIX_FS),y)
+minix-objs += initrd.o
+endif
fs/minix/initrd.c new file +23 -0
new file mode 100644
index 0000000000000000000000000000000000000000..18b39b9afe9994ec3dd9770eb516f9c25c183140
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/fs.h>
+#include <linux/initrd.h>
+#include <linux/magic.h>
+#include <linux/minix_fs.h>
+
+static size_t __init detect_minixfs(void *block_data)
+{
+	struct minix_super_block *minixsb =
+		(struct minix_super_block *)block_data;
+	BUILD_BUG_ON(sizeof(*minixsb) > BLOCK_SIZE);
+
+	if (minixsb->s_magic != MINIX_SUPER_MAGIC &&
+	    minixsb->s_magic != MINIX_SUPER_MAGIC2)
+		return 0;
+
+
+	return minixsb->s_nzones
+		<< (minixsb->s_log_zone_size + BLOCK_SIZE_BITS);
+}
+
+initrd_fs_detect(detect_minixfs, BLOCK_SIZE);
init/do_mounts_rd.c +1 -15
index 56c1fa935c7ee780870142923046a3d2fd2d6d96..f7e5d4ccf029b2707bc8524ecdbe200c8b305b00 100644
@@ -1,7 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/kernel.h>
 #include <linux/fs.h>
-#include <linux/minix_fs.h>
 #include <linux/ext2_fs.h>
 #include <linux/romfs_fs.h>
 #include <uapi/linux/cramfs_fs.h>
@@ -42,7 +41,6 @@ static int __init crd_load(decompress_fn deco);
  * numbers could not be found.
  *
  * We currently check for the following magic numbers:
- *	minix
  *	ext2
  *	romfs
  *	cramfs
@@ -59,7 +57,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		decompress_fn *decompressor)
 {
 	const int size = BLOCK_SIZE;
-	struct minix_super_block *minixsb;
 	struct romfs_super_block *romfsb;
 	struct cramfs_super *cramfsb;
 	struct squashfs_super_block *squashfsb;
@@ -74,7 +71,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	if (!buf)
 		return -ENOMEM;
 
-	minixsb = (struct minix_super_block *) buf;
 	romfsb = (struct romfs_super_block *) buf;
 	cramfsb = (struct cramfs_super *) buf;
 	squashfsb = (struct squashfs_super_block *) buf;
@@ -141,21 +137,11 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	}
 
 	/*
-	 * Read block 1 to test for minix and ext2 superblock
+	 * Read block 1 to test for ext2 superblock
 	 */
 	pos = (start_block + 1) * BLOCK_SIZE;
 	kernel_read(file, buf, size, &pos);
 
-	/* Try minix */
-	if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
-	    minixsb->s_magic == MINIX_SUPER_MAGIC2) {
-		printk(KERN_NOTICE
-		       "RAMDISK: Minix filesystem found at block %d\n",
-		       start_block);
-		nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
-		goto done;
-	}
-
 	/* Try ext2 */
 	n = ext2_image_size(buf);
 	if (n) {
Signature · 1 line
2.47.0
#7 lore raw

Re: [PATCH v2 4/9] fs: minix: register an initrd fs detector

kernel test robot noticed the following build errors:

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-4-d66ee4a2c756%40cyberus-technology.de
patch subject: [PATCH v2 4/9] fs: minix: register an initrd fs detector
config: um-randconfig-002-20250323 (https://download.01.org/0day-ci/archive/20250323/[email protected]/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250323/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

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

2 errors generated.

vim +23 fs/minix/initrd.c

    22	
  > 23	initrd_fs_detect(detect_minixfs, BLOCK_SIZE);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw

PATCH v2 5/9 fs: cramfs: register an initrd fs detector

In reply to Julian Stecklina

Port cramfs to the new initrd_fs_detect API. There are no functional changes.

---
 fs/cramfs/Makefile  |  5 +++++
 fs/cramfs/initrd.c  | 41 +++++++++++++++++++++++++++++++++++++++++
 init/do_mounts_rd.c | 28 ++--------------------------
 3 files changed, 48 insertions(+), 26 deletions(-)
fs/cramfs/Makefile +5 -0
index 8c3ed298241924b0312fb489b1fb54d274d25c22..d10e2b72aae06bf9ac42690e3967cfd90ac34d4f 100644
@@ -6,3 +6,8 @@
 obj-$(CONFIG_CRAMFS) += cramfs.o
 
 cramfs-objs := inode.o uncompress.o
+
+# If we are built-in, we provide support for cramfs on initrds.
+ifeq ($(CONFIG_CRAMFS),y)
+cramfs-objs += initrd.o
+endif
fs/cramfs/initrd.c new file +41 -0

41 lines not on this page · raw message

init/do_mounts_rd.c +2 -26
index f7e5d4ccf029b2707bc8524ecdbe200c8b305b00..cdc39baaf3a1a65daad5fe6571a82faf3fc95b62 100644
@@ -3,7 +3,7 @@
 #include <linux/fs.h>
 #include <linux/ext2_fs.h>
 #include <linux/romfs_fs.h>
-#include <uapi/linux/cramfs_fs.h>
+
 #include <linux/initrd.h>
 #include <linux/string.h>
 #include <linux/slab.h>
@@ -43,7 +43,6 @@ static int __init crd_load(decompress_fn deco);
  * We currently check for the following magic numbers:
  *	ext2
  *	romfs
- *	cramfs
  *	squashfs
  *	gzip
  *	bzip2
@@ -58,7 +57,7 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 {
 	const int size = BLOCK_SIZE;
 	struct romfs_super_block *romfsb;
-	struct cramfs_super *cramfsb;
+
 	struct squashfs_super_block *squashfsb;
 	int nblocks = -1;
 	unsigned char *buf;
@@ -72,7 +71,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		return -ENOMEM;
 
 	romfsb = (struct romfs_super_block *) buf;
-	cramfsb = (struct cramfs_super *) buf;
 	squashfsb = (struct squashfs_super_block *) buf;
 	memset(buf, 0xe5, size);
 
@@ -104,14 +102,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		goto done;
 	}
 
-	if (cramfsb->magic == CRAMFS_MAGIC) {
-		printk(KERN_NOTICE
-		       "RAMDISK: cramfs filesystem found at block %d\n",
-		       start_block);
-		nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
-		goto done;
-	}
-
 	/* squashfs is at block zero too */
 	if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
 		printk(KERN_NOTICE
@@ -122,20 +112,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		goto done;
 	}
 
-	/*
-	 * Read 512 bytes further to check if cramfs is padded
-	 */
-	pos = start_block * BLOCK_SIZE + 0x200;
-	kernel_read(file, buf, size, &pos);
-
-	if (cramfsb->magic == CRAMFS_MAGIC) {
-		printk(KERN_NOTICE
-		       "RAMDISK: cramfs filesystem found at block %d\n",
-		       start_block);
-		nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
-		goto done;
-	}
-
 	/*
 	 * Read block 1 to test for ext2 superblock
 	 */
Signature · 1 line
2.47.0
#9 lore raw

PATCH v2 6/9 fs: romfs: register an initrd fs detector

In reply to Julian Stecklina

Port romfs from to the new initrd_fs_detect API. There are no functional changes.

---
 fs/romfs/Makefile   |  4 ++++
 fs/romfs/initrd.c   | 22 ++++++++++++++++++++++
 init/do_mounts_rd.c | 14 --------------
 3 files changed, 26 insertions(+), 14 deletions(-)
fs/romfs/Makefile +4 -0
index 844928f1571160abed9d5aff54152b5508eaf7be..eb15dc3a78721d7f650560a404a92706260e9b63 100644
@@ -11,3 +11,7 @@ ifneq ($(CONFIG_MMU),y)
 romfs-$(CONFIG_ROMFS_ON_MTD) += mmap-nommu.o
 endif
 
+# If we are built-in, we provide support for romfs on initrds.
+ifeq ($(CONFIG_ROMFS_FS),y)
+romfs-y += initrd.o
+endif
fs/romfs/initrd.c new file +22 -0
new file mode 100644
index 0000000000000000000000000000000000000000..0ec7b4c9d1f79fac892b7fb1d8e17122092008a8
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/fs.h>
+#include <linux/initrd.h>
+#include <linux/magic.h>
+#include <linux/romfs_fs.h>
+
+static size_t __init detect_romfs(void *block_data)
+{
+	struct romfs_super_block *romfsb
+		= (struct romfs_super_block *)block_data;
+	BUILD_BUG_ON(sizeof(*romfsb) > BLOCK_SIZE);
+
+	/* The definitions of ROMSB_WORD* already handle endianness. */
+	if (romfsb->word0 != ROMSB_WORD0 ||
+	    romfsb->word1 != ROMSB_WORD1)
+		return 0;
+
+	return be32_to_cpu(romfsb->size);
+}
+
+initrd_fs_detect(detect_romfs, 0);
init/do_mounts_rd.c +0 -14
index cdc39baaf3a1a65daad5fe6571a82faf3fc95b62..9f9a04cce505e532d444e2aef77037bc2b01da08 100644
@@ -2,7 +2,6 @@
 #include <linux/kernel.h>
 #include <linux/fs.h>
 #include <linux/ext2_fs.h>
-#include <linux/romfs_fs.h>
 
 #include <linux/initrd.h>
 #include <linux/string.h>
@@ -42,7 +41,6 @@ static int __init crd_load(decompress_fn deco);
  *
  * We currently check for the following magic numbers:
  *	ext2
- *	romfs
  *	squashfs
  *	gzip
  *	bzip2
@@ -56,7 +54,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		decompress_fn *decompressor)
 {
 	const int size = BLOCK_SIZE;
-	struct romfs_super_block *romfsb;
 
 	struct squashfs_super_block *squashfsb;
 	int nblocks = -1;
@@ -70,7 +67,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	if (!buf)
 		return -ENOMEM;
 
-	romfsb = (struct romfs_super_block *) buf;
 	squashfsb = (struct squashfs_super_block *) buf;
 	memset(buf, 0xe5, size);
 
@@ -92,16 +88,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		goto done;
 	}
 
-	/* romfs is at block zero too */
-	if (romfsb->word0 == ROMSB_WORD0 &&
-	    romfsb->word1 == ROMSB_WORD1) {
-		printk(KERN_NOTICE
-		       "RAMDISK: romfs filesystem found at block %d\n",
-		       start_block);
-		nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
-		goto done;
-	}
-
 	/* squashfs is at block zero too */
 	if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
 		printk(KERN_NOTICE
Signature · 1 line
2.47.0
#10 lore raw

Re: [PATCH v2 6/9] fs: romfs: register an initrd fs detector

kernel test robot noticed the following build errors:

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-6-d66ee4a2c756%40cyberus-technology.de
patch subject: [PATCH v2 6/9] fs: romfs: register an initrd fs detector
config: i386-buildonly-randconfig-002-20250323 (https://download.01.org/0day-ci/archive/20250323/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250323/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

>> fs/romfs/initrd.c:22:33: error: macro "initrd_fs_detect" passed 2 arguments, but takes just 1
      22 | initrd_fs_detect(detect_romfs, 0);
         |                                 ^
   In file included from fs/romfs/initrd.c:4:
   include/linux/initrd.h:63: note: macro "initrd_fs_detect" defined here
      63 | #define initrd_fs_detect(detectfn)
         | 
>> fs/romfs/initrd.c:22:1: warning: data definition has no type or storage class
      22 | initrd_fs_detect(detect_romfs, 0);
         | ^~~~~~~~~~~~~~~~
>> fs/romfs/initrd.c:22:1: error: type defaults to 'int' in declaration of 'initrd_fs_detect' [-Werror=implicit-int]
>> fs/romfs/initrd.c:8:22: warning: 'detect_romfs' defined but not used [-Wunused-function]

8 | static size_t __init detect_romfs(void *block_data)
| ^~~~~~~~~~~~
cc1: some warnings being treated as errors

vim +/initrd_fs_detect +22 fs/romfs/initrd.c

     2	
     3	#include <linux/fs.h>
   > 4	#include <linux/initrd.h>
     5	#include <linux/magic.h>
     6	#include <linux/romfs_fs.h>
     7	
   > 8	static size_t __init detect_romfs(void *block_data)
     9	{
    10		struct romfs_super_block *romfsb
    11			= (struct romfs_super_block *)block_data;
    12		BUILD_BUG_ON(sizeof(*romfsb) > BLOCK_SIZE);
    13	
    14		/* The definitions of ROMSB_WORD* already handle endianness. */
    15		if (romfsb->word0 != ROMSB_WORD0 ||
    16		    romfsb->word1 != ROMSB_WORD1)
    17			return 0;
    18	
    19		return be32_to_cpu(romfsb->size);
    20	}
    21	
  > 22	initrd_fs_detect(detect_romfs, 0);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw

PATCH v2 7/9 fs: squashfs: register an initrd fs detector

In reply to Julian Stecklina

Port squashfs from to the new initrd_fs_detect API. There are no functional changes.

---
 fs/squashfs/Makefile |  5 +++++
 fs/squashfs/initrd.c | 23 +++++++++++++++++++++++
 init/do_mounts_rd.c  | 14 --------------
 3 files changed, 28 insertions(+), 14 deletions(-)
fs/squashfs/Makefile +5 -0
index 477c89a519ee8825e4dfc88f76e09fd733e90625..fa64f0b9a45e52e9b3e78bd16446474c0b3dc158 100644
@@ -17,3 +17,8 @@ squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o
 squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o
 squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o
 squashfs-$(CONFIG_SQUASHFS_ZSTD) += zstd_wrapper.o
+
+# If we are built-in, we provide support for squashfs on initrds.
+ifeq ($(CONFIG_SQUASHFS),y)
+squashfs-y += initrd.o
+endif
fs/squashfs/initrd.c new file +23 -0
new file mode 100644
index 0000000000000000000000000000000000000000..bb99fc40083c6c5fdf47b2e28bcdc525d36520b4
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/initrd.h>
+#include <linux/fs.h>
+#include <linux/magic.h>
+
+#include "squashfs_fs.h"
+
+static size_t __init detect_squashfs(void *block_data)
+{
+	struct squashfs_super_block *squashfsb
+		= (struct squashfs_super_block *)block_data;
+	BUILD_BUG_ON(sizeof(*squashfsb) > BLOCK_SIZE);
+
+		/* squashfs is at block zero too */
+	if (le32_to_cpu(squashfsb->s_magic) != SQUASHFS_MAGIC)
+		return 0;
+
+
+	return le64_to_cpu(squashfsb->bytes_used);
+}
+
+initrd_fs_detect(detect_squashfs, 0);
init/do_mounts_rd.c +0 -14
index 9f9a04cce505e532d444e2aef77037bc2b01da08..2a6cb08d0b4872ef8e861a813ef89dc1e9a150af 100644
@@ -8,7 +8,6 @@
 #include <linux/slab.h>
 
 #include "do_mounts.h"
-#include "../fs/squashfs/squashfs_fs.h"
 
 #include <linux/decompress/generic.h>
 
@@ -41,7 +40,6 @@ static int __init crd_load(decompress_fn deco);
  *
  * We currently check for the following magic numbers:
  *	ext2
- *	squashfs
  *	gzip
  *	bzip2
  *	lzma
@@ -55,7 +53,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 {
 	const int size = BLOCK_SIZE;
 
-	struct squashfs_super_block *squashfsb;
 	int nblocks = -1;
 	unsigned char *buf;
 	const char *compress_name;
@@ -67,7 +64,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 	if (!buf)
 		return -ENOMEM;
 
-	squashfsb = (struct squashfs_super_block *) buf;
 	memset(buf, 0xe5, size);
 
 	/*
@@ -88,16 +84,6 @@ identify_ramdisk_image(struct file *file, loff_t pos,
 		goto done;
 	}
 
-	/* squashfs is at block zero too */
-	if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
-		printk(KERN_NOTICE
-		       "RAMDISK: squashfs filesystem found at block %d\n",
-		       start_block);
-		nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
-			 >> BLOCK_SIZE_BITS;
-		goto done;
-	}
-
 	/*
 	 * Read block 1 to test for ext2 superblock
 	 */
Signature · 1 line
2.47.0
#12 lore raw

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

In reply to Julian Stecklina

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.

---
 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(-)
fs/ext2/Makefile +5 -0
index 8860948ef9ca4e0a9c3f90311c3cecf0c5b70c63..c38a5b209023f93c84e8a6b8995d1db0214bb01a 100644
@@ -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
fs/ext2/initrd.c new file +27 -0
new file mode 100644
index 0000000000000000000000000000000000000000..572930512b8b3bee0d733553117a026af6e2f833
@@ -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);
fs/ext4/Makefile +4 -0
index 72206a2926765feba6fc59332ffeca7c03c8677b..907c80c33c8fc1e5dee85f8e862c8b27615f1a04 100644
@@ -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
include/linux/ext2_fs.h +0 -9
index 1fef885690370e5c039871ac8dd99d649d72aa64..0662827c0c69c3b77fb74850e1b0f3626c14c713 100644
@@ -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 */
init/do_mounts_rd.c +0 -19
index 2a6cb08d0b4872ef8e861a813ef89dc1e9a150af..45d2c5f7da044166524bef808bb97bee46c3324b 100644
@@ -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;
Signature · 1 line
2.47.0
#13 lore raw

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

kernel test robot noticed the following build errors:

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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#14 lore raw

PATCH v2 9/9 fs: erofs: register an initrd fs detector

In reply to Julian Stecklina

Allow erofs to be used as a filesystem for initrds. It offers similar advantages as squashfs, but with higher performance and arguably nicer tooling. If we support squashfs, there is no reason not to support erofs as well.

---
 fs/erofs/Makefile |  5 +++++
 fs/erofs/initrd.c | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)
fs/erofs/Makefile +5 -0
index 4331d53c7109550a0518f2ed8df456deecdd2f8c..cea46a51dea2b9e22e4ba1478dd30de3262fe6cb 100644
@@ -9,3 +9,8 @@ erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o
 erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o
 erofs-$(CONFIG_EROFS_FS_BACKED_BY_FILE) += fileio.o
 erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
+
+# If we are built-in, we provide support for erofs on initrds.
+ifeq ($(CONFIG_EROFS_FS),y)
+erofs-objs += initrd.o
+endif
fs/erofs/initrd.c new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..e2bb313f55211a305e201f529d7da810898252ac
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/initrd.h>
+
+#include "internal.h"
+
+static size_t __init detect_erofs(void *block_data)
+{
+	struct erofs_super_block *erofsb = block_data;
+
+	BUILD_BUG_ON(sizeof(*erofsb) > BLOCK_SIZE);
+
+	if (le32_to_cpu(erofsb->magic) != EROFS_SUPER_MAGIC_V1)
+		return 0;
+
+	return le32_to_cpu(erofsb->blocks) << erofsb->blkszbits;
+}
+
+initrd_fs_detect(detect_erofs, EROFS_SUPER_OFFSET);
Signature · 1 line
2.47.0