1 /*
2 * block2mtd.c - create an mtd from a block device
3 *
4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
5 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
6 *
7 * Licence: GPL
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 /*
13 * When the first attempt at device initialization fails, we may need to
14 * wait a little bit and retry. This timeout, by default 3 seconds, gives
15 * device time to start up. Required on BCM2708 and a few other chipsets.
16 */
17 #define MTD_DEFAULT_TIMEOUT 3
18
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/fs.h>
22 #include <linux/blkdev.h>
23 #include <linux/backing-dev.h>
24 #include <linux/bio.h>
25 #include <linux/pagemap.h>
26 #include <linux/list.h>
27 #include <linux/init.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mutex.h>
30 #include <linux/mount.h>
31 #include <linux/slab.h>
32 #include <linux/major.h>
33
34 /* Maximum number of comma-separated items in the 'block2mtd=' parameter */
35 #define BLOCK2MTD_PARAM_MAX_COUNT 3
36
37 /* Info for the block device */
38 struct block2mtd_dev {
39 struct list_head list;
40 struct block_device *blkdev;
41 struct mtd_info mtd;
42 struct mutex write_mutex;
43 };
44
45
46 /* Static info about the MTD, used in cleanup_module */
47 static LIST_HEAD(blkmtd_device_list);
48
49
page_read(struct address_space * mapping,pgoff_t index)50 static struct page *page_read(struct address_space *mapping, pgoff_t index)
51 {
52 return read_mapping_page(mapping, index, NULL);
53 }
54
55 /* erase a specified part of the device */
_block2mtd_erase(struct block2mtd_dev * dev,loff_t to,size_t len)56 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
57 {
58 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
59 struct page *page;
60 pgoff_t index = to >> PAGE_SHIFT; // page index
61 int pages = len >> PAGE_SHIFT;
62 u_long *p;
63 u_long *max;
64
65 while (pages) {
66 page = page_read(mapping, index);
67 if (IS_ERR(page))
68 return PTR_ERR(page);
69
70 max = page_address(page) + PAGE_SIZE;
71 for (p=page_address(page); p<max; p++)
72 if (*p != -1UL) {
73 lock_page(page);
74 memset(page_address(page), 0xff, PAGE_SIZE);
75 set_page_dirty(page);
76 unlock_page(page);
77 balance_dirty_pages_ratelimited(mapping);
78 break;
79 }
80
81 put_page(page);
82 pages--;
83 index++;
84 }
85 return 0;
86 }
block2mtd_erase(struct mtd_info * mtd,struct erase_info * instr)87 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
88 {
89 struct block2mtd_dev *dev = mtd->priv;
90 size_t from = instr->addr;
91 size_t len = instr->len;
92 int err;
93
94 mutex_lock(&dev->write_mutex);
95 err = _block2mtd_erase(dev, from, len);
96 mutex_unlock(&dev->write_mutex);
97 if (err)
98 pr_err("erase failed err = %d\n", err);
99
100 return err;
101 }
102
103
block2mtd_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)104 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
105 size_t *retlen, u_char *buf)
106 {
107 struct block2mtd_dev *dev = mtd->priv;
108 struct page *page;
109 pgoff_t index = from >> PAGE_SHIFT;
110 int offset = from & (PAGE_SIZE-1);
111 int cpylen;
112
113 while (len) {
114 if ((offset + len) > PAGE_SIZE)
115 cpylen = PAGE_SIZE - offset; // multiple pages
116 else
117 cpylen = len; // this page
118 len = len - cpylen;
119
120 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
121 if (IS_ERR(page))
122 return PTR_ERR(page);
123
124 memcpy(buf, page_address(page) + offset, cpylen);
125 put_page(page);
126
127 if (retlen)
128 *retlen += cpylen;
129 buf += cpylen;
130 offset = 0;
131 index++;
132 }
133 return 0;
134 }
135
136
137 /* write data to the underlying device */
_block2mtd_write(struct block2mtd_dev * dev,const u_char * buf,loff_t to,size_t len,size_t * retlen)138 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
139 loff_t to, size_t len, size_t *retlen)
140 {
141 struct page *page;
142 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
143 pgoff_t index = to >> PAGE_SHIFT; // page index
144 int offset = to & ~PAGE_MASK; // page offset
145 int cpylen;
146
147 while (len) {
148 if ((offset+len) > PAGE_SIZE)
149 cpylen = PAGE_SIZE - offset; // multiple pages
150 else
151 cpylen = len; // this page
152 len = len - cpylen;
153
154 page = page_read(mapping, index);
155 if (IS_ERR(page))
156 return PTR_ERR(page);
157
158 if (memcmp(page_address(page)+offset, buf, cpylen)) {
159 lock_page(page);
160 memcpy(page_address(page) + offset, buf, cpylen);
161 set_page_dirty(page);
162 unlock_page(page);
163 balance_dirty_pages_ratelimited(mapping);
164 }
165 put_page(page);
166
167 if (retlen)
168 *retlen += cpylen;
169
170 buf += cpylen;
171 offset = 0;
172 index++;
173 }
174 return 0;
175 }
176
177
block2mtd_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)178 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
180 {
181 struct block2mtd_dev *dev = mtd->priv;
182 int err;
183
184 mutex_lock(&dev->write_mutex);
185 err = _block2mtd_write(dev, buf, to, len, retlen);
186 mutex_unlock(&dev->write_mutex);
187 if (err > 0)
188 err = 0;
189 return err;
190 }
191
192
193 /* sync the device - wait until the write queue is empty */
block2mtd_sync(struct mtd_info * mtd)194 static void block2mtd_sync(struct mtd_info *mtd)
195 {
196 struct block2mtd_dev *dev = mtd->priv;
197 sync_blockdev(dev->blkdev);
198 return;
199 }
200
201
block2mtd_free_device(struct block2mtd_dev * dev)202 static void block2mtd_free_device(struct block2mtd_dev *dev)
203 {
204 if (!dev)
205 return;
206
207 kfree(dev->mtd.name);
208
209 if (dev->blkdev) {
210 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
211 0, -1);
212 blkdev_put(dev->blkdev, NULL);
213 }
214
215 kfree(dev);
216 }
217
218 /*
219 * This function is marked __ref because it calls the __init marked
220 * early_lookup_bdev when called from the early boot code.
221 */
mdtblock_early_get_bdev(const char * devname,blk_mode_t mode,int timeout,struct block2mtd_dev * dev)222 static struct block_device __ref *mdtblock_early_get_bdev(const char *devname,
223 blk_mode_t mode, int timeout, struct block2mtd_dev *dev)
224 {
225 struct block_device *bdev = ERR_PTR(-ENODEV);
226 #ifndef MODULE
227 int i;
228
229 /*
230 * We can't use early_lookup_bdev from a running system.
231 */
232 if (system_state >= SYSTEM_RUNNING)
233 return bdev;
234
235 /*
236 * We might not have the root device mounted at this point.
237 * Try to resolve the device name by other means.
238 */
239 for (i = 0; i <= timeout; i++) {
240 dev_t devt;
241
242 if (i)
243 /*
244 * Calling wait_for_device_probe in the first loop
245 * was not enough, sleep for a bit in subsequent
246 * go-arounds.
247 */
248 msleep(1000);
249 wait_for_device_probe();
250
251 if (!early_lookup_bdev(devname, &devt)) {
252 bdev = blkdev_get_by_dev(devt, mode, dev, NULL);
253 if (!IS_ERR(bdev))
254 break;
255 }
256 }
257 #endif
258 return bdev;
259 }
260
add_device(char * devname,int erase_size,char * label,int timeout)261 static struct block2mtd_dev *add_device(char *devname, int erase_size,
262 char *label, int timeout)
263 {
264 const blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE;
265 struct block_device *bdev;
266 struct block2mtd_dev *dev;
267 char *name;
268
269 if (!devname)
270 return NULL;
271
272 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
273 if (!dev)
274 return NULL;
275
276 /* Get a handle on the device */
277 bdev = blkdev_get_by_path(devname, mode, dev, NULL);
278 if (IS_ERR(bdev))
279 bdev = mdtblock_early_get_bdev(devname, mode, timeout, dev);
280 if (IS_ERR(bdev)) {
281 pr_err("error: cannot open device %s\n", devname);
282 goto err_free_block2mtd;
283 }
284 dev->blkdev = bdev;
285
286 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
287 pr_err("attempting to use an MTD device as a block device\n");
288 goto err_free_block2mtd;
289 }
290
291 if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
292 pr_err("erasesize must be a divisor of device size\n");
293 goto err_free_block2mtd;
294 }
295
296 mutex_init(&dev->write_mutex);
297
298 /* Setup the MTD structure */
299 /* make the name contain the block device in */
300 if (!label)
301 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
302 else
303 name = kstrdup(label, GFP_KERNEL);
304 if (!name)
305 goto err_destroy_mutex;
306
307 dev->mtd.name = name;
308
309 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
310 dev->mtd.erasesize = erase_size;
311 dev->mtd.writesize = 1;
312 dev->mtd.writebufsize = PAGE_SIZE;
313 dev->mtd.type = MTD_RAM;
314 dev->mtd.flags = MTD_CAP_RAM;
315 dev->mtd._erase = block2mtd_erase;
316 dev->mtd._write = block2mtd_write;
317 dev->mtd._sync = block2mtd_sync;
318 dev->mtd._read = block2mtd_read;
319 dev->mtd.priv = dev;
320 dev->mtd.owner = THIS_MODULE;
321
322 if (mtd_device_register(&dev->mtd, NULL, 0)) {
323 /* Device didn't get added, so free the entry */
324 goto err_destroy_mutex;
325 }
326
327 list_add(&dev->list, &blkmtd_device_list);
328 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
329 dev->mtd.index,
330 label ? label : dev->mtd.name + strlen("block2mtd: "),
331 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
332 return dev;
333
334 err_destroy_mutex:
335 mutex_destroy(&dev->write_mutex);
336 err_free_block2mtd:
337 block2mtd_free_device(dev);
338 return NULL;
339 }
340
341
342 /* This function works similar to reguler strtoul. In addition, it
343 * allows some suffixes for a more human-readable number format:
344 * ki, Ki, kiB, KiB - multiply result with 1024
345 * Mi, MiB - multiply result with 1024^2
346 * Gi, GiB - multiply result with 1024^3
347 */
ustrtoul(const char * cp,char ** endp,unsigned int base)348 static int ustrtoul(const char *cp, char **endp, unsigned int base)
349 {
350 unsigned long result = simple_strtoul(cp, endp, base);
351 switch (**endp) {
352 case 'G' :
353 result *= 1024;
354 fallthrough;
355 case 'M':
356 result *= 1024;
357 fallthrough;
358 case 'K':
359 case 'k':
360 result *= 1024;
361 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
362 if ((*endp)[1] == 'i') {
363 if ((*endp)[2] == 'B')
364 (*endp) += 3;
365 else
366 (*endp) += 2;
367 }
368 }
369 return result;
370 }
371
372
parse_num(size_t * num,const char * token)373 static int parse_num(size_t *num, const char *token)
374 {
375 char *endp;
376 size_t n;
377
378 n = (size_t) ustrtoul(token, &endp, 0);
379 if (*endp)
380 return -EINVAL;
381
382 *num = n;
383 return 0;
384 }
385
386
kill_final_newline(char * str)387 static inline void kill_final_newline(char *str)
388 {
389 char *newline = strrchr(str, '\n');
390 if (newline && !newline[1])
391 *newline = 0;
392 }
393
394
395 #ifndef MODULE
396 static int block2mtd_init_called = 0;
397 /* 80 for device, 12 for erase size */
398 static char block2mtd_paramline[80 + 12];
399 #endif
400
block2mtd_setup2(const char * val)401 static int block2mtd_setup2(const char *val)
402 {
403 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
404 char buf[80 + 12 + 80 + 8];
405 char *str = buf;
406 char *token[BLOCK2MTD_PARAM_MAX_COUNT];
407 char *name;
408 char *label = NULL;
409 size_t erase_size = PAGE_SIZE;
410 unsigned long timeout = MTD_DEFAULT_TIMEOUT;
411 int i, ret;
412
413 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
414 pr_err("parameter too long\n");
415 return 0;
416 }
417
418 strcpy(str, val);
419 kill_final_newline(str);
420
421 for (i = 0; i < BLOCK2MTD_PARAM_MAX_COUNT; i++)
422 token[i] = strsep(&str, ",");
423
424 if (str) {
425 pr_err("too many arguments\n");
426 return 0;
427 }
428
429 if (!token[0]) {
430 pr_err("no argument\n");
431 return 0;
432 }
433
434 name = token[0];
435 if (strlen(name) + 1 > 80) {
436 pr_err("device name too long\n");
437 return 0;
438 }
439
440 /* Optional argument when custom label is used */
441 if (token[1] && strlen(token[1])) {
442 ret = parse_num(&erase_size, token[1]);
443 if (ret) {
444 pr_err("illegal erase size\n");
445 return 0;
446 }
447 }
448
449 if (token[2]) {
450 label = token[2];
451 pr_info("Using custom MTD label '%s' for dev %s\n", label, name);
452 }
453
454 add_device(name, erase_size, label, timeout);
455
456 return 0;
457 }
458
459
block2mtd_setup(const char * val,const struct kernel_param * kp)460 static int block2mtd_setup(const char *val, const struct kernel_param *kp)
461 {
462 #ifdef MODULE
463 return block2mtd_setup2(val);
464 #else
465 /* If more parameters are later passed in via
466 /sys/module/block2mtd/parameters/block2mtd
467 and block2mtd_init() has already been called,
468 we can parse the argument now. */
469
470 if (block2mtd_init_called)
471 return block2mtd_setup2(val);
472
473 /* During early boot stage, we only save the parameters
474 here. We must parse them later: if the param passed
475 from kernel boot command line, block2mtd_setup() is
476 called so early that it is not possible to resolve
477 the device (even kmalloc() fails). Deter that work to
478 block2mtd_setup2(). */
479
480 strscpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
481
482 return 0;
483 #endif
484 }
485
486
487 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
488 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,[<erasesize>][,<label>]]\"");
489
block2mtd_init(void)490 static int __init block2mtd_init(void)
491 {
492 int ret = 0;
493
494 #ifndef MODULE
495 if (strlen(block2mtd_paramline))
496 ret = block2mtd_setup2(block2mtd_paramline);
497 block2mtd_init_called = 1;
498 #endif
499
500 return ret;
501 }
502
503
block2mtd_exit(void)504 static void block2mtd_exit(void)
505 {
506 struct list_head *pos, *next;
507
508 /* Remove the MTD devices */
509 list_for_each_safe(pos, next, &blkmtd_device_list) {
510 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
511 block2mtd_sync(&dev->mtd);
512 mtd_device_unregister(&dev->mtd);
513 mutex_destroy(&dev->write_mutex);
514 pr_info("mtd%d: [%s] removed\n",
515 dev->mtd.index,
516 dev->mtd.name + strlen("block2mtd: "));
517 list_del(&dev->list);
518 block2mtd_free_device(dev);
519 }
520 }
521
522 late_initcall(block2mtd_init);
523 module_exit(block2mtd_exit);
524
525 MODULE_LICENSE("GPL");
526 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
527 MODULE_DESCRIPTION("Emulate an MTD using a block device");
528