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