2 * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
4 * block2mtd.c - create an mtd from a block device
6 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
7 * Copyright (C) 2004-2006 Jörn Engel <joern@wh.fh-wedel.de>
11 #include <linux/module.h>
13 #include <linux/blkdev.h>
14 #include <linux/bio.h>
15 #include <linux/pagemap.h>
16 #include <linux/list.h>
17 #include <linux/init.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/buffer_head.h>
20 #include <linux/mutex.h>
21 #include <linux/mount.h>
23 #define VERSION "$Revision: 1.30 $"
26 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
27 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
30 /* Info for the block device */
31 struct block2mtd_dev {
32 struct list_head list;
33 struct block_device *blkdev;
35 struct mutex write_mutex;
39 /* Static info about the MTD, used in cleanup_module */
40 static LIST_HEAD(blkmtd_device_list);
43 #define PAGE_READAHEAD 64
44 static void cache_readahead(struct address_space *mapping, int index)
46 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
49 unsigned long end_index;
52 struct inode *inode = mapping->host;
53 loff_t isize = i_size_read(inode);
56 INFO("iSize=0 in cache_readahead\n");
60 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
62 read_lock_irq(&mapping->tree_lock);
63 for (i = 0; i < PAGE_READAHEAD; i++) {
65 if (pagei > end_index) {
66 INFO("Overrun end of disk in cache readahead\n");
69 page = radix_tree_lookup(&mapping->page_tree, pagei);
74 read_unlock_irq(&mapping->tree_lock);
75 page = page_cache_alloc_cold(mapping);
76 read_lock_irq(&mapping->tree_lock);
80 list_add(&page->lru, &page_pool);
83 read_unlock_irq(&mapping->tree_lock);
85 read_cache_pages(mapping, &page_pool, filler, NULL);
89 static struct page* page_readahead(struct address_space *mapping, int index)
91 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
92 cache_readahead(mapping, index);
93 return read_cache_page(mapping, index, filler, NULL);
97 /* erase a specified part of the device */
98 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
100 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
102 int index = to >> PAGE_SHIFT; // page index
103 int pages = len >> PAGE_SHIFT;
108 page = page_readahead(mapping, index);
112 return PTR_ERR(page);
114 max = (u_long*)page_address(page) + PAGE_SIZE;
115 for (p=(u_long*)page_address(page); p<max; p++)
118 memset(page_address(page), 0xff, PAGE_SIZE);
119 set_page_dirty(page);
124 page_cache_release(page);
130 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
132 struct block2mtd_dev *dev = mtd->priv;
133 size_t from = instr->addr;
134 size_t len = instr->len;
137 instr->state = MTD_ERASING;
138 mutex_lock(&dev->write_mutex);
139 err = _block2mtd_erase(dev, from, len);
140 mutex_unlock(&dev->write_mutex);
142 ERROR("erase failed err = %d", err);
143 instr->state = MTD_ERASE_FAILED;
145 instr->state = MTD_ERASE_DONE;
147 instr->state = MTD_ERASE_DONE;
148 mtd_erase_callback(instr);
153 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
154 size_t *retlen, u_char *buf)
156 struct block2mtd_dev *dev = mtd->priv;
158 int index = from >> PAGE_SHIFT;
159 int offset = from & (PAGE_SIZE-1);
162 if (from > mtd->size)
164 if (from + len > mtd->size)
165 len = mtd->size - from;
171 if ((offset + len) > PAGE_SIZE)
172 cpylen = PAGE_SIZE - offset; // multiple pages
174 cpylen = len; // this page
178 page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
182 return PTR_ERR(page);
184 memcpy(buf, page_address(page) + offset, cpylen);
185 page_cache_release(page);
197 /* write data to the underlying device */
198 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
199 loff_t to, size_t len, size_t *retlen)
202 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
203 int index = to >> PAGE_SHIFT; // page index
204 int offset = to & ~PAGE_MASK; // page offset
210 if ((offset+len) > PAGE_SIZE)
211 cpylen = PAGE_SIZE - offset; // multiple pages
213 cpylen = len; // this page
217 page = page_readahead(mapping, index);
221 return PTR_ERR(page);
223 if (memcmp(page_address(page)+offset, buf, cpylen)) {
225 memcpy(page_address(page) + offset, buf, cpylen);
226 set_page_dirty(page);
229 page_cache_release(page);
242 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
243 size_t *retlen, const u_char *buf)
245 struct block2mtd_dev *dev = mtd->priv;
252 if (to + len > mtd->size)
253 len = mtd->size - to;
255 mutex_lock(&dev->write_mutex);
256 err = _block2mtd_write(dev, buf, to, len, retlen);
257 mutex_unlock(&dev->write_mutex);
264 /* sync the device - wait until the write queue is empty */
265 static void block2mtd_sync(struct mtd_info *mtd)
267 struct block2mtd_dev *dev = mtd->priv;
268 sync_blockdev(dev->blkdev);
273 static void block2mtd_free_device(struct block2mtd_dev *dev)
278 kfree(dev->mtd.name);
281 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
282 close_bdev_excl(dev->blkdev);
289 /* FIXME: ensure that mtd->size % erase_size == 0 */
290 static struct block2mtd_dev *add_device(char *devname, int erase_size)
292 struct block_device *bdev;
293 struct block2mtd_dev *dev;
298 dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
301 memset(dev, 0, sizeof(*dev));
303 /* Get a handle on the device */
304 bdev = open_bdev_excl(devname, O_RDWR, NULL);
308 /* We might not have rootfs mounted at this point. Try
309 to resolve the device name by other means. */
311 dev_t dev = name_to_dev_t(devname);
313 bdev = open_by_devnum(dev, FMODE_WRITE | FMODE_READ);
319 ERROR("error: cannot open device %s", devname);
324 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
325 ERROR("attempting to use an MTD device as a block device");
329 mutex_init(&dev->write_mutex);
331 /* Setup the MTD structure */
332 /* make the name contain the block device in */
333 dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
338 sprintf(dev->mtd.name, "block2mtd: %s", devname);
340 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
341 dev->mtd.erasesize = erase_size;
342 dev->mtd.writesize = 1;
343 dev->mtd.type = MTD_RAM;
344 dev->mtd.flags = MTD_CAP_RAM;
345 dev->mtd.erase = block2mtd_erase;
346 dev->mtd.write = block2mtd_write;
347 dev->mtd.writev = default_mtd_writev;
348 dev->mtd.sync = block2mtd_sync;
349 dev->mtd.read = block2mtd_read;
351 dev->mtd.owner = THIS_MODULE;
353 if (add_mtd_device(&dev->mtd)) {
354 /* Device didnt get added, so free the entry */
357 list_add(&dev->list, &blkmtd_device_list);
358 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
359 dev->mtd.name + strlen("blkmtd: "),
360 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
364 block2mtd_free_device(dev);
369 /* This function works similar to reguler strtoul. In addition, it
370 * allows some suffixes for a more human-readable number format:
371 * ki, Ki, kiB, KiB - multiply result with 1024
372 * Mi, MiB - multiply result with 1024^2
373 * Gi, GiB - multiply result with 1024^3
375 static int ustrtoul(const char *cp, char **endp, unsigned int base)
377 unsigned long result = simple_strtoul(cp, endp, base);
386 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
387 if ((*endp)[1] == 'i') {
388 if ((*endp)[2] == 'B')
398 static int parse_num(size_t *num, const char *token)
403 n = (size_t) ustrtoul(token, &endp, 0);
412 static inline void kill_final_newline(char *str)
414 char *newline = strrchr(str, '\n');
415 if (newline && !newline[1])
420 #define parse_err(fmt, args...) do { \
421 ERROR("block2mtd: " fmt "\n", ## args); \
426 static int block2mtd_init_called = 0;
427 static __initdata char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
431 static int block2mtd_setup2(const char *val)
433 char buf[80 + 12]; /* 80 for device, 12 for erase size */
437 size_t erase_size = PAGE_SIZE;
440 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
441 parse_err("parameter too long");
444 kill_final_newline(str);
446 for (i = 0; i < 2; i++)
447 token[i] = strsep(&str, ",");
450 parse_err("too many arguments");
453 parse_err("no argument");
456 if (strlen(name) + 1 > 80)
457 parse_err("device name too long");
460 ret = parse_num(&erase_size, token[1]);
463 parse_err("illegal erase size");
467 add_device(name, erase_size);
473 static int block2mtd_setup(const char *val, struct kernel_param *kp)
476 return block2mtd_setup2(val);
478 /* If more parameters are later passed in via
479 /sys/module/block2mtd/parameters/block2mtd
480 and block2mtd_init() has already been called,
481 we can parse the argument now. */
483 if (block2mtd_init_called)
484 return block2mtd_setup2(val);
486 /* During early boot stage, we only save the parameters
487 here. We must parse them later: if the param passed
488 from kernel boot command line, block2mtd_setup() is
489 called so early that it is not possible to resolve
490 the device (even kmalloc() fails). Deter that work to
491 block2mtd_setup2(). */
493 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
500 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
501 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
503 static int __init block2mtd_init(void)
506 INFO("version " VERSION);
509 if (strlen(block2mtd_paramline))
510 ret = block2mtd_setup2(block2mtd_paramline);
511 block2mtd_init_called = 1;
518 static void __devexit block2mtd_exit(void)
520 struct list_head *pos, *next;
522 /* Remove the MTD devices */
523 list_for_each_safe(pos, next, &blkmtd_device_list) {
524 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
525 block2mtd_sync(&dev->mtd);
526 del_mtd_device(&dev->mtd);
527 INFO("mtd%d: [%s] removed", dev->mtd.index,
528 dev->mtd.name + strlen("blkmtd: "));
529 list_del(&dev->list);
530 block2mtd_free_device(dev);
535 module_init(block2mtd_init);
536 module_exit(block2mtd_exit);
538 MODULE_LICENSE("GPL");
539 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
540 MODULE_DESCRIPTION("Emulate an MTD using a block device");