2005-04-17 02:20:36 +04:00
/*
* block2mtd . c - create an mtd from a block device
*
* Copyright ( C ) 2001 , 2002 Simon Evans < spse @ secret . org . uk >
2008-02-06 12:38:02 +03:00
* Copyright ( C ) 2004 - 2006 Joern Engel < joern @ wh . fh - wedel . de >
2005-04-17 02:20:36 +04:00
*
* Licence : GPL
*/
2013-04-19 21:59:35 +04:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2014-11-09 15:21:13 +03:00
/*
* When the first attempt at device initialization fails , we may need to
* wait a little bit and retry . This timeout , by default 3 seconds , gives
* device time to start up . Required on BCM2708 and a few other chipsets .
*/
# define MTD_DEFAULT_TIMEOUT 3
2005-04-17 02:20:36 +04:00
# include <linux/module.h>
2014-11-09 15:21:13 +03:00
# include <linux/delay.h>
2005-04-17 02:20:36 +04:00
# 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>
2006-03-31 14:29:41 +04: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 01:31:16 +04:00
# include <linux/mount.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2013-10-14 01:05:23 +04:00
# include <linux/major.h>
2005-04-17 02:20:36 +04:00
/* Info for the block device */
struct block2mtd_dev {
struct list_head list ;
struct block_device * blkdev ;
struct mtd_info mtd ;
2006-03-31 14:29:41 +04:00
struct mutex write_mutex ;
2005-04-17 02:20:36 +04:00
} ;
/* Static info about the MTD, used in cleanup_module */
static LIST_HEAD ( blkmtd_device_list ) ;
2007-05-07 01:49:04 +04:00
static struct page * page_read ( struct address_space * mapping , int index )
2005-04-17 02:20:36 +04:00
{
2007-05-07 01:49:04 +04:00
return read_mapping_page ( mapping , index , NULL ) ;
2005-04-17 02:20:36 +04:00
}
/* 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 ) {
2007-02-20 22:22:22 +03:00
page = page_read ( mapping , index ) ;
2005-04-17 02:20:36 +04:00
if ( IS_ERR ( page ) )
return PTR_ERR ( page ) ;
2007-02-20 22:20:58 +03:00
max = page_address ( page ) + PAGE_SIZE ;
for ( p = page_address ( page ) ; p < max ; p + + )
2005-04-17 02:20:36 +04:00
if ( * p ! = - 1UL ) {
lock_page ( page ) ;
memset ( page_address ( page ) , 0xff , PAGE_SIZE ) ;
set_page_dirty ( page ) ;
unlock_page ( page ) ;
2012-12-12 12:32:15 +04:00
balance_dirty_pages_ratelimited ( mapping ) ;
2005-04-17 02:20:36 +04:00
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 14:29:41 +04:00
mutex_lock ( & dev - > write_mutex ) ;
2005-04-17 02:20:36 +04:00
err = _block2mtd_erase ( dev , from , len ) ;
2006-03-31 14:29:41 +04:00
mutex_unlock ( & dev - > write_mutex ) ;
2005-04-17 02:20:36 +04:00
if ( err ) {
2013-04-19 21:59:35 +04:00
pr_err ( " erase failed err = %d \n " , err ) ;
2005-04-17 02:20:36 +04:00
instr - > state = MTD_ERASE_FAILED ;
} else
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-07 23:29:09 +03:00
int offset = from & ( PAGE_SIZE - 1 ) ;
2005-04-17 02:20:36 +04:00
int cpylen ;
while ( len ) {
if ( ( offset + len ) > PAGE_SIZE )
cpylen = PAGE_SIZE - offset ; // multiple pages
else
cpylen = len ; // this page
len = len - cpylen ;
2007-02-20 22:22:22 +03:00
page = page_read ( dev - > blkdev - > bd_inode - > i_mapping , index ) ;
2005-04-17 02:20:36 +04:00
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 ;
while ( len ) {
2005-11-07 14:15:40 +03:00
if ( ( offset + len ) > PAGE_SIZE )
2005-04-17 02:20:36 +04:00
cpylen = PAGE_SIZE - offset ; // multiple pages
else
cpylen = len ; // this page
len = len - cpylen ;
2007-02-20 22:22:22 +03:00
page = page_read ( mapping , index ) ;
2005-04-17 02:20:36 +04:00
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 ) ;
2012-12-12 12:32:15 +04:00
balance_dirty_pages_ratelimited ( mapping ) ;
2005-04-17 02:20:36 +04:00
}
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 01:31:16 +04:00
2005-04-17 02:20:36 +04: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 ;
2006-03-31 14:29:41 +04:00
mutex_lock ( & dev - > write_mutex ) ;
2005-04-17 02:20:36 +04:00
err = _block2mtd_write ( dev , buf , to , len , retlen ) ;
2006-03-31 14:29:41 +04:00
mutex_unlock ( & dev - > write_mutex ) ;
2005-04-17 02:20:36 +04: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 12:45:39 +03:00
invalidate_mapping_pages ( dev - > blkdev - > bd_inode - > i_mapping ,
0 , - 1 ) ;
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 13:55:17 +03:00
blkdev_put ( dev - > blkdev , FMODE_READ | FMODE_WRITE | FMODE_EXCL ) ;
2005-04-17 02:20:36 +04:00
}
kfree ( dev ) ;
}
2014-11-09 15:21:13 +03:00
static struct block2mtd_dev * add_device ( char * devname , int erase_size ,
int timeout )
2005-04-17 02:20:36 +04:00
{
2014-11-09 15:21:13 +03:00
# ifndef MODULE
int i ;
# endif
block: make blkdev_get/put() handle exclusive access
Over time, block layer has accumulated a set of APIs dealing with bdev
open, close, claim and release.
* blkdev_get/put() are the primary open and close functions.
* bd_claim/release() deal with exclusive open.
* open/close_bdev_exclusive() are combination of open and claim and
the other way around, respectively.
* bd_link/unlink_disk_holder() to create and remove holder/slave
symlinks.
* open_by_devnum() wraps bdget() + blkdev_get().
The interface is a bit confusing and the decoupling of open and claim
makes it impossible to properly guarantee exclusive access as
in-kernel open + claim sequence can disturb the existing exclusive
open even before the block layer knows the current open if for another
exclusive access. Reorganize the interface such that,
* blkdev_get() is extended to include exclusive access management.
@holder argument is added and, if is @FMODE_EXCL specified, it will
gain exclusive access atomically w.r.t. other exclusive accesses.
* blkdev_put() is similarly extended. It now takes @mode argument and
if @FMODE_EXCL is set, it releases an exclusive access. Also, when
the last exclusive claim is released, the holder/slave symlinks are
removed automatically.
* bd_claim/release() and close_bdev_exclusive() are no longer
necessary and either made static or removed.
* bd_link_disk_holder() remains the same but bd_unlink_disk_holder()
is no longer necessary and removed.
* open_bdev_exclusive() becomes a simple wrapper around lookup_bdev()
and blkdev_get(). It also has an unexpected extra bdev_read_only()
test which probably should be moved into blkdev_get().
* open_by_devnum() is modified to take @holder argument and pass it to
blkdev_get().
Most of bdev open/close operations are unified into blkdev_get/put()
and most exclusive accesses are tested atomically at the open time (as
it should). This cleans up code and removes some, both valid and
invalid, but unnecessary all the same, corner cases.
open_bdev_exclusive() and open_by_devnum() can use further cleanup -
rename to blkdev_get_by_path() and blkdev_get_by_devt() and drop
special features. Well, let's leave them for another day.
Most conversions are straight-forward. drbd conversion is a bit more
involved as there was some reordering, but the logic should stay the
same.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Neil Brown <neilb@suse.de>
Acked-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Peter Osterlund <petero2@telia.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <joel.becker@oracle.com>
Cc: Alex Elder <aelder@sgi.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: dm-devel@redhat.com
Cc: drbd-dev@lists.linbit.com
Cc: Leo Chen <leochen@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Chris Mason <chris.mason@oracle.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Dave Kleikamp <shaggy@linux.vnet.ibm.com>
Cc: Joern Engel <joern@logfs.org>
Cc: reiserfs-devel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
2010-11-13 13:55:17 +03:00
const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL ;
2014-11-09 15:21:13 +03:00
struct block_device * bdev = ERR_PTR ( - ENODEV ) ;
2005-04-17 02:20:36 +04:00
struct block2mtd_dev * dev ;
2008-07-02 23:46:22 +04:00
char * name ;
2005-04-17 02:20:36 +04:00
if ( ! devname )
return NULL ;
2006-11-15 22:10:29 +03:00
dev = kzalloc ( sizeof ( struct block2mtd_dev ) , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! dev )
return NULL ;
/* Get a handle on the device */
2010-11-13 13:55:18 +03:00
bdev = blkdev_get_by_path ( devname , mode , dev ) ;
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 01:31:16 +04:00
2014-11-09 15:21:13 +03:00
# ifndef MODULE
/*
* We might not have the root device mounted at this point .
* Try to resolve the device name by other means .
*/
for ( i = 0 ; IS_ERR ( bdev ) & & i < = timeout ; i + + ) {
dev_t devt ;
if ( i )
/*
* Calling wait_for_device_probe in the first loop
* was not enough , sleep for a bit in subsequent
* go - arounds .
*/
msleep ( 1000 ) ;
wait_for_device_probe ( ) ;
devt = name_to_dev_t ( devname ) ;
if ( ! devt )
continue ;
bdev = blkdev_get_by_dev ( devt , mode , dev ) ;
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 01:31:16 +04:00
}
# endif
2005-04-17 02:20:36 +04:00
if ( IS_ERR ( bdev ) ) {
2013-04-19 21:59:35 +04:00
pr_err ( " error: cannot open device %s \n " , devname ) ;
2014-01-25 06:45:08 +04:00
goto err_free_block2mtd ;
2005-04-17 02:20:36 +04:00
}
dev - > blkdev = bdev ;
if ( MAJOR ( bdev - > bd_dev ) = = MTD_BLOCK_MAJOR ) {
2013-04-19 21:59:35 +04:00
pr_err ( " attempting to use an MTD device as a block device \n " ) ;
2014-01-25 06:45:08 +04:00
goto err_free_block2mtd ;
2005-04-17 02:20:36 +04:00
}
2014-03-06 14:04:22 +04:00
if ( ( long ) dev - > blkdev - > bd_inode - > i_size % erase_size ) {
pr_err ( " erasesize must be a divisor of device size \n " ) ;
goto err_free_block2mtd ;
}
2006-03-31 14:29:41 +04:00
mutex_init ( & dev - > write_mutex ) ;
2005-04-17 02:20:36 +04:00
/* Setup the MTD structure */
/* make the name contain the block device in */
2010-03-11 00:15:19 +03:00
name = kasprintf ( GFP_KERNEL , " block2mtd: %s " , devname ) ;
2008-07-02 23:46:22 +04:00
if ( ! name )
2014-01-25 06:45:08 +04:00
goto err_destroy_mutex ;
2005-04-17 02:20:36 +04:00
2008-07-02 23:46:22 +04:00
dev - > mtd . name = name ;
2005-04-17 02:20:36 +04:00
dev - > mtd . size = dev - > blkdev - > bd_inode - > i_size & PAGE_MASK ;
dev - > mtd . erasesize = erase_size ;
2006-06-22 18:15:48 +04:00
dev - > mtd . writesize = 1 ;
2012-02-03 11:32:44 +04:00
dev - > mtd . writebufsize = PAGE_SIZE ;
2006-06-15 00:39:48 +04:00
dev - > mtd . type = MTD_RAM ;
2005-04-17 02:20:36 +04:00
dev - > mtd . flags = MTD_CAP_RAM ;
2012-01-30 16:58:32 +04:00
dev - > mtd . _erase = block2mtd_erase ;
dev - > mtd . _write = block2mtd_write ;
dev - > mtd . _sync = block2mtd_sync ;
dev - > mtd . _read = block2mtd_read ;
2005-04-17 02:20:36 +04:00
dev - > mtd . priv = dev ;
dev - > mtd . owner = THIS_MODULE ;
2011-05-23 13:23:40 +04:00
if ( mtd_device_register ( & dev - > mtd , NULL , 0 ) ) {
2011-03-31 05:57:33 +04:00
/* Device didn't get added, so free the entry */
2014-01-25 06:45:08 +04:00
goto err_destroy_mutex ;
2005-04-17 02:20:36 +04:00
}
2014-11-09 15:21:13 +03:00
2005-04-17 02:20:36 +04:00
list_add ( & dev - > list , & blkmtd_device_list ) ;
2013-04-19 21:59:35 +04:00
pr_info ( " mtd%d: [%s] erase_size = %dKiB [%d] \n " ,
dev - > mtd . index ,
dev - > mtd . name + strlen ( " block2mtd: " ) ,
dev - > mtd . erasesize > > 10 , dev - > mtd . erasesize ) ;
2005-04-17 02:20:36 +04:00
return dev ;
2014-01-25 06:45:08 +04:00
err_destroy_mutex :
mutex_destroy ( & dev - > write_mutex ) ;
err_free_block2mtd :
2005-04-17 02:20:36 +04:00
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 08:03:08 +04: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 02:20:36 +04: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 08:03:08 +04:00
case ' K ' :
2005-04-17 02:20:36 +04: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 08:03:08 +04:00
if ( ( * endp ) [ 1 ] = = ' i ' ) {
if ( ( * endp ) [ 2 ] = = ' B ' )
( * endp ) + = 3 ;
else
( * endp ) + = 2 ;
}
2005-04-17 02:20:36 +04:00
}
return result ;
}
2005-03-20 01:40:47 +03:00
static int parse_num ( size_t * num , const char * token )
2005-04-17 02:20:36 +04:00
{
char * endp ;
2005-03-20 01:40:47 +03:00
size_t n ;
2005-04-17 02:20:36 +04:00
2005-03-20 01:40:47 +03:00
n = ( size_t ) ustrtoul ( token , & endp , 0 ) ;
2005-04-17 02:20:36 +04:00
if ( * endp )
return - EINVAL ;
2005-03-20 01:40:47 +03:00
* num = n ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
static inline void kill_final_newline ( char * str )
{
char * newline = strrchr ( str , ' \n ' ) ;
if ( newline & & ! newline [ 1 ] )
* newline = 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 01:31:16 +04:00
# ifndef MODULE
static int block2mtd_init_called = 0 ;
2014-11-09 15:21:13 +03:00
/* 80 for device, 12 for erase size */
static char block2mtd_paramline [ 80 + 12 ] ;
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 01:31:16 +04:00
# endif
static int block2mtd_setup2 ( const char * val )
2005-04-17 02:20:36 +04:00
{
2014-11-09 15:21:13 +03:00
/* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
char buf [ 80 + 12 + 80 + 8 ] ;
2006-05-14 03:42:25 +04:00
char * str = buf ;
2005-04-17 02:20:36 +04:00
char * token [ 2 ] ;
char * name ;
2005-03-20 01:40:47 +03:00
size_t erase_size = PAGE_SIZE ;
2014-11-09 15:21:13 +03:00
unsigned long timeout = MTD_DEFAULT_TIMEOUT ;
2005-04-17 02:20:36 +04:00
int i , ret ;
2013-04-19 21:59:35 +04:00
if ( strnlen ( val , sizeof ( buf ) ) > = sizeof ( buf ) ) {
pr_err ( " parameter too long \n " ) ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
strcpy ( str , val ) ;
kill_final_newline ( str ) ;
2006-05-14 03:42:25 +04:00
for ( i = 0 ; i < 2 ; i + + )
2005-04-17 02:20:36 +04:00
token [ i ] = strsep ( & str , " , " ) ;
2013-04-19 21:59:35 +04:00
if ( str ) {
pr_err ( " too many arguments \n " ) ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
2013-04-19 21:59:35 +04:00
if ( ! token [ 0 ] ) {
pr_err ( " no argument \n " ) ;
return 0 ;
}
2005-04-17 02:20:36 +04: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 01:31:16 +04:00
name = token [ 0 ] ;
2013-04-19 21:59:35 +04:00
if ( strlen ( name ) + 1 > 80 ) {
pr_err ( " device name too long \n " ) ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
if ( token [ 1 ] ) {
2005-03-20 01:40:47 +03:00
ret = parse_num ( & erase_size , token [ 1 ] ) ;
2006-05-14 03:42:25 +04:00
if ( ret ) {
2013-04-19 21:59:35 +04:00
pr_err ( " illegal erase size \n " ) ;
return 0 ;
2006-05-14 03:42:25 +04:00
}
2005-04-17 02:20:36 +04:00
}
2014-11-09 15:21:13 +03:00
add_device ( name , erase_size , timeout ) ;
2005-04-17 02:20:36 +04:00
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 01:31:16 +04: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 02:20:36 +04: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 01:31:16 +04:00
int ret = 0 ;
# ifndef MODULE
if ( strlen ( block2mtd_paramline ) )
ret = block2mtd_setup2 ( block2mtd_paramline ) ;
block2mtd_init_called = 1 ;
# endif
return ret ;
2005-04-17 02:20:36 +04:00
}
2012-11-19 22:26:04 +04:00
static void block2mtd_exit ( void )
2005-04-17 02:20:36 +04:00
{
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 ) ;
2011-05-23 13:23:40 +04:00
mtd_device_unregister ( & dev - > mtd ) ;
2014-01-25 06:45:08 +04:00
mutex_destroy ( & dev - > write_mutex ) ;
2013-04-19 21:59:35 +04:00
pr_info ( " mtd%d: [%s] removed \n " ,
dev - > mtd . index ,
dev - > mtd . name + strlen ( " block2mtd: " ) ) ;
2005-04-17 02:20:36 +04:00
list_del ( & dev - > list ) ;
block2mtd_free_device ( dev ) ;
}
}
2014-11-09 15:21:13 +03:00
late_initcall ( block2mtd_init ) ;
2005-04-17 02:20:36 +04:00
module_exit ( block2mtd_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
2008-02-06 12:38:02 +03:00
MODULE_AUTHOR ( " Joern Engel <joern@lazybastard.org> " ) ;
2005-04-17 02:20:36 +04:00
MODULE_DESCRIPTION ( " Emulate an MTD using a block device " ) ;