]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/mtd/devices/block2mtd.c
Merge git://git.infradead.org/mtd-2.6
[linux-2.6-omap-h63xx.git] / drivers / mtd / devices / block2mtd.c
index 0d98c223c5fc2e188d1f1e58ad0ea53ca0d1a506..be4b9948c762e33b577cbd192dd836ff50c75e46 100644 (file)
@@ -8,7 +8,6 @@
  *
  * Licence: GPL
  */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/fs.h>
 #include <linux/blkdev.h>
@@ -19,6 +18,7 @@
 #include <linux/mtd/mtd.h>
 #include <linux/buffer_head.h>
 #include <linux/mutex.h>
+#include <linux/mount.h>
 
 #define VERSION "$Revision: 1.30 $"
 
@@ -40,60 +40,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);
-}
-
-
-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);
+       return read_mapping_page(mapping, index, NULL);
 }
 
-
 /* erase a specified part of the device */
 static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
 {
@@ -105,14 +56,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 +125,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 +163,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 +186,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,7 +227,8 @@ 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);
+               invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
+                                       0, -1);
                close_bdev_excl(dev->blkdev);
        }
 
@@ -293,13 +245,25 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
        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);
+#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;
@@ -324,6 +288,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
 
        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;
@@ -393,26 +358,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');
@@ -426,9 +371,15 @@ static inline void kill_final_newline(char *str)
        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,13 +401,9 @@ 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]);
@@ -472,13 +419,48 @@ 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)
 {
+       int ret = 0;
        INFO("version " VERSION);
-       return 0;
+
+#ifndef MODULE
+       if (strlen(block2mtd_paramline))
+               ret = block2mtd_setup2(block2mtd_paramline);
+       block2mtd_init_called = 1;
+#endif
+
+       return ret;
 }