Commit 5a22d00c authored by Dave Jones's avatar Dave Jones Committed by Linus Torvalds

[PATCH] MTD update.

Numerous bugfixes brought forward from 2.4. I added some quick bio fixes
to mtdblock.c, which seemed to work for me whilst testing JFFS2 changes.
parent 34a7eea9
/* /*
* $Id: blkmtd.c,v 1.3 2001/10/02 15:33:20 dwmw2 Exp $ * $Id: blkmtd.c,v 1.7 2001/11/10 17:06:30 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 <spse@secret.org.uk> * Copyright (C) 2001 Simon Evans
* *
* Licence: GPL * Licence: GPL
* *
...@@ -13,7 +14,7 @@ ...@@ -13,7 +14,7 @@
* 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 but make a copy of the new page(s) and then a kernel
* thread writes pages out to the device in the background. This * thread writes pages out to the device in the background. This
* ensures tht writes are order even if a page is updated twice. * ensures that writes are order even if a page is updated twice.
* Also, since pages in the page cache are never marked as dirty, * Also, since pages in the page cache are never marked as dirty,
* we dont have to worry about writepage() being called on some * we dont have to worry about writepage() being called on some
* random page which may not be in the write order. * random page which may not be in the write order.
...@@ -30,7 +31,7 @@ ...@@ -30,7 +31,7 @@
* small memory systems and too small on large memory systems. * small memory systems and too small on large memory systems.
* *
* Page cache usage may still be a bit wrong. Check we are doing * Page cache usage may still be a bit wrong. Check we are doing
* everything proberly. * everything properly.
* *
* Somehow allow writes to dirty the page cache so we dont use too * Somehow allow writes to dirty the page cache so we dont use too
* much memory making copies of outgoing pages. Need to handle case * much memory making copies of outgoing pages. Need to handle case
...@@ -39,19 +40,14 @@ ...@@ -39,19 +40,14 @@
* *
* Reading should read multiple pages at once rather than using * Reading should read multiple pages at once rather than using
* readpage() for each one. This is easy and will be fixed asap. * readpage() for each one. This is easy and will be fixed asap.
*
* Dont run the write_thread if readonly. This is also easy and will
* be fixed asap.
*
* Even though the multiple erase regions are used if the default erase
* block size doesnt match the device properly, erases currently wont
* work on the last page if it is not a full page.
*/ */
#include <linux/config.h> #include <linux/config.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/iobuf.h> #include <linux/iobuf.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -59,19 +55,30 @@ ...@@ -59,19 +55,30 @@
#include <linux/mtd/compatmac.h> #include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h> #include <linux/mtd/mtd.h>
#ifdef CONFIG_MTD_DEBUG
#ifdef CONFIG_PROC_FS
# include <linux/proc_fs.h>
# define BLKMTD_PROC_DEBUG
static struct proc_dir_entry *blkmtd_proc;
#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
#define VERSION "1.1" #define VERSION "1.7"
extern int *blk_size[]; extern int *blk_size[];
/* Info for the block device */ /* Info for the block device */
typedef struct mtd_raw_dev_data_s { typedef struct mtd_raw_dev_data_s {
struct block_device *binding; struct block_device *binding;
int sector_size, sector_bits, total_sectors; int sector_size, sector_bits;
int partial_last_page; // 0 if device ends on page boundary, else page no of last page
int last_page_sectors; // Number of sectors in last page if partial_last_page != 0
size_t totalsize; size_t totalsize;
int readonly; int readonly;
struct address_space as; struct address_space as;
struct file *file; struct mtd_info mtd_info;
} mtd_raw_dev_data_t; } mtd_raw_dev_data_t;
/* Info for each queue item in the write queue */ /* Info for each queue item in the write queue */
...@@ -84,24 +91,28 @@ typedef struct mtdblkdev_write_queue_s { ...@@ -84,24 +91,28 @@ typedef struct mtdblkdev_write_queue_s {
} mtdblkdev_write_queue_t; } 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 struct mtd_info *mtd_info; static mtd_raw_dev_data_t *mtd_rawdevice;
/* Write queue fixed size */ /* Write queue fixed size */
#define WRITE_QUEUE_SZ 512 #define WRITE_QUEUE_SZ 512
/* Storage for the write queue */ /* Storage for the write queue */
static mtdblkdev_write_queue_t write_queue[WRITE_QUEUE_SZ]; 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_head;
static int volatile write_queue_tail; static int volatile write_queue_tail;
static int volatile write_queue_cnt; static int volatile write_queue_cnt;
static spinlock_t mbd_writeq_lock = SPIN_LOCK_UNLOCKED; static spinlock_t mbd_writeq_lock = SPIN_LOCK_UNLOCKED;
/* Tell the write thread to finish */ /* Tell the write thread to finish */
static volatile int write_task_finish = 0; static volatile int write_task_finish;
/* ipc with the write thread */ /* ipc with the write thread */
#if LINUX_VERSION_CODE > 0x020300 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
static DECLARE_MUTEX_LOCKED(thread_sem); static DECLARE_MUTEX_LOCKED(thread_sem);
static DECLARE_WAIT_QUEUE_HEAD(thr_wq); static DECLARE_WAIT_QUEUE_HEAD(thr_wq);
static DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq); static DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
...@@ -112,13 +123,14 @@ DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq); ...@@ -112,13 +123,14 @@ DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
#endif #endif
/* Module parameters passed by insmod/modprobe */ /* Module parameters passed by insmod/modprobe */
char *device; /* the block device to use */ char *device; /* the block device to use */
int erasesz; /* optional default erase size */ int erasesz; /* optional default erase size */
int ro; /* optional read only flag */ int ro; /* optional read only flag */
int bs; /* optionally force the block size (avoid using) */ int bs; /* optionally force the block size (avoid using) */
int count; /* optionally force the block count (avoid using) */ 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) #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
...@@ -134,10 +146,10 @@ MODULE_PARM(bs, "i"); ...@@ -134,10 +146,10 @@ MODULE_PARM(bs, "i");
MODULE_PARM_DESC(bs, "force the block size in bytes"); MODULE_PARM_DESC(bs, "force the block size in bytes");
MODULE_PARM(count, "i"); MODULE_PARM(count, "i");
MODULE_PARM_DESC(count, "force the block count"); MODULE_PARM_DESC(count, "force the block count");
MODULE_PARM(wqs, "i");
#endif #endif
/* Page cache stuff */ /* Page cache stuff */
/* writepage() - should never be called - catch it anyway */ /* writepage() - should never be called - catch it anyway */
...@@ -149,13 +161,13 @@ static int blkmtd_writepage(struct page *page) ...@@ -149,13 +161,13 @@ static int blkmtd_writepage(struct page *page)
/* readpage() - reads one page from the block device */ /* readpage() - reads one page from the block device */
static int blkmtd_readpage(struct file *file, struct page *page) static int blkmtd_readpage(mtd_raw_dev_data_t *rawdevice, struct page *page)
{ {
int err; int err;
int sectornr, sectors, i; int sectornr, sectors, i;
struct kiobuf *iobuf; struct kiobuf *iobuf;
mtd_raw_dev_data_t *rawdevice = (mtd_raw_dev_data_t *)file->private_data;
kdev_t dev; kdev_t dev;
unsigned long *blocks;
if(!rawdevice) { if(!rawdevice) {
printk("blkmtd: readpage: PANIC file->private_data == NULL\n"); printk("blkmtd: readpage: PANIC file->private_data == NULL\n");
...@@ -167,7 +179,7 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -167,7 +179,7 @@ static int blkmtd_readpage(struct file *file, struct page *page)
bdevname(dev), page, page->index); bdevname(dev), page, page->index);
if(Page_Uptodate(page)) { if(Page_Uptodate(page)) {
DEBUG(1, "blkmtd: readpage page %ld is already upto date\n", page->index); DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
UnlockPage(page); UnlockPage(page);
return 0; return 0;
} }
...@@ -183,8 +195,9 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -183,8 +195,9 @@ static int blkmtd_readpage(struct file *file, struct page *page)
mtdblkdev_write_queue_t *item = &write_queue[i]; mtdblkdev_write_queue_t *item = &write_queue[i];
if(page->index >= item->pagenr && page->index < item->pagenr+item->pagecnt) { if(page->index >= item->pagenr && page->index < item->pagenr+item->pagecnt) {
/* yes it is */ /* yes it is */
int index = item->pagenr - page->index; int index = page->index - item->pagenr;
DEBUG(1, "blkmtd: readpage: found page %ld in outgoing write queue\n",
DEBUG(2, "blkmtd: readpage: found page %ld in outgoing write queue\n",
page->index); page->index);
if(item->iserase) { if(item->iserase) {
memset(page_address(page), 0xff, PAGE_SIZE); memset(page_address(page), 0xff, PAGE_SIZE);
...@@ -198,7 +211,7 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -198,7 +211,7 @@ static int blkmtd_readpage(struct file *file, struct page *page)
return 0; return 0;
} }
i++; i++;
i %= WRITE_QUEUE_SZ; i %= write_queue_sz;
} }
} }
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
...@@ -207,8 +220,24 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -207,8 +220,24 @@ static int blkmtd_readpage(struct file *file, struct page *page)
DEBUG(3, "blkmtd: readpage: getting kiovec\n"); DEBUG(3, "blkmtd: readpage: getting kiovec\n");
err = alloc_kiovec(1, &iobuf); err = alloc_kiovec(1, &iobuf);
if (err) { if (err) {
printk("blkmtd: cant allocate kiobuf\n");
SetPageError(page);
return err; return err;
} }
/* Pre 2.4.4 doesnt 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: cant allocate iobuf blocks\n");
free_kiovec(1, &iobuf);
SetPageError(page);
return -ENOMEM;
}
#else
blocks = iobuf->blocks;
#endif
iobuf->offset = 0; iobuf->offset = 0;
iobuf->nr_pages = 1; iobuf->nr_pages = 1;
iobuf->length = PAGE_SIZE; iobuf->length = PAGE_SIZE;
...@@ -216,16 +245,34 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -216,16 +245,34 @@ static int blkmtd_readpage(struct file *file, struct page *page)
iobuf->maplist[0] = page; iobuf->maplist[0] = page;
sectornr = page->index << (PAGE_SHIFT - rawdevice->sector_bits); sectornr = page->index << (PAGE_SHIFT - rawdevice->sector_bits);
sectors = 1 << (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); DEBUG(3, "blkmtd: readpage: sectornr = %d sectors = %d\n", sectornr, sectors);
for(i = 0; i < sectors; i++) { for(i = 0; i < sectors; i++) {
iobuf->blocks[i] = sectornr++; 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"); DEBUG(3, "bklmtd: readpage: starting brw_kiovec\n");
err = brw_kiovec(READ, 1, &iobuf, dev, iobuf->blocks, rawdevice->sector_size); err = brw_kiovec(READ, 1, &iobuf, dev, blocks, rawdevice->sector_size);
DEBUG(3, "blkmtd: readpage: finished, err = %d\n", err); DEBUG(3, "blkmtd: readpage: finished, err = %d\n", err);
iobuf->locked = 0; iobuf->locked = 0;
free_kiovec(1, &iobuf); free_kiovec(1, &iobuf);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
kfree(blocks);
#endif
if(err != PAGE_SIZE) { if(err != PAGE_SIZE) {
printk("blkmtd: readpage: error reading page %ld\n", page->index); printk("blkmtd: readpage: error reading page %ld\n", page->index);
memset(page_address(page), 0, PAGE_SIZE); memset(page_address(page), 0, PAGE_SIZE);
...@@ -245,7 +292,7 @@ static int blkmtd_readpage(struct file *file, struct page *page) ...@@ -245,7 +292,7 @@ static int blkmtd_readpage(struct file *file, struct page *page)
static struct address_space_operations blkmtd_aops = { static struct address_space_operations blkmtd_aops = {
writepage: blkmtd_writepage, writepage: blkmtd_writepage,
readpage: blkmtd_readpage, readpage: NULL,
}; };
...@@ -255,6 +302,7 @@ static int write_queue_task(void *data) ...@@ -255,6 +302,7 @@ static int write_queue_task(void *data)
int err; int err;
struct task_struct *tsk = current; struct task_struct *tsk = current;
struct kiobuf *iobuf; struct kiobuf *iobuf;
unsigned long *blocks;
DECLARE_WAITQUEUE(wait, tsk); DECLARE_WAITQUEUE(wait, tsk);
DEBUG(1, "blkmtd: writetask: starting (pid = %d)\n", tsk->pid); DEBUG(1, "blkmtd: writetask: starting (pid = %d)\n", tsk->pid);
...@@ -266,8 +314,23 @@ static int write_queue_task(void *data) ...@@ -266,8 +314,23 @@ static int write_queue_task(void *data)
recalc_sigpending(); recalc_sigpending();
spin_unlock_irq(&tsk->sigmask_lock); spin_unlock_irq(&tsk->sigmask_lock);
if(alloc_kiovec(1, &iobuf)) if(alloc_kiovec(1, &iobuf)) {
printk("blkmtd: write_queue_task cant allocate kiobuf\n");
return 0; return 0;
}
/* Pre 2.4.4 doesnt 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 cant allocate iobuf blocks\n");
free_kiovec(1, &iobuf);
return 0;
}
#else
blocks = iobuf->blocks;
#endif
DEBUG(2, "blkmtd: writetask: entering main loop\n"); DEBUG(2, "blkmtd: writetask: entering main loop\n");
add_wait_queue(&thr_wq, &wait); add_wait_queue(&thr_wq, &wait);
...@@ -278,30 +341,39 @@ static int write_queue_task(void *data) ...@@ -278,30 +341,39 @@ static int write_queue_task(void *data)
/* If nothing in the queue, wake up anyone wanting to know when there /* If nothing in the queue, wake up anyone wanting to know when there
is space in the queue then sleep for 2*HZ */ is space in the queue then sleep for 2*HZ */
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
DEBUG(3, "blkmtd: writetask: queue empty\n"); DEBUG(4, "blkmtd: writetask: queue empty\n");
if(waitqueue_active(&mtbd_sync_wq)) if(waitqueue_active(&mtbd_sync_wq))
wake_up(&mtbd_sync_wq); wake_up(&mtbd_sync_wq);
interruptible_sleep_on_timeout(&thr_wq, 2*HZ); interruptible_sleep_on_timeout(&thr_wq, 2*HZ);
DEBUG(3, "blkmtd: writetask: woken up\n"); DEBUG(4, "blkmtd: writetask: woken up\n");
if(write_task_finish) if(write_task_finish)
break; break;
} else { } else {
/* we have stuff to write */ /* we have stuff to write */
mtdblkdev_write_queue_t *item = &write_queue[write_queue_tail]; mtdblkdev_write_queue_t *item = &write_queue[write_queue_tail];
struct page **pages = item->pages; struct page **pages = item->pages;
int pagecnt = item->pagecnt;
int pagenr = item->pagenr;
int i; 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); int max_sectors = KIO_MAX_SECTORS >> (item->rawdevice->sector_bits - 9);
kdev_t dev = to_kdev_t(item->rawdevice->binding->bd_dev); kdev_t dev = to_kdev_t(item->rawdevice->binding->bd_dev);
/* If we are writing to the last page on the device and it doesnt end
* on a page boundary, subtract the number of sectors that dont 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); DEBUG(3, "blkmtd: writetask: got %d queue items\n", write_queue_cnt);
set_current_state(TASK_RUNNING); set_current_state(TASK_RUNNING);
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtd: write_task: writing pagenr = %d pagecnt = %d", DEBUG(2, "blkmtd: writetask: writing pagenr = %d pagecnt = %d sectornr = %d sectorcnt = %d\n",
item->pagenr, item->pagecnt); item->pagenr, item->pagecnt, sectornr, sectorcnt);
iobuf->offset = 0; iobuf->offset = 0;
iobuf->locked = 1; iobuf->locked = 1;
...@@ -309,31 +381,33 @@ static int write_queue_task(void *data) ...@@ -309,31 +381,33 @@ static int write_queue_task(void *data)
/* Loop through all the pages to be written in the queue item, remembering /* Loop through all the pages to be written in the queue item, remembering
we can only write KIO_MAX_SECTORS at a time */ we can only write KIO_MAX_SECTORS at a time */
while(pagecnt) { while(sectorcnt) {
int sectornr = pagenr << (PAGE_SHIFT - item->rawdevice->sector_bits);
int sectorcnt = pagecnt << (PAGE_SHIFT - item->rawdevice->sector_bits);
int cursectors = (sectorcnt < max_sectors) ? sectorcnt : max_sectors; int cursectors = (sectorcnt < max_sectors) ? sectorcnt : max_sectors;
int cpagecnt = (cursectors << item->rawdevice->sector_bits) + PAGE_SIZE-1; int cpagecnt = (cursectors << item->rawdevice->sector_bits) + PAGE_SIZE-1;
cpagecnt >>= PAGE_SHIFT; cpagecnt >>= PAGE_SHIFT;
for(i = 0; i < cpagecnt; i++) for(i = 0; i < cpagecnt; i++) {
if(item->iserase) {
iobuf->maplist[i] = erase_page;
} else {
iobuf->maplist[i] = *(pages++); iobuf->maplist[i] = *(pages++);
}
}
for(i = 0; i < cursectors; i++) { for(i = 0; i < cursectors; i++) {
iobuf->blocks[i] = sectornr++; blocks[i] = sectornr++;
} }
iobuf->nr_pages = cpagecnt; iobuf->nr_pages = cpagecnt;
iobuf->length = cursectors << item->rawdevice->sector_bits; iobuf->length = cursectors << item->rawdevice->sector_bits;
DEBUG(3, "blkmtd: write_task: about to kiovec\n"); DEBUG(3, "blkmtd: write_task: about to kiovec\n");
err = brw_kiovec(WRITE, 1, &iobuf, dev, iobuf->blocks, item->rawdevice->sector_size); err = brw_kiovec(WRITE, 1, &iobuf, dev, blocks, item->rawdevice->sector_size);
DEBUG(3, "bklmtd: write_task: done, err = %d\n", err); DEBUG(3, "bklmtd: write_task: done, err = %d\n", err);
if(err != (cursectors << item->rawdevice->sector_bits)) { if(err != (cursectors << item->rawdevice->sector_bits)) {
/* if an error occured - set this to exit the loop */ /* if an error occured - set this to exit the loop */
pagecnt = 0; sectorcnt = 0;
} else { } else {
pagenr += cpagecnt; sectorcnt -= cursectors;
pagecnt -= cpagecnt;
} }
} }
...@@ -343,12 +417,14 @@ static int write_queue_task(void *data) ...@@ -343,12 +417,14 @@ static int write_queue_task(void *data)
spin_lock(&mbd_writeq_lock); spin_lock(&mbd_writeq_lock);
write_queue_cnt--; write_queue_cnt--;
write_queue_tail++; write_queue_tail++;
write_queue_tail %= WRITE_QUEUE_SZ; write_queue_tail %= write_queue_sz;
if(!item->iserase) {
for(i = 0 ; i < item->pagecnt; i++) { for(i = 0 ; i < item->pagecnt; i++) {
UnlockPage(item->pages[i]); UnlockPage(item->pages[i]);
__free_pages(item->pages[i], 0); __free_pages(item->pages[i], 0);
} }
kfree(item->pages); kfree(item->pages);
}
item->pages = NULL; item->pages = NULL;
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
/* Tell others there is some space in the write queue */ /* Tell others there is some space in the write queue */
...@@ -359,6 +435,11 @@ static int write_queue_task(void *data) ...@@ -359,6 +435,11 @@ static int write_queue_task(void *data)
remove_wait_queue(&thr_wq, &wait); remove_wait_queue(&thr_wq, &wait);
DEBUG(1, "blkmtd: writetask: exiting\n"); DEBUG(1, "blkmtd: writetask: exiting\n");
free_kiovec(1, &iobuf); free_kiovec(1, &iobuf);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
kfree(blocks);
#endif
/* Tell people we have exitd */ /* Tell people we have exitd */
up(&thread_sem); up(&thread_sem);
return 0; return 0;
...@@ -370,19 +451,20 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, ...@@ -370,19 +451,20 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
int pagenr, int pagecnt, int iserase) int pagenr, int pagecnt, int iserase)
{ {
struct page *outpage; struct page *outpage;
struct page **new_pages; struct page **new_pages = NULL;
mtdblkdev_write_queue_t *item; mtdblkdev_write_queue_t *item;
int i; int i;
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
DEBUG(2, "mtdblkdev: queue_page_write: adding pagenr = %d pagecnt = %d\n", pagenr, pagecnt); DEBUG(2, "blkmtd: queue_page_write: adding pagenr = %d pagecnt = %d\n", pagenr, pagecnt);
if(!pagecnt) if(!pagecnt)
return 0; return 0;
if(pages == NULL) if(pages == NULL && !iserase)
return -EINVAL; return -EINVAL;
/* create a array for the list of pages */ /* create a array for the list of pages */
if(!iserase) {
new_pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL); new_pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
if(new_pages == NULL) if(new_pages == NULL)
return -ENOMEM; return -ENOMEM;
...@@ -402,11 +484,12 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, ...@@ -402,11 +484,12 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
memcpy(page_address(outpage), page_address(pages[i]), PAGE_SIZE); memcpy(page_address(outpage), page_address(pages[i]), PAGE_SIZE);
new_pages[i] = outpage; new_pages[i] = outpage;
} }
}
/* wait until there is some space in the write queue */ /* wait until there is some space in the write queue */
test_lock: test_lock:
spin_lock(&mbd_writeq_lock); spin_lock(&mbd_writeq_lock);
if(write_queue_cnt == WRITE_QUEUE_SZ) { if(write_queue_cnt == write_queue_sz) {
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
DEBUG(3, "blkmtd: queue_page: Queue full\n"); DEBUG(3, "blkmtd: queue_page: Queue full\n");
current->state = TASK_UNINTERRUPTIBLE; current->state = TASK_UNINTERRUPTIBLE;
...@@ -415,11 +498,11 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, ...@@ -415,11 +498,11 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
schedule(); schedule();
current->state = TASK_RUNNING; current->state = TASK_RUNNING;
remove_wait_queue(&mtbd_sync_wq, &wait); remove_wait_queue(&mtbd_sync_wq, &wait);
DEBUG(3, "blkmtd: queue_page: Queue has %d items in it\n", write_queue_cnt); DEBUG(3, "blkmtd: queue_page_write: Queue has %d items in it\n", write_queue_cnt);
goto test_lock; goto test_lock;
} }
DEBUG(3, "blkmtd: queue_write_page: qhead: %d qtail: %d qcnt: %d\n", DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
write_queue_head, write_queue_tail, write_queue_cnt); write_queue_head, write_queue_tail, write_queue_cnt);
/* fix up the queue item */ /* fix up the queue item */
...@@ -431,9 +514,9 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, ...@@ -431,9 +514,9 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
item->iserase = iserase; item->iserase = iserase;
write_queue_head++; write_queue_head++;
write_queue_head %= WRITE_QUEUE_SZ; write_queue_head %= write_queue_sz;
write_queue_cnt++; write_queue_cnt++;
DEBUG(3, "blkmtd: queue_write_page: qhead: %d qtail: %d qcnt: %d\n", DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
write_queue_head, write_queue_tail, write_queue_cnt); write_queue_head, write_queue_tail, write_queue_cnt);
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtd: queue_page_write: finished\n"); DEBUG(2, "blkmtd: queue_page_write: finished\n");
...@@ -445,6 +528,8 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages, ...@@ -445,6 +528,8 @@ static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
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; mtd_raw_dev_data_t *rawdevice = mtd->priv;
struct mtd_erase_region_info *einfo = mtd->eraseregions;
int numregions = mtd->numeraseregions;
size_t from; size_t from;
u_long len; u_long len;
int err = 0; int err = 0;
...@@ -460,17 +545,23 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -460,17 +545,23 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
from = instr->addr; from = instr->addr;
len = instr->len; len = instr->len;
/* check page alignment of start and length */ /* check erase region has valid start and length */
DEBUG(2, "blkmtd: erase: dev = `%s' from = %d len = %ld\n", DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%x len = 0x%lx\n",
bdevname(rawdevice->binding->bd_dev), from, len); bdevname(rawdevice->binding->bd_dev), from, len);
if(from % PAGE_SIZE) { while(numregions) {
printk("blkmtd: erase: addr not page aligned (addr = %d)\n", from); DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
instr->state = MTD_ERASE_FAILED; einfo->offset, einfo->erasesize, einfo->numblocks);
err = -EIO; if(from >= einfo->offset && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
if(len == einfo->erasesize && ( (from - einfo->offset) % einfo->erasesize == 0))
break;
}
numregions--;
einfo++;
} }
if(len % PAGE_SIZE) { if(!numregions) {
printk("blkmtd: erase: len not a whole number of pages (len = %ld)\n", len); /* Not a valid erase block */
printk("blkmtd: erase: invalid erase request 0x%lX @ 0x%08X\n", len, from);
instr->state = MTD_ERASE_FAILED; instr->state = MTD_ERASE_FAILED;
err = -EIO; err = -EIO;
} }
...@@ -481,9 +572,14 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -481,9 +572,14 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
struct page *page, **pages; struct page *page, **pages;
int i = 0; int i = 0;
/* Handle the last page of the device not being whole */
if(len < PAGE_SIZE)
len = PAGE_SIZE;
pagenr = from >> PAGE_SHIFT; pagenr = from >> PAGE_SHIFT;
pagecnt = len >> PAGE_SHIFT; pagecnt = len >> PAGE_SHIFT;
DEBUG(3, "blkmtd: erase: pagenr = %d pagecnt = %d\n", pagenr, pagecnt); DEBUG(3, "blkmtd: erase: pagenr = %d pagecnt = %d\n", pagenr, pagecnt);
pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL); pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
if(pages == NULL) { if(pages == NULL) {
err = -ENOMEM; err = -ENOMEM;
...@@ -491,9 +587,10 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -491,9 +587,10 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
goto erase_out; goto erase_out;
} }
while(pagecnt) { while(pagecnt) {
/* get the page via the page cache */ /* get the page via the page cache */
DEBUG(3, "blkmtd: erase: doing grap_cache_page() for page %d\n", pagenr); DEBUG(3, "blkmtd: erase: doing grab_cache_page() for page %d\n", pagenr);
page = grab_cache_page(&rawdevice->as, pagenr); page = grab_cache_page(&rawdevice->as, pagenr);
if(!page) { if(!page) {
DEBUG(3, "blkmtd: erase: grab_cache_page() failed for page %d\n", pagenr); DEBUG(3, "blkmtd: erase: grab_cache_page() failed for page %d\n", pagenr);
...@@ -509,7 +606,7 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -509,7 +606,7 @@ static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
i++; i++;
} }
DEBUG(3, "blkmtd: erase: queuing page write\n"); DEBUG(3, "blkmtd: erase: queuing page write\n");
err = queue_page_write(rawdevice, pages, from >> PAGE_SHIFT, len >> PAGE_SHIFT, 1); err = queue_page_write(rawdevice, NULL, from >> PAGE_SHIFT, len >> PAGE_SHIFT, 1);
pagecnt = len >> PAGE_SHIFT; pagecnt = len >> PAGE_SHIFT;
if(!err) { if(!err) {
while(pagecnt--) { while(pagecnt--) {
...@@ -565,7 +662,7 @@ static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -565,7 +662,7 @@ static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
struct page *page; struct page *page;
int cpylen; int cpylen;
DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr); DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice->file); page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
if(IS_ERR(page)) { if(IS_ERR(page)) {
return PTR_ERR(page); return PTR_ERR(page);
} }
...@@ -681,7 +778,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -681,7 +778,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
struct page *page; struct page *page;
DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %d offset = %d\n", pagenr, len1, offset); 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->file); page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
if(IS_ERR(page)) { if(IS_ERR(page)) {
kfree(pages); kfree(pages);
...@@ -714,6 +811,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -714,6 +811,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
memcpy(page_address(page), buf, PAGE_SIZE); memcpy(page_address(page), buf, PAGE_SIZE);
pages[pagecnt++] = page; pages[pagecnt++] = page;
UnlockPage(page); UnlockPage(page);
SetPageUptodate(page);
pagenr++; pagenr++;
pagesc--; pagesc--;
buf += PAGE_SIZE; buf += PAGE_SIZE;
...@@ -726,7 +824,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -726,7 +824,7 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
/* do the third region */ /* do the third region */
struct page *page; struct page *page;
DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %d\n", pagenr, len3); 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->file); page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
if(IS_ERR(page)) { if(IS_ERR(page)) {
err = PTR_ERR(page); err = PTR_ERR(page);
goto write_err; goto write_err;
...@@ -764,6 +862,10 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -764,6 +862,10 @@ static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
static void blkmtd_sync(struct mtd_info *mtd) static void blkmtd_sync(struct mtd_info *mtd)
{ {
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
mtd_raw_dev_data_t *rawdevice = mtd->priv;
if(rawdevice->readonly)
return;
DEBUG(2, "blkmtd: sync: called\n"); DEBUG(2, "blkmtd: sync: called\n");
stuff_inq: stuff_inq:
...@@ -782,36 +884,144 @@ static void blkmtd_sync(struct mtd_info *mtd) ...@@ -782,36 +884,144 @@ static void blkmtd_sync(struct mtd_info *mtd)
} }
spin_unlock(&mbd_writeq_lock); spin_unlock(&mbd_writeq_lock);
DEBUG(2, "blkmtdL sync: finished\n"); DEBUG(2, "blkmtd: sync: finished\n");
} }
#ifdef BLKMTD_PROC_DEBUG
/* 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;
struct list_head *temp;
int i, len, pages = 0, cnt;
MOD_INC_USE_COUNT;
spin_lock(&mbd_writeq_lock);
cnt = write_queue_cnt;
i = write_queue_tail;
while(cnt) {
if(!write_queue[i].iserase)
pages += write_queue[i].pagecnt;
i++;
i %= write_queue_sz;
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 */ /* Cleanup and exit - sync the device and kill of the kernel thread */
static void __exit cleanup_blkmtd(void) static void __exit cleanup_blkmtd(void)
{ {
if (mtd_info) { #ifdef BLKMTD_PROC_DEBUG
mtd_raw_dev_data_t *rawdevice = mtd_info->priv; if(blkmtd_proc) {
// sync the device remove_proc_entry("blkmtd_debug", NULL);
if (rawdevice) { }
blkmtd_sync(mtd_info); #endif
if (mtd_rawdevice) {
/* sync the device */
if (!mtd_rawdevice->readonly) {
blkmtd_sync(&mtd_rawdevice->mtd_info);
write_task_finish = 1; write_task_finish = 1;
wake_up_interruptible(&thr_wq); wake_up_interruptible(&thr_wq);
down(&thread_sem); down(&thread_sem);
if(rawdevice->binding != NULL)
blkdev_put(rawdevice->binding, BDEV_RAW);
filp_close(rawdevice->file, NULL);
kfree(mtd_info->priv);
} }
if(mtd_info->eraseregions) del_mtd_device(&mtd_rawdevice->mtd_info);
kfree(mtd_info->eraseregions); if(mtd_rawdevice->binding != NULL)
del_mtd_device(mtd_info); blkdev_put(mtd_rawdevice->binding, BDEV_RAW);
kfree(mtd_info);
mtd_info = NULL; if(mtd_rawdevice->mtd_info.eraseregions)
kfree(mtd_rawdevice->mtd_info.eraseregions);
if(mtd_rawdevice->mtd_info.name)
kfree(mtd_rawdevice->mtd_info.name);
kfree(mtd_rawdevice);
}
if(write_queue)
kfree(write_queue);
if(erase_page) {
UnlockPage(erase_page);
__free_pages(erase_page, 0);
} }
printk("blkmtd: unloaded for %s\n", device); printk("blkmtd: unloaded for %s\n", device);
} }
extern struct module __this_module; extern struct module __this_module;
#ifndef MODULE
/* Handle kernel boot params */
static int __init param_blkmtd_device(char *str)
{
device = str;
return 1;
}
static int __init param_blkmtd_erasesz(char *str)
{
erasesz = simple_strtol(str, NULL, 0);
return 1;
}
static int __init param_blkmtd_ro(char *str)
{
ro = simple_strtol(str, NULL, 0);
return 1;
}
static int __init param_blkmtd_bs(char *str)
{
bs = simple_strtol(str, NULL, 0);
return 1;
}
static int __init param_blkmtd_count(char *str)
{
count = simple_strtol(str, NULL, 0);
return 1;
}
__setup("blkmtd_device=", param_blkmtd_device);
__setup("blkmtd_erasesz=", param_blkmtd_erasesz);
__setup("blkmtd_ro=", param_blkmtd_ro);
__setup("blkmtd_bs=", param_blkmtd_bs);
__setup("blkmtd_count=", param_blkmtd_count);
#endif
/* for a given size and initial erase size, calculate the number and size of each /* for a given size and initial erase size, calculate the number and size of each
erase region */ erase region */
static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t erase_size, size_t total_size) static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t erase_size, size_t total_size)
...@@ -840,12 +1050,16 @@ static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t ...@@ -840,12 +1050,16 @@ static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t
} }
extern kdev_t name_to_kdev_t(char *line) __init;
/* Startup */ /* Startup */
static int __init init_blkmtd(void) static int __init init_blkmtd(void)
{ {
#ifdef MODULE
struct file *file = NULL; struct file *file = NULL;
struct inode *inode; struct inode *inode;
mtd_raw_dev_data_t *rawdevice = NULL; #endif
int maj, min; int maj, min;
int i, blocksize, blocksize_bits; int i, blocksize, blocksize_bits;
loff_t size = 0; loff_t size = 0;
...@@ -854,15 +1068,12 @@ static int __init init_blkmtd(void) ...@@ -854,15 +1068,12 @@ static int __init init_blkmtd(void)
kdev_t rdev; kdev_t rdev;
int err; int err;
int mode; int mode;
int totalsize = 0, total_sectors = 0;
int regions; int regions;
mtd_info = NULL; /* Check args */
// Check args
if(device == 0) { if(device == 0) {
printk("blkmtd: error, missing `device' name\n"); printk("blkmtd: error, missing `device' name\n");
return 1; return -EINVAL;
} }
if(ro) if(ro)
...@@ -871,12 +1082,25 @@ static int __init init_blkmtd(void) ...@@ -871,12 +1082,25 @@ static int __init init_blkmtd(void)
if(erasesz) if(erasesz)
erase_size = erasesz; erase_size = erasesz;
DEBUG(1, "blkmtd: got device = `%s' erase size = %dK readonly = %s\n", device, erase_size, readonly ? "yes" : "no"); if(wqs) {
// Get a handle on the device 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; mode = (readonly) ? O_RDONLY : O_RDWR;
#ifdef MODULE
file = filp_open(device, mode, 0); file = filp_open(device, mode, 0);
if(IS_ERR(file)) { if(IS_ERR(file)) {
DEBUG(2, "blkmtd: open_namei returned %ld\n", PTR_ERR(file)); printk("blkmtd: error, cant open device %s\n", device);
DEBUG(2, "blkmtd: filp_open returned %ld\n", PTR_ERR(file));
return 1; return 1;
} }
...@@ -889,11 +1113,19 @@ static int __init init_blkmtd(void) ...@@ -889,11 +1113,19 @@ static int __init init_blkmtd(void)
return 1; return 1;
} }
rdev = inode->i_rdev; rdev = inode->i_rdev;
//filp_close(file, NULL); filp_close(file, NULL);
DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n", #else
MAJOR(rdev), MINOR(rdev)); rdev = name_to_kdev_t(device);
maj = MAJOR(rdev); #endif
min = MINOR(rdev);
maj = major(rdev);
min = minor(rdev);
DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n", maj, min);
if(kdev_none(rdev)) {
printk("blkmtd: bad block device: `%s'\n", device);
return 1;
}
if(maj == MTD_BLOCK_MAJOR) { if(maj == MTD_BLOCK_MAJOR) {
printk("blkmtd: attempting to use an MTD device as a block device\n"); printk("blkmtd: attempting to use an MTD device as a block device\n");
...@@ -918,115 +1150,158 @@ static int __init init_blkmtd(void) ...@@ -918,115 +1150,158 @@ static int __init init_blkmtd(void)
size = ((loff_t) blk_size[maj][min] << BLOCK_SIZE_BITS) >> blocksize_bits; size = ((loff_t) blk_size[maj][min] << BLOCK_SIZE_BITS) >> blocksize_bits;
} }
} }
total_sectors = size;
size *= blocksize; size *= blocksize;
totalsize = size;
DEBUG(1, "blkmtd: size = %ld\n", (long int)size); DEBUG(1, "blkmtd: size = %ld\n", (long int)size);
if(size == 0) { if(size == 0) {
printk("blkmtd: cant determine size\n"); printk("blkmtd: cant determine size\n");
return 1; return 1;
} }
rawdevice = (mtd_raw_dev_data_t *)kmalloc(sizeof(mtd_raw_dev_data_t), GFP_KERNEL);
if(rawdevice == NULL) { mtd_rawdevice = (mtd_raw_dev_data_t *)kmalloc(sizeof(mtd_raw_dev_data_t), GFP_KERNEL);
if(mtd_rawdevice == NULL) {
err = -ENOMEM; err = -ENOMEM;
goto init_err; goto init_err;
} }
memset(rawdevice, 0, sizeof(mtd_raw_dev_data_t)); memset(mtd_rawdevice, 0, sizeof(mtd_raw_dev_data_t));
// get the block device /* get the block device */
rawdevice->binding = bdget(kdev_t_to_nr(MKDEV(maj, min))); mtd_rawdevice->binding = bdget(kdev_t_to_nr(mk_kdev(maj, min)));
err = blkdev_get(rawdevice->binding, mode, 0, BDEV_RAW); err = blkdev_get(mtd_rawdevice->binding, mode, 0, BDEV_RAW);
if (err) { if (err) {
goto init_err; goto init_err;
} }
rawdevice->totalsize = totalsize; mtd_rawdevice->totalsize = size;
rawdevice->total_sectors = total_sectors; mtd_rawdevice->sector_size = blocksize;
rawdevice->sector_size = blocksize; mtd_rawdevice->sector_bits = blocksize_bits;
rawdevice->sector_bits = blocksize_bits; mtd_rawdevice->readonly = readonly;
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\n", rawdevice->sector_size, rawdevice->sector_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);
mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL); /* Setup the MTD structure */
if (mtd_info == NULL) { /* make the name contain the block device in */
err = -ENOMEM; mtd_rawdevice->mtd_info.name = kmalloc(9 + strlen(device), GFP_KERNEL);
if(mtd_rawdevice->mtd_info.name == NULL)
goto init_err; goto init_err;
}
memset(mtd_info, 0, sizeof(*mtd_info));
// Setup the MTD structure sprintf(mtd_rawdevice->mtd_info.name, "blkmtd: %s", device);
mtd_info->name = "blkmtd block device";
if(readonly) { if(readonly) {
mtd_info->type = MTD_ROM; mtd_rawdevice->mtd_info.type = MTD_ROM;
mtd_info->flags = MTD_CAP_ROM; mtd_rawdevice->mtd_info.flags = MTD_CAP_ROM;
mtd_info->erasesize = erase_size << 10; mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
} else { } else {
mtd_info->type = MTD_RAM; mtd_rawdevice->mtd_info.type = MTD_RAM;
mtd_info->flags = MTD_CAP_RAM; mtd_rawdevice->mtd_info.flags = MTD_CAP_RAM;
mtd_info->erasesize = erase_size << 10; mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
} }
mtd_info->size = size; mtd_rawdevice->mtd_info.size = size;
mtd_info->erase = blkmtd_erase; mtd_rawdevice->mtd_info.erase = blkmtd_erase;
mtd_info->read = blkmtd_read; mtd_rawdevice->mtd_info.read = blkmtd_read;
mtd_info->write = blkmtd_write; mtd_rawdevice->mtd_info.write = blkmtd_write;
mtd_info->sync = blkmtd_sync; mtd_rawdevice->mtd_info.sync = blkmtd_sync;
mtd_info->point = 0; mtd_rawdevice->mtd_info.point = 0;
mtd_info->unpoint = 0; mtd_rawdevice->mtd_info.unpoint = 0;
mtd_info->priv = rawdevice; mtd_rawdevice->mtd_info.priv = mtd_rawdevice;
regions = calc_erase_regions(NULL, erase_size << 10, size); regions = calc_erase_regions(NULL, erase_size << 10, size);
DEBUG(1, "blkmtd: init: found %d erase regions\n", regions); DEBUG(1, "blkmtd: init: found %d erase regions\n", regions);
mtd_info->eraseregions = kmalloc(regions * sizeof(struct mtd_erase_region_info), GFP_KERNEL); mtd_rawdevice->mtd_info.eraseregions = kmalloc(regions * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
if(mtd_info->eraseregions == NULL) { if(mtd_rawdevice->mtd_info.eraseregions == NULL) {
err = -ENOMEM;
goto init_err;
} }
mtd_info->numeraseregions = regions; mtd_rawdevice->mtd_info.numeraseregions = regions;
calc_erase_regions(mtd_info->eraseregions, erase_size << 10, size); calc_erase_regions(mtd_rawdevice->mtd_info.eraseregions, erase_size << 10, size);
/* setup the page cache info */ /* setup the page cache info */
INIT_LIST_HEAD(&rawdevice->as.clean_pages);
INIT_LIST_HEAD(&rawdevice->as.dirty_pages); mtd_rawdevice->as.nrpages = 0;
INIT_LIST_HEAD(&rawdevice->as.locked_pages); INIT_LIST_HEAD(&mtd_rawdevice->as.clean_pages);
rawdevice->as.nrpages = 0; INIT_LIST_HEAD(&mtd_rawdevice->as.dirty_pages);
rawdevice->as.a_ops = &blkmtd_aops; INIT_LIST_HEAD(&mtd_rawdevice->as.locked_pages);
rawdevice->as.host = inode; mtd_rawdevice->as.host = NULL;
INIT_LIST_HEAD(&rawdevice->as.i_mmap); spin_lock_init(&(mtd_rawdevice->as.i_shared_lock));
INIT_LIST_HEAD(&rawdevice->as.i_mmap_shared);
spin_lock_init(&rawdevice->as.i_shared_lock); mtd_rawdevice->as.a_ops = &blkmtd_aops;
rawdevice->as.gfp_mask = GFP_KERNEL; INIT_LIST_HEAD(&mtd_rawdevice->as.i_mmap);
rawdevice->file = file; INIT_LIST_HEAD(&mtd_rawdevice->as.i_mmap_shared);
mtd_rawdevice->as.gfp_mask = GFP_KERNEL;
file->private_data = rawdevice;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
mtd_info->module = THIS_MODULE; mtd_rawdevice->mtd_info.module = THIS_MODULE;
#endif #endif
if (add_mtd_device(mtd_info)) { if (add_mtd_device(&mtd_rawdevice->mtd_info)) {
err = -EIO; err = -EIO;
goto init_err; 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(&thr_wq);
init_waitqueue_head(&mtbd_sync_wq); init_waitqueue_head(&mtbd_sync_wq);
DEBUG(3, "blkmtd: init: kernel task @ %p\n", write_queue_task); DEBUG(3, "blkmtd: init: kernel task @ %p\n", write_queue_task);
DEBUG(2, "blkmtd: init: starting kernel task\n"); DEBUG(2, "blkmtd: init: starting kernel task\n");
kernel_thread(write_queue_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); kernel_thread(write_queue_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
DEBUG(2, "blkmtd: init: started\n"); 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)" : ""); 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; return 0;
init_err: init_err:
if(!rawdevice) {
if(rawdevice->binding)
blkdev_put(rawdevice->binding, BDEV_RAW);
kfree(rawdevice); if(mtd_rawdevice) {
rawdevice = NULL; 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(mtd_info) {
if(mtd_info->eraseregions) if(write_queue) {
kfree(mtd_info->eraseregions); kfree(write_queue);
kfree(mtd_info); write_queue = NULL;
mtd_info = NULL;
} }
if(erase_page)
__free_pages(erase_page, 0);
return err; return err;
} }
......
...@@ -16,11 +16,8 @@ ...@@ -16,11 +16,8 @@
#define MAJOR_NR MTD_BLOCK_MAJOR #define MAJOR_NR MTD_BLOCK_MAJOR
#define DEVICE_NAME "mtdblock" #define DEVICE_NAME "mtdblock"
#define DEVICE_REQUEST mtdblock_request
#define DEVICE_NR(device) (device) #define DEVICE_NR(device) (device)
#define DEVICE_ON(device) #define LOCAL_END_REQUEST
#define DEVICE_OFF(device)
#define DEVICE_NO_RANDOM
#include <linux/blk.h> #include <linux/blk.h>
/* for old kernels... */ /* for old kernels... */
#ifndef QUEUE_EMPTY #ifndef QUEUE_EMPTY
...@@ -283,7 +280,7 @@ static int mtdblock_open(struct inode *inode, struct file *file) ...@@ -283,7 +280,7 @@ static int mtdblock_open(struct inode *inode, struct file *file)
if (!inode) if (!inode)
return -EINVAL; return -EINVAL;
dev = MINOR(inode->i_rdev); dev = minor(inode->i_rdev);
if (dev >= MAX_MTD_DEVICES) if (dev >= MAX_MTD_DEVICES)
return -EINVAL; return -EINVAL;
...@@ -373,7 +370,7 @@ static release_t mtdblock_release(struct inode *inode, struct file *file) ...@@ -373,7 +370,7 @@ static release_t mtdblock_release(struct inode *inode, struct file *file)
invalidate_device(inode->i_rdev, 1); invalidate_device(inode->i_rdev, 1);
dev = MINOR(inode->i_rdev); dev = minor(inode->i_rdev);
mtdblk = mtdblks[dev]; mtdblk = mtdblks[dev];
down(&mtdblk->cache_sem); down(&mtdblk->cache_sem);
...@@ -417,18 +414,20 @@ static void handle_mtdblock_request(void) ...@@ -417,18 +414,20 @@ static void handle_mtdblock_request(void)
INIT_REQUEST; INIT_REQUEST;
req = CURRENT; req = CURRENT;
spin_unlock_irq(&QUEUE->queue_lock); spin_unlock_irq(&QUEUE->queue_lock);
mtdblk = mtdblks[MINOR(req->rq_dev)]; mtdblk = mtdblks[minor(req->rq_dev)];
res = 0; res = 0;
if (MINOR(req->rq_dev) >= MAX_MTD_DEVICES) if (minor(req->rq_dev) >= MAX_MTD_DEVICES)
panic(__FUNCTION__": minor out of bound"); panic(__FUNCTION__": minor out of bound");
if (req->flags & REQ_CMD)
goto end_req;
if ((req->sector + req->current_nr_sectors) > (mtdblk->mtd->size >> 9)) if ((req->sector + req->current_nr_sectors) > (mtdblk->mtd->size >> 9))
goto end_req; goto end_req;
// Handle the request // Handle the request
switch (req->cmd) switch (rq_data_dir(CURRENT)) {
{
int err; int err;
case READ: case READ:
...@@ -459,7 +458,11 @@ static void handle_mtdblock_request(void) ...@@ -459,7 +458,11 @@ static void handle_mtdblock_request(void)
end_req: end_req:
spin_lock_irq(&QUEUE->queue_lock); spin_lock_irq(&QUEUE->queue_lock);
end_request(res); if (!end_that_request_first(req, res, req->hard_cur_sectors)) {
blkdev_dequeue_request(req);
end_that_request_last(req);
}
} }
} }
...@@ -519,7 +522,7 @@ static int mtdblock_ioctl(struct inode * inode, struct file * file, ...@@ -519,7 +522,7 @@ static int mtdblock_ioctl(struct inode * inode, struct file * file,
{ {
struct mtdblk_dev *mtdblk; struct mtdblk_dev *mtdblk;
mtdblk = mtdblks[MINOR(inode->i_rdev)]; mtdblk = mtdblks[minor(inode->i_rdev)];
#ifdef PARANOIA #ifdef PARANOIA
if (!mtdblk) if (!mtdblk)
...@@ -597,6 +600,8 @@ static void mtd_notify_remove(struct mtd_info* mtd) ...@@ -597,6 +600,8 @@ static void mtd_notify_remove(struct mtd_info* mtd)
} }
#endif #endif
static spinlock_t mtddev_lock = SPIN_LOCK_UNLOCKED;
int __init init_mtdblock(void) int __init init_mtdblock(void)
{ {
int i; int i;
...@@ -630,7 +635,7 @@ int __init init_mtdblock(void) ...@@ -630,7 +635,7 @@ int __init init_mtdblock(void)
blksize_size[MAJOR_NR] = mtd_blksizes; blksize_size[MAJOR_NR] = mtd_blksizes;
blk_size[MAJOR_NR] = mtd_sizes; blk_size[MAJOR_NR] = mtd_sizes;
blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), &mtdblock_request); blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), &mtdblock_request, &mtddev_lock);
kernel_thread (mtdblock_thread, NULL, CLONE_FS|CLONE_FILES|CLONE_SIGHAND); kernel_thread (mtdblock_thread, NULL, CLONE_FS|CLONE_FILES|CLONE_SIGHAND);
return 0; return 0;
} }
......
...@@ -16,13 +16,10 @@ ...@@ -16,13 +16,10 @@
#include <linux/mtd/mtd.h> #include <linux/mtd/mtd.h>
#include <linux/mtd/compatmac.h> #include <linux/mtd/compatmac.h>
#define LOCAL_END_REQUEST
#define MAJOR_NR MTD_BLOCK_MAJOR #define MAJOR_NR MTD_BLOCK_MAJOR
#define DEVICE_NAME "mtdblock" #define DEVICE_NAME "mtdblock"
#define DEVICE_REQUEST mtdblock_request
#define DEVICE_NR(device) (device) #define DEVICE_NR(device) (device)
#define DEVICE_ON(device)
#define DEVICE_OFF(device)
#define DEVICE_NO_RANDOM
#include <linux/blk.h> #include <linux/blk.h>
#if LINUX_VERSION_CODE < 0x20300 #if LINUX_VERSION_CODE < 0x20300
...@@ -52,7 +49,7 @@ static int mtdblock_open(struct inode *inode, struct file *file) ...@@ -52,7 +49,7 @@ static int mtdblock_open(struct inode *inode, struct file *file)
if (inode == 0) if (inode == 0)
return -EINVAL; return -EINVAL;
dev = MINOR(inode->i_rdev); dev = minor(inode->i_rdev);
mtd = get_mtd_device(NULL, dev); mtd = get_mtd_device(NULL, dev);
if (!mtd) if (!mtd)
...@@ -81,7 +78,7 @@ static release_t mtdblock_release(struct inode *inode, struct file *file) ...@@ -81,7 +78,7 @@ static release_t mtdblock_release(struct inode *inode, struct file *file)
invalidate_device(inode->i_rdev, 1); invalidate_device(inode->i_rdev, 1);
dev = MINOR(inode->i_rdev); dev = minor(inode->i_rdev);
mtd = __get_mtd_device(NULL, dev); mtd = __get_mtd_device(NULL, dev);
if (!mtd) { if (!mtd) {
...@@ -99,6 +96,13 @@ static release_t mtdblock_release(struct inode *inode, struct file *file) ...@@ -99,6 +96,13 @@ static release_t mtdblock_release(struct inode *inode, struct file *file)
release_return(0); release_return(0);
} }
static inline void mtdblock_end_request(struct request *req, int uptodate)
{
if (end_that_request_first(req, uptodate, req->hard_cur_sectors))
return;
blkdev_dequeue_request(req);
end_that_request_last(req);
}
static void mtdblock_request(RQFUNC_ARG) static void mtdblock_request(RQFUNC_ARG)
{ {
...@@ -113,19 +117,19 @@ static void mtdblock_request(RQFUNC_ARG) ...@@ -113,19 +117,19 @@ static void mtdblock_request(RQFUNC_ARG)
INIT_REQUEST; INIT_REQUEST;
current_request = CURRENT; current_request = CURRENT;
if (MINOR(current_request->rq_dev) >= MAX_MTD_DEVICES) if (minor(current_request->rq_dev) >= MAX_MTD_DEVICES)
{ {
printk("mtd: Unsupported device!\n"); printk("mtd: Unsupported device!\n");
end_request(0); mtdblock_end_request(current_request, 0);
continue; continue;
} }
// Grab our MTD structure // Grab our MTD structure
mtd = __get_mtd_device(NULL, MINOR(current_request->rq_dev)); mtd = __get_mtd_device(NULL, minor(current_request->rq_dev));
if (!mtd) { if (!mtd) {
printk("MTD device %d doesn't appear to exist any more\n", CURRENT_DEV); printk("MTD device %d doesn't appear to exist any more\n", CURRENT_DEV);
end_request(0); mtdblock_end_request(current_request, 0);
} }
if (current_request->sector << 9 > mtd->size || if (current_request->sector << 9 > mtd->size ||
...@@ -133,7 +137,7 @@ static void mtdblock_request(RQFUNC_ARG) ...@@ -133,7 +137,7 @@ static void mtdblock_request(RQFUNC_ARG)
{ {
printk("mtd: Attempt to read past end of device!\n"); printk("mtd: Attempt to read past end of device!\n");
printk("size: %x, sector: %lx, nr_sectors %lx\n", mtd->size, current_request->sector, current_request->nr_sectors); printk("size: %x, sector: %lx, nr_sectors %lx\n", mtd->size, current_request->sector, current_request->nr_sectors);
end_request(0); mtdblock_end_request(current_request, 0);
continue; continue;
} }
...@@ -192,7 +196,7 @@ static void mtdblock_request(RQFUNC_ARG) ...@@ -192,7 +196,7 @@ static void mtdblock_request(RQFUNC_ARG)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
spin_lock_irq(&io_request_lock); spin_lock_irq(&io_request_lock);
#endif #endif
end_request(res); mtdblock_end_request(current_request, res);
} }
} }
...@@ -203,7 +207,7 @@ static int mtdblock_ioctl(struct inode * inode, struct file * file, ...@@ -203,7 +207,7 @@ static int mtdblock_ioctl(struct inode * inode, struct file * file,
{ {
struct mtd_info *mtd; struct mtd_info *mtd;
mtd = __get_mtd_device(NULL, MINOR(inode->i_rdev)); mtd = __get_mtd_device(NULL, minor(inode->i_rdev));
if (!mtd) return -EINVAL; if (!mtd) return -EINVAL;
......
...@@ -64,7 +64,7 @@ static loff_t mtd_lseek (struct file *file, loff_t offset, int orig) ...@@ -64,7 +64,7 @@ static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
static int mtd_open(struct inode *inode, struct file *file) static int mtd_open(struct inode *inode, struct file *file)
{ {
int minor = MINOR(inode->i_rdev); int minor = minor(inode->i_rdev);
int devnum = minor >> 1; int devnum = minor >> 1;
struct mtd_info *mtd; struct mtd_info *mtd;
......
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