2 * block2mtd.c - create an mtd from a block device
4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
5 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
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 /* Maximum number of comma-separated items in the 'block2mtd=' parameter */
35 #define BLOCK2MTD_PARAM_MAX_COUNT 3
37 /* Info for the block device */
38 struct block2mtd_dev {
39 struct list_head list;
40 struct block_device *blkdev;
42 struct mutex write_mutex;
46 /* Static info about the MTD, used in cleanup_module */
47 static LIST_HEAD(blkmtd_device_list);
50 static struct page *page_read(struct address_space *mapping, pgoff_t index)
52 return read_mapping_page(mapping, index, NULL);
55 /* erase a specified part of the device */
56 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
58 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
60 pgoff_t index = to >> PAGE_SHIFT; // page index
61 int pages = len >> PAGE_SHIFT;
66 page = page_read(mapping, index);
70 max = page_address(page) + PAGE_SIZE;
71 for (p=page_address(page); p<max; p++)
74 memset(page_address(page), 0xff, PAGE_SIZE);
77 balance_dirty_pages_ratelimited(mapping);
87 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
89 struct block2mtd_dev *dev = mtd->priv;
90 size_t from = instr->addr;
91 size_t len = instr->len;
94 mutex_lock(&dev->write_mutex);
95 err = _block2mtd_erase(dev, from, len);
96 mutex_unlock(&dev->write_mutex);
98 pr_err("erase failed err = %d\n", err);
104 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
105 size_t *retlen, u_char *buf)
107 struct block2mtd_dev *dev = mtd->priv;
109 pgoff_t index = from >> PAGE_SHIFT;
110 int offset = from & (PAGE_SIZE-1);
114 if ((offset + len) > PAGE_SIZE)
115 cpylen = PAGE_SIZE - offset; // multiple pages
117 cpylen = len; // this page
120 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
122 return PTR_ERR(page);
124 memcpy(buf, page_address(page) + offset, cpylen);
137 /* write data to the underlying device */
138 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
139 loff_t to, size_t len, size_t *retlen)
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
148 if ((offset+len) > PAGE_SIZE)
149 cpylen = PAGE_SIZE - offset; // multiple pages
151 cpylen = len; // this page
154 page = page_read(mapping, index);
156 return PTR_ERR(page);
158 if (memcmp(page_address(page)+offset, buf, cpylen)) {
160 memcpy(page_address(page) + offset, buf, cpylen);
161 set_page_dirty(page);
163 balance_dirty_pages_ratelimited(mapping);
178 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
181 struct block2mtd_dev *dev = mtd->priv;
184 mutex_lock(&dev->write_mutex);
185 err = _block2mtd_write(dev, buf, to, len, retlen);
186 mutex_unlock(&dev->write_mutex);
193 /* sync the device - wait until the write queue is empty */
194 static void block2mtd_sync(struct mtd_info *mtd)
196 struct block2mtd_dev *dev = mtd->priv;
197 sync_blockdev(dev->blkdev);
202 static void block2mtd_free_device(struct block2mtd_dev *dev)
207 kfree(dev->mtd.name);
210 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
212 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
219 static struct block2mtd_dev *add_device(char *devname, int erase_size,
220 char *label, int timeout)
225 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
226 struct block_device *bdev;
227 struct block2mtd_dev *dev;
233 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
237 /* Get a handle on the device */
238 bdev = blkdev_get_by_path(devname, mode, dev);
242 * We might not have the root device mounted at this point.
243 * Try to resolve the device name by other means.
245 for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
250 * Calling wait_for_device_probe in the first loop
251 * was not enough, sleep for a bit in subsequent
255 wait_for_device_probe();
257 devt = name_to_dev_t(devname);
260 bdev = blkdev_get_by_dev(devt, mode, dev);
265 pr_err("error: cannot open device %s\n", devname);
266 goto err_free_block2mtd;
270 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
271 pr_err("attempting to use an MTD device as a block device\n");
272 goto err_free_block2mtd;
275 if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
276 pr_err("erasesize must be a divisor of device size\n");
277 goto err_free_block2mtd;
280 mutex_init(&dev->write_mutex);
282 /* Setup the MTD structure */
283 /* make the name contain the block device in */
285 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
287 name = kstrdup(label, GFP_KERNEL);
289 goto err_destroy_mutex;
291 dev->mtd.name = name;
293 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
294 dev->mtd.erasesize = erase_size;
295 dev->mtd.writesize = 1;
296 dev->mtd.writebufsize = PAGE_SIZE;
297 dev->mtd.type = MTD_RAM;
298 dev->mtd.flags = MTD_CAP_RAM;
299 dev->mtd._erase = block2mtd_erase;
300 dev->mtd._write = block2mtd_write;
301 dev->mtd._sync = block2mtd_sync;
302 dev->mtd._read = block2mtd_read;
304 dev->mtd.owner = THIS_MODULE;
306 if (mtd_device_register(&dev->mtd, NULL, 0)) {
307 /* Device didn't get added, so free the entry */
308 goto err_destroy_mutex;
311 list_add(&dev->list, &blkmtd_device_list);
312 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
314 label ? label : dev->mtd.name + strlen("block2mtd: "),
315 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
319 mutex_destroy(&dev->write_mutex);
321 block2mtd_free_device(dev);
326 /* This function works similar to reguler strtoul. In addition, it
327 * allows some suffixes for a more human-readable number format:
328 * ki, Ki, kiB, KiB - multiply result with 1024
329 * Mi, MiB - multiply result with 1024^2
330 * Gi, GiB - multiply result with 1024^3
332 static int ustrtoul(const char *cp, char **endp, unsigned int base)
334 unsigned long result = simple_strtoul(cp, endp, base);
345 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
346 if ((*endp)[1] == 'i') {
347 if ((*endp)[2] == 'B')
357 static int parse_num(size_t *num, const char *token)
362 n = (size_t) ustrtoul(token, &endp, 0);
371 static inline void kill_final_newline(char *str)
373 char *newline = strrchr(str, '\n');
374 if (newline && !newline[1])
380 static int block2mtd_init_called = 0;
381 /* 80 for device, 12 for erase size */
382 static char block2mtd_paramline[80 + 12];
385 static int block2mtd_setup2(const char *val)
387 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
388 char buf[80 + 12 + 80 + 8];
390 char *token[BLOCK2MTD_PARAM_MAX_COUNT];
393 size_t erase_size = PAGE_SIZE;
394 unsigned long timeout = MTD_DEFAULT_TIMEOUT;
397 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
398 pr_err("parameter too long\n");
403 kill_final_newline(str);
405 for (i = 0; i < BLOCK2MTD_PARAM_MAX_COUNT; i++)
406 token[i] = strsep(&str, ",");
409 pr_err("too many arguments\n");
414 pr_err("no argument\n");
419 if (strlen(name) + 1 > 80) {
420 pr_err("device name too long\n");
424 /* Optional argument when custom label is used */
425 if (token[1] && strlen(token[1])) {
426 ret = parse_num(&erase_size, token[1]);
428 pr_err("illegal erase size\n");
435 pr_info("Using custom MTD label '%s' for dev %s\n", label, name);
438 add_device(name, erase_size, label, timeout);
444 static int block2mtd_setup(const char *val, const struct kernel_param *kp)
447 return block2mtd_setup2(val);
449 /* If more parameters are later passed in via
450 /sys/module/block2mtd/parameters/block2mtd
451 and block2mtd_init() has already been called,
452 we can parse the argument now. */
454 if (block2mtd_init_called)
455 return block2mtd_setup2(val);
457 /* During early boot stage, we only save the parameters
458 here. We must parse them later: if the param passed
459 from kernel boot command line, block2mtd_setup() is
460 called so early that it is not possible to resolve
461 the device (even kmalloc() fails). Deter that work to
462 block2mtd_setup2(). */
464 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
471 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
472 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,[<erasesize>][,<label>]]\"");
474 static int __init block2mtd_init(void)
479 if (strlen(block2mtd_paramline))
480 ret = block2mtd_setup2(block2mtd_paramline);
481 block2mtd_init_called = 1;
488 static void block2mtd_exit(void)
490 struct list_head *pos, *next;
492 /* Remove the MTD devices */
493 list_for_each_safe(pos, next, &blkmtd_device_list) {
494 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
495 block2mtd_sync(&dev->mtd);
496 mtd_device_unregister(&dev->mtd);
497 mutex_destroy(&dev->write_mutex);
498 pr_info("mtd%d: [%s] removed\n",
500 dev->mtd.name + strlen("block2mtd: "));
501 list_del(&dev->list);
502 block2mtd_free_device(dev);
506 late_initcall(block2mtd_init);
507 module_exit(block2mtd_exit);
509 MODULE_LICENSE("GPL");
510 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
511 MODULE_DESCRIPTION("Emulate an MTD using a block device");