mtd: Introduce an expert mode for forensics and debugging purposes
authorMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 18 Nov 2021 11:46:59 +0000 (12:46 +0100)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Thu, 9 Dec 2021 16:51:59 +0000 (17:51 +0100)
When developping NAND controller drivers or when debugging filesystem
corruptions, it is quite common to need hacking locally into the
MTD/NAND core in order to get access to the content of the bad
blocks. Instead of having multiple implementations out there let's
provide a simple yet effective specific MTD-wide debugfs entry to fully
disable these checks on purpose.

A warning is added to inform the user when this mode gets enabled.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20211118114659.1282855-1-miquel.raynal@bootlin.com
drivers/mtd/mtdcore.c
drivers/mtd/nand/core.c
drivers/mtd/nand/raw/nand_base.c
drivers/mtd/nand/raw/nand_bbt.c
include/linux/mtd/mtd.h

index a7723a708ea7ef3b1cc5a629792fb94f86dbfd01..70f492dce15892aa30020f019e1db63fffec7596 100644 (file)
@@ -2370,6 +2370,14 @@ static struct backing_dev_info * __init mtd_bdi_init(const char *name)
        return ret ? ERR_PTR(ret) : bdi;
 }
 
+char *mtd_expert_analysis_warning =
+       "Bad block checks have been entirely disabled.\n"
+       "This is only reserved for post-mortem forensics and debug purposes.\n"
+       "Never enable this mode if you do not know what you are doing!\n";
+EXPORT_SYMBOL_GPL(mtd_expert_analysis_warning);
+bool mtd_expert_analysis_mode;
+EXPORT_SYMBOL_GPL(mtd_expert_analysis_mode);
+
 static struct proc_dir_entry *proc_mtd;
 
 static int __init init_mtd(void)
@@ -2393,6 +2401,8 @@ static int __init init_mtd(void)
                goto out_procfs;
 
        dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
+       debugfs_create_bool("expert_analysis_mode", 0600, dfs_dir_mtd,
+                           &mtd_expert_analysis_mode);
 
        return 0;
 
index 5e13a03d2b32f69b9a2a149caf2a7c7fcf59e824..416947f28b67d862d1be5d0fafb09e25801d4646 100644 (file)
@@ -21,6 +21,9 @@
  */
 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos)
 {
+       if (WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning))
+               return false;
+
        if (nanddev_bbt_is_initialized(nand)) {
                unsigned int entry;
                int status;
index 3d6c6e88052072751886f8391ae1561f960e8b72..b3a9bc08b4bbbff590b830db04366ab3deb97aca 100644 (file)
@@ -321,6 +321,9 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
        if (nand_region_is_secured(chip, ofs, mtd->erasesize))
                return -EIO;
 
+       if (WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning))
+               return 0;
+
        if (chip->legacy.block_bad)
                return chip->legacy.block_bad(chip, ofs);
 
index b7ad030225f8e7e53c8026bb59f544c236a23e9a..ab630af3a30959c9680532619c3501ad27f873b1 100644 (file)
@@ -1455,6 +1455,9 @@ int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
        pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
                 (unsigned int)offs, block, res);
 
+       if (WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning))
+               return 0;
+
        switch (res) {
        case BBT_BLOCK_GOOD:
                return 0;
index f5e7dfc2e4e91b95d1e7d94e086bfb80cc3835df..1ffa933121f6e3f86422d5a7d1fa2ea311f31b29 100644 (file)
@@ -711,4 +711,7 @@ static inline int mtd_is_bitflip_or_eccerr(int err) {
 
 unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
 
+extern char *mtd_expert_analysis_warning;
+extern bool mtd_expert_analysis_mode;
+
 #endif /* __MTD_MTD_H__ */