2 * block2mtd.c - create an mtd from a block device
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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.
17 #define MTD_DEFAULT_TIMEOUT 3
19 #include <linux/module.h>
20 #include <linux/delay.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>
34 /* Info for the block device */
35 struct block2mtd_dev {
36 struct list_head list;
37 struct block_device *blkdev;
39 struct mutex write_mutex;
43 /* Static info about the MTD, used in cleanup_module */
44 static LIST_HEAD(blkmtd_device_list);
47 static struct page *page_read(struct address_space *mapping, int index)
49 return read_mapping_page(mapping, index, NULL);
52 /* erase a specified part of the device */
53 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
55 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
57 int index = to >> PAGE_SHIFT; // page index
58 int pages = len >> PAGE_SHIFT;
63 page = page_read(mapping, index);
67 max = page_address(page) + PAGE_SIZE;
68 for (p=page_address(page); p<max; p++)
71 memset(page_address(page), 0xff, PAGE_SIZE);
74 balance_dirty_pages_ratelimited(mapping);
84 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
86 struct block2mtd_dev *dev = mtd->priv;
87 size_t from = instr->addr;
88 size_t len = instr->len;
91 mutex_lock(&dev->write_mutex);
92 err = _block2mtd_erase(dev, from, len);
93 mutex_unlock(&dev->write_mutex);
95 pr_err("erase failed err = %d\n", err);
101 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
102 size_t *retlen, u_char *buf)
104 struct block2mtd_dev *dev = mtd->priv;
106 int index = from >> PAGE_SHIFT;
107 int offset = from & (PAGE_SIZE-1);
111 if ((offset + len) > PAGE_SIZE)
112 cpylen = PAGE_SIZE - offset; // multiple pages
114 cpylen = len; // this page
117 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
119 return PTR_ERR(page);
121 memcpy(buf, page_address(page) + offset, cpylen);
134 /* write data to the underlying device */
135 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
136 loff_t to, size_t len, size_t *retlen)
139 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
140 int index = to >> PAGE_SHIFT; // page index
141 int offset = to & ~PAGE_MASK; // page offset
145 if ((offset+len) > PAGE_SIZE)
146 cpylen = PAGE_SIZE - offset; // multiple pages
148 cpylen = len; // this page
151 page = page_read(mapping, index);
153 return PTR_ERR(page);
155 if (memcmp(page_address(page)+offset, buf, cpylen)) {
157 memcpy(page_address(page) + offset, buf, cpylen);
158 set_page_dirty(page);
160 balance_dirty_pages_ratelimited(mapping);
175 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
176 size_t *retlen, const u_char *buf)
178 struct block2mtd_dev *dev = mtd->priv;
181 mutex_lock(&dev->write_mutex);
182 err = _block2mtd_write(dev, buf, to, len, retlen);
183 mutex_unlock(&dev->write_mutex);
190 /* sync the device - wait until the write queue is empty */
191 static void block2mtd_sync(struct mtd_info *mtd)
193 struct block2mtd_dev *dev = mtd->priv;
194 sync_blockdev(dev->blkdev);
199 static void block2mtd_free_device(struct block2mtd_dev *dev)
204 kfree(dev->mtd.name);
207 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
209 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
216 static struct block2mtd_dev *add_device(char *devname, int erase_size,
222 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
223 struct block_device *bdev;
224 struct block2mtd_dev *dev;
230 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
234 /* Get a handle on the device */
235 bdev = blkdev_get_by_path(devname, mode, dev);
239 * We might not have the root device mounted at this point.
240 * Try to resolve the device name by other means.
242 for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
247 * Calling wait_for_device_probe in the first loop
248 * was not enough, sleep for a bit in subsequent
252 wait_for_device_probe();
254 devt = name_to_dev_t(devname);
257 bdev = blkdev_get_by_dev(devt, mode, dev);
262 pr_err("error: cannot open device %s\n", devname);
263 goto err_free_block2mtd;
267 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
268 pr_err("attempting to use an MTD device as a block device\n");
269 goto err_free_block2mtd;
272 if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
273 pr_err("erasesize must be a divisor of device size\n");
274 goto err_free_block2mtd;
277 mutex_init(&dev->write_mutex);
279 /* Setup the MTD structure */
280 /* make the name contain the block device in */
281 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
283 goto err_destroy_mutex;
285 dev->mtd.name = name;
287 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
288 dev->mtd.erasesize = erase_size;
289 dev->mtd.writesize = 1;
290 dev->mtd.writebufsize = PAGE_SIZE;
291 dev->mtd.type = MTD_RAM;
292 dev->mtd.flags = MTD_CAP_RAM;
293 dev->mtd._erase = block2mtd_erase;
294 dev->mtd._write = block2mtd_write;
295 dev->mtd._sync = block2mtd_sync;
296 dev->mtd._read = block2mtd_read;
298 dev->mtd.owner = THIS_MODULE;
300 if (mtd_device_register(&dev->mtd, NULL, 0)) {
301 /* Device didn't get added, so free the entry */
302 goto err_destroy_mutex;
305 list_add(&dev->list, &blkmtd_device_list);
306 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
308 dev->mtd.name + strlen("block2mtd: "),
309 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
313 mutex_destroy(&dev->write_mutex);
315 block2mtd_free_device(dev);
320 /* This function works similar to reguler strtoul. In addition, it
321 * allows some suffixes for a more human-readable number format:
322 * ki, Ki, kiB, KiB - multiply result with 1024
323 * Mi, MiB - multiply result with 1024^2
324 * Gi, GiB - multiply result with 1024^3
326 static int ustrtoul(const char *cp, char **endp, unsigned int base)
328 unsigned long result = simple_strtoul(cp, endp, base);
339 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
340 if ((*endp)[1] == 'i') {
341 if ((*endp)[2] == 'B')
351 static int parse_num(size_t *num, const char *token)
356 n = (size_t) ustrtoul(token, &endp, 0);
365 static inline void kill_final_newline(char *str)
367 char *newline = strrchr(str, '\n');
368 if (newline && !newline[1])
374 static int block2mtd_init_called = 0;
375 /* 80 for device, 12 for erase size */
376 static char block2mtd_paramline[80 + 12];
379 static int block2mtd_setup2(const char *val)
381 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
382 char buf[80 + 12 + 80 + 8];
386 size_t erase_size = PAGE_SIZE;
387 unsigned long timeout = MTD_DEFAULT_TIMEOUT;
390 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
391 pr_err("parameter too long\n");
396 kill_final_newline(str);
398 for (i = 0; i < 2; i++)
399 token[i] = strsep(&str, ",");
402 pr_err("too many arguments\n");
407 pr_err("no argument\n");
412 if (strlen(name) + 1 > 80) {
413 pr_err("device name too long\n");
418 ret = parse_num(&erase_size, token[1]);
420 pr_err("illegal erase size\n");
425 add_device(name, erase_size, timeout);
431 static int block2mtd_setup(const char *val, const struct kernel_param *kp)
434 return block2mtd_setup2(val);
436 /* If more parameters are later passed in via
437 /sys/module/block2mtd/parameters/block2mtd
438 and block2mtd_init() has already been called,
439 we can parse the argument now. */
441 if (block2mtd_init_called)
442 return block2mtd_setup2(val);
444 /* During early boot stage, we only save the parameters
445 here. We must parse them later: if the param passed
446 from kernel boot command line, block2mtd_setup() is
447 called so early that it is not possible to resolve
448 the device (even kmalloc() fails). Deter that work to
449 block2mtd_setup2(). */
451 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
458 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
459 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
461 static int __init block2mtd_init(void)
466 if (strlen(block2mtd_paramline))
467 ret = block2mtd_setup2(block2mtd_paramline);
468 block2mtd_init_called = 1;
475 static void block2mtd_exit(void)
477 struct list_head *pos, *next;
479 /* Remove the MTD devices */
480 list_for_each_safe(pos, next, &blkmtd_device_list) {
481 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
482 block2mtd_sync(&dev->mtd);
483 mtd_device_unregister(&dev->mtd);
484 mutex_destroy(&dev->write_mutex);
485 pr_info("mtd%d: [%s] removed\n",
487 dev->mtd.name + strlen("block2mtd: "));
488 list_del(&dev->list);
489 block2mtd_free_device(dev);
493 late_initcall(block2mtd_init);
494 module_exit(block2mtd_exit);
496 MODULE_LICENSE("GPL");
498 MODULE_DESCRIPTION("Emulate an MTD using a block device");