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>
9 #include <linux/module.h>
11 #include <linux/blkdev.h>
12 #include <linux/bio.h>
13 #include <linux/pagemap.h>
14 #include <linux/list.h>
15 #include <linux/init.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/buffer_head.h>
18 #include <linux/mutex.h>
19 #include <linux/mount.h>
20 #include <linux/slab.h>
22 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
23 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
26 /* Info for the block device */
27 struct block2mtd_dev {
28 struct list_head list;
29 struct block_device *blkdev;
31 struct mutex write_mutex;
35 /* Static info about the MTD, used in cleanup_module */
36 static LIST_HEAD(blkmtd_device_list);
39 static struct page *page_read(struct address_space *mapping, int index)
41 return read_mapping_page(mapping, index, NULL);
44 /* erase a specified part of the device */
45 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
47 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
49 int index = to >> PAGE_SHIFT; // page index
50 int pages = len >> PAGE_SHIFT;
55 page = page_read(mapping, index);
61 max = page_address(page) + PAGE_SIZE;
62 for (p=page_address(page); p<max; p++)
65 memset(page_address(page), 0xff, PAGE_SIZE);
71 page_cache_release(page);
77 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
79 struct block2mtd_dev *dev = mtd->priv;
80 size_t from = instr->addr;
81 size_t len = instr->len;
84 instr->state = MTD_ERASING;
85 mutex_lock(&dev->write_mutex);
86 err = _block2mtd_erase(dev, from, len);
87 mutex_unlock(&dev->write_mutex);
89 ERROR("erase failed err = %d", err);
90 instr->state = MTD_ERASE_FAILED;
92 instr->state = MTD_ERASE_DONE;
94 instr->state = MTD_ERASE_DONE;
95 mtd_erase_callback(instr);
100 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
101 size_t *retlen, u_char *buf)
103 struct block2mtd_dev *dev = mtd->priv;
105 int index = from >> PAGE_SHIFT;
106 int offset = from & (PAGE_SIZE-1);
109 if (from > mtd->size)
111 if (from + len > mtd->size)
112 len = mtd->size - from;
118 if ((offset + len) > PAGE_SIZE)
119 cpylen = PAGE_SIZE - offset; // multiple pages
121 cpylen = len; // this page
124 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
128 return PTR_ERR(page);
130 memcpy(buf, page_address(page) + offset, cpylen);
131 page_cache_release(page);
143 /* write data to the underlying device */
144 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
145 loff_t to, size_t len, size_t *retlen)
148 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
149 int index = to >> PAGE_SHIFT; // page index
150 int offset = to & ~PAGE_MASK; // page offset
156 if ((offset+len) > PAGE_SIZE)
157 cpylen = PAGE_SIZE - offset; // multiple pages
159 cpylen = len; // this page
162 page = page_read(mapping, index);
166 return PTR_ERR(page);
168 if (memcmp(page_address(page)+offset, buf, cpylen)) {
170 memcpy(page_address(page) + offset, buf, cpylen);
171 set_page_dirty(page);
174 page_cache_release(page);
187 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
188 size_t *retlen, const u_char *buf)
190 struct block2mtd_dev *dev = mtd->priv;
197 if (to + len > mtd->size)
198 len = mtd->size - to;
200 mutex_lock(&dev->write_mutex);
201 err = _block2mtd_write(dev, buf, to, len, retlen);
202 mutex_unlock(&dev->write_mutex);
209 /* sync the device - wait until the write queue is empty */
210 static void block2mtd_sync(struct mtd_info *mtd)
212 struct block2mtd_dev *dev = mtd->priv;
213 sync_blockdev(dev->blkdev);
218 static void block2mtd_free_device(struct block2mtd_dev *dev)
223 kfree(dev->mtd.name);
226 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
228 close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
235 /* FIXME: ensure that mtd->size % erase_size == 0 */
236 static struct block2mtd_dev *add_device(char *devname, int erase_size)
238 struct block_device *bdev;
239 struct block2mtd_dev *dev;
245 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
249 /* Get a handle on the device */
250 bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
254 /* We might not have rootfs mounted at this point. Try
255 to resolve the device name by other means. */
257 dev_t devt = name_to_dev_t(devname);
259 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
265 ERROR("error: cannot open device %s", devname);
270 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
271 ERROR("attempting to use an MTD device as a block device");
275 mutex_init(&dev->write_mutex);
277 /* Setup the MTD structure */
278 /* make the name contain the block device in */
279 name = kmalloc(sizeof("block2mtd: ") + strlen(devname) + 1,
284 sprintf(name, "block2mtd: %s", devname);
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.type = MTD_RAM;
291 dev->mtd.flags = MTD_CAP_RAM;
292 dev->mtd.erase = block2mtd_erase;
293 dev->mtd.write = block2mtd_write;
294 dev->mtd.writev = default_mtd_writev;
295 dev->mtd.sync = block2mtd_sync;
296 dev->mtd.read = block2mtd_read;
298 dev->mtd.owner = THIS_MODULE;
300 if (add_mtd_device(&dev->mtd)) {
301 /* Device didnt get added, so free the entry */
304 list_add(&dev->list, &blkmtd_device_list);
305 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
306 dev->mtd.name + strlen("block2mtd: "),
307 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
311 block2mtd_free_device(dev);
316 /* This function works similar to reguler strtoul. In addition, it
317 * allows some suffixes for a more human-readable number format:
318 * ki, Ki, kiB, KiB - multiply result with 1024
319 * Mi, MiB - multiply result with 1024^2
320 * Gi, GiB - multiply result with 1024^3
322 static int ustrtoul(const char *cp, char **endp, unsigned int base)
324 unsigned long result = simple_strtoul(cp, endp, base);
333 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
334 if ((*endp)[1] == 'i') {
335 if ((*endp)[2] == 'B')
345 static int parse_num(size_t *num, const char *token)
350 n = (size_t) ustrtoul(token, &endp, 0);
359 static inline void kill_final_newline(char *str)
361 char *newline = strrchr(str, '\n');
362 if (newline && !newline[1])
367 #define parse_err(fmt, args...) do { \
368 ERROR(fmt, ## args); \
373 static int block2mtd_init_called = 0;
374 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
378 static int block2mtd_setup2(const char *val)
380 char buf[80 + 12]; /* 80 for device, 12 for erase size */
384 size_t erase_size = PAGE_SIZE;
387 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
388 parse_err("parameter too long");
391 kill_final_newline(str);
393 for (i = 0; i < 2; i++)
394 token[i] = strsep(&str, ",");
397 parse_err("too many arguments");
400 parse_err("no argument");
403 if (strlen(name) + 1 > 80)
404 parse_err("device name too long");
407 ret = parse_num(&erase_size, token[1]);
409 parse_err("illegal erase size");
413 add_device(name, erase_size);
419 static int block2mtd_setup(const char *val, struct kernel_param *kp)
422 return block2mtd_setup2(val);
424 /* If more parameters are later passed in via
425 /sys/module/block2mtd/parameters/block2mtd
426 and block2mtd_init() has already been called,
427 we can parse the argument now. */
429 if (block2mtd_init_called)
430 return block2mtd_setup2(val);
432 /* During early boot stage, we only save the parameters
433 here. We must parse them later: if the param passed
434 from kernel boot command line, block2mtd_setup() is
435 called so early that it is not possible to resolve
436 the device (even kmalloc() fails). Deter that work to
437 block2mtd_setup2(). */
439 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
446 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
447 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
449 static int __init block2mtd_init(void)
454 if (strlen(block2mtd_paramline))
455 ret = block2mtd_setup2(block2mtd_paramline);
456 block2mtd_init_called = 1;
463 static void __devexit block2mtd_exit(void)
465 struct list_head *pos, *next;
467 /* Remove the MTD devices */
468 list_for_each_safe(pos, next, &blkmtd_device_list) {
469 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
470 block2mtd_sync(&dev->mtd);
471 del_mtd_device(&dev->mtd);
472 INFO("mtd%d: [%s] removed", dev->mtd.index,
473 dev->mtd.name + strlen("block2mtd: "));
474 list_del(&dev->list);
475 block2mtd_free_device(dev);
480 module_init(block2mtd_init);
481 module_exit(block2mtd_exit);
483 MODULE_LICENSE("GPL");
484 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
485 MODULE_DESCRIPTION("Emulate an MTD using a block device");