440-block2mtd_init.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: block2mtd
  3. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  4. ---
  5. drivers/mtd/devices/block2mtd.c | 30 ++++++++++++++++++++----------
  6. 1 file changed, 20 insertions(+), 10 deletions(-)
  7. --- a/drivers/mtd/devices/block2mtd.c
  8. +++ b/drivers/mtd/devices/block2mtd.c
  9. @@ -26,6 +26,7 @@
  10. #include <linux/list.h>
  11. #include <linux/init.h>
  12. #include <linux/mtd/mtd.h>
  13. +#include <linux/mtd/partitions.h>
  14. #include <linux/mutex.h>
  15. #include <linux/mount.h>
  16. #include <linux/slab.h>
  17. @@ -219,7 +220,7 @@ static void block2mtd_free_device(struct
  18. static struct block2mtd_dev *add_device(char *devname, int erase_size,
  19. - int timeout)
  20. + const char *mtdname, int timeout)
  21. {
  22. #ifndef MODULE
  23. int i;
  24. @@ -227,6 +228,7 @@ static struct block2mtd_dev *add_device(
  25. const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
  26. struct block_device *bdev = ERR_PTR(-ENODEV);
  27. struct block2mtd_dev *dev;
  28. + struct mtd_partition *part;
  29. char *name;
  30. if (!devname)
  31. @@ -283,13 +285,16 @@ static struct block2mtd_dev *add_device(
  32. /* Setup the MTD structure */
  33. /* make the name contain the block device in */
  34. - name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
  35. + if (!mtdname)
  36. + mtdname = devname;
  37. + name = kmalloc(strlen(mtdname) + 1, GFP_KERNEL);
  38. if (!name)
  39. goto err_destroy_mutex;
  40. + strcpy(name, mtdname);
  41. dev->mtd.name = name;
  42. - dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
  43. + dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK & ~(erase_size - 1);
  44. dev->mtd.erasesize = erase_size;
  45. dev->mtd.writesize = 1;
  46. dev->mtd.writebufsize = PAGE_SIZE;
  47. @@ -302,7 +307,11 @@ static struct block2mtd_dev *add_device(
  48. dev->mtd.priv = dev;
  49. dev->mtd.owner = THIS_MODULE;
  50. - if (mtd_device_register(&dev->mtd, NULL, 0)) {
  51. + part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
  52. + part->name = name;
  53. + part->offset = 0;
  54. + part->size = dev->mtd.size;
  55. + if (mtd_device_register(&dev->mtd, part, 1)) {
  56. /* Device didn't get added, so free the entry */
  57. goto err_destroy_mutex;
  58. }
  59. @@ -310,8 +319,7 @@ static struct block2mtd_dev *add_device(
  60. list_add(&dev->list, &blkmtd_device_list);
  61. pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
  62. dev->mtd.index,
  63. - dev->mtd.name + strlen("block2mtd: "),
  64. - dev->mtd.erasesize >> 10, dev->mtd.erasesize);
  65. + mtdname, dev->mtd.erasesize >> 10, dev->mtd.erasesize);
  66. return dev;
  67. err_destroy_mutex:
  68. @@ -384,7 +392,7 @@ static int block2mtd_setup2(const char *
  69. /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
  70. char buf[80 + 12 + 80 + 8];
  71. char *str = buf;
  72. - char *token[2];
  73. + char *token[3];
  74. char *name;
  75. size_t erase_size = PAGE_SIZE;
  76. unsigned long timeout = MTD_DEFAULT_TIMEOUT;
  77. @@ -398,7 +406,7 @@ static int block2mtd_setup2(const char *
  78. strcpy(str, val);
  79. kill_final_newline(str);
  80. - for (i = 0; i < 2; i++)
  81. + for (i = 0; i < 3; i++)
  82. token[i] = strsep(&str, ",");
  83. if (str) {
  84. @@ -424,8 +432,10 @@ static int block2mtd_setup2(const char *
  85. return 0;
  86. }
  87. }
  88. + if (token[2] && (strlen(token[2]) + 1 > 80))
  89. + pr_err("mtd device name too long\n");
  90. - add_device(name, erase_size, timeout);
  91. + add_device(name, erase_size, token[2], timeout);
  92. return 0;
  93. }
  94. @@ -459,7 +469,7 @@ static int block2mtd_setup(const char *v
  95. module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
  96. -MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
  97. +MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>[,<name>]]\"");
  98. static int __init block2mtd_init(void)
  99. {