Commit ad6ab150 authored by Simon Evans's avatar Simon Evans Committed by Linus Torvalds

[PATCH] blkmtd for 2.6

This updates the blkmtd driver with the latest which has been in the MTD
CVS for quite a while.  It is a rewrite from the 2.4 version to work
with the new block layer changes.
parent 3503ee60
/* /*
* $Id: blkmtd.c,v 1.7 2001/11/10 17:06:30 spse Exp $ * $Id: blkmtd-25.c,v 1.5 2003/07/16 06:48:27 spse Exp $
* *
* blkmtd.c - use a block device as a fake MTD * blkmtd.c - use a block device as a fake MTD
* *
* Author: Simon Evans <spse@secret.org.uk> * Author: Simon Evans <spse@secret.org.uk>
* *
* Copyright (C) 2001 Simon Evans * Copyright (C) 2001,2002 Simon Evans
* *
* Licence: GPL * Licence: GPL
* *
* How it works: * How it works:
* The driver uses raw/io to read/write the device and the page * The driver uses raw/io to read/write the device and the page
* cache to cache access. Writes update the page cache with the * cache to cache access. Writes update the page cache with the
* new data but make a copy of the new page(s) and then a kernel * new data and mark it dirty and add the page into a BIO which
* thread writes pages out to the device in the background. This * is then written out.
* ensures that writes are order even if a page is updated twice.
* Also, since pages in the page cache are never marked as dirty,
* we don't have to worry about writepage() being called on some
* random page which may not be in the write order.
*
* Erases are handled like writes, so the callback is called after
* the page cache has been updated. Sync()ing will wait until it is
* all done.
* *
* It can be loaded Read-Only to prevent erases and writes to the * It can be loaded Read-Only to prevent erases and writes to the
* medium. * medium.
* *
* Todo:
* Make the write queue size dynamic so this it is not too big on
* small memory systems and too small on large memory systems.
*
* Page cache usage may still be a bit wrong. Check we are doing
* everything properly.
*
* Somehow allow writes to dirty the page cache so we don't use too
* much memory making copies of outgoing pages. Need to handle case
* where page x is written to, then page y, then page x again before
* any of them have been committed to disk.
*
* Reading should read multiple pages at once rather than using
* readpage() for each one. This is easy and will be fixed asap.
*/ */
#include <linux/config.h> #include <linux/config.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/blkdev.h> #include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/iobuf.h> #include <linux/list.h>
#include <linux/slab.h> #include <linux/init.h>
#include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h> #include <linux/mtd/mtd.h>
#ifdef CONFIG_MTD_DEBUG
#ifdef CONFIG_PROC_FS #define err(format, arg...) printk(KERN_ERR "blkmtd: " format "\n" , ## arg)
# include <linux/proc_fs.h> #define info(format, arg...) printk(KERN_INFO "blkmtd: " format "\n" , ## arg)
# define BLKMTD_PROC_DEBUG #define warn(format, arg...) printk(KERN_WARNING "blkmtd: " format "\n" , ## arg)
static struct proc_dir_entry *blkmtd_proc; #define crit(format, arg...) printk(KERN_CRIT "blkmtd: " format "\n" , ## arg)
#endif
#endif
/* Default erase size in K, always make it a multiple of PAGE_SIZE */ /* Default erase size in K, always make it a multiple of PAGE_SIZE */
#define CONFIG_MTD_BLKDEV_ERASESIZE 128 #define CONFIG_MTD_BLKDEV_ERASESIZE (128 << 10) /* 128KiB */
#define VERSION "1.7" #define VERSION "$Revision: 1.5 $"
/* Info for the block device */ /* Info for the block device */
typedef struct mtd_raw_dev_data_s { struct blkmtd_dev {
struct block_device *binding; struct list_head list;
int sector_size, sector_bits; struct block_device *blkdev;
int partial_last_page; // 0 if device ends on page boundary, else page no of last page struct mtd_info mtd_info;
int last_page_sectors; // Number of sectors in last page if partial_last_page != 0 struct semaphore wrbuf_mutex;
size_t totalsize; };
int readonly;
struct address_space as;
struct mtd_info mtd_info;
} mtd_raw_dev_data_t;
/* Info for each queue item in the write queue */
typedef struct mtdblkdev_write_queue_s {
mtd_raw_dev_data_t *rawdevice;
struct page **pages;
int pagenr;
int pagecnt;
int iserase;
} mtdblkdev_write_queue_t;
/* Our erase page - always remains locked. */
static struct page *erase_page;
/* Static info about the MTD, used in cleanup_module */ /* Static info about the MTD, used in cleanup_module */
static mtd_raw_dev_data_t *mtd_rawdevice; static LIST_HEAD(blkmtd_device_list);
/* Write queue fixed size */
#define WRITE_QUEUE_SZ 512
/* Storage for the write queue */
static mtdblkdev_write_queue_t *write_queue;
static int write_queue_sz = WRITE_QUEUE_SZ;
static int volatile write_queue_head;
static int volatile write_queue_tail;
static int volatile write_queue_cnt;
static spinlock_t mbd_writeq_lock = SPIN_LOCK_UNLOCKED;
/* Tell the write thread to finish */
static volatile int write_task_finish;
/* ipc with the write thread */
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
static DECLARE_MUTEX_LOCKED(thread_sem);
static DECLARE_WAIT_QUEUE_HEAD(thr_wq);
static DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
#else
static struct semaphore thread_sem = MUTEX_LOCKED;
DECLARE_WAIT_QUEUE_HEAD(thr_wq);
DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
#endif
static void blkmtd_sync(struct mtd_info *mtd);
#define MAX_DEVICES 4
/* Module parameters passed by insmod/modprobe */ /* Module parameters passed by insmod/modprobe */
char *device; /* the block device to use */ char *device[MAX_DEVICES]; /* the block device to use */
int erasesz; /* optional default erase size */ int erasesz[MAX_DEVICES]; /* optional default erase size */
int ro; /* optional read only flag */ int ro[MAX_DEVICES]; /* optional read only flag */
int bs; /* optionally force the block size (avoid using) */ int sync;
int count; /* optionally force the block count (avoid using) */
int wqs; /* optionally set the write queue size */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>"); MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
MODULE_DESCRIPTION("Emulate an MTD using a block device"); MODULE_DESCRIPTION("Emulate an MTD using a block device");
MODULE_PARM(device, "s"); MODULE_PARM(device, "1-4s");
MODULE_PARM_DESC(device, "block device to use"); MODULE_PARM_DESC(device, "block device to use");
MODULE_PARM(erasesz, "i"); MODULE_PARM(erasesz, "1-4i");
MODULE_PARM_DESC(erasesz, "optional erase size to use in KB. eg 4=4K."); MODULE_PARM_DESC(erasesz, "optional erase size to use in KiB. eg 4=4KiB.");
MODULE_PARM(ro, "i"); MODULE_PARM(ro, "1-4i");
MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors"); MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors");
MODULE_PARM(bs, "i"); MODULE_PARM(sync, "i");
MODULE_PARM_DESC(bs, "force the block size in bytes"); MODULE_PARM_DESC(sync, "1=Synchronous writes");
MODULE_PARM(count, "i");
MODULE_PARM_DESC(count, "force the block count");
MODULE_PARM(wqs, "i");
#endif
/* Page cache stuff */ /* completion handler for BIO reads */
static int bi_read_complete(struct bio *bio, unsigned int bytes_done, int error)
{
if (bio->bi_size)
return 1;
complete((struct completion*)bio->bi_private);
return 0;
}
/* writepage() - should never be called - catch it anyway */ /* completion handler for BIO writes */
static int blkmtd_writepage(struct page *page, struct writeback_control *wbc) static int bi_write_complete(struct bio *bio, unsigned int bytes_done, int error)
{ {
printk("blkmtd: writepage called!!!\n"); const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
unlock_page(page); struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
return -EIO;
if (bio->bi_size)
return 1;
if(!uptodate)
err("bi_write_complete: not uptodate\n");
do {
struct page *page = bvec->bv_page;
DEBUG(3, "Cleaning up page %ld\n", page->index);
if (--bvec >= bio->bi_io_vec)
prefetchw(&bvec->bv_page->flags);
if (uptodate) {
SetPageUptodate(page);
} else {
ClearPageUptodate(page);
SetPageError(page);
}
ClearPageDirty(page);
unlock_page(page);
page_cache_release(page);
} while (bvec >= bio->bi_io_vec);
complete((struct completion*)bio->bi_private);
return 0;
} }
/* readpage() - reads one page from the block device */ /* read one page from the block device */
static int blkmtd_readpage(mtd_raw_dev_data_t *rawdevice, struct page *page) static int blkmtd_readpage(struct blkmtd_dev *dev, struct page *page)
{ {
int err; struct bio *bio;
int sectornr, sectors, i; struct completion event;
struct kiobuf *iobuf; int err = -ENOMEM;
sector_t *blocks;
char b[BDEVNAME_SIZE]; if(PageUptodate(page)) {
DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
if(!rawdevice) { unlock_page(page);
printk("blkmtd: readpage: PANIC file->private_data == NULL\n"); return 0;
return -EIO;
}
DEBUG(2, "blkmtd: readpage called, dev = `%s' page = %p index = %ld\n",
bdevname(rawdevice->binding, b), page, page->index);
if(PageUptodate(page)) {
DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
unlock_page(page);
return 0;
}
ClearPageUptodate(page);
ClearPageError(page);
/* see if page is in the outgoing write queue */
spin_lock(&mbd_writeq_lock);
if(write_queue_cnt) {
int i = write_queue_tail;
while(i != write_queue_head) {
mtdblkdev_write_queue_t *item = &write_queue[i];
if(page->index >= item->pagenr && page->index < item->pagenr+item->pagecnt) {
/* yes it is */
int index = page->index - item->pagenr;
DEBUG(2, "blkmtd: readpage: found page %ld in outgoing write queue\n",
page->index);
if(item->iserase) {
memset(page_address(page), 0xff, PAGE_SIZE);
} else {
memcpy(page_address(page), page_address(item->pages[index]), PAGE_SIZE);
} }
SetPageUptodate(page);
ClearPageUptodate(page);
ClearPageError(page);
bio = bio_alloc(GFP_KERNEL, 1);
if(bio) {
init_completion(&event);
bio->bi_bdev = dev->blkdev;
bio->bi_sector = page->index << (PAGE_SHIFT-9);
bio->bi_private = &event;
bio->bi_end_io = bi_read_complete;
if(bio_add_page(bio, page, PAGE_SIZE, 0) == PAGE_SIZE) {
submit_bio(READ, bio);
blk_run_queues();
wait_for_completion(&event);
err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
bio_put(bio);
}
}
if(err)
SetPageError(page);
else
SetPageUptodate(page);
flush_dcache_page(page); flush_dcache_page(page);
unlock_page(page); unlock_page(page);
spin_unlock(&mbd_writeq_lock); return err;
return 0; }
}
i++;
i %= write_queue_sz;
}
}
spin_unlock(&mbd_writeq_lock);
DEBUG(3, "blkmtd: readpage: getting kiovec\n");
err = alloc_kiovec(1, &iobuf);
if (err) {
printk("blkmtd: can't allocate kiobuf\n");
SetPageError(page);
return err;
}
/* Pre 2.4.4 doesn't have space for the block list in the kiobuf */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
blocks = kmalloc(KIO_MAX_SECTORS * sizeof(*blocks));
if(blocks == NULL) {
printk("blkmtd: can't allocate iobuf blocks\n");
free_kiovec(1, &iobuf);
SetPageError(page);
return -ENOMEM;
}
#else
blocks = iobuf->blocks;
#endif
iobuf->offset = 0;
iobuf->nr_pages = 1;
iobuf->length = PAGE_SIZE;
iobuf->locked = 1;
iobuf->maplist[0] = page;
sectornr = page->index << (PAGE_SHIFT - rawdevice->sector_bits);
sectors = 1 << (PAGE_SHIFT - rawdevice->sector_bits);
if(rawdevice->partial_last_page && page->index == rawdevice->partial_last_page) {
DEBUG(3, "blkmtd: handling partial last page\n");
sectors = rawdevice->last_page_sectors;
}
DEBUG(3, "blkmtd: readpage: sectornr = %d sectors = %d\n", sectornr, sectors);
for(i = 0; i < sectors; i++) {
blocks[i] = sectornr++;
}
/* If only a partial page read in, clear the rest of the page */
if(rawdevice->partial_last_page && page->index == rawdevice->partial_last_page) {
int offset = rawdevice->last_page_sectors << rawdevice->sector_bits;
int count = PAGE_SIZE-offset;
DEBUG(3, "blkmtd: clear last partial page: offset = %d count = %d\n", offset, count);
memset(page_address(page)+offset, 0, count);
sectors = rawdevice->last_page_sectors;
}
DEBUG(3, "bklmtd: readpage: starting brw_kiovec\n");
err = brw_kiovec(READ, 1, &iobuf, rawdevice->binding, blocks, rawdevice->sector_size);
DEBUG(3, "blkmtd: readpage: finished, err = %d\n", err);
iobuf->locked = 0;
free_kiovec(1, &iobuf);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
kfree(blocks);
#endif
if(err != PAGE_SIZE) { /* write out the current BIO and wait for it to finish */
printk("blkmtd: readpage: error reading page %ld\n", page->index); static int blkmtd_write_out(struct bio *bio)
memset(page_address(page), 0, PAGE_SIZE); {
SetPageError(page); struct completion event;
err = -EIO; int err;
} else {
DEBUG(3, "blkmtd: readpage: setting page upto date\n"); if(!bio->bi_vcnt) {
SetPageUptodate(page); bio_put(bio);
err = 0; return 0;
} }
flush_dcache_page(page);
unlock_page(page); init_completion(&event);
DEBUG(2, "blkmtd: readpage: finished, err = %d\n", err); bio->bi_private = &event;
return 0; bio->bi_end_io = bi_write_complete;
submit_bio(WRITE, bio);
blk_run_queues();
wait_for_completion(&event);
DEBUG(3, "submit_bio completed, bi_vcnt = %d\n", bio->bi_vcnt);
err = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : -EIO;
bio_put(bio);
return err;
} }
static struct address_space_operations blkmtd_aops = {
.writepage = blkmtd_writepage,
};
/* This is the kernel thread that empties the write queue to disk */ /**
static int write_queue_task(void *data) * blkmtd_add_page - add a page to the current BIO
* @bio: bio to add to (NULL to alloc initial bio)
* @blkdev: block device
* @page: page to add
* @pagecnt: pages left to add
*
* Adds a page to the current bio, allocating it if necessary. If it cannot be
* added, the current bio is written out and a new one is allocated. Returns
* the new bio to add or NULL on error
*/
static struct bio *blkmtd_add_page(struct bio *bio, struct block_device *blkdev,
struct page *page, int pagecnt)
{ {
int err;
struct task_struct *tsk = current;
struct kiobuf *iobuf;
sector_t *blocks;
DECLARE_WAITQUEUE(wait, tsk);
DEBUG(1, "blkmtd: writetask: starting (pid = %d)\n", tsk->pid);
daemonize("blkmtdd");
if(alloc_kiovec(1, &iobuf)) {
printk("blkmtd: write_queue_task can't allocate kiobuf\n");
return 0;
}
/* Pre 2.4.4 doesn't have space for the block list in the kiobuf */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
blocks = kmalloc(KIO_MAX_SECTORS * sizeof(unsigned long));
if(blocks == NULL) {
printk("blkmtd: write_queue_task can't allocate iobuf blocks\n");
free_kiovec(1, &iobuf);
return 0;
}
#else
blocks = iobuf->blocks;
#endif
DEBUG(2, "blkmtd: writetask: entering main loop\n"); retry:
add_wait_queue(&thr_wq, &wait); if(!bio) {
bio = bio_alloc(GFP_KERNEL, pagecnt);
while(1) { if(!bio)
spin_lock(&mbd_writeq_lock); return NULL;
bio->bi_sector = page->index << (PAGE_SHIFT-9);
if(!write_queue_cnt) { bio->bi_bdev = blkdev;
/* If nothing in the queue, wake up anyone wanting to know when there
is space in the queue then sleep for 2*HZ */
spin_unlock(&mbd_writeq_lock);
DEBUG(4, "blkmtd: writetask: queue empty\n");
if(waitqueue_active(&mtbd_sync_wq))
wake_up(&mtbd_sync_wq);
interruptible_sleep_on_timeout(&thr_wq, 2*HZ);
DEBUG(4, "blkmtd: writetask: woken up\n");
if(write_task_finish)
break;
} else {
/* we have stuff to write */
mtdblkdev_write_queue_t *item = &write_queue[write_queue_tail];
struct page **pages = item->pages;
int i;
int sectornr = item->pagenr << (PAGE_SHIFT - item->rawdevice->sector_bits);
int sectorcnt = item->pagecnt << (PAGE_SHIFT - item->rawdevice->sector_bits);
int max_sectors = KIO_MAX_SECTORS >> (item->rawdevice->sector_bits - 9);
/* If we are writing to the last page on the device and it doesn't end
* on a page boundary, subtract the number of sectors that don't exist.
*/
if(item->rawdevice->partial_last_page &&
(item->pagenr + item->pagecnt -1) == item->rawdevice->partial_last_page) {
sectorcnt -= (1 << (PAGE_SHIFT - item->rawdevice->sector_bits));
sectorcnt += item->rawdevice->last_page_sectors;
}
DEBUG(3, "blkmtd: writetask: got %d queue items\n", write_queue_cnt);
set_current_state(TASK_RUNNING);
spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtd: writetask: writing pagenr = %d pagecnt = %d sectornr = %d sectorcnt = %d\n",
item->pagenr, item->pagecnt, sectornr, sectorcnt);
iobuf->offset = 0;
iobuf->locked = 1;
/* Loop through all the pages to be written in the queue item, remembering
we can only write KIO_MAX_SECTORS at a time */
while(sectorcnt) {
int cursectors = (sectorcnt < max_sectors) ? sectorcnt : max_sectors;
int cpagecnt = (cursectors << item->rawdevice->sector_bits) + PAGE_SIZE-1;
cpagecnt >>= PAGE_SHIFT;
for(i = 0; i < cpagecnt; i++) {
if(item->iserase) {
iobuf->maplist[i] = erase_page;
} else {
iobuf->maplist[i] = *(pages++);
}
} }
for(i = 0; i < cursectors; i++) {
blocks[i] = sectornr++;
}
iobuf->nr_pages = cpagecnt;
iobuf->length = cursectors << item->rawdevice->sector_bits;
DEBUG(3, "blkmtd: write_task: about to kiovec\n");
err = brw_kiovec(WRITE, 1, &iobuf, item->rawdevice->binding, blocks, item->rawdevice->sector_size);
DEBUG(3, "bklmtd: write_task: done, err = %d\n", err);
if(err != (cursectors << item->rawdevice->sector_bits)) {
/* if an error occurred - set this to exit the loop */
sectorcnt = 0;
} else {
sectorcnt -= cursectors;
}
}
/* free up the pages used in the write and list of pages used in the write
queue item */
iobuf->locked = 0;
spin_lock(&mbd_writeq_lock);
write_queue_cnt--;
write_queue_tail++;
write_queue_tail %= write_queue_sz;
if(!item->iserase) {
for(i = 0 ; i < item->pagecnt; i++) {
unlock_page(item->pages[i]);
__free_pages(item->pages[i], 0);
}
kfree(item->pages);
}
item->pages = NULL;
spin_unlock(&mbd_writeq_lock);
/* Tell others there is some space in the write queue */
if(waitqueue_active(&mtbd_sync_wq))
wake_up(&mtbd_sync_wq);
}
}
remove_wait_queue(&thr_wq, &wait);
DEBUG(1, "blkmtd: writetask: exiting\n");
free_kiovec(1, &iobuf);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
kfree(blocks);
#endif
/* Tell people we have exitd */ if(bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) {
up(&thread_sem); blkmtd_write_out(bio);
return 0; bio = NULL;
goto retry;
}
return bio;
} }
/* Add a range of pages into the outgoing write queue, making copies of them */ /**
static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, * write_pages - write block of data to device via the page cache
int pagenr, int pagecnt, int iserase) * @dev: device to write to
* @buf: data source or NULL if erase (output is set to 0xff)
* @to: offset into output device
* @len: amount to data to write
* @retlen: amount of data written
*
* Grab pages from the page cache and fill them with the source data.
* Non page aligned start and end result in a readin of the page and
* part of the page being modified. Pages are added to the bio and then written
* out.
*/
static int write_pages(struct blkmtd_dev *dev, const u_char *buf, loff_t to,
size_t len, size_t *retlen)
{ {
struct page *outpage; int pagenr, offset;
struct page **new_pages = NULL; size_t start_len = 0, end_len;
mtdblkdev_write_queue_t *item; int pagecnt = 0;
int i; int err = 0;
DECLARE_WAITQUEUE(wait, current); struct bio *bio = NULL;
DEBUG(2, "blkmtd: queue_page_write: adding pagenr = %d pagecnt = %d\n", pagenr, pagecnt); size_t thislen = 0;
if(!pagecnt) pagenr = to >> PAGE_SHIFT;
return 0; offset = to & ~PAGE_MASK;
if(pages == NULL && !iserase) DEBUG(2, "blkmtd: write_pages: buf = %p to = %ld len = %d pagenr = %d offset = %d\n",
return -EINVAL; buf, (long)to, len, pagenr, offset);
/* create a array for the list of pages */ /* see if we have to do a partial write at the start */
if(!iserase) { if(offset) {
new_pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL); start_len = ((offset + len) > PAGE_SIZE) ? PAGE_SIZE - offset : len;
if(new_pages == NULL) len -= start_len;
return -ENOMEM; }
/* make copies of the pages in the page cache */ /* calculate the length of the other two regions */
for(i = 0; i < pagecnt; i++) { end_len = len & ~PAGE_MASK;
outpage = alloc_pages(GFP_KERNEL, 0); len -= end_len;
if(!outpage) {
while(i--) { if(start_len)
unlock_page(new_pages[i]); pagecnt++;
__free_pages(new_pages[i], 0);
if(len)
pagecnt += len >> PAGE_SHIFT;
if(end_len)
pagecnt++;
down(&dev->wrbuf_mutex);
DEBUG(3, "blkmtd: write: start_len = %d len = %d end_len = %d pagecnt = %d\n",
start_len, len, end_len, pagecnt);
if(start_len) {
/* do partial start region */
struct page *page;
DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %d offset = %d\n",
pagenr, start_len, offset);
BUG_ON(!buf);
page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
lock_page(page);
if(PageDirty(page)) {
err("to = %lld start_len = %d len = %d end_len = %d pagenr = %d\n",
to, start_len, len, end_len, pagenr);
BUG();
}
memcpy(page_address(page)+offset, buf, start_len);
SetPageDirty(page);
SetPageUptodate(page);
buf += start_len;
thislen = start_len;
bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
if(!bio) {
err = -ENOMEM;
err("bio_add_page failed\n");
goto write_err;
}
pagecnt--;
pagenr++;
}
/* Now do the main loop to a page aligned, n page sized output */
if(len) {
int pagesc = len >> PAGE_SHIFT;
DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n",
pagenr, pagesc);
while(pagesc) {
struct page *page;
/* see if page is in the page cache */
DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
page = grab_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr);
if(PageDirty(page)) {
BUG();
}
if(!page) {
warn("write: cannot grab cache page %d", pagenr);
err = -ENOMEM;
goto write_err;
}
if(!buf) {
memset(page_address(page), 0xff, PAGE_SIZE);
} else {
memcpy(page_address(page), buf, PAGE_SIZE);
buf += PAGE_SIZE;
}
bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
if(!bio) {
err = -ENOMEM;
err("bio_add_page failed\n");
goto write_err;
}
pagenr++;
pagecnt--;
SetPageDirty(page);
SetPageUptodate(page);
pagesc--;
thislen += PAGE_SIZE;
}
}
if(end_len) {
/* do the third region */
struct page *page;
DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %d\n",
pagenr, end_len);
BUG_ON(!buf);
page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
lock_page(page);
if(PageDirty(page)) {
err("to = %lld start_len = %d len = %d end_len = %d pagenr = %d\n",
to, start_len, len, end_len, pagenr);
BUG();
}
memcpy(page_address(page), buf, end_len);
SetPageDirty(page);
SetPageUptodate(page);
DEBUG(3, "blkmtd: write: writing out partial end\n");
thislen += end_len;
bio = blkmtd_add_page(bio, dev->blkdev, page, pagecnt);
if(!bio) {
err = -ENOMEM;
err("bio_add_page failed\n");
goto write_err;
}
pagenr++;
} }
kfree(new_pages);
return -ENOMEM; DEBUG(3, "blkmtd: write: got %d vectors to write\n", bio->bi_vcnt);
} write_err:
lock_page(outpage); if(bio)
memcpy(page_address(outpage), page_address(pages[i]), PAGE_SIZE); blkmtd_write_out(bio);
new_pages[i] = outpage;
} DEBUG(2, "blkmtd: write: end, retlen = %d, err = %d\n", *retlen, err);
} up(&dev->wrbuf_mutex);
/* wait until there is some space in the write queue */ if(retlen)
test_lock: *retlen = thislen;
spin_lock(&mbd_writeq_lock); return err;
if(write_queue_cnt == write_queue_sz) {
spin_unlock(&mbd_writeq_lock);
DEBUG(3, "blkmtd: queue_page: Queue full\n");
current->state = TASK_UNINTERRUPTIBLE;
add_wait_queue(&mtbd_sync_wq, &wait);
wake_up_interruptible(&thr_wq);
schedule();
current->state = TASK_RUNNING;
remove_wait_queue(&mtbd_sync_wq, &wait);
DEBUG(3, "blkmtd: queue_page_write: Queue has %d items in it\n", write_queue_cnt);
goto test_lock;
}
DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
write_queue_head, write_queue_tail, write_queue_cnt);
/* fix up the queue item */
item = &write_queue[write_queue_head];
item->pages = new_pages;
item->pagenr = pagenr;
item->pagecnt = pagecnt;
item->rawdevice = rawdevice;
item->iserase = iserase;
write_queue_head++;
write_queue_head %= write_queue_sz;
write_queue_cnt++;
DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
write_queue_head, write_queue_tail, write_queue_cnt);
spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtd: queue_page_write: finished\n");
return 0;
} }
/* erase a specified part of the device */ /* erase a specified part of the device */
static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr) static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
{ {
mtd_raw_dev_data_t *rawdevice = mtd->priv; struct blkmtd_dev *dev = mtd->priv;
struct mtd_erase_region_info *einfo = mtd->eraseregions; struct mtd_erase_region_info *einfo = mtd->eraseregions;
int numregions = mtd->numeraseregions; int numregions = mtd->numeraseregions;
size_t from; size_t from;
u_long len; u_long len;
int err = 0; int err = -EIO;
char b[BDEVNAME_SIZE]; int retlen;
/* check readonly */ instr->state = MTD_ERASING;
if(rawdevice->readonly) { from = instr->addr;
printk("blkmtd: error: trying to erase readonly device %s\n", device); len = instr->len;
instr->state = MTD_ERASE_FAILED;
goto erase_callback; /* check erase region has valid start and length */
} DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%x len = 0x%lx\n",
mtd->name+9, from, len);
instr->state = MTD_ERASING; while(numregions) {
from = instr->addr; DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
len = instr->len; einfo->offset, einfo->erasesize, einfo->numblocks);
if(from >= einfo->offset
/* check erase region has valid start and length */ && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%x len = 0x%lx\n", if(len == einfo->erasesize
bdevname(rawdevice->binding, b), from, len); && ( (from - einfo->offset) % einfo->erasesize == 0))
while(numregions) { break;
DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n", }
einfo->offset, einfo->erasesize, einfo->numblocks); numregions--;
if(from >= einfo->offset && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) { einfo++;
if(len == einfo->erasesize && ( (from - einfo->offset) % einfo->erasesize == 0)) }
break;
} if(!numregions) {
numregions--; /* Not a valid erase block */
einfo++; err("erase: invalid erase request 0x%lX @ 0x%08X", len, from);
} instr->state = MTD_ERASE_FAILED;
err = -EIO;
if(!numregions) { }
/* Not a valid erase block */
printk("blkmtd: erase: invalid erase request 0x%lX @ 0x%08X\n", len, from); if(instr->state != MTD_ERASE_FAILED) {
instr->state = MTD_ERASE_FAILED; /* do the erase */
err = -EIO; DEBUG(3, "Doing erase from = %d len = %ld\n", from, len);
} err = write_pages(dev, NULL, from, len, &retlen);
if(err || retlen != len) {
if(instr->state != MTD_ERASE_FAILED) { err("erase failed err = %d", err);
/* start the erase */ instr->state = MTD_ERASE_FAILED;
int pagenr, pagecnt; } else {
struct page *page, **pages; instr->state = MTD_ERASE_DONE;
int i = 0; }
}
/* Handle the last page of the device not being whole */
if(len < PAGE_SIZE) DEBUG(3, "blkmtd: erase: checking callback\n");
len = PAGE_SIZE; if (instr->callback) {
(*(instr->callback))(instr);
pagenr = from >> PAGE_SHIFT; }
pagecnt = len >> PAGE_SHIFT; DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
DEBUG(3, "blkmtd: erase: pagenr = %d pagecnt = %d\n", pagenr, pagecnt); return err;
pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
if(pages == NULL) {
err = -ENOMEM;
instr->state = MTD_ERASE_FAILED;
goto erase_out;
}
while(pagecnt) {
/* get the page via the page cache */
DEBUG(3, "blkmtd: erase: doing grab_cache_page() for page %d\n", pagenr);
page = grab_cache_page(&rawdevice->as, pagenr);
if(!page) {
DEBUG(3, "blkmtd: erase: grab_cache_page() failed for page %d\n", pagenr);
kfree(pages);
err = -EIO;
instr->state = MTD_ERASE_FAILED;
goto erase_out;
}
memset(page_address(page), 0xff, PAGE_SIZE);
pages[i] = page;
pagecnt--;
pagenr++;
i++;
}
DEBUG(3, "blkmtd: erase: queuing page write\n");
err = queue_page_write(rawdevice, NULL, from >> PAGE_SHIFT, len >> PAGE_SHIFT, 1);
pagecnt = len >> PAGE_SHIFT;
if(!err) {
while(pagecnt--) {
SetPageUptodate(pages[pagecnt]);
unlock_page(pages[pagecnt]);
page_cache_release(pages[pagecnt]);
flush_dcache_page(pages[pagecnt]);
}
kfree(pages);
instr->state = MTD_ERASE_DONE;
} else {
while(pagecnt--) {
SetPageError(pages[pagecnt]);
page_cache_release(pages[pagecnt]);
}
kfree(pages);
instr->state = MTD_ERASE_FAILED;
}
}
erase_out:
DEBUG(3, "blkmtd: erase: checking callback\n");
erase_callback:
if (instr->callback) {
(*(instr->callback))(instr);
}
DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
return err;
} }
/* read a range of the data via the page cache */ /* read a range of the data via the page cache */
static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len, static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf) size_t *retlen, u_char *buf)
{ {
mtd_raw_dev_data_t *rawdevice = mtd->priv; struct blkmtd_dev *dev = mtd->priv;
int err = 0; int err = 0;
int offset; int offset;
int pagenr, pages; int pagenr, pages;
char b[BDEVNAME_SIZE]; size_t thislen = 0;
*retlen = 0; DEBUG(2, "blkmtd: read: dev = `%s' from = %ld len = %d buf = %p\n",
mtd->name+9, (long int)from, len, buf);
DEBUG(2, "blkmtd: read: dev = `%s' from = %ld len = %d buf = %p\n",
bdevname(rawdevice->binding, b), (long int)from, len, buf); if(from > mtd->size)
return -EINVAL;
pagenr = from >> PAGE_SHIFT; if(from + len > mtd->size)
offset = from - (pagenr << PAGE_SHIFT); len = mtd->size - from;
pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT; pagenr = from >> PAGE_SHIFT;
DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n", pagenr, offset, pages); offset = from - (pagenr << PAGE_SHIFT);
/* just loop through each page, getting it via readpage() - slow but easy */ pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT;
while(pages) { DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n",
struct page *page; pagenr, offset, pages);
int cpylen;
DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr); while(pages) {
page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice); struct page *page;
if(IS_ERR(page)) { int cpylen;
return PTR_ERR(page);
} DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
wait_on_page_locked(page); page = read_cache_page(dev->blkdev->bd_inode->i_mapping, pagenr, (filler_t *)blkmtd_readpage, dev);
if(!PageUptodate(page)) { if(IS_ERR(page)) {
/* error reading page */ err = -EIO;
printk("blkmtd: read: page not uptodate\n"); goto readerr;
page_cache_release(page); }
return -EIO;
} cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE;
if(offset+cpylen > PAGE_SIZE)
cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE; cpylen = PAGE_SIZE-offset;
if(offset+cpylen > PAGE_SIZE)
cpylen = PAGE_SIZE-offset; memcpy(buf + thislen, page_address(page) + offset, cpylen);
offset = 0;
memcpy(buf + *retlen, page_address(page) + offset, cpylen); len -= cpylen;
offset = 0; thislen += cpylen;
len -= cpylen; pagenr++;
*retlen += cpylen; pages--;
pagenr++; if(!PageDirty(page))
pages--; page_cache_release(page);
page_cache_release(page); }
}
readerr:
DEBUG(2, "blkmtd: end read: retlen = %d, err = %d\n", *retlen, err); if(retlen)
return err; *retlen = thislen;
DEBUG(2, "blkmtd: end read: retlen = %d, err = %d\n", thislen, err);
return err;
} }
/* write a range of the data via the page cache. /* write data to the underlying device */
*
* Basic operation. break the write into three parts.
*
* 1. From a page unaligned start up until the next page boundary
* 2. Page sized, page aligned blocks
* 3. From end of last aligned block to end of range
*
* 1,3 are read via the page cache and readpage() since these are partial
* pages, 2 we just grab pages from the page cache, not caring if they are
* already in memory or not since they will be completly overwritten.
*
*/
static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len, static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf) size_t *retlen, const u_char *buf)
{ {
mtd_raw_dev_data_t *rawdevice = mtd->priv; struct blkmtd_dev *dev = mtd->priv;
int err = 0; int err;
int offset;
int pagenr;
size_t len1 = 0, len2 = 0, len3 = 0;
struct page **pages;
int pagecnt = 0;
char b[BDEVNAME_SIZE];
*retlen = 0;
DEBUG(2, "blkmtd: write: dev = `%s' to = %ld len = %d buf = %p\n",
bdevname(rawdevice->binding, b), (long int)to, len, buf);
/* handle readonly and out of range numbers */
if(rawdevice->readonly) {
printk("blkmtd: error: trying to write to a readonly device %s\n", device);
return -EROFS;
}
if(to >= rawdevice->totalsize) {
return -ENOSPC;
}
if(to + len > rawdevice->totalsize) {
len = (rawdevice->totalsize - to);
}
pagenr = to >> PAGE_SHIFT;
offset = to - (pagenr << PAGE_SHIFT);
/* see if we have to do a partial write at the start */
if(offset) {
if((offset + len) > PAGE_SIZE) {
len1 = PAGE_SIZE - offset;
len -= len1;
} else {
len1 = len;
len = 0;
}
}
/* calculate the length of the other two regions */
len3 = len & ~PAGE_MASK;
len -= len3;
len2 = len;
if(len1)
pagecnt++;
if(len2)
pagecnt += len2 >> PAGE_SHIFT;
if(len3)
pagecnt++;
DEBUG(3, "blkmtd: write: len1 = %d len2 = %d len3 = %d pagecnt = %d\n", len1, len2, len3, pagecnt);
/* get space for list of pages */
pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
if(pages == NULL) {
return -ENOMEM;
}
pagecnt = 0;
if(len1) {
/* do partial start region */
struct page *page;
DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %d offset = %d\n", pagenr, len1, offset);
page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
if(IS_ERR(page)) {
kfree(pages);
return PTR_ERR(page);
}
memcpy(page_address(page)+offset, buf, len1);
pages[pagecnt++] = page;
buf += len1;
*retlen = len1;
err = 0;
pagenr++;
}
/* Now do the main loop to a page aligned, n page sized output */
if(len2) {
int pagesc = len2 >> PAGE_SHIFT;
DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n", pagenr, pagesc);
while(pagesc) {
struct page *page;
/* see if page is in the page cache */
DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
page = grab_cache_page(&rawdevice->as, pagenr);
DEBUG(3, "blkmtd: write: got page %d from page cache\n", pagenr);
if(!page) {
printk("blkmtd: write: can't grab cache page %d\n", pagenr);
err = -EIO;
goto write_err;
}
memcpy(page_address(page), buf, PAGE_SIZE);
pages[pagecnt++] = page;
unlock_page(page);
SetPageUptodate(page);
pagenr++;
pagesc--;
buf += PAGE_SIZE;
*retlen += PAGE_SIZE;
}
}
if(len3) {
/* do the third region */
struct page *page;
DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %d\n", pagenr, len3);
page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
if(IS_ERR(page)) {
err = PTR_ERR(page);
goto write_err;
}
memcpy(page_address(page), buf, len3);
DEBUG(3, "blkmtd: write: writing out partial end\n");
pages[pagecnt++] = page;
*retlen += len3;
err = 0;
}
DEBUG(2, "blkmtd: write: end, retlen = %d, err = %d\n", *retlen, err);
/* submit it to the write task */
err = queue_page_write(rawdevice, pages, to >> PAGE_SHIFT, pagecnt, 0);
if(!err) {
while(pagecnt--) {
SetPageUptodate(pages[pagecnt]);
flush_dcache_page(pages[pagecnt]);
page_cache_release(pages[pagecnt]);
}
kfree(pages);
return 0;
}
write_err: if(!len)
while(--pagecnt) { return 0;
SetPageError(pages[pagecnt]);
page_cache_release(pages[pagecnt]); DEBUG(2, "blkmtd: write: dev = `%s' to = %ld len = %d buf = %p\n",
} mtd->name+9, (long int)to, len, buf);
kfree(pages);
return err; if(to >= mtd->size) {
return -ENOSPC;
}
if(to + len > mtd->size) {
len = mtd->size - to;
}
err = write_pages(dev, buf, to, len, retlen);
if(err > 0)
err = 0;
DEBUG(2, "blkmtd: write: end, err = %d\n", err);
return err;
} }
/* sync the device - wait until the write queue is empty */ /* sync the device - wait until the write queue is empty */
static void blkmtd_sync(struct mtd_info *mtd) static void blkmtd_sync(struct mtd_info *mtd)
{ {
DECLARE_WAITQUEUE(wait, current); /* Currently all writes are synchronous */
mtd_raw_dev_data_t *rawdevice = mtd->priv;
if(rawdevice->readonly)
return;
DEBUG(2, "blkmtd: sync: called\n");
stuff_inq:
spin_lock(&mbd_writeq_lock);
if(write_queue_cnt) {
spin_unlock(&mbd_writeq_lock);
current->state = TASK_UNINTERRUPTIBLE;
add_wait_queue(&mtbd_sync_wq, &wait);
DEBUG(3, "blkmtd: sync: waking up task\n");
wake_up_interruptible(&thr_wq);
schedule();
current->state = TASK_RUNNING;
remove_wait_queue(&mtbd_sync_wq, &wait);
DEBUG(3, "blkmtd: sync: waking up after write task\n");
goto stuff_inq;
}
spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtd: sync: finished\n");
} }
#ifdef BLKMTD_PROC_DEBUG static void free_device(struct blkmtd_dev *dev)
/* procfs stuff */
static int blkmtd_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{ {
int clean = 0, dirty = 0, locked = 0; DEBUG(2, "blkmtd: free_device() dev = %p\n", dev);
struct list_head *temp; if(dev) {
int i, len, pages = 0, cnt; if(dev->mtd_info.eraseregions)
MOD_INC_USE_COUNT; kfree(dev->mtd_info.eraseregions);
spin_lock(&mbd_writeq_lock); if(dev->mtd_info.name)
cnt = write_queue_cnt; kfree(dev->mtd_info.name);
i = write_queue_tail;
while(cnt) { if(dev->blkdev) {
if(!write_queue[i].iserase) invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
pages += write_queue[i].pagecnt; close_bdev_excl(dev->blkdev, BDEV_RAW);
i++; }
i %= write_queue_sz; kfree(dev);
cnt--; }
}
/* Count the size of the page lists */
list_for_each(temp, &mtd_rawdevice->as.clean_pages) {
clean++;
}
list_for_each(temp, &mtd_rawdevice->as.dirty_pages) {
dirty++;
}
list_for_each(temp, &mtd_rawdevice->as.locked_pages) {
locked++;
}
len = sprintf(page, "Write queue head: %d\nWrite queue tail: %d\n"
"Write queue count: %d\nPages in queue: %d (%dK)\n"
"Clean Pages: %d\nDirty Pages: %d\nLocked Pages: %d\n"
"nrpages: %ld\n",
write_queue_head, write_queue_tail, write_queue_cnt,
pages, pages << (PAGE_SHIFT-10), clean, dirty, locked,
mtd_rawdevice->as.nrpages);
if(len <= count)
*eof = 1;
spin_unlock(&mbd_writeq_lock);
MOD_DEC_USE_COUNT;
return len;
} }
#endif
/* Cleanup and exit - sync the device and kill of the kernel thread */ /* For a given size and initial erase size, calculate the number
static void __exit cleanup_blkmtd(void) * and size of each erase region. Goes round the loop twice,
* once to find out how many regions, then allocates space,
* then round the loop again to fill it in.
*/
static struct mtd_erase_region_info *calc_erase_regions(
size_t erase_size, size_t total_size, int *regions)
{
struct mtd_erase_region_info *info = NULL;
DEBUG(2, "calc_erase_regions, es = %d size = %d regions = %d\n",
erase_size, total_size, *regions);
/* Make any user specified erasesize be a power of 2
and at least PAGE_SIZE */
if(erase_size) {
int es = erase_size;
erase_size = 1;
while(es != 1) {
es >>= 1;
erase_size <<= 1;
}
if(erase_size < PAGE_SIZE)
erase_size = PAGE_SIZE;
} else {
erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
}
*regions = 0;
do {
int tot_size = total_size;
int er_size = erase_size;
int count = 0, offset = 0, regcnt = 0;
while(tot_size) {
count = tot_size / er_size;
if(count) {
tot_size = tot_size % er_size;
if(info) {
DEBUG(2, "adding to erase info off=%d er=%d cnt=%d\n",
offset, er_size, count);
(info+regcnt)->offset = offset;
(info+regcnt)->erasesize = er_size;
(info+regcnt)->numblocks = count;
(*regions)++;
}
regcnt++;
offset += (count * er_size);
}
while(er_size > tot_size)
er_size >>= 1;
}
if(info == NULL) {
info = kmalloc(regcnt * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
if(!info)
break;
}
} while(!(*regions));
DEBUG(2, "calc_erase_regions done, es = %d size = %d regions = %d\n",
erase_size, total_size, *regions);
return info;
}
extern dev_t __init name_to_dev_t(const char *line);
static struct blkmtd_dev *add_device(char *devname, int readonly, int erase_size)
{ {
#ifdef BLKMTD_PROC_DEBUG struct block_device *bdev;
if(blkmtd_proc) { int mode;
remove_proc_entry("blkmtd_debug", NULL); struct blkmtd_dev *dev;
}
if(!devname)
return NULL;
/* Get a handle on the device */
#ifdef MODULE
mode = (readonly) ? O_RDONLY : O_RDWR;
bdev = open_bdev_excl(devname, mode, BDEV_RAW, NULL);
#else
mode = (readonly) ? FMODE_READ : FMODE_WRITE;
bdev = open_by_devnum(name_to_dev_t(devname), mode, BDEV_RAW);
#endif #endif
if(IS_ERR(bdev)) {
err("error: cannot open device %s", devname);
DEBUG(2, "blkmtd: opening bdev returned %ld\n", PTR_ERR(bdev));
return NULL;
}
DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n",
MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
if(MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
err("attempting to use an MTD device as a block device");
blkdev_put(bdev, BDEV_RAW);
return NULL;
}
dev = kmalloc(sizeof(struct blkmtd_dev), GFP_KERNEL);
if(dev == NULL) {
blkdev_put(bdev, BDEV_RAW);
return NULL;
}
memset(dev, 0, sizeof(struct blkmtd_dev));
atomic_set(&(dev->blkdev->bd_inode->i_mapping->truncate_count), 0);
if(!readonly) {
init_MUTEX(&dev->wrbuf_mutex);
}
dev->blkdev = bdev;
dev->mtd_info.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
/* Setup the MTD structure */
/* make the name contain the block device in */
dev->mtd_info.name = kmalloc(sizeof("blkmtd: ") + strlen(devname), GFP_KERNEL);
if(dev->mtd_info.name == NULL)
goto devinit_err;
if (mtd_rawdevice) { sprintf(dev->mtd_info.name, "blkmtd: %s", devname);
/* sync the device */ dev->mtd_info.eraseregions = calc_erase_regions(erase_size, dev->mtd_info.size,
if (!mtd_rawdevice->readonly) { &dev->mtd_info.numeraseregions);
blkmtd_sync(&mtd_rawdevice->mtd_info); if(dev->mtd_info.eraseregions == NULL)
write_task_finish = 1; goto devinit_err;
wake_up_interruptible(&thr_wq);
down(&thread_sem); dev->mtd_info.erasesize = dev->mtd_info.eraseregions->erasesize;
} DEBUG(1, "blkmtd: init: found %d erase regions\n",
del_mtd_device(&mtd_rawdevice->mtd_info); dev->mtd_info.numeraseregions);
if(mtd_rawdevice->binding != NULL)
blkdev_put(mtd_rawdevice->binding, BDEV_RAW); if(readonly) {
dev->mtd_info.type = MTD_ROM;
if(mtd_rawdevice->mtd_info.eraseregions) dev->mtd_info.flags = MTD_CAP_ROM;
kfree(mtd_rawdevice->mtd_info.eraseregions); } else {
if(mtd_rawdevice->mtd_info.name) dev->mtd_info.type = MTD_RAM;
kfree(mtd_rawdevice->mtd_info.name); dev->mtd_info.flags = MTD_CAP_RAM;
dev->mtd_info.erase = blkmtd_erase;
kfree(mtd_rawdevice); dev->mtd_info.write = blkmtd_write;
} dev->mtd_info.writev = default_mtd_writev;
if(write_queue) dev->mtd_info.sync = blkmtd_sync;
kfree(write_queue); }
dev->mtd_info.read = blkmtd_read;
if(erase_page) { dev->mtd_info.readv = default_mtd_readv;
unlock_page(erase_page); dev->mtd_info.priv = dev;
__free_pages(erase_page, 0); dev->mtd_info.owner = THIS_MODULE;
}
printk("blkmtd: unloaded for %s\n", device); list_add(&dev->list, &blkmtd_device_list);
if (add_mtd_device(&dev->mtd_info)) {
/* Device didnt get added, so free the entry */
list_del(&dev->list);
goto devinit_err;
} else {
info("mtd%d: [%s] erase_size = %dKiB %s",
dev->mtd_info.index, dev->mtd_info.name + strlen("blkmtd: "),
dev->mtd_info.erasesize >> 10,
readonly ? "(read-only)" : "");
}
return dev;
devinit_err:
free_device(dev);
return NULL;
} }
extern struct module __this_module;
/* Cleanup and exit - sync the device and kill of the kernel thread */
static void __devexit cleanup_blkmtd(void)
{
struct list_head *temp1, *temp2;
/* Remove the MTD devices */
list_for_each_safe(temp1, temp2, &blkmtd_device_list) {
struct blkmtd_dev *dev = list_entry(temp1, struct blkmtd_dev,
list);
blkmtd_sync(&dev->mtd_info);
del_mtd_device(&dev->mtd_info);
info("mtd%d: [%s] removed", dev->mtd_info.index,
dev->mtd_info.name + strlen("blkmtd: "));
list_del(&dev->list);
free_device(dev);
}
}
#ifndef MODULE #ifndef MODULE
...@@ -973,296 +749,79 @@ extern struct module __this_module; ...@@ -973,296 +749,79 @@ extern struct module __this_module;
static int __init param_blkmtd_device(char *str) static int __init param_blkmtd_device(char *str)
{ {
device = str; int i;
return 1;
for(i = 0; i < MAX_DEVICES; i++) {
device[i] = str;
DEBUG(2, "blkmtd: device setup: %d = %s\n", i, device[i]);
strsep(&str, ",");
}
return 1;
} }
static int __init param_blkmtd_erasesz(char *str) static int __init param_blkmtd_erasesz(char *str)
{ {
erasesz = simple_strtol(str, NULL, 0); int i;
return 1; for(i = 0; i < MAX_DEVICES; i++) {
char *val = strsep(&str, ",");
if(val)
erasesz[i] = simple_strtoul(val, NULL, 0);
DEBUG(2, "blkmtd: erasesz setup: %d = %d\n", i, erasesz[i]);
}
return 1;
} }
static int __init param_blkmtd_ro(char *str) static int __init param_blkmtd_ro(char *str)
{ {
ro = simple_strtol(str, NULL, 0); int i;
return 1; for(i = 0; i < MAX_DEVICES; i++) {
} char *val = strsep(&str, ",");
if(val)
ro[i] = simple_strtoul(val, NULL, 0);
DEBUG(2, "blkmtd: ro setup: %d = %d\n", i, ro[i]);
}
static int __init param_blkmtd_bs(char *str) return 1;
{
bs = simple_strtol(str, NULL, 0);
return 1;
} }
static int __init param_blkmtd_count(char *str) static int __init param_blkmtd_sync(char *str)
{ {
count = simple_strtol(str, NULL, 0); if(str[0] == '1')
return 1; sync = 1;
return 1;
} }
__setup("blkmtd_device=", param_blkmtd_device); __setup("blkmtd_device=", param_blkmtd_device);
__setup("blkmtd_erasesz=", param_blkmtd_erasesz); __setup("blkmtd_erasesz=", param_blkmtd_erasesz);
__setup("blkmtd_ro=", param_blkmtd_ro); __setup("blkmtd_ro=", param_blkmtd_ro);
__setup("blkmtd_bs=", param_blkmtd_bs); __setup("blkmtd_sync=", param_blkmtd_sync);
__setup("blkmtd_count=", param_blkmtd_count);
#endif #endif
/* for a given size and initial erase size, calculate the number and size of each
erase region */
static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t erase_size, size_t total_size)
{
int count = 0;
int offset = 0;
int regions = 0;
while(total_size) {
count = total_size / erase_size;
if(count) {
total_size = total_size % erase_size;
if(info) {
info->offset = offset;
info->erasesize = erase_size;
info->numblocks = count;
info++;
}
offset += (count * erase_size);
regions++;
}
while(erase_size > total_size)
erase_size >>= 1;
}
return regions;
}
extern dev_t name_to_dev_t(char *line) __init;
/* Startup */ /* Startup */
static int __init init_blkmtd(void) static int __init init_blkmtd(void)
{ {
int i, blocksize, blocksize_bits; int i;
loff_t size;
int readonly = 0;
int erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
struct block_device *bdev;
int err;
int mode;
int regions;
char b[BDEVNAME_SIZE];
/* Check args */
if(device == 0) {
printk("blkmtd: error, missing `device' name\n");
return -EINVAL;
}
if(ro)
readonly = 1;
if(erasesz)
erase_size = erasesz;
if(wqs) {
if(wqs < 16)
wqs = 16;
if(wqs > 4*WRITE_QUEUE_SZ)
wqs = 4*WRITE_QUEUE_SZ;
write_queue_sz = wqs;
}
DEBUG(1, "blkmtd: device = `%s' erase size = %dK readonly = %s queue size = %d\n",
device, erase_size, readonly ? "yes" : "no", write_queue_sz);
/* Get a handle on the device */
mode = (readonly) ? O_RDONLY : O_RDWR;
#ifdef MODULE info("version " VERSION);
bdev = open_bdev_excl(device, mode, BDEV_RAW, NULL); /* Check args - device[0] is the bare minimum*/
#else if(!device[0]) {
bdev = open_by_devnum(name_to_dev_t(device), FMODE_READ, BDEV_RAW); err("error: missing `device' name\n");
#endif return -EINVAL;
}
if (IS_ERR(bdev)){ for(i = 0; i < MAX_DEVICES; i++)
printk("blkmtd: error, can't open device %s\n", device); add_device(device[i], ro[i], erasesz[i] << 10);
DEBUG(2, "blkmtd: opening bdev returned %ld\n", PTR_ERR(bdev));
return 1; if(list_empty(&blkmtd_device_list))
} return -EINVAL;
DEBUG(1, "blkmtd: devname = %s\n", bdevname(bdev, b)); return 0;
if(MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
printk("blkmtd: attempting to use an MTD device as a block device\n");
blkdev_put(bdev, BDEV_RAW);
return 1;
}
blocksize = BLOCK_SIZE;
blocksize = bs ? bs : block_size(bdev);
i = blocksize;
blocksize_bits = 0;
while(i != 1) {
blocksize_bits++;
i >>= 1;
}
size = count ? count*blocksize : bdev->bd_inode->i_size;
DEBUG(1, "blkmtd: size = %ld\n", (long int)size);
if(size == 0) {
printk("blkmtd: can't determine size\n");
blkdev_put(bdev, BDEV_RAW);
return 1;
}
mtd_rawdevice = (mtd_raw_dev_data_t *)kmalloc(sizeof(mtd_raw_dev_data_t), GFP_KERNEL);
if(mtd_rawdevice == NULL) {
blkdev_put(bdev, BDEV_RAW);
err = -ENOMEM;
goto init_err;
}
memset(mtd_rawdevice, 0, sizeof(mtd_raw_dev_data_t));
mtd_rawdevice->binding = bdev;
mtd_rawdevice->totalsize = size;
mtd_rawdevice->sector_size = blocksize;
mtd_rawdevice->sector_bits = blocksize_bits;
mtd_rawdevice->readonly = readonly;
/* See if device ends on page boundary */
if(size % PAGE_SIZE) {
mtd_rawdevice->partial_last_page = size >> PAGE_SHIFT;
mtd_rawdevice->last_page_sectors = (size & (PAGE_SIZE-1)) >> blocksize_bits;
}
DEBUG(2, "sector_size = %d, sector_bits = %d, partial_last_page = %d last_page_sectors = %d\n",
mtd_rawdevice->sector_size, mtd_rawdevice->sector_bits,
mtd_rawdevice->partial_last_page, mtd_rawdevice->last_page_sectors);
/* Setup the MTD structure */
/* make the name contain the block device in */
mtd_rawdevice->mtd_info.name = kmalloc(9 + strlen(device), GFP_KERNEL);
if(mtd_rawdevice->mtd_info.name == NULL)
goto init_err;
sprintf(mtd_rawdevice->mtd_info.name, "blkmtd: %s", device);
if(readonly) {
mtd_rawdevice->mtd_info.type = MTD_ROM;
mtd_rawdevice->mtd_info.flags = MTD_CAP_ROM;
mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
} else {
mtd_rawdevice->mtd_info.type = MTD_RAM;
mtd_rawdevice->mtd_info.flags = MTD_CAP_RAM;
mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
}
mtd_rawdevice->mtd_info.size = size;
mtd_rawdevice->mtd_info.erase = blkmtd_erase;
mtd_rawdevice->mtd_info.read = blkmtd_read;
mtd_rawdevice->mtd_info.write = blkmtd_write;
mtd_rawdevice->mtd_info.sync = blkmtd_sync;
mtd_rawdevice->mtd_info.point = 0;
mtd_rawdevice->mtd_info.unpoint = 0;
mtd_rawdevice->mtd_info.priv = mtd_rawdevice;
regions = calc_erase_regions(NULL, erase_size << 10, size);
DEBUG(1, "blkmtd: init: found %d erase regions\n", regions);
mtd_rawdevice->mtd_info.eraseregions = kmalloc(regions * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
if(mtd_rawdevice->mtd_info.eraseregions == NULL) {
err = -ENOMEM;
goto init_err;
}
mtd_rawdevice->mtd_info.numeraseregions = regions;
calc_erase_regions(mtd_rawdevice->mtd_info.eraseregions, erase_size << 10, size);
/* setup the page cache info */
mtd_rawdevice->as.nrpages = 0;
INIT_LIST_HEAD(&mtd_rawdevice->as.clean_pages);
INIT_LIST_HEAD(&mtd_rawdevice->as.dirty_pages);
INIT_LIST_HEAD(&mtd_rawdevice->as.locked_pages);
mtd_rawdevice->as.host = NULL;
init_MUTEX(&(mtd_rawdevice->as.i_shared_sem));
atomic_set(&(mtd_rawdevice->as.truncate_count), 0);
mtd_rawdevice->as.a_ops = &blkmtd_aops;
INIT_LIST_HEAD(&mtd_rawdevice->as.i_mmap);
INIT_LIST_HEAD(&mtd_rawdevice->as.i_mmap_shared);
mtd_rawdevice->as.gfp_mask = GFP_KERNEL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
mtd_rawdevice->mtd_info.module = THIS_MODULE;
#endif
if (add_mtd_device(&mtd_rawdevice->mtd_info)) {
err = -EIO;
goto init_err;
}
if(!mtd_rawdevice->readonly) {
/* Allocate the write queue */
write_queue = kmalloc(write_queue_sz * sizeof(mtdblkdev_write_queue_t), GFP_KERNEL);
if(!write_queue) {
err = -ENOMEM;
goto init_err;
}
/* Set up the erase page */
erase_page = alloc_pages(GFP_KERNEL, 0);
if(erase_page == NULL) {
err = -ENOMEM;
goto init_err;
}
memset(page_address(erase_page), 0xff, PAGE_SIZE);
lock_page(erase_page);
init_waitqueue_head(&thr_wq);
init_waitqueue_head(&mtbd_sync_wq);
DEBUG(3, "blkmtd: init: kernel task @ %p\n", write_queue_task);
DEBUG(2, "blkmtd: init: starting kernel task\n");
kernel_thread(write_queue_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
DEBUG(2, "blkmtd: init: started\n");
printk("blkmtd loaded: version = %s using %s erase_size = %dK %s\n",
VERSION, device, erase_size, (readonly) ? "(read-only)" : "");
}
#ifdef BLKMTD_PROC_DEBUG
/* create proc entry */
DEBUG(2, "Creating /proc/blkmtd_debug\n");
blkmtd_proc = create_proc_read_entry("blkmtd_debug", 0444,
NULL, blkmtd_proc_read, NULL);
if(blkmtd_proc == NULL) {
printk("Cant create /proc/blkmtd_debug\n");
} else {
blkmtd_proc->owner = THIS_MODULE;
}
#endif
/* Everything is ok if we got here */
return 0;
init_err:
if(mtd_rawdevice) {
if(mtd_rawdevice->mtd_info.eraseregions)
kfree(mtd_rawdevice->mtd_info.eraseregions);
if(mtd_rawdevice->mtd_info.name)
kfree(mtd_rawdevice->mtd_info.name);
if(mtd_rawdevice->binding)
blkdev_put(mtd_rawdevice->binding, BDEV_RAW);
kfree(mtd_rawdevice);
}
if(write_queue) {
kfree(write_queue);
write_queue = NULL;
}
if(erase_page)
__free_pages(erase_page, 0);
return err;
} }
module_init(init_blkmtd); module_init(init_blkmtd);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment