2 * $Id: block2mtd.c,v 1.28 2005/03/19 22:40:44 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,2005 Jörn Engel <joern@wh.fh-wedel.de>
11 #include <linux/config.h>
12 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/bio.h>
16 #include <linux/pagemap.h>
17 #include <linux/list.h>
18 #include <linux/init.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/buffer_head.h>
22 #define VERSION "$Revision: 1.28 $"
25 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
26 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
29 /* Info for the block device */
30 struct block2mtd_dev {
31 struct list_head list;
32 struct block_device *blkdev;
34 struct semaphore write_mutex;
38 /* Static info about the MTD, used in cleanup_module */
39 static LIST_HEAD(blkmtd_device_list);
42 #define PAGE_READAHEAD 64
43 void cache_readahead(struct address_space *mapping, int index)
45 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
48 unsigned long end_index;
51 struct inode *inode = mapping->host;
52 loff_t isize = i_size_read(inode);
55 INFO("iSize=0 in cache_readahead\n");
59 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
61 read_lock_irq(&mapping->tree_lock);
62 for (i = 0; i < PAGE_READAHEAD; i++) {
64 if (pagei > end_index) {
65 INFO("Overrun end of disk in cache readahead\n");
68 page = radix_tree_lookup(&mapping->page_tree, pagei);
73 read_unlock_irq(&mapping->tree_lock);
74 page = page_cache_alloc_cold(mapping);
75 read_lock_irq(&mapping->tree_lock);
79 list_add(&page->lru, &page_pool);
82 read_unlock_irq(&mapping->tree_lock);
84 read_cache_pages(mapping, &page_pool, filler, NULL);
88 static struct page* page_readahead(struct address_space *mapping, int index)
90 filler_t *filler = (filler_t*)mapping->a_ops->readpage;
91 cache_readahead(mapping, index);
92 return read_cache_page(mapping, index, filler, NULL);
96 /* erase a specified part of the device */
97 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
99 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
101 int index = to >> PAGE_SHIFT; // page index
102 int pages = len >> PAGE_SHIFT;
107 page = page_readahead(mapping, index);
111 return PTR_ERR(page);
113 max = (u_long*)page_address(page) + PAGE_SIZE;
114 for (p=(u_long*)page_address(page); p<max; p++)
117 memset(page_address(page), 0xff, PAGE_SIZE);
118 set_page_dirty(page);
123 page_cache_release(page);
129 static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
131 struct block2mtd_dev *dev = mtd->priv;
132 size_t from = instr->addr;
133 size_t len = instr->len;
136 instr->state = MTD_ERASING;
137 down(&dev->write_mutex);
138 err = _block2mtd_erase(dev, from, len);
139 up(&dev->write_mutex);
141 ERROR("erase failed err = %d", err);
142 instr->state = MTD_ERASE_FAILED;
144 instr->state = MTD_ERASE_DONE;
146 instr->state = MTD_ERASE_DONE;
147 mtd_erase_callback(instr);
152 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
153 size_t *retlen, u_char *buf)
155 struct block2mtd_dev *dev = mtd->priv;
157 int index = from >> PAGE_SHIFT;
158 int offset = from & (PAGE_SIZE-1);
161 if (from > mtd->size)
163 if (from + len > mtd->size)
164 len = mtd->size - from;
170 if ((offset + len) > PAGE_SIZE)
171 cpylen = PAGE_SIZE - offset; // multiple pages
173 cpylen = len; // this page
177 page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
181 return PTR_ERR(page);
183 memcpy(buf, page_address(page) + offset, cpylen);
184 page_cache_release(page);
196 /* write data to the underlying device */
197 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
198 loff_t to, size_t len, size_t *retlen)
201 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
202 int index = to >> PAGE_SHIFT; // page index
203 int offset = to & ~PAGE_MASK; // page offset
209 if ((offset+len) > PAGE_SIZE)
210 cpylen = PAGE_SIZE - offset; // multiple pages
212 cpylen = len; // this page
216 page = page_readahead(mapping, index);
220 return PTR_ERR(page);
222 if (memcmp(page_address(page)+offset, buf, cpylen)) {
224 memcpy(page_address(page) + offset, buf, cpylen);
225 set_page_dirty(page);
228 page_cache_release(page);
239 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
240 size_t *retlen, const u_char *buf)
242 struct block2mtd_dev *dev = mtd->priv;
249 if (to + len > mtd->size)
250 len = mtd->size - to;
252 down(&dev->write_mutex);
253 err = _block2mtd_write(dev, buf, to, len, retlen);
254 up(&dev->write_mutex);
261 /* sync the device - wait until the write queue is empty */
262 static void block2mtd_sync(struct mtd_info *mtd)
264 struct block2mtd_dev *dev = mtd->priv;
265 sync_blockdev(dev->blkdev);
270 static void block2mtd_free_device(struct block2mtd_dev *dev)
275 kfree(dev->mtd.name);
278 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
279 close_bdev_excl(dev->blkdev);
286 /* FIXME: ensure that mtd->size % erase_size == 0 */
287 static struct block2mtd_dev *add_device(char *devname, int erase_size)
289 struct block_device *bdev;
290 struct block2mtd_dev *dev;
295 dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
298 memset(dev, 0, sizeof(*dev));
300 /* Get a handle on the device */
301 bdev = open_bdev_excl(devname, O_RDWR, NULL);
303 ERROR("error: cannot open device %s", devname);
308 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
309 ERROR("attempting to use an MTD device as a block device");
313 init_MUTEX(&dev->write_mutex);
315 /* Setup the MTD structure */
316 /* make the name contain the block device in */
317 dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
322 sprintf(dev->mtd.name, "block2mtd: %s", devname);
324 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
325 dev->mtd.erasesize = erase_size;
326 dev->mtd.type = MTD_RAM;
327 dev->mtd.flags = MTD_CAP_RAM;
328 dev->mtd.erase = block2mtd_erase;
329 dev->mtd.write = block2mtd_write;
330 dev->mtd.writev = default_mtd_writev;
331 dev->mtd.sync = block2mtd_sync;
332 dev->mtd.read = block2mtd_read;
333 dev->mtd.readv = default_mtd_readv;
335 dev->mtd.owner = THIS_MODULE;
337 if (add_mtd_device(&dev->mtd)) {
338 /* Device didnt get added, so free the entry */
341 list_add(&dev->list, &blkmtd_device_list);
342 INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
343 dev->mtd.name + strlen("blkmtd: "),
344 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
348 block2mtd_free_device(dev);
353 static int ustrtoul(const char *cp, char **endp, unsigned int base)
355 unsigned long result = simple_strtoul(cp, endp, base);
363 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
364 if ((*endp)[1] == 'i')
371 static int parse_num(size_t *num, const char *token)
376 n = (size_t) ustrtoul(token, &endp, 0);
385 static int parse_name(char **pname, const char *token, size_t limit)
390 len = strlen(token) + 1;
394 name = kmalloc(len, GFP_KERNEL);
405 static inline void kill_final_newline(char *str)
407 char *newline = strrchr(str, '\n');
408 if (newline && !newline[1])
413 #define parse_err(fmt, args...) do { \
414 ERROR("block2mtd: " fmt "\n", ## args); \
418 static int block2mtd_setup(const char *val, struct kernel_param *kp)
420 char buf[80+12], *str=buf; /* 80 for device, 12 for erase size */
423 size_t erase_size = PAGE_SIZE;
426 if (strnlen(val, sizeof(buf)) >= sizeof(buf))
427 parse_err("parameter too long");
430 kill_final_newline(str);
433 token[i] = strsep(&str, ",");
436 parse_err("too many arguments");
439 parse_err("no argument");
441 ret = parse_name(&name, token[0], 80);
443 parse_err("out of memory");
445 parse_err("name too long");
450 ret = parse_num(&erase_size, token[1]);
452 parse_err("illegal erase size");
455 add_device(name, erase_size);
461 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
462 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
464 static int __init block2mtd_init(void)
466 INFO("version " VERSION);
471 static void __devexit block2mtd_exit(void)
473 struct list_head *pos, *next;
475 /* Remove the MTD devices */
476 list_for_each_safe(pos, next, &blkmtd_device_list) {
477 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
478 block2mtd_sync(&dev->mtd);
479 del_mtd_device(&dev->mtd);
480 INFO("mtd%d: [%s] removed", dev->mtd.index,
481 dev->mtd.name + strlen("blkmtd: "));
482 list_del(&dev->list);
483 block2mtd_free_device(dev);
488 module_init(block2mtd_init);
489 module_exit(block2mtd_exit);
491 MODULE_LICENSE("GPL");
492 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
493 MODULE_DESCRIPTION("Emulate an MTD using a block device");