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/mtd/mtd.h>
34 #define err(format, arg...) printk(KERN_ERR "blkmtd: " format "\n" , ## arg)
35 #define info(format, arg...) printk(KERN_INFO "blkmtd: " format "\n" , ## arg)
36 #define warn(format, arg...) printk(KERN_WARNING "blkmtd: " format "\n" , ## arg)
37 #define crit(format, arg...) printk(KERN_CRIT "blkmtd: " format "\n" , ## arg)
40 /* Default erase size in K, always make it a multiple of PAGE_SIZE */
41 #define CONFIG_MTD_BLKDEV_ERASESIZE (128 << 10) /* 128KiB */
42 #define VERSION "$Revision: 1.27 $"
44 /* Info for the block device */
46 struct list_head list;
47 struct block_device *blkdev;
48 struct mtd_info mtd_info;
49 struct semaphore wrbuf_mutex;
53 /* Static info about the MTD, used in cleanup_module */
54 static LIST_HEAD(blkmtd_device_list);
57 static void blkmtd_sync(struct mtd_info *mtd);
61 /* Module parameters passed by insmod/modprobe */
62 static char *device[MAX_DEVICES]; /* the block device to use */
63 static int erasesz[MAX_DEVICES]; /* optional default erase size */
64 static int ro[MAX_DEVICES]; /* optional read only flag */
68 MODULE_LICENSE("GPL");
69 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
70 MODULE_DESCRIPTION("Emulate an MTD using a block device");
71 module_param_array(device, charp, NULL, 0);
72 MODULE_PARM_DESC(device, "block device to use");
73 module_param_array(erasesz, int, NULL, 0);
74 MODULE_PARM_DESC(erasesz, "optional erase size to use in KiB. eg 4=4KiB.");
75 module_param_array(ro, bool, NULL, 0);
76 MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors");
77 module_param(sync, bool, 0);
78 MODULE_PARM_DESC(sync, "1=Synchronous writes");
81 /* completion handler for BIO reads */
82 static int bi_read_complete(struct bio *bio, unsigned int bytes_done, int error)
87 complete((struct completion*)bio->bi_private);
92 /* completion handler for BIO writes */
93 static int bi_write_complete(struct bio *bio, unsigned int bytes_done, int error)
95 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
96 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
102 err("bi_write_complete: not uptodate\n");
105 struct page *page = bvec->bv_page;
106 DEBUG(3, "Cleaning up page %ld\n", page->index);
107 if (--bvec >= bio->bi_io_vec)
108 prefetchw(&bvec->bv_page->flags);
111 SetPageUptodate(page);
113 ClearPageUptodate(page);
116 ClearPageDirty(page);
118 page_cache_release(page);
119 } while (bvec >= bio->bi_io_vec);
121 complete((struct completion*)bio->bi_private);
126 /* read one page from the block device */
127 static int blkmtd_readpage(struct blkmtd_dev *dev, struct page *page)
130 struct completion event;
133 if(PageUptodate(page)) {
134 DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
139 ClearPageUptodate(page);
140 ClearPageError(page);
142 bio = bio_alloc(GFP_KERNEL, 1);
144 init_completion(&event);
145 bio->bi_bdev = dev->blkdev;
146 bio->bi_sector = page->index << (PAGE_SHIFT-9);
147 bio->bi_private = &event;
148 bio->bi_end_io = bi_read_complete;
149 if(bio_add_page(bio, page, PAGE_SIZE, 0) == PAGE_SIZE) {
150 submit_bio(READ_SYNC, bio);
151 wait_for_completion(&event);
152 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
160 SetPageUptodate(page);
161 flush_dcache_page(page);
167 /* write out the current BIO and wait for it to finish */
168 static int blkmtd_write_out(struct bio *bio)
170 struct completion event;
178 init_completion(&event);
179 bio->bi_private = &event;
180 bio->bi_end_io = bi_write_complete;
181 submit_bio(WRITE_SYNC, bio);
182 wait_for_completion(&event);
183 DEBUG(3, "submit_bio completed, bi_vcnt = %d\n", bio->bi_vcnt);
184 err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
191 * blkmtd_add_page - add a page to the current BIO
192 * @bio: bio to add to (NULL to alloc initial bio)
193 * @blkdev: block device
195 * @pagecnt: pages left to add
197 * Adds a page to the current bio, allocating it if necessary. If it cannot be
198 * added, the current bio is written out and a new one is allocated. Returns
199 * the new bio to add or NULL on error
201 static struct bio *blkmtd_add_page(struct bio *bio, struct block_device *blkdev,
202 struct page *page, int pagecnt)
207 bio = bio_alloc(GFP_KERNEL, pagecnt);
210 bio->bi_sector = page->index << (PAGE_SHIFT-9);
211 bio->bi_bdev = blkdev;
214 if(bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
215 blkmtd_write_out(bio);
224 * write_pages - write block of data to device via the page cache
225 * @dev: device to write to
226 * @buf: data source or NULL if erase (output is set to 0xff)
227 * @to: offset into output device
228 * @len: amount to data to write
229 * @retlen: amount of data written
231 * Grab pages from the page cache and fill them with the source data.
232 * Non page aligned start and end result in a readin of the page and
233 * part of the page being modified. Pages are added to the bio and then written
236 static int write_pages(struct blkmtd_dev *dev, const u_char *buf, loff_t to,
237 size_t len, size_t *retlen)
240 size_t start_len = 0, end_len;
243 struct bio *bio = NULL;
246 pagenr = to >> PAGE_SHIFT;
247 offset = to & ~PAGE_MASK;
249 DEBUG(2, "blkmtd: write_pages: buf = %p to = %ld len = %zd pagenr = %d offset = %d\n",
250 buf, (long)to, len, pagenr, offset);
252 /* see if we have to do a partial write at the start */
254 start_len = ((offset + len) > PAGE_SIZE) ? PAGE_SIZE - offset : len;
258 /* calculate the length of the other two regions */
259 end_len = len & ~PAGE_MASK;
266 pagecnt += len >> PAGE_SHIFT;
271 down(&dev->wrbuf_mutex);
273 DEBUG(3, "blkmtd: write: start_len = %zd len = %zd end_len = %zd pagecnt = %d\n",
274 start_len, len, end_len, pagecnt);
277 /* do partial start region */
280 DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %zd offset = %d\n",
281 pagenr, start_len, offset);
284 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
286 if(PageDirty(page)) {
287 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
288 to, start_len, len, end_len, pagenr);
291 memcpy(page_address(page)+offset, buf, start_len);
293 SetPageUptodate(page);
296 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
299 err("bio_add_page failed\n");
306 /* Now do the main loop to a page aligned, n page sized output */
308 int pagesc = len >> PAGE_SHIFT;
309 DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n",
314 /* see if page is in the page cache */
315 DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
316 page = grab_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr);
317 if(PageDirty(page)) {
321 warn("write: cannot grab cache page %d", pagenr);
326 memset(page_address(page), 0xff, PAGE_SIZE);
328 memcpy(page_address(page), buf, PAGE_SIZE);
331 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
334 err("bio_add_page failed\n");
340 SetPageUptodate(page);
342 thislen += PAGE_SIZE;
347 /* do the third region */
349 DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %zd\n",
352 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
354 if(PageDirty(page)) {
355 err("to = %lld start_len = %zd len = %zd end_len = %zd pagenr = %d\n",
356 to, start_len, len, end_len, pagenr);
359 memcpy(page_address(page), buf, end_len);
361 SetPageUptodate(page);
362 DEBUG(3, "blkmtd: write: writing out partial end\n");
364 bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
367 err("bio_add_page failed\n");
373 DEBUG(3, "blkmtd: write: got %d vectors to write\n", bio->bi_vcnt);
376 blkmtd_write_out(bio);
378 DEBUG(2, "blkmtd: write: end, retlen = %zd, err = %d\n", *retlen, err);
379 up(&dev->wrbuf_mutex);
387 /* erase a specified part of the device */
388 static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
390 struct blkmtd_dev *dev = mtd->priv;
391 struct mtd_erase_region_info *einfo = mtd->eraseregions;
392 int numregions = mtd->numeraseregions;
398 instr->state = MTD_ERASING;
402 /* check erase region has valid start and length */
403 DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%zx len = 0x%lx\n",
404 mtd->name+9, from, len);
406 DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
407 einfo->offset, einfo->erasesize, einfo->numblocks);
408 if(from >= einfo->offset
409 && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
410 if(len == einfo->erasesize
411 && ( (from - einfo->offset) % einfo->erasesize == 0))
419 /* Not a valid erase block */
420 err("erase: invalid erase request 0x%lX @ 0x%08zX", len, from);
421 instr->state = MTD_ERASE_FAILED;
425 if(instr->state != MTD_ERASE_FAILED) {
427 DEBUG(3, "Doing erase from = %zd len = %ld\n", from, len);
428 err = write_pages(dev, NULL, from, len, &retlen);
429 if(err || retlen != len) {
430 err("erase failed err = %d", err);
431 instr->state = MTD_ERASE_FAILED;
433 instr->state = MTD_ERASE_DONE;
437 DEBUG(3, "blkmtd: erase: checking callback\n");
438 mtd_erase_callback(instr);
439 DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
444 /* read a range of the data via the page cache */
445 static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
446 size_t *retlen, u_char *buf)
448 struct blkmtd_dev *dev = mtd->priv;
454 DEBUG(2, "blkmtd: read: dev = `%s' from = %lld len = %zd buf = %p\n",
455 mtd->name+9, from, len, buf);
459 if(from + len > mtd->size)
460 len = mtd->size - from;
462 pagenr = from >> PAGE_SHIFT;
463 offset = from - (pagenr << PAGE_SHIFT);
465 pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT;
466 DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n",
467 pagenr, offset, pages);
473 DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
474 page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
480 cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE;
481 if(offset+cpylen > PAGE_SIZE)
482 cpylen = PAGE_SIZE-offset;
484 memcpy(buf + thislen, page_address(page) + offset, cpylen);
491 page_cache_release(page);
497 DEBUG(2, "blkmtd: end read: retlen = %zd, err = %d\n", thislen, err);
502 /* write data to the underlying device */
503 static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
504 size_t *retlen, const u_char *buf)
506 struct blkmtd_dev *dev = mtd->priv;
512 DEBUG(2, "blkmtd: write: dev = `%s' to = %lld len = %zd buf = %p\n",
513 mtd->name+9, to, len, buf);
515 if(to >= mtd->size) {
519 if(to + len > mtd->size) {
520 len = mtd->size - to;
523 err = write_pages(dev, buf, to, len, retlen);
526 DEBUG(2, "blkmtd: write: end, err = %d\n", err);
531 /* sync the device - wait until the write queue is empty */
532 static void blkmtd_sync(struct mtd_info *mtd)
534 /* Currently all writes are synchronous */
538 static void free_device(struct blkmtd_dev *dev)
540 DEBUG(2, "blkmtd: free_device() dev = %p\n", dev);
542 kfree(dev->mtd_info.eraseregions);
543 kfree(dev->mtd_info.name);
545 invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
546 close_bdev_excl(dev->blkdev);
553 /* For a given size and initial erase size, calculate the number
554 * and size of each erase region. Goes round the loop twice,
555 * once to find out how many regions, then allocates space,
556 * then round the loop again to fill it in.
558 static struct mtd_erase_region_info *calc_erase_regions(
559 size_t erase_size, size_t total_size, int *regions)
561 struct mtd_erase_region_info *info = NULL;
563 DEBUG(2, "calc_erase_regions, es = %zd size = %zd regions = %d\n",
564 erase_size, total_size, *regions);
565 /* Make any user specified erasesize be a power of 2
566 and at least PAGE_SIZE */
574 if(erase_size < PAGE_SIZE)
575 erase_size = PAGE_SIZE;
577 erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
583 int tot_size = total_size;
584 int er_size = erase_size;
585 int count = 0, offset = 0, regcnt = 0;
588 count = tot_size / er_size;
590 tot_size = tot_size % er_size;
592 DEBUG(2, "adding to erase info off=%d er=%d cnt=%d\n",
593 offset, er_size, count);
594 (info+regcnt)->offset = offset;
595 (info+regcnt)->erasesize = er_size;
596 (info+regcnt)->numblocks = count;
600 offset += (count * er_size);
602 while(er_size > tot_size)
606 info = kmalloc(regcnt * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
610 } while(!(*regions));
611 DEBUG(2, "calc_erase_regions done, es = %zd size = %zd regions = %d\n",
612 erase_size, total_size, *regions);
617 extern dev_t __init name_to_dev_t(const char *line);
619 static struct blkmtd_dev *add_device(char *devname, int readonly, int erase_size)
621 struct block_device *bdev;
623 struct blkmtd_dev *dev;
628 /* Get a handle on the device */
632 mode = (readonly) ? O_RDONLY : O_RDWR;
633 bdev = open_bdev_excl(devname, mode, NULL);
635 mode = (readonly) ? FMODE_READ : FMODE_WRITE;
636 bdev = open_by_devnum(name_to_dev_t(devname), mode);
639 err("error: cannot open device %s", devname);
640 DEBUG(2, "blkmtd: opening bdev returned %ld\n", PTR_ERR(bdev));
644 DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n",
645 MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
647 if(MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
648 err("attempting to use an MTD device as a block device");
653 dev = kmalloc(sizeof(struct blkmtd_dev), GFP_KERNEL);
659 memset(dev, 0, sizeof(struct blkmtd_dev));
662 init_MUTEX(&dev->wrbuf_mutex);
665 dev->mtd_info.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
667 /* Setup the MTD structure */
668 /* make the name contain the block device in */
669 dev->mtd_info.name = kmalloc(sizeof("blkmtd: ") + strlen(devname), GFP_KERNEL);
670 if(dev->mtd_info.name == NULL)
673 sprintf(dev->mtd_info.name, "blkmtd: %s", devname);
674 dev->mtd_info.eraseregions = calc_erase_regions(erase_size, dev->mtd_info.size,
675 &dev->mtd_info.numeraseregions);
676 if(dev->mtd_info.eraseregions == NULL)
679 dev->mtd_info.erasesize = dev->mtd_info.eraseregions->erasesize;
680 DEBUG(1, "blkmtd: init: found %d erase regions\n",
681 dev->mtd_info.numeraseregions);
684 dev->mtd_info.type = MTD_ROM;
685 dev->mtd_info.flags = MTD_CAP_ROM;
687 dev->mtd_info.type = MTD_RAM;
688 dev->mtd_info.flags = MTD_CAP_RAM;
689 dev->mtd_info.erase = blkmtd_erase;
690 dev->mtd_info.write = blkmtd_write;
691 dev->mtd_info.writev = default_mtd_writev;
692 dev->mtd_info.sync = blkmtd_sync;
694 dev->mtd_info.read = blkmtd_read;
695 dev->mtd_info.readv = default_mtd_readv;
696 dev->mtd_info.priv = dev;
697 dev->mtd_info.owner = THIS_MODULE;
699 list_add(&dev->list, &blkmtd_device_list);
700 if (add_mtd_device(&dev->mtd_info)) {
701 /* Device didnt get added, so free the entry */
702 list_del(&dev->list);
705 info("mtd%d: [%s] erase_size = %dKiB %s",
706 dev->mtd_info.index, dev->mtd_info.name + strlen("blkmtd: "),
707 dev->mtd_info.erasesize >> 10,
708 readonly ? "(read-only)" : "");
719 /* Cleanup and exit - sync the device and kill of the kernel thread */
720 static void __devexit cleanup_blkmtd(void)
722 struct list_head *temp1, *temp2;
724 /* Remove the MTD devices */
725 list_for_each_safe(temp1, temp2, &blkmtd_device_list) {
726 struct blkmtd_dev *dev = list_entry(temp1, struct blkmtd_dev,
728 blkmtd_sync(&dev->mtd_info);
729 del_mtd_device(&dev->mtd_info);
730 info("mtd%d: [%s] removed", dev->mtd_info.index,
731 dev->mtd_info.name + strlen("blkmtd: "));
732 list_del(&dev->list);
739 /* Handle kernel boot params */
742 static int __init param_blkmtd_device(char *str)
746 for(i = 0; i < MAX_DEVICES; i++) {
748 DEBUG(2, "blkmtd: device setup: %d = %s\n", i, device[i]);
755 static int __init param_blkmtd_erasesz(char *str)
758 for(i = 0; i < MAX_DEVICES; i++) {
759 char *val = strsep(&str, ",");
761 erasesz[i] = simple_strtoul(val, NULL, 0);
762 DEBUG(2, "blkmtd: erasesz setup: %d = %d\n", i, erasesz[i]);
769 static int __init param_blkmtd_ro(char *str)
772 for(i = 0; i < MAX_DEVICES; i++) {
773 char *val = strsep(&str, ",");
775 ro[i] = simple_strtoul(val, NULL, 0);
776 DEBUG(2, "blkmtd: ro setup: %d = %d\n", i, ro[i]);
783 static int __init param_blkmtd_sync(char *str)
790 __setup("blkmtd_device=", param_blkmtd_device);
791 __setup("blkmtd_erasesz=", param_blkmtd_erasesz);
792 __setup("blkmtd_ro=", param_blkmtd_ro);
793 __setup("blkmtd_sync=", param_blkmtd_sync);
799 static int __init init_blkmtd(void)
803 info("version " VERSION);
804 /* Check args - device[0] is the bare minimum*/
806 err("error: missing `device' name\n");
810 for(i = 0; i < MAX_DEVICES; i++)
811 add_device(device[i], ro[i], erasesz[i] << 10);
813 if(list_empty(&blkmtd_device_list))
819 module_init(init_blkmtd);
820 module_exit(cleanup_blkmtd);