[PATCH 4/5] mtd: block2mtd: Add support for deleting block2mtd mapping

From: Pali RohÃr
Date: Fri Jun 02 2017 - 11:44:33 EST


This patch allows user to delete block2mtd mapping via parameters file
/sys/module/block2mtd/parameters/block2mtd

Syntax is "del=device", e.g.:

$ echo -n del=/dev/loop0 > /sys/module/block2mtd/parameters/block2mtd

Signed-off-by: Pali RohÃr <pali.rohar@xxxxxxxxx>
---
drivers/mtd/devices/block2mtd.c | 35 +++++++++++++++++++++++++++--------
1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/drivers/mtd/devices/block2mtd.c b/drivers/mtd/devices/block2mtd.c
index ad96937..9b6f7b5 100644
--- a/drivers/mtd/devices/block2mtd.c
+++ b/drivers/mtd/devices/block2mtd.c
@@ -355,6 +355,19 @@ static struct block2mtd_dev *add_device(char *devname, uint32_t erase_size,
return NULL;
}

+static void del_device(struct block2mtd_dev *dev)
+{
+ if (!dev->ro_mode)
+ block2mtd_sync(&dev->mtd);
+ mtd_device_unregister(&dev->mtd);
+ mutex_destroy(&dev->write_mutex);
+ pr_info("mtd%d: [%s] removed\n",
+ dev->mtd.index,
+ dev->mtd.name + strlen("block2mtd: "));
+ list_del(&dev->list);
+ block2mtd_free_device(dev);
+}
+

/* This function works similar to reguler strtoul. In addition, it
* allows some suffixes for a more human-readable number format:
@@ -477,6 +490,19 @@ static int block2mtd_setup2(const char *val)
}
}

+ if (strncmp(name, "del=", strlen("del=")) == 0) {
+ struct list_head *pos, *next;
+ list_for_each_safe(pos, next, &blkmtd_device_list) {
+ struct block2mtd_dev *dev =
+ list_entry(pos, typeof(*dev), list);
+ if (strcmp(dev->mtd.name + strlen("block2mtd: "),
+ name + strlen("del=")) != 0)
+ continue;
+ del_device(dev);
+ return 0;
+ }
+ }
+
add_device(name, erase_size, write_size, subpage_sft, timeout);

return 0;
@@ -536,14 +562,7 @@ static void block2mtd_exit(void)
/* Remove the MTD devices */
list_for_each_safe(pos, next, &blkmtd_device_list) {
struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
- block2mtd_sync(&dev->mtd);
- mtd_device_unregister(&dev->mtd);
- mutex_destroy(&dev->write_mutex);
- pr_info("mtd%d: [%s] removed\n",
- dev->mtd.index,
- dev->mtd.name + strlen("block2mtd: "));
- list_del(&dev->list);
- block2mtd_free_device(dev);
+ del_device(dev);
}
}

--
1.7.9.5