ARM: OMAP3: SDRC: add timing data for Micron MT46H32M32LF-6, v2
[safe/jmp/linux-2.6] / drivers / mtd / devices / block2mtd.c
index 0d98c22..8c295f4 100644 (file)
@@ -1,14 +1,11 @@
 /*
- * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
- *
  * block2mtd.c - create an mtd from a block device
  *
  * Copyright (C) 2001,2002     Simon Evans <spse@secret.org.uk>
- * Copyright (C) 2004-2006     Jรถrn Engel <joern@wh.fh-wedel.de>
+ * Copyright (C) 2004-2006     Joern Engel <joern@wh.fh-wedel.de>
  *
  * Licence: GPL
  */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/fs.h>
 #include <linux/blkdev.h>
@@ -19,9 +16,7 @@
 #include <linux/mtd/mtd.h>
 #include <linux/buffer_head.h>
 #include <linux/mutex.h>
-
-#define VERSION "$Revision: 1.30 $"
-
+#include <linux/mount.h>
 
 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
@@ -40,60 +35,11 @@ struct block2mtd_dev {
 static LIST_HEAD(blkmtd_device_list);
 
 
-#define PAGE_READAHEAD 64
-static void cache_readahead(struct address_space *mapping, int index)
+static struct page *page_read(struct address_space *mapping, int index)
 {
-       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);
+       return read_mapping_page(mapping, index, 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)
 {
@@ -105,14 +51,14 @@ static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
        u_long *max;
 
        while (pages) {
-               page = page_readahead(mapping, index);
+               page = page_read(mapping, index);
                if (!page)
                        return -ENOMEM;
                if (IS_ERR(page))
                        return PTR_ERR(page);
 
-               max = (u_long*)page_address(page) + PAGE_SIZE;
-               for (p=(u_long*)page_address(page); p<max; p++)
+               max = page_address(page) + PAGE_SIZE;
+               for (p=page_address(page); p<max; p++)
                        if (*p != -1UL) {
                                lock_page(page);
                                memset(page_address(page), 0xff, PAGE_SIZE);
@@ -174,8 +120,7 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
                        cpylen = len;   // this page
                len = len - cpylen;
 
-               //      Get page
-               page = page_readahead(dev->blkdev->bd_inode->i_mapping, index);
+               page = page_read(dev->blkdev->bd_inode->i_mapping, index);
                if (!page)
                        return -ENOMEM;
                if (IS_ERR(page))
@@ -213,8 +158,7 @@ static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
                        cpylen = len;                   // this page
                len = len - cpylen;
 
-               //      Get page
-               page = page_readahead(mapping, index);
+               page = page_read(mapping, index);
                if (!page)
                        return -ENOMEM;
                if (IS_ERR(page))
@@ -237,6 +181,8 @@ static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
        }
        return 0;
 }
+
+
 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
                size_t *retlen, const u_char *buf)
 {
@@ -276,8 +222,9 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
        kfree(dev->mtd.name);
 
        if (dev->blkdev) {
-               invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping);
-               close_bdev_excl(dev->blkdev);
+               invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
+                                       0, -1);
+               close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
        }
 
        kfree(dev);
@@ -289,17 +236,30 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
 {
        struct block_device *bdev;
        struct block2mtd_dev *dev;
+       char *name;
 
        if (!devname)
                return NULL;
 
-       dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
+       dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
        if (!dev)
                return NULL;
-       memset(dev, 0, sizeof(*dev));
 
        /* Get a handle on the device */
-       bdev = open_bdev_excl(devname, O_RDWR, NULL);
+       bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
+#ifndef MODULE
+       if (IS_ERR(bdev)) {
+
+               /* We might not have rootfs mounted at this point. Try
+                  to resolve the device name by other means. */
+
+               dev_t devt = name_to_dev_t(devname);
+               if (devt) {
+                       bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
+               }
+       }
+#endif
+
        if (IS_ERR(bdev)) {
                ERROR("error: cannot open device %s", devname);
                goto devinit_err;
@@ -315,15 +275,17 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
 
        /* Setup the MTD structure */
        /* make the name contain the block device in */
-       dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
+       name = kmalloc(sizeof("block2mtd: ") + strlen(devname) + 1,
                        GFP_KERNEL);
-       if (!dev->mtd.name)
+       if (!name)
                goto devinit_err;
 
-       sprintf(dev->mtd.name, "block2mtd: %s", devname);
+       sprintf(name, "block2mtd: %s", devname);
+       dev->mtd.name = name;
 
        dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
        dev->mtd.erasesize = erase_size;
+       dev->mtd.writesize = 1;
        dev->mtd.type = MTD_RAM;
        dev->mtd.flags = MTD_CAP_RAM;
        dev->mtd.erase = block2mtd_erase;
@@ -340,7 +302,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
        }
        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.name + strlen("block2mtd: "),
                        dev->mtd.erasesize >> 10, dev->mtd.erasesize);
        return dev;
 
@@ -393,26 +355,6 @@ static int parse_num(size_t *num, const char *token)
 }
 
 
-static int parse_name(char **pname, const char *token, size_t limit)
-{
-       size_t len;
-       char *name;
-
-       len = strlen(token) + 1;
-       if (len > limit)
-               return -ENOSPC;
-
-       name = kmalloc(len, GFP_KERNEL);
-       if (!name)
-               return -ENOMEM;
-
-       strcpy(name, token);
-
-       *pname = name;
-       return 0;
-}
-
-
 static inline void kill_final_newline(char *str)
 {
        char *newline = strrchr(str, '\n');
@@ -421,14 +363,20 @@ static inline void kill_final_newline(char *str)
 }
 
 
-#define parse_err(fmt, args...) do {           \
-       ERROR("block2mtd: " fmt "\n", ## args); \
-       return 0;                               \
+#define parse_err(fmt, args...) do {   \
+       ERROR(fmt, ## args);            \
+       return 0;                       \
 } while (0)
 
-static int block2mtd_setup(const char *val, struct kernel_param *kp)
+#ifndef MODULE
+static int block2mtd_init_called = 0;
+static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
+#endif
+
+
+static int block2mtd_setup2(const char *val)
 {
-       char buf[80+12]; /* 80 for device, 12 for erase size */
+       char buf[80 + 12]; /* 80 for device, 12 for erase size */
        char *str = buf;
        char *token[2];
        char *name;
@@ -450,18 +398,13 @@ static int block2mtd_setup(const char *val, struct kernel_param *kp)
        if (!token[0])
                parse_err("no argument");
 
-       ret = parse_name(&name, token[0], 80);
-       if (ret == -ENOMEM)
-               parse_err("out of memory");
-       if (ret == -ENOSPC)
-               parse_err("name too long");
-       if (ret)
-               return 0;
+       name = token[0];
+       if (strlen(name) + 1 > 80)
+               parse_err("device name too long");
 
        if (token[1]) {
                ret = parse_num(&erase_size, token[1]);
                if (ret) {
-                       kfree(name);
                        parse_err("illegal erase size");
                }
        }
@@ -472,13 +415,47 @@ static int block2mtd_setup(const char *val, struct kernel_param *kp)
 }
 
 
+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
+}
+
+
 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)
 {
-       INFO("version " VERSION);
-       return 0;
+       int ret = 0;
+
+#ifndef MODULE
+       if (strlen(block2mtd_paramline))
+               ret = block2mtd_setup2(block2mtd_paramline);
+       block2mtd_init_called = 1;
+#endif
+
+       return ret;
 }
 
 
@@ -492,7 +469,7 @@ static void __devexit block2mtd_exit(void)
                block2mtd_sync(&dev->mtd);
                del_mtd_device(&dev->mtd);
                INFO("mtd%d: [%s] removed", dev->mtd.index,
-                               dev->mtd.name + strlen("blkmtd: "));
+                               dev->mtd.name + strlen("block2mtd: "));
                list_del(&dev->list);
                block2mtd_free_device(dev);
        }
@@ -503,5 +480,5 @@ module_init(block2mtd_init);
 module_exit(block2mtd_exit);
 
 MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
+MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
 MODULE_DESCRIPTION("Emulate an MTD using a block device");