block: don't probe mtdblock on NAND (with legacy exceptions)
authorDaniel Golle <daniel@makrotopia.org>
Sun, 1 May 2022 13:13:01 +0000 (14:13 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Sun, 1 May 2022 15:56:39 +0000 (16:56 +0100)
Recent kernels started to spill warnings on the log if a userspace
process open()s an mtdblock device backed by NAND flash:

mtdblock: MTD device 'foo' is NAND, please consider using UBI block devices instead.

The warning itself is legitimate -- one really shouldn't be using
mtdblock on NAND.
Hence make fstools skip probing mtdblock devices if their underlaying
mtd device is of type 'nand'. As we don't want to break boards actually
using JFFS2 and squashfs directly on NAND, still probe the mtdblock
device in case the mtd device name is 'rootfs' or 'rootfs_data'.
This will then also trigger the kernel warning as it should.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
block.c

diff --git a/block.c b/block.c
index 9f5cebf28f5adddbf42791fa9b6b00bff11b567b..3773a6eb22e9ccecb3dd5d75265c7763c467c657 100644 (file)
--- a/block.c
+++ b/block.c
@@ -481,11 +481,64 @@ static int config_load(char *cfg)
        return 0;
 }
 
        return 0;
 }
 
+static bool mtdblock_is_nand(char *mtdnum)
+{
+       char tmppath[64];
+       char buf[16];
+       FILE *fp;
+
+       snprintf(tmppath, sizeof(tmppath) - 1, "/sys/class/mtd/mtd%s/type", mtdnum);
+       fp = fopen(tmppath, "r");
+       if (!fp)
+               return false;
+
+       if (!fgets(buf, sizeof(buf), fp)) {
+               fclose(fp);
+               return false;
+       }
+       fclose(fp);
+       buf[sizeof(buf) - 1] = '\0'; /* make sure buf is 0-terminated */
+       buf[strlen(buf) - 1] = '\0'; /* strip final char (newline) */
+
+       if (strcmp(buf, "nand"))
+               return false;
+
+       /*
+        * --- CUT HERE ---
+        * Keep probing rootfs and rootfs_data in the meantime to not break
+        * devices using JFFS2 on NAND but only trigger the kernel warnings.
+        * Remove this once all devices using JFFS2 and squashfs directly on
+        * NAND have been converted to UBI.
+        */
+       snprintf(tmppath, sizeof(tmppath) - 1, "/sys/class/mtd/mtd%s/name", mtdnum);
+       fp = fopen(tmppath, "r");
+       if (!fp)
+               return false;
+
+       if (!fgets(buf, sizeof(buf), fp)) {
+               fclose(fp);
+               return false;
+       }
+       fclose(fp);
+       buf[sizeof(buf) - 1] = '\0'; /* make sure buf is 0-terminated */
+       buf[strlen(buf) - 1] = '\0'; /* strip final char (newline) */
+
+       /* only return true if name differs from 'rootfs' and 'rootfs_data' */
+       if (strcmp(buf, "rootfs") && strcmp(buf, "rootfs_data"))
+               return true;
+
+       /* --- CUT HERE --- */
+       return false;
+}
+
 static struct probe_info* _probe_path(char *path)
 {
        struct probe_info *pr, *epr;
        char tmppath[64];
 
 static struct probe_info* _probe_path(char *path)
 {
        struct probe_info *pr, *epr;
        char tmppath[64];
 
+       if (!strncmp(path, "/dev/mtdblock", 13) && mtdblock_is_nand(path + 13))
+               return NULL;
+
        pr = probe_path(path);
        if (!pr)
                return NULL;
        pr = probe_path(path);
        if (!pr)
                return NULL;