2005-04-17 05:20:36 +07:00
|
|
|
/*
|
2005-11-29 21:49:38 +07:00
|
|
|
* $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
|
|
|
* block2mtd.c - create an mtd from a block device
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
|
2006-05-14 07:51:54 +07:00
|
|
|
* Copyright (C) 2004-2006 Jörn Engel <joern@wh.fh-wedel.de>
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
|
|
|
* Licence: GPL
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/buffer_head.h>
|
2006-03-31 17:29:41 +07:00
|
|
|
#include <linux/mutex.h>
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
#include <linux/mount.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2005-11-29 21:49:38 +07:00
|
|
|
#define VERSION "$Revision: 1.30 $"
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
|
|
|
|
#define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
|
|
|
|
#define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
|
|
|
|
|
|
|
|
|
|
|
|
/* Info for the block device */
|
|
|
|
struct block2mtd_dev {
|
|
|
|
struct list_head list;
|
|
|
|
struct block_device *blkdev;
|
|
|
|
struct mtd_info mtd;
|
2006-03-31 17:29:41 +07:00
|
|
|
struct mutex write_mutex;
|
2005-04-17 05:20:36 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Static info about the MTD, used in cleanup_module */
|
|
|
|
static LIST_HEAD(blkmtd_device_list);
|
|
|
|
|
|
|
|
|
|
|
|
#define PAGE_READAHEAD 64
|
2005-11-29 21:49:38 +07:00
|
|
|
static void cache_readahead(struct address_space *mapping, int index)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
filler_t *filler = (filler_t*)mapping->a_ops->readpage;
|
|
|
|
int i, pagei;
|
|
|
|
unsigned ret = 0;
|
|
|
|
unsigned long end_index;
|
|
|
|
struct page *page;
|
|
|
|
LIST_HEAD(page_pool);
|
|
|
|
struct inode *inode = mapping->host;
|
|
|
|
loff_t isize = i_size_read(inode);
|
|
|
|
|
|
|
|
if (!isize) {
|
|
|
|
INFO("iSize=0 in cache_readahead\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
|
|
|
|
|
|
|
|
read_lock_irq(&mapping->tree_lock);
|
|
|
|
for (i = 0; i < PAGE_READAHEAD; i++) {
|
|
|
|
pagei = index + i;
|
|
|
|
if (pagei > end_index) {
|
|
|
|
INFO("Overrun end of disk in cache readahead\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
page = radix_tree_lookup(&mapping->page_tree, pagei);
|
|
|
|
if (page && (!i))
|
|
|
|
break;
|
|
|
|
if (page)
|
|
|
|
continue;
|
|
|
|
read_unlock_irq(&mapping->tree_lock);
|
|
|
|
page = page_cache_alloc_cold(mapping);
|
|
|
|
read_lock_irq(&mapping->tree_lock);
|
|
|
|
if (!page)
|
|
|
|
break;
|
|
|
|
page->index = pagei;
|
|
|
|
list_add(&page->lru, &page_pool);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
read_unlock_irq(&mapping->tree_lock);
|
|
|
|
if (ret)
|
|
|
|
read_cache_pages(mapping, &page_pool, filler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct page* page_readahead(struct address_space *mapping, int index)
|
|
|
|
{
|
|
|
|
filler_t *filler = (filler_t*)mapping->a_ops->readpage;
|
|
|
|
cache_readahead(mapping, index);
|
|
|
|
return read_cache_page(mapping, index, filler, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* erase a specified part of the device */
|
|
|
|
static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
|
|
|
|
struct page *page;
|
|
|
|
int index = to >> PAGE_SHIFT; // page index
|
|
|
|
int pages = len >> PAGE_SHIFT;
|
|
|
|
u_long *p;
|
|
|
|
u_long *max;
|
|
|
|
|
|
|
|
while (pages) {
|
|
|
|
page = page_readahead(mapping, index);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (IS_ERR(page))
|
|
|
|
return PTR_ERR(page);
|
|
|
|
|
2007-02-21 02:20:58 +07:00
|
|
|
max = page_address(page) + PAGE_SIZE;
|
|
|
|
for (p=page_address(page); p<max; p++)
|
2005-04-17 05:20:36 +07:00
|
|
|
if (*p != -1UL) {
|
|
|
|
lock_page(page);
|
|
|
|
memset(page_address(page), 0xff, PAGE_SIZE);
|
|
|
|
set_page_dirty(page);
|
|
|
|
unlock_page(page);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
page_cache_release(page);
|
|
|
|
pages--;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
|
|
|
|
{
|
|
|
|
struct block2mtd_dev *dev = mtd->priv;
|
|
|
|
size_t from = instr->addr;
|
|
|
|
size_t len = instr->len;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
instr->state = MTD_ERASING;
|
2006-03-31 17:29:41 +07:00
|
|
|
mutex_lock(&dev->write_mutex);
|
2005-04-17 05:20:36 +07:00
|
|
|
err = _block2mtd_erase(dev, from, len);
|
2006-03-31 17:29:41 +07:00
|
|
|
mutex_unlock(&dev->write_mutex);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (err) {
|
|
|
|
ERROR("erase failed err = %d", err);
|
|
|
|
instr->state = MTD_ERASE_FAILED;
|
|
|
|
} else
|
|
|
|
instr->state = MTD_ERASE_DONE;
|
|
|
|
|
|
|
|
instr->state = MTD_ERASE_DONE;
|
|
|
|
mtd_erase_callback(instr);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t *retlen, u_char *buf)
|
|
|
|
{
|
|
|
|
struct block2mtd_dev *dev = mtd->priv;
|
|
|
|
struct page *page;
|
|
|
|
int index = from >> PAGE_SHIFT;
|
2005-03-08 03:29:09 +07:00
|
|
|
int offset = from & (PAGE_SIZE-1);
|
2005-04-17 05:20:36 +07:00
|
|
|
int cpylen;
|
|
|
|
|
|
|
|
if (from > mtd->size)
|
|
|
|
return -EINVAL;
|
|
|
|
if (from + len > mtd->size)
|
|
|
|
len = mtd->size - from;
|
|
|
|
|
|
|
|
if (retlen)
|
|
|
|
*retlen = 0;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
if ((offset + len) > PAGE_SIZE)
|
|
|
|
cpylen = PAGE_SIZE - offset; // multiple pages
|
|
|
|
else
|
|
|
|
cpylen = len; // this page
|
|
|
|
len = len - cpylen;
|
|
|
|
|
|
|
|
// Get page
|
|
|
|
page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (IS_ERR(page))
|
|
|
|
return PTR_ERR(page);
|
|
|
|
|
|
|
|
memcpy(buf, page_address(page) + offset, cpylen);
|
|
|
|
page_cache_release(page);
|
|
|
|
|
|
|
|
if (retlen)
|
|
|
|
*retlen += cpylen;
|
|
|
|
buf += cpylen;
|
|
|
|
offset = 0;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* write data to the underlying device */
|
|
|
|
static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
|
|
|
|
loff_t to, size_t len, size_t *retlen)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
|
|
|
|
int index = to >> PAGE_SHIFT; // page index
|
|
|
|
int offset = to & ~PAGE_MASK; // page offset
|
|
|
|
int cpylen;
|
|
|
|
|
|
|
|
if (retlen)
|
|
|
|
*retlen = 0;
|
|
|
|
while (len) {
|
2005-11-07 18:15:40 +07:00
|
|
|
if ((offset+len) > PAGE_SIZE)
|
2005-04-17 05:20:36 +07:00
|
|
|
cpylen = PAGE_SIZE - offset; // multiple pages
|
|
|
|
else
|
|
|
|
cpylen = len; // this page
|
|
|
|
len = len - cpylen;
|
|
|
|
|
|
|
|
// Get page
|
|
|
|
page = page_readahead(mapping, index);
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (IS_ERR(page))
|
|
|
|
return PTR_ERR(page);
|
|
|
|
|
|
|
|
if (memcmp(page_address(page)+offset, buf, cpylen)) {
|
|
|
|
lock_page(page);
|
|
|
|
memcpy(page_address(page) + offset, buf, cpylen);
|
|
|
|
set_page_dirty(page);
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
page_cache_release(page);
|
|
|
|
|
|
|
|
if (retlen)
|
|
|
|
*retlen += cpylen;
|
|
|
|
|
|
|
|
buf += cpylen;
|
|
|
|
offset = 0;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|
|
|
size_t *retlen, const u_char *buf)
|
|
|
|
{
|
|
|
|
struct block2mtd_dev *dev = mtd->priv;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
if (to >= mtd->size)
|
|
|
|
return -ENOSPC;
|
|
|
|
if (to + len > mtd->size)
|
|
|
|
len = mtd->size - to;
|
|
|
|
|
2006-03-31 17:29:41 +07:00
|
|
|
mutex_lock(&dev->write_mutex);
|
2005-04-17 05:20:36 +07:00
|
|
|
err = _block2mtd_write(dev, buf, to, len, retlen);
|
2006-03-31 17:29:41 +07:00
|
|
|
mutex_unlock(&dev->write_mutex);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (err > 0)
|
|
|
|
err = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* sync the device - wait until the write queue is empty */
|
|
|
|
static void block2mtd_sync(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct block2mtd_dev *dev = mtd->priv;
|
|
|
|
sync_blockdev(dev->blkdev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void block2mtd_free_device(struct block2mtd_dev *dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
kfree(dev->mtd.name);
|
|
|
|
|
|
|
|
if (dev->blkdev) {
|
2007-02-10 16:45:39 +07:00
|
|
|
invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
|
|
|
|
0, -1);
|
2005-04-17 05:20:36 +07:00
|
|
|
close_bdev_excl(dev->blkdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: ensure that mtd->size % erase_size == 0 */
|
|
|
|
static struct block2mtd_dev *add_device(char *devname, int erase_size)
|
|
|
|
{
|
|
|
|
struct block_device *bdev;
|
|
|
|
struct block2mtd_dev *dev;
|
|
|
|
|
|
|
|
if (!devname)
|
|
|
|
return NULL;
|
|
|
|
|
2006-11-16 02:10:29 +07:00
|
|
|
dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (!dev)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Get a handle on the device */
|
|
|
|
bdev = open_bdev_excl(devname, O_RDWR, NULL);
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
#ifndef MODULE
|
|
|
|
if (IS_ERR(bdev)) {
|
|
|
|
|
|
|
|
/* We might not have rootfs mounted at this point. Try
|
|
|
|
to resolve the device name by other means. */
|
|
|
|
|
2007-02-21 02:21:41 +07:00
|
|
|
dev_t devt = name_to_dev_t(devname);
|
|
|
|
if (devt) {
|
|
|
|
bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
if (IS_ERR(bdev)) {
|
|
|
|
ERROR("error: cannot open device %s", devname);
|
|
|
|
goto devinit_err;
|
|
|
|
}
|
|
|
|
dev->blkdev = bdev;
|
|
|
|
|
|
|
|
if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
|
|
|
|
ERROR("attempting to use an MTD device as a block device");
|
|
|
|
goto devinit_err;
|
|
|
|
}
|
|
|
|
|
2006-03-31 17:29:41 +07:00
|
|
|
mutex_init(&dev->write_mutex);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
/* Setup the MTD structure */
|
|
|
|
/* make the name contain the block device in */
|
|
|
|
dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!dev->mtd.name)
|
|
|
|
goto devinit_err;
|
|
|
|
|
|
|
|
sprintf(dev->mtd.name, "block2mtd: %s", devname);
|
|
|
|
|
|
|
|
dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
|
|
|
|
dev->mtd.erasesize = erase_size;
|
2006-06-22 21:15:48 +07:00
|
|
|
dev->mtd.writesize = 1;
|
2006-06-15 03:39:48 +07:00
|
|
|
dev->mtd.type = MTD_RAM;
|
2005-04-17 05:20:36 +07:00
|
|
|
dev->mtd.flags = MTD_CAP_RAM;
|
|
|
|
dev->mtd.erase = block2mtd_erase;
|
|
|
|
dev->mtd.write = block2mtd_write;
|
|
|
|
dev->mtd.writev = default_mtd_writev;
|
|
|
|
dev->mtd.sync = block2mtd_sync;
|
|
|
|
dev->mtd.read = block2mtd_read;
|
|
|
|
dev->mtd.priv = dev;
|
|
|
|
dev->mtd.owner = THIS_MODULE;
|
|
|
|
|
|
|
|
if (add_mtd_device(&dev->mtd)) {
|
|
|
|
/* Device didnt get added, so free the entry */
|
|
|
|
goto devinit_err;
|
|
|
|
}
|
|
|
|
list_add(&dev->list, &blkmtd_device_list);
|
|
|
|
INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
|
|
|
|
dev->mtd.name + strlen("blkmtd: "),
|
|
|
|
dev->mtd.erasesize >> 10, dev->mtd.erasesize);
|
|
|
|
return dev;
|
|
|
|
|
|
|
|
devinit_err:
|
|
|
|
block2mtd_free_device(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
mtd: improve parameter parsing for block2mtd
Expand the parameter parsing for block2mtd. It now accepts:
Ki, Mi, Gi - the official prefixes for binary multiples,
see http://physics.nist.gov/cuu/Units/binary.html,
ki - mistake on my side and analog to "k" for decimal multiples,
KiB, MiB, GiB - for people that prefer to add a "B" for byte,
kiB - combination of the above.
There were complaints about not accepting "k" for 1024. This has long
been common practice, but is known to lead to confusion. Hence the new
SI units and hence block2mtd only accepts units that cannot be confused
with decimal units. Diverging from common practice doesn't always please
people, even if the change is for the better.
Signed-off-by: Joern Engel <joern@wohnheim.fh-wedel.de>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-04-19 11:03:08 +07:00
|
|
|
/* This function works similar to reguler strtoul. In addition, it
|
|
|
|
* allows some suffixes for a more human-readable number format:
|
|
|
|
* ki, Ki, kiB, KiB - multiply result with 1024
|
|
|
|
* Mi, MiB - multiply result with 1024^2
|
|
|
|
* Gi, GiB - multiply result with 1024^3
|
|
|
|
*/
|
2005-04-17 05:20:36 +07:00
|
|
|
static int ustrtoul(const char *cp, char **endp, unsigned int base)
|
|
|
|
{
|
|
|
|
unsigned long result = simple_strtoul(cp, endp, base);
|
|
|
|
switch (**endp) {
|
|
|
|
case 'G' :
|
|
|
|
result *= 1024;
|
|
|
|
case 'M':
|
|
|
|
result *= 1024;
|
mtd: improve parameter parsing for block2mtd
Expand the parameter parsing for block2mtd. It now accepts:
Ki, Mi, Gi - the official prefixes for binary multiples,
see http://physics.nist.gov/cuu/Units/binary.html,
ki - mistake on my side and analog to "k" for decimal multiples,
KiB, MiB, GiB - for people that prefer to add a "B" for byte,
kiB - combination of the above.
There were complaints about not accepting "k" for 1024. This has long
been common practice, but is known to lead to confusion. Hence the new
SI units and hence block2mtd only accepts units that cannot be confused
with decimal units. Diverging from common practice doesn't always please
people, even if the change is for the better.
Signed-off-by: Joern Engel <joern@wohnheim.fh-wedel.de>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-04-19 11:03:08 +07:00
|
|
|
case 'K':
|
2005-04-17 05:20:36 +07:00
|
|
|
case 'k':
|
|
|
|
result *= 1024;
|
|
|
|
/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
|
mtd: improve parameter parsing for block2mtd
Expand the parameter parsing for block2mtd. It now accepts:
Ki, Mi, Gi - the official prefixes for binary multiples,
see http://physics.nist.gov/cuu/Units/binary.html,
ki - mistake on my side and analog to "k" for decimal multiples,
KiB, MiB, GiB - for people that prefer to add a "B" for byte,
kiB - combination of the above.
There were complaints about not accepting "k" for 1024. This has long
been common practice, but is known to lead to confusion. Hence the new
SI units and hence block2mtd only accepts units that cannot be confused
with decimal units. Diverging from common practice doesn't always please
people, even if the change is for the better.
Signed-off-by: Joern Engel <joern@wohnheim.fh-wedel.de>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
2006-04-19 11:03:08 +07:00
|
|
|
if ((*endp)[1] == 'i') {
|
|
|
|
if ((*endp)[2] == 'B')
|
|
|
|
(*endp) += 3;
|
|
|
|
else
|
|
|
|
(*endp) += 2;
|
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-20 05:40:47 +07:00
|
|
|
static int parse_num(size_t *num, const char *token)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
|
|
|
char *endp;
|
2005-03-20 05:40:47 +07:00
|
|
|
size_t n;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2005-03-20 05:40:47 +07:00
|
|
|
n = (size_t) ustrtoul(token, &endp, 0);
|
2005-04-17 05:20:36 +07:00
|
|
|
if (*endp)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2005-03-20 05:40:47 +07:00
|
|
|
*num = n;
|
2005-04-17 05:20:36 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void kill_final_newline(char *str)
|
|
|
|
{
|
|
|
|
char *newline = strrchr(str, '\n');
|
|
|
|
if (newline && !newline[1])
|
|
|
|
*newline = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define parse_err(fmt, args...) do { \
|
|
|
|
ERROR("block2mtd: " fmt "\n", ## args); \
|
|
|
|
return 0; \
|
|
|
|
} while (0)
|
|
|
|
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
#ifndef MODULE
|
|
|
|
static int block2mtd_init_called = 0;
|
|
|
|
static __initdata char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static int block2mtd_setup2(const char *val)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
char buf[80 + 12]; /* 80 for device, 12 for erase size */
|
2006-05-14 06:42:25 +07:00
|
|
|
char *str = buf;
|
2005-04-17 05:20:36 +07:00
|
|
|
char *token[2];
|
|
|
|
char *name;
|
2005-03-20 05:40:47 +07:00
|
|
|
size_t erase_size = PAGE_SIZE;
|
2005-04-17 05:20:36 +07:00
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
if (strnlen(val, sizeof(buf)) >= sizeof(buf))
|
|
|
|
parse_err("parameter too long");
|
|
|
|
|
|
|
|
strcpy(str, val);
|
|
|
|
kill_final_newline(str);
|
|
|
|
|
2006-05-14 06:42:25 +07:00
|
|
|
for (i = 0; i < 2; i++)
|
2005-04-17 05:20:36 +07:00
|
|
|
token[i] = strsep(&str, ",");
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
parse_err("too many arguments");
|
|
|
|
|
|
|
|
if (!token[0])
|
|
|
|
parse_err("no argument");
|
|
|
|
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
name = token[0];
|
|
|
|
if (strlen(name) + 1 > 80)
|
|
|
|
parse_err("device name too long");
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
if (token[1]) {
|
2005-03-20 05:40:47 +07:00
|
|
|
ret = parse_num(&erase_size, token[1]);
|
2006-05-14 06:42:25 +07:00
|
|
|
if (ret) {
|
|
|
|
kfree(name);
|
2005-04-17 05:20:36 +07:00
|
|
|
parse_err("illegal erase size");
|
2006-05-14 06:42:25 +07:00
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
add_device(name, erase_size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
static int block2mtd_setup(const char *val, struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
#ifdef MODULE
|
|
|
|
return block2mtd_setup2(val);
|
|
|
|
#else
|
|
|
|
/* If more parameters are later passed in via
|
|
|
|
/sys/module/block2mtd/parameters/block2mtd
|
|
|
|
and block2mtd_init() has already been called,
|
|
|
|
we can parse the argument now. */
|
|
|
|
|
|
|
|
if (block2mtd_init_called)
|
|
|
|
return block2mtd_setup2(val);
|
|
|
|
|
|
|
|
/* During early boot stage, we only save the parameters
|
|
|
|
here. We must parse them later: if the param passed
|
|
|
|
from kernel boot command line, block2mtd_setup() is
|
|
|
|
called so early that it is not possible to resolve
|
|
|
|
the device (even kmalloc() fails). Deter that work to
|
|
|
|
block2mtd_setup2(). */
|
|
|
|
|
|
|
|
strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
|
|
|
|
MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
|
|
|
|
|
|
|
|
static int __init block2mtd_init(void)
|
|
|
|
{
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
int ret = 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
INFO("version " VERSION);
|
block2mtd.c: Make kernel boot command line arguments work (try 4)
Trying to pass kernel command line arguments to block2mtd at boot-time does
not work currently. block2mtd_setup() is called so early that kmalloc()
fails nevermind being able to do open_bdev_excl() (which requires rootfs to
be mounted. This patch only saves the option string at the early boot stage,
and parses them later when block2mtd_init() is called. If open_bdev_excl()
fails, open_by_devnum(name_to_dev_t()) is tried instead, which makes it
possible to initialize the driver before rootfs has been mounted. Also gets
rid of the superfluous parse_name() that only checks if name is longer than
80 chars and copies it to a string that is not kfreed.
With this patch, I can boot statically compiled block2mtd, and mount jffs2
as rootfs (without modules or initrd), with lilo config like this:
root=/dev/mtdblock0
append="rootfstype=jffs2 block2mtd.block2mtd=/dev/hdc2,65536"
(Note that rootfstype=jffs2 is required, since the kernel only tries
filesystems without "nodev" attribute by default, and jffs is "nodev").
Compared to first version of this patch, this one does not copy the
parameters to the global buffer if init has already been called, and the
global array is marked as __initdata.
Compared to the second version of this patch, module build is fixed.
Compared to the third version of this patch, statically compiled block2mtd
driver with no boot-time parameter no longer gives spurious error 'cannot
open device ""'
Signed-off-by: Ville Herva <vherva@vianova.fi>
Acked-by: Jörn Engel <joern@wohnheim.fh-wedel.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-07-14 04:31:16 +07:00
|
|
|
|
|
|
|
#ifndef MODULE
|
|
|
|
if (strlen(block2mtd_paramline))
|
|
|
|
ret = block2mtd_setup2(block2mtd_paramline);
|
|
|
|
block2mtd_init_called = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void __devexit block2mtd_exit(void)
|
|
|
|
{
|
|
|
|
struct list_head *pos, *next;
|
|
|
|
|
|
|
|
/* Remove the MTD devices */
|
|
|
|
list_for_each_safe(pos, next, &blkmtd_device_list) {
|
|
|
|
struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
|
|
|
|
block2mtd_sync(&dev->mtd);
|
|
|
|
del_mtd_device(&dev->mtd);
|
|
|
|
INFO("mtd%d: [%s] removed", dev->mtd.index,
|
|
|
|
dev->mtd.name + strlen("blkmtd: "));
|
|
|
|
list_del(&dev->list);
|
|
|
|
block2mtd_free_device(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module_init(block2mtd_init);
|
|
|
|
module_exit(block2mtd_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
|
|
|
|
MODULE_DESCRIPTION("Emulate an MTD using a block device");
|