2 * $Id: blkmtd.c,v 1.27 2005/11/07 11:14:24 gleixner Exp $
4 * blkmtd.c - use a block device as a fake MTD
6 * Author: Simon Evans <spse@secret.org.uk>
8 * Copyright (C) 2001,2002 Simon Evans
13 * The driver uses raw/io to read/write the device and the page
14 * cache to cache access. Writes update the page cache with the
15 * new data and mark it dirty and add the page into a BIO which
16 * is then written out.
18 * It can be loaded Read-Only to prevent erases and writes to the
23 #include <linux/config.h>
24 #include <linux/module.h>
26 #include <linux/blkdev.h>
27 #include <linux/bio.h>
28 #include <linux/pagemap.h>
29 #include <linux/list.h>
30 #include <linux/init.h>
31 #include <linux/mount.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mutex.h>
35 #define err(format, arg...) printk(KERN_ERR "blkmtd: " format "\n" , ## arg)
36 #define info(format, arg...) printk(KERN_INFO "blkmtd: " format "\n" , ## arg)
37 #define warn(format, arg...) printk(KERN_WARNING "blkmtd: " format "\n" , ## arg)
38 #define crit(format, arg...) printk(KERN_CRIT "blkmtd: " format "\n" , ## arg)
41 /* Default erase size in K, always make it a multiple of PAGE_SIZE */
42 #define CONFIG_MTD_BLKDEV_ERASESIZE (128 << 10) /* 128KiB */
43 #define VERSION "$Revision: 1.27 $"
45 /* Info for the block device */
47 struct list_head list;
48 struct block_device *blkdev;
49 struct mtd_info mtd_info;
50 struct mutex wrbuf_mutex;
54 /* Static info about the MTD, used in cleanup_module */
55 static LIST_HEAD(blkmtd_device_list);
58 static void blkmtd_sync(struct mtd_info *mtd);
62 /* Module parameters passed by insmod/modprobe */
63 static char *device[MAX_DEVICES]; /* the block device to use */
64 static int erasesz[MAX_DEVICES]; /* optional default erase size */
65 static int ro[MAX_DEVICES]; /* optional read only flag */
69 MODULE_LICENSE("GPL");
70 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
71 MODULE_DESCRIPTION("Emulate an MTD using a block device");
72 module_param_array(device, charp, NULL, 0);
73 MODULE_PARM_DESC(device, "block device to use");
74 module_param_array(erasesz, int, NULL, 0);
75 MODULE_PARM_DESC(erasesz, "optional erase size to use in KiB. eg 4=4KiB.");
76 module_param_array(ro, bool, NULL, 0);
77 MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors");
78 module_param(sync, bool, 0);
79 MODULE_PARM_DESC(sync, "1=Synchronous writes");
82 /* completion handler for BIO reads */
83 static int bi_read_complete(struct bio *bio, unsigned int bytes_done, int error)
88 complete((struct completion*)bio->bi_private);
93 /* completion handler for BIO writes */
94 static int bi_write_complete(struct bio *bio, unsigned int bytes_done, int error)
96 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
97 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
103 err("bi_write_complete: not uptodate\n");
106 struct page *page = bvec->bv_page;
107 DEBUG(3, "Cleaning up page %ld\n", page->index);
108 if (--bvec >= bio->bi_io_vec)
109 prefetchw(&bvec->bv_page->flags);
112 SetPageUptodate(page);
114 ClearPageUptodate(page);
117 clear_page_dirty(page);
119 page_cache_release(page);
120 } while (bvec >= bio->bi_io_vec);
122 complete((struct completion*)bio->bi_private);
127 /* read one page from the block device */
128 static int blkmtd_readpage(struct blkmtd_dev *dev, struct page *page)
131 struct completion event;
134 if(PageUptodate(page)) {
135 DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
140 ClearPageUptodate(page);
141 ClearPageError(page);
143 bio = bio_alloc(GFP_KERNEL, 1);
145 init_completion(&event);
146 bio->bi_bdev = dev->blkdev;
147 bio->bi_sector = page->index << (PAGE_SHIFT-9);
148 bio->bi_private = &event;
149 bio->bi_end_io = bi_read_complete;
150 if(bio_add_page(bio, page, PAGE_SIZE, 0) == PAGE_SIZE) {
151 submit_bio(READ_SYNC, bio);
152 wait_for_completion(&event);
153 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
161 SetPageUptodate(page);
162 flush_dcache_page(page);
168 /* write out the current BIO and wait for it to finish */
169 static int blkmtd_write_out(struct bio *bio)
171 struct completion event;
179 init_completion(&event);
180 bio->bi_private = &event;
181 bio->bi_end_io = bi_write_complete;
182 submit_bio(WRITE_SYNC, bio);
183 wait_for_completion(&event);
184 DEBUG(3, "submit_bio completed, bi_vcnt = %d\n", bio->bi_vcnt);
185 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
192 * blkmtd_add_page - add a page to the current BIO
193 * @bio: bio to add to (NULL to alloc initial bio)
194 * @blkdev: block device
196 * @pagecnt: pages left to add
198 * Adds a page to the current bio, allocating it if necessary. If it cannot be
199 * added, the current bio is written out and a new one is allocated. Returns
200 * the new bio to add or NULL on error
202 static struct bio *blkmtd_add_page(struct bio *bio, struct block_device *blkdev,
203 struct page *page, int pagecnt)
208 bio = bio_alloc(GFP_KERNEL, pagecnt);
211 bio->bi_sector = page->index << (PAGE_SHIFT-9);
212 bio->bi_bdev = blkdev;
215 if(bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
216 blkmtd_write_out(bio);
225 * write_pages - write block of data to device via the page cache
226 * @dev: device to write to
227 * @buf: data source or NULL if erase (output is set to 0xff)
228 * @to: offset into output device
229 * @len: amount to data to write
230 * @retlen: amount of data written
232 * Grab pages from the page cache and fill them with the source data.
233 * Non page aligned start and end result in a readin of the page and
234 * part of the page being modified. Pages are added to the bio and then written
237 static int write_pages(struct blkmtd_dev *dev, const u_char *buf, loff_t to,
238 size_t len, size_t *retlen)
241 size_t start_len = 0, end_len;
244 struct bio *bio = NULL;
247 pagenr = to >> PAGE_SHIFT;
248 offset = to & ~PAGE_MASK;
250 DEBUG(2, "blkmtd: write_pages: buf = %p to = %ld len = %zd pagenr = %d offset = %d\n",
251 buf, (long)to, len, pagenr, offset);
253 /* see if we have to do a partial write at the start */
255 start_len = ((offset + len) > PAGE_SIZE) ? PAGE_SIZE - offset : len;
259 /* calculate the length of the other two regions */
260 end_len = len & ~PAGE_MASK;
267 pagecnt += len >> PAGE_SHIFT;
272 mutex_lock(&dev->wrbuf_mutex);
274 DEBUG(3, "blkmtd: write: start_len = %zd len = %zd end_len = %zd pagecnt = %d\n",
275 start_len, len, end_len, pagecnt);
278 /* do partial start region */
281 DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %zd offset = %d\n",
282 pagenr, start_len, offset);
285 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
287 if(PageDirty(page)) {
288 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
289 to, start_len, len, end_len, pagenr);
292 memcpy(page_address(page)+offset, buf, start_len);
293 set_page_dirty(page);
294 SetPageUptodate(page);
297 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
300 err("bio_add_page failed\n");
307 /* Now do the main loop to a page aligned, n page sized output */
309 int pagesc = len >> PAGE_SHIFT;
310 DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n",
315 /* see if page is in the page cache */
316 DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
317 page = grab_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr);
318 if(PageDirty(page)) {
322 warn("write: cannot grab cache page %d", pagenr);
327 memset(page_address(page), 0xff, PAGE_SIZE);
329 memcpy(page_address(page), buf, PAGE_SIZE);
332 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
335 err("bio_add_page failed\n");
340 set_page_dirty(page);
341 SetPageUptodate(page);
343 thislen += PAGE_SIZE;
348 /* do the third region */
350 DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %zd\n",
353 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
355 if(PageDirty(page)) {
356 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
357 to, start_len, len, end_len, pagenr);
360 memcpy(page_address(page), buf, end_len);
361 set_page_dirty(page);
362 SetPageUptodate(page);
363 DEBUG(3, "blkmtd: write: writing out partial end\n");
365 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
368 err("bio_add_page failed\n");
374 DEBUG(3, "blkmtd: write: got %d vectors to write\n", bio->bi_vcnt);
377 blkmtd_write_out(bio);
379 DEBUG(2, "blkmtd: write: end, retlen = %zd, err = %d\n", *retlen, err);
380 mutex_unlock(&dev->wrbuf_mutex);
388 /* erase a specified part of the device */
389 static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
391 struct blkmtd_dev *dev = mtd->priv;
392 struct mtd_erase_region_info *einfo = mtd->eraseregions;
393 int numregions = mtd->numeraseregions;
399 instr->state = MTD_ERASING;
403 /* check erase region has valid start and length */
404 DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%zx len = 0x%lx\n",
405 mtd->name+9, from, len);
407 DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
408 einfo->offset, einfo->erasesize, einfo->numblocks);
409 if(from >= einfo->offset
410 && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
411 if(len == einfo->erasesize
412 && ( (from - einfo->offset) % einfo->erasesize == 0))
420 /* Not a valid erase block */
421 err("erase: invalid erase request 0x%lX @ 0x%08zX", len, from);
422 instr->state = MTD_ERASE_FAILED;
426 if(instr->state != MTD_ERASE_FAILED) {
428 DEBUG(3, "Doing erase from = %zd len = %ld\n", from, len);
429 err = write_pages(dev, NULL, from, len, &retlen);
430 if(err || retlen != len) {
431 err("erase failed err = %d", err);
432 instr->state = MTD_ERASE_FAILED;
434 instr->state = MTD_ERASE_DONE;
438 DEBUG(3, "blkmtd: erase: checking callback\n");
439 mtd_erase_callback(instr);
440 DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
445 /* read a range of the data via the page cache */
446 static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
447 size_t *retlen, u_char *buf)
449 struct blkmtd_dev *dev = mtd->priv;
455 DEBUG(2, "blkmtd: read: dev = `%s' from = %lld len = %zd buf = %p\n",
456 mtd->name+9, from, len, buf);
460 if(from + len > mtd->size)
461 len = mtd->size - from;
463 pagenr = from >> PAGE_SHIFT;
464 offset = from - (pagenr << PAGE_SHIFT);
466 pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT;
467 DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n",
468 pagenr, offset, pages);
474 DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
475 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
481 cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE;
482 if(offset+cpylen > PAGE_SIZE)
483 cpylen = PAGE_SIZE-offset;
485 memcpy(buf + thislen, page_address(page) + offset, cpylen);
492 page_cache_release(page);
498 DEBUG(2, "blkmtd: end read: retlen = %zd, err = %d\n", thislen, err);
503 /* write data to the underlying device */
504 static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
505 size_t *retlen, const u_char *buf)
507 struct blkmtd_dev *dev = mtd->priv;
513 DEBUG(2, "blkmtd: write: dev = `%s' to = %lld len = %zd buf = %p\n",
514 mtd->name+9, to, len, buf);
516 if(to >= mtd->size) {
520 if(to + len > mtd->size) {
521 len = mtd->size - to;
524 err = write_pages(dev, buf, to, len, retlen);
527 DEBUG(2, "blkmtd: write: end, err = %d\n", err);
532 /* sync the device - wait until the write queue is empty */
533 static void blkmtd_sync(struct mtd_info *mtd)
535 /* Currently all writes are synchronous */
539 static void free_device(struct blkmtd_dev *dev)
541 DEBUG(2, "blkmtd: free_device() dev = %p\n", dev);
543 kfree(dev->mtd_info.eraseregions);
544 kfree(dev->mtd_info.name);
546 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
547 close_bdev_excl(dev->blkdev);
554 /* For a given size and initial erase size, calculate the number
555 * and size of each erase region. Goes round the loop twice,
556 * once to find out how many regions, then allocates space,
557 * then round the loop again to fill it in.
559 static struct mtd_erase_region_info *calc_erase_regions(
560 size_t erase_size, size_t total_size, int *regions)
562 struct mtd_erase_region_info *info = NULL;
564 DEBUG(2, "calc_erase_regions, es = %zd size = %zd regions = %d\n",
565 erase_size, total_size, *regions);
566 /* Make any user specified erasesize be a power of 2
567 and at least PAGE_SIZE */
575 if(erase_size < PAGE_SIZE)
576 erase_size = PAGE_SIZE;
578 erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
584 int tot_size = total_size;
585 int er_size = erase_size;
586 int count = 0, offset = 0, regcnt = 0;
589 count = tot_size / er_size;
591 tot_size = tot_size % er_size;
593 DEBUG(2, "adding to erase info off=%d er=%d cnt=%d\n",
594 offset, er_size, count);
595 (info+regcnt)->offset = offset;
596 (info+regcnt)->erasesize = er_size;
597 (info+regcnt)->numblocks = count;
601 offset += (count * er_size);
603 while(er_size > tot_size)
607 info = kmalloc(regcnt * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
611 } while(!(*regions));
612 DEBUG(2, "calc_erase_regions done, es = %zd size = %zd regions = %d\n",
613 erase_size, total_size, *regions);
618 static struct blkmtd_dev *add_device(char *devname, int readonly, int erase_size)
620 struct block_device *bdev;
622 struct blkmtd_dev *dev;
627 /* Get a handle on the device */
631 mode = (readonly) ? O_RDONLY : O_RDWR;
632 bdev = open_bdev_excl(devname, mode, NULL);
634 mode = (readonly) ? FMODE_READ : FMODE_WRITE;
635 bdev = open_by_devnum(name_to_dev_t(devname), mode);
638 err("error: cannot open device %s", devname);
639 DEBUG(2, "blkmtd: opening bdev returned %ld\n", PTR_ERR(bdev));
643 DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n",
644 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
646 if(MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
647 err("attempting to use an MTD device as a block device");
652 dev = kmalloc(sizeof(struct blkmtd_dev), GFP_KERNEL);
658 memset(dev, 0, sizeof(struct blkmtd_dev));
661 mutex_init(&dev->wrbuf_mutex);
664 dev->mtd_info.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
666 /* Setup the MTD structure */
667 /* make the name contain the block device in */
668 dev->mtd_info.name = kmalloc(sizeof("blkmtd: ") + strlen(devname), GFP_KERNEL);
669 if(dev->mtd_info.name == NULL)
672 sprintf(dev->mtd_info.name, "blkmtd: %s", devname);
673 dev->mtd_info.eraseregions = calc_erase_regions(erase_size, dev->mtd_info.size,
674 &dev->mtd_info.numeraseregions);
675 if(dev->mtd_info.eraseregions == NULL)
678 dev->mtd_info.erasesize = dev->mtd_info.eraseregions->erasesize;
679 DEBUG(1, "blkmtd: init: found %d erase regions\n",
680 dev->mtd_info.numeraseregions);
683 dev->mtd_info.type = MTD_ROM;
684 dev->mtd_info.flags = MTD_CAP_ROM;
686 dev->mtd_info.type = MTD_RAM;
687 dev->mtd_info.flags = MTD_CAP_RAM;
688 dev->mtd_info.erase = blkmtd_erase;
689 dev->mtd_info.write = blkmtd_write;
690 dev->mtd_info.writev = default_mtd_writev;
691 dev->mtd_info.sync = blkmtd_sync;
693 dev->mtd_info.read = blkmtd_read;
694 dev->mtd_info.readv = default_mtd_readv;
695 dev->mtd_info.priv = dev;
696 dev->mtd_info.owner = THIS_MODULE;
698 list_add(&dev->list, &blkmtd_device_list);
699 if (add_mtd_device(&dev->mtd_info)) {
700 /* Device didnt get added, so free the entry */
701 list_del(&dev->list);
704 info("mtd%d: [%s] erase_size = %dKiB %s",
705 dev->mtd_info.index, dev->mtd_info.name + strlen("blkmtd: "),
706 dev->mtd_info.erasesize >> 10,
707 readonly ? "(read-only)" : "");
718 /* Cleanup and exit - sync the device and kill of the kernel thread */
719 static void __devexit cleanup_blkmtd(void)
721 struct list_head *temp1, *temp2;
723 /* Remove the MTD devices */
724 list_for_each_safe(temp1, temp2, &blkmtd_device_list) {
725 struct blkmtd_dev *dev = list_entry(temp1, struct blkmtd_dev,
727 blkmtd_sync(&dev->mtd_info);
728 del_mtd_device(&dev->mtd_info);
729 info("mtd%d: [%s] removed", dev->mtd_info.index,
730 dev->mtd_info.name + strlen("blkmtd: "));
731 list_del(&dev->list);
738 /* Handle kernel boot params */
741 static int __init param_blkmtd_device(char *str)
745 for(i = 0; i < MAX_DEVICES; i++) {
747 DEBUG(2, "blkmtd: device setup: %d = %s\n", i, device[i]);
754 static int __init param_blkmtd_erasesz(char *str)
757 for(i = 0; i < MAX_DEVICES; i++) {
758 char *val = strsep(&str, ",");
760 erasesz[i] = simple_strtoul(val, NULL, 0);
761 DEBUG(2, "blkmtd: erasesz setup: %d = %d\n", i, erasesz[i]);
768 static int __init param_blkmtd_ro(char *str)
771 for(i = 0; i < MAX_DEVICES; i++) {
772 char *val = strsep(&str, ",");
774 ro[i] = simple_strtoul(val, NULL, 0);
775 DEBUG(2, "blkmtd: ro setup: %d = %d\n", i, ro[i]);
782 static int __init param_blkmtd_sync(char *str)
789 __setup("blkmtd_device=", param_blkmtd_device);
790 __setup("blkmtd_erasesz=", param_blkmtd_erasesz);
791 __setup("blkmtd_ro=", param_blkmtd_ro);
792 __setup("blkmtd_sync=", param_blkmtd_sync);
798 static int __init init_blkmtd(void)
802 info("version " VERSION);
803 /* Check args - device[0] is the bare minimum*/
805 err("error: missing `device' name\n");
809 for(i = 0; i < MAX_DEVICES; i++)
810 add_device(device[i], ro[i], erasesz[i] << 10);
812 if(list_empty(&blkmtd_device_list))
818 module_init(init_blkmtd);
819 module_exit(cleanup_blkmtd);