Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / mtdchar.c
index 24d35ba..f2f482b 100644 (file)
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/sched.h>
-#include <linux/smp_lock.h>
+#include <linux/mutex.h>
 #include <linux/backing-dev.h>
 #include <linux/compat.h>
 #include <linux/mount.h>
-
+#include <linux/blkpg.h>
+#include <linux/magic.h>
 #include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
 #include <linux/mtd/map.h>
 
 #include <asm/uaccess.h>
 
-#define MTD_INODE_FS_MAGIC 0x11307854
-static struct vfsmount *mtd_inode_mnt __read_mostly;
+static DEFINE_MUTEX(mtd_mutex);
 
 /*
  * Data structure to hold the pointer to the mtd device as well
- * as mode information ofr various use cases.
+ * as mode information of various use cases.
  */
 struct mtd_file_info {
        struct mtd_info *mtd;
@@ -49,7 +50,7 @@ struct mtd_file_info {
        enum mtd_file_modes mode;
 };
 
-static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
+static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
@@ -73,9 +74,11 @@ static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
        return -EINVAL;
 }
 
+static int count;
+static struct vfsmount *mnt;
+static struct file_system_type mtd_inodefs_type;
 
-
-static int mtd_open(struct inode *inode, struct file *file)
+static int mtdchar_open(struct inode *inode, struct file *file)
 {
        int minor = iminor(inode);
        int devnum = minor >> 1;
@@ -84,13 +87,17 @@ static int mtd_open(struct inode *inode, struct file *file)
        struct mtd_file_info *mfi;
        struct inode *mtd_ino;
 
-       DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
+       pr_debug("MTD_open\n");
 
        /* You can't open the RO devices RW */
        if ((file->f_mode & FMODE_WRITE) && (minor & 1))
                return -EACCES;
 
-       lock_kernel();
+       ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
+       if (ret)
+               return ret;
+
+       mutex_lock(&mtd_mutex);
        mtd = get_mtd_device(NULL, devnum);
 
        if (IS_ERR(mtd)) {
@@ -99,16 +106,14 @@ static int mtd_open(struct inode *inode, struct file *file)
        }
 
        if (mtd->type == MTD_ABSENT) {
-               put_mtd_device(mtd);
                ret = -ENODEV;
-               goto out;
+               goto out1;
        }
 
-       mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
+       mtd_ino = iget_locked(mnt->mnt_sb, devnum);
        if (!mtd_ino) {
-               put_mtd_device(mtd);
                ret = -ENOMEM;
-               goto out;
+               goto out1;
        }
        if (mtd_ino->i_state & I_NEW) {
                mtd_ino->i_private = mtd;
@@ -120,66 +125,85 @@ static int mtd_open(struct inode *inode, struct file *file)
 
        /* You can't open it RW if it's not a writeable device */
        if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
-               iput(mtd_ino);
-               put_mtd_device(mtd);
                ret = -EACCES;
-               goto out;
+               goto out2;
        }
 
        mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
        if (!mfi) {
-               iput(mtd_ino);
-               put_mtd_device(mtd);
                ret = -ENOMEM;
-               goto out;
+               goto out2;
        }
        mfi->ino = mtd_ino;
        mfi->mtd = mtd;
        file->private_data = mfi;
+       mutex_unlock(&mtd_mutex);
+       return 0;
 
+out2:
+       iput(mtd_ino);
+out1:
+       put_mtd_device(mtd);
 out:
-       unlock_kernel();
+       mutex_unlock(&mtd_mutex);
+       simple_release_fs(&mnt, &count);
        return ret;
-} /* mtd_open */
+} /* mtdchar_open */
 
 /*====================================================================*/
 
-static int mtd_close(struct inode *inode, struct file *file)
+static int mtdchar_close(struct inode *inode, struct file *file)
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
 
-       DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
+       pr_debug("MTD_close\n");
 
        /* Only sync if opened RW */
-       if ((file->f_mode & FMODE_WRITE) && mtd->sync)
-               mtd->sync(mtd);
+       if ((file->f_mode & FMODE_WRITE))
+               mtd_sync(mtd);
 
        iput(mfi->ino);
 
        put_mtd_device(mtd);
        file->private_data = NULL;
        kfree(mfi);
+       simple_release_fs(&mnt, &count);
 
        return 0;
-} /* mtd_close */
+} /* mtdchar_close */
 
-/* FIXME: This _really_ needs to die. In 2.5, we should lock the
-   userspace buffer down and use it directly with readv/writev.
-*/
-#define MAX_KMALLOC_SIZE 0x20000
+/* Back in June 2001, dwmw2 wrote:
+ *
+ *   FIXME: This _really_ needs to die. In 2.5, we should lock the
+ *   userspace buffer down and use it directly with readv/writev.
+ *
+ * The implementation below, using mtd_kmalloc_up_to, mitigates
+ * allocation failures when the system is under low-memory situations
+ * or if memory is highly fragmented at the cost of reducing the
+ * performance of the requested transfer due to a smaller buffer size.
+ *
+ * A more complex but more memory-efficient implementation based on
+ * get_user_pages and iovecs to cover extents of those pages is a
+ * longer-term goal, as intimated by dwmw2 above. However, for the
+ * write case, this requires yet more complex head and tail transfer
+ * handling when those head and tail offsets and sizes are such that
+ * alignment requirements are not met in the NAND subdriver.
+ */
 
-static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
+static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
+                       loff_t *ppos)
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
-       size_t retlen=0;
+       size_t retlen;
        size_t total_retlen=0;
        int ret=0;
        int len;
+       size_t size = count;
        char *kbuf;
 
-       DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
+       pr_debug("MTD_read\n");
 
        if (*ppos + count > mtd->size)
                count = mtd->size - *ppos;
@@ -187,57 +211,48 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
        if (!count)
                return 0;
 
-       /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
-          and pass them directly to the MTD functions */
-
-       if (count > MAX_KMALLOC_SIZE)
-               kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
-       else
-               kbuf=kmalloc(count, GFP_KERNEL);
-
+       kbuf = mtd_kmalloc_up_to(mtd, &size);
        if (!kbuf)
                return -ENOMEM;
 
        while (count) {
-
-               if (count > MAX_KMALLOC_SIZE)
-                       len = MAX_KMALLOC_SIZE;
-               else
-                       len = count;
+               len = min_t(size_t, count, size);
 
                switch (mfi->mode) {
-               case MTD_MODE_OTP_FACTORY:
-                       ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+               case MTD_FILE_MODE_OTP_FACTORY:
+                       ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
+                                                    &retlen, kbuf);
                        break;
-               case MTD_MODE_OTP_USER:
-                       ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+               case MTD_FILE_MODE_OTP_USER:
+                       ret = mtd_read_user_prot_reg(mtd, *ppos, len,
+                                                    &retlen, kbuf);
                        break;
-               case MTD_MODE_RAW:
+               case MTD_FILE_MODE_RAW:
                {
                        struct mtd_oob_ops ops;
 
-                       ops.mode = MTD_OOB_RAW;
+                       ops.mode = MTD_OPS_RAW;
                        ops.datbuf = kbuf;
                        ops.oobbuf = NULL;
                        ops.len = len;
 
-                       ret = mtd->read_oob(mtd, *ppos, &ops);
+                       ret = mtd_read_oob(mtd, *ppos, &ops);
                        retlen = ops.retlen;
                        break;
                }
                default:
-                       ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
+                       ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
                }
-               /* Nand returns -EBADMSG on ecc errors, but it returns
+               /* Nand returns -EBADMSG on ECC errors, but it returns
                 * the data. For our userspace tools it is important
-                * to dump areas with ecc errors !
+                * to dump areas with ECC errors!
                 * For kernel internal usage it also might return -EUCLEAN
-                * to signal the caller that a bitflip has occured and has
+                * to signal the caller that a bitflip has occurred and has
                 * been corrected by the ECC algorithm.
                 * Userspace software which accesses NAND this way
                 * must be aware of the fact that it deals with NAND
                 */
-               if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
+               if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
                        *ppos += retlen;
                        if (copy_to_user(buf, kbuf, retlen)) {
                                kfree(kbuf);
@@ -260,19 +275,21 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
 
        kfree(kbuf);
        return total_retlen;
-} /* mtd_read */
+} /* mtdchar_read */
 
-static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
+static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
+                       loff_t *ppos)
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
+       size_t size = count;
        char *kbuf;
        size_t retlen;
        size_t total_retlen=0;
        int ret=0;
        int len;
 
-       DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
+       pr_debug("MTD_write\n");
 
        if (*ppos == mtd->size)
                return -ENOSPC;
@@ -283,20 +300,12 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
        if (!count)
                return 0;
 
-       if (count > MAX_KMALLOC_SIZE)
-               kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
-       else
-               kbuf=kmalloc(count, GFP_KERNEL);
-
+       kbuf = mtd_kmalloc_up_to(mtd, &size);
        if (!kbuf)
                return -ENOMEM;
 
        while (count) {
-
-               if (count > MAX_KMALLOC_SIZE)
-                       len = MAX_KMALLOC_SIZE;
-               else
-                       len = count;
+               len = min_t(size_t, count, size);
 
                if (copy_from_user(kbuf, buf, len)) {
                        kfree(kbuf);
@@ -304,33 +313,31 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
                }
 
                switch (mfi->mode) {
-               case MTD_MODE_OTP_FACTORY:
+               case MTD_FILE_MODE_OTP_FACTORY:
                        ret = -EROFS;
                        break;
-               case MTD_MODE_OTP_USER:
-                       if (!mtd->write_user_prot_reg) {
-                               ret = -EOPNOTSUPP;
-                               break;
-                       }
-                       ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+               case MTD_FILE_MODE_OTP_USER:
+                       ret = mtd_write_user_prot_reg(mtd, *ppos, len,
+                                                     &retlen, kbuf);
                        break;
 
-               case MTD_MODE_RAW:
+               case MTD_FILE_MODE_RAW:
                {
                        struct mtd_oob_ops ops;
 
-                       ops.mode = MTD_OOB_RAW;
+                       ops.mode = MTD_OPS_RAW;
                        ops.datbuf = kbuf;
                        ops.oobbuf = NULL;
+                       ops.ooboffs = 0;
                        ops.len = len;
 
-                       ret = mtd->write_oob(mtd, *ppos, &ops);
+                       ret = mtd_write_oob(mtd, *ppos, &ops);
                        retlen = ops.retlen;
                        break;
                }
 
                default:
-                       ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
+                       ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
                }
                if (!ret) {
                        *ppos += retlen;
@@ -346,7 +353,7 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
 
        kfree(kbuf);
        return total_retlen;
-} /* mtd_write */
+} /* mtdchar_write */
 
 /*======================================================================
 
@@ -362,20 +369,22 @@ static void mtdchar_erase_callback (struct erase_info *instr)
 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
 {
        struct mtd_info *mtd = mfi->mtd;
+       size_t retlen;
        int ret = 0;
 
+       /*
+        * Make a fake call to mtd_read_fact_prot_reg() to check if OTP
+        * operations are supported.
+        */
+       if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP)
+               return -EOPNOTSUPP;
+
        switch (mode) {
        case MTD_OTP_FACTORY:
-               if (!mtd->read_fact_prot_reg)
-                       ret = -EOPNOTSUPP;
-               else
-                       mfi->mode = MTD_MODE_OTP_FACTORY;
+               mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
                break;
        case MTD_OTP_USER:
-               if (!mtd->read_fact_prot_reg)
-                       ret = -EOPNOTSUPP;
-               else
-                       mfi->mode = MTD_MODE_OTP_USER;
+               mfi->mode = MTD_FILE_MODE_OTP_USER;
                break;
        default:
                ret = -EINVAL;
@@ -388,10 +397,11 @@ static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
 # define otp_select_filemode(f,m)      -EOPNOTSUPP
 #endif
 
-static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
+static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
        uint64_t start, uint32_t length, void __user *ptr,
        uint32_t __user *retp)
 {
+       struct mtd_file_info *mfi = file->private_data;
        struct mtd_oob_ops ops;
        uint32_t retlen;
        int ret = 0;
@@ -402,7 +412,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
        if (length > 4096)
                return -EINVAL;
 
-       if (!mtd->write_oob)
+       if (!mtd->_write_oob)
                ret = -EOPNOTSUPP;
        else
                ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
@@ -411,9 +421,10 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
                return ret;
 
        ops.ooblen = length;
-       ops.ooboffs = start & (mtd->oobsize - 1);
+       ops.ooboffs = start & (mtd->writesize - 1);
        ops.datbuf = NULL;
-       ops.mode = MTD_OOB_PLACE;
+       ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
+               MTD_OPS_PLACE_OOB;
 
        if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
                return -EINVAL;
@@ -422,8 +433,8 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
        if (IS_ERR(ops.oobbuf))
                return PTR_ERR(ops.oobbuf);
 
-       start &= ~((uint64_t)mtd->oobsize - 1);
-       ret = mtd->write_oob(mtd, start, &ops);
+       start &= ~((uint64_t)mtd->writesize - 1);
+       ret = mtd_write_oob(mtd, start, &ops);
 
        if (ops.oobretlen > 0xFFFFFFFFU)
                ret = -EOVERFLOW;
@@ -435,27 +446,25 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
        return ret;
 }
 
-static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
-       uint32_t length, void __user *ptr, uint32_t __user *retp)
+static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
+       uint64_t start, uint32_t length, void __user *ptr,
+       uint32_t __user *retp)
 {
+       struct mtd_file_info *mfi = file->private_data;
        struct mtd_oob_ops ops;
        int ret = 0;
 
        if (length > 4096)
                return -EINVAL;
 
-       if (!mtd->read_oob)
-               ret = -EOPNOTSUPP;
-       else
-               ret = access_ok(VERIFY_WRITE, ptr,
-                               length) ? 0 : -EFAULT;
-       if (ret)
-               return ret;
+       if (!access_ok(VERIFY_WRITE, ptr, length))
+               return -EFAULT;
 
        ops.ooblen = length;
-       ops.ooboffs = start & (mtd->oobsize - 1);
+       ops.ooboffs = start & (mtd->writesize - 1);
        ops.datbuf = NULL;
-       ops.mode = MTD_OOB_PLACE;
+       ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
+               MTD_OPS_PLACE_OOB;
 
        if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
                return -EINVAL;
@@ -464,8 +473,8 @@ static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
        if (!ops.oobbuf)
                return -ENOMEM;
 
-       start &= ~((uint64_t)mtd->oobsize - 1);
-       ret = mtd->read_oob(mtd, start, &ops);
+       start &= ~((uint64_t)mtd->writesize - 1);
+       ret = mtd_read_oob(mtd, start, &ops);
 
        if (put_user(ops.oobretlen, retp))
                ret = -EFAULT;
@@ -474,13 +483,29 @@ static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
                ret = -EFAULT;
 
        kfree(ops.oobbuf);
+
+       /*
+        * NAND returns -EBADMSG on ECC errors, but it returns the OOB
+        * data. For our userspace tools it is important to dump areas
+        * with ECC errors!
+        * For kernel internal usage it also might return -EUCLEAN
+        * to signal the caller that a bitflip has occured and has
+        * been corrected by the ECC algorithm.
+        *
+        * Note: currently the standard NAND function, nand_read_oob_std,
+        * does not calculate ECC for the OOB area, so do not rely on
+        * this behavior unless you have replaced it with your own.
+        */
+       if (mtd_is_bitflip_or_eccerr(ret))
+               return 0;
+
        return ret;
 }
 
 /*
  * Copies (and truncates, if necessary) data from the larger struct,
  * nand_ecclayout, to the smaller, deprecated layout struct,
- * nand_ecclayout_user. This is necessary only to suppport the deprecated
+ * nand_ecclayout_user. This is necessary only to support the deprecated
  * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  * nand_ecclayout flexibly (i.e. the struct may change size in new
  * releases without requiring major rewrites).
@@ -495,7 +520,7 @@ static int shrink_ecclayout(const struct nand_ecclayout *from,
 
        memset(to, 0, sizeof(*to));
 
-       to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES_OLD);
+       to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
        for (i = 0; i < to->eccbytes; i++)
                to->eccpos[i] = from->eccpos[i];
 
@@ -510,7 +535,92 @@ static int shrink_ecclayout(const struct nand_ecclayout *from,
        return 0;
 }
 
-static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
+static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
+                          struct blkpg_ioctl_arg __user *arg)
+{
+       struct blkpg_ioctl_arg a;
+       struct blkpg_partition p;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
+               return -EFAULT;
+
+       if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
+               return -EFAULT;
+
+       switch (a.op) {
+       case BLKPG_ADD_PARTITION:
+
+               /* Only master mtd device must be used to add partitions */
+               if (mtd_is_partition(mtd))
+                       return -EINVAL;
+
+               return mtd_add_partition(mtd, p.devname, p.start, p.length);
+
+       case BLKPG_DEL_PARTITION:
+
+               if (p.pno < 0)
+                       return -EINVAL;
+
+               return mtd_del_partition(mtd, p.pno);
+
+       default:
+               return -EINVAL;
+       }
+}
+
+static int mtdchar_write_ioctl(struct mtd_info *mtd,
+               struct mtd_write_req __user *argp)
+{
+       struct mtd_write_req req;
+       struct mtd_oob_ops ops;
+       void __user *usr_data, *usr_oob;
+       int ret;
+
+       if (copy_from_user(&req, argp, sizeof(req)) ||
+                       !access_ok(VERIFY_READ, req.usr_data, req.len) ||
+                       !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
+               return -EFAULT;
+       if (!mtd->_write_oob)
+               return -EOPNOTSUPP;
+
+       ops.mode = req.mode;
+       ops.len = (size_t)req.len;
+       ops.ooblen = (size_t)req.ooblen;
+       ops.ooboffs = 0;
+
+       usr_data = (void __user *)(uintptr_t)req.usr_data;
+       usr_oob = (void __user *)(uintptr_t)req.usr_oob;
+
+       if (req.usr_data) {
+               ops.datbuf = memdup_user(usr_data, ops.len);
+               if (IS_ERR(ops.datbuf))
+                       return PTR_ERR(ops.datbuf);
+       } else {
+               ops.datbuf = NULL;
+       }
+
+       if (req.usr_oob) {
+               ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
+               if (IS_ERR(ops.oobbuf)) {
+                       kfree(ops.datbuf);
+                       return PTR_ERR(ops.oobbuf);
+               }
+       } else {
+               ops.oobbuf = NULL;
+       }
+
+       ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
+
+       kfree(ops.datbuf);
+       kfree(ops.oobbuf);
+
+       return ret;
+}
+
+static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
@@ -519,7 +629,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
        u_long size;
        struct mtd_info_user info;
 
-       DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
+       pr_debug("MTD_ioctl\n");
 
        size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
        if (cmd & IOC_IN) {
@@ -546,6 +656,9 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (get_user(ur_idx, &(ur->regionindex)))
                        return -EFAULT;
 
+               if (ur_idx >= mtd->numeraseregions)
+                       return -EINVAL;
+
                kr = &(mtd->eraseregions[ur_idx]);
 
                if (put_user(kr->offset, &(ur->offset))
@@ -557,15 +670,15 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
        }
 
        case MEMGETINFO:
+               memset(&info, 0, sizeof(info));
                info.type       = mtd->type;
                info.flags      = mtd->flags;
                info.size       = mtd->size;
                info.erasesize  = mtd->erasesize;
                info.writesize  = mtd->writesize;
                info.oobsize    = mtd->oobsize;
-               /* The below fields are obsolete */
-               info.ecctype    = -1;
-               info.eccsize    = 0;
+               /* The below field is obsolete */
+               info.padding    = 0;
                if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
                        return -EFAULT;
                break;
@@ -621,7 +734,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                          wq_head is no longer there when the
                          callback routine tries to wake us up.
                        */
-                       ret = mtd->erase(mtd, erase);
+                       ret = mtd_erase(mtd, erase);
                        if (!ret) {
                                set_current_state(TASK_UNINTERRUPTIBLE);
                                add_wait_queue(&waitq, &wait);
@@ -647,7 +760,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
+                       ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
                                buf.ptr, &buf_user->length);
                break;
        }
@@ -661,7 +774,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_readoob(mtd, buf.start, buf.length,
+                       ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
                                buf.ptr, &buf_user->start);
                break;
        }
@@ -674,7 +787,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
+                       ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
                                (void __user *)(uintptr_t)buf.usr_ptr,
                                &buf_user->length);
                break;
@@ -688,12 +801,19 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_readoob(mtd, buf.start, buf.length,
+                       ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
                                (void __user *)(uintptr_t)buf.usr_ptr,
                                &buf_user->length);
                break;
        }
 
+       case MEMWRITE:
+       {
+               ret = mtdchar_write_ioctl(mtd,
+                     (struct mtd_write_req __user *)arg);
+               break;
+       }
+
        case MEMLOCK:
        {
                struct erase_info_user einfo;
@@ -701,10 +821,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&einfo, argp, sizeof(einfo)))
                        return -EFAULT;
 
-               if (!mtd->lock)
-                       ret = -EOPNOTSUPP;
-               else
-                       ret = mtd->lock(mtd, einfo.start, einfo.length);
+               ret = mtd_lock(mtd, einfo.start, einfo.length);
                break;
        }
 
@@ -715,10 +832,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&einfo, argp, sizeof(einfo)))
                        return -EFAULT;
 
-               if (!mtd->unlock)
-                       ret = -EOPNOTSUPP;
-               else
-                       ret = mtd->unlock(mtd, einfo.start, einfo.length);
+               ret = mtd_unlock(mtd, einfo.start, einfo.length);
                break;
        }
 
@@ -729,10 +843,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&einfo, argp, sizeof(einfo)))
                        return -EFAULT;
 
-               if (!mtd->is_locked)
-                       ret = -EOPNOTSUPP;
-               else
-                       ret = mtd->is_locked(mtd, einfo.start, einfo.length);
+               ret = mtd_is_locked(mtd, einfo.start, einfo.length);
                break;
        }
 
@@ -763,10 +874,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 
                if (copy_from_user(&offs, argp, sizeof(loff_t)))
                        return -EFAULT;
-               if (!mtd->block_isbad)
-                       ret = -EOPNOTSUPP;
-               else
-                       return mtd->block_isbad(mtd, offs);
+               return mtd_block_isbad(mtd, offs);
                break;
        }
 
@@ -776,10 +884,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
 
                if (copy_from_user(&offs, argp, sizeof(loff_t)))
                        return -EFAULT;
-               if (!mtd->block_markbad)
-                       ret = -EOPNOTSUPP;
-               else
-                       return mtd->block_markbad(mtd, offs);
+               return mtd_block_markbad(mtd, offs);
                break;
        }
 
@@ -790,7 +895,7 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                if (copy_from_user(&mode, argp, sizeof(int)))
                        return -EFAULT;
 
-               mfi->mode = MTD_MODE_NORMAL;
+               mfi->mode = MTD_FILE_MODE_NORMAL;
 
                ret = otp_select_filemode(mfi, mode);
 
@@ -804,17 +909,15 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
                if (!buf)
                        return -ENOMEM;
-               ret = -EOPNOTSUPP;
                switch (mfi->mode) {
-               case MTD_MODE_OTP_FACTORY:
-                       if (mtd->get_fact_prot_info)
-                               ret = mtd->get_fact_prot_info(mtd, buf, 4096);
+               case MTD_FILE_MODE_OTP_FACTORY:
+                       ret = mtd_get_fact_prot_info(mtd, buf, 4096);
                        break;
-               case MTD_MODE_OTP_USER:
-                       if (mtd->get_user_prot_info)
-                               ret = mtd->get_user_prot_info(mtd, buf, 4096);
+               case MTD_FILE_MODE_OTP_USER:
+                       ret = mtd_get_user_prot_info(mtd, buf, 4096);
                        break;
                default:
+                       ret = -EINVAL;
                        break;
                }
                if (ret >= 0) {
@@ -834,18 +937,16 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
        {
                struct otp_info oinfo;
 
-               if (mfi->mode != MTD_MODE_OTP_USER)
+               if (mfi->mode != MTD_FILE_MODE_OTP_USER)
                        return -EINVAL;
                if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
                        return -EFAULT;
-               if (!mtd->lock_user_prot_reg)
-                       return -EOPNOTSUPP;
-               ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
+               ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
                break;
        }
 #endif
 
-       /* This ioctl is being deprecated - it truncates the ecc layout */
+       /* This ioctl is being deprecated - it truncates the ECC layout */
        case ECCGETLAYOUT:
        {
                struct nand_ecclayout_user *usrlay;
@@ -878,17 +979,17 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                mfi->mode = 0;
 
                switch(arg) {
-               case MTD_MODE_OTP_FACTORY:
-               case MTD_MODE_OTP_USER:
+               case MTD_FILE_MODE_OTP_FACTORY:
+               case MTD_FILE_MODE_OTP_USER:
                        ret = otp_select_filemode(mfi, arg);
                        break;
 
-               case MTD_MODE_RAW:
-                       if (!mtd->read_oob || !mtd->write_oob)
+               case MTD_FILE_MODE_RAW:
+                       if (!mtd_has_oob(mtd))
                                return -EOPNOTSUPP;
                        mfi->mode = arg;
 
-               case MTD_MODE_NORMAL:
+               case MTD_FILE_MODE_NORMAL:
                        break;
                default:
                        ret = -EINVAL;
@@ -897,6 +998,20 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
                break;
        }
 
+       case BLKPG:
+       {
+               ret = mtdchar_blkpg_ioctl(mtd,
+                     (struct blkpg_ioctl_arg __user *)arg);
+               break;
+       }
+
+       case BLKRRPART:
+       {
+               /* No reread partition feature. Just return ok */
+               ret = 0;
+               break;
+       }
+
        default:
                ret = -ENOTTY;
        }
@@ -904,13 +1019,13 @@ static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
        return ret;
 } /* memory_ioctl */
 
-static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
+static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
 {
        int ret;
 
-       lock_kernel();
-       ret = mtd_ioctl(file, cmd, arg);
-       unlock_kernel();
+       mutex_lock(&mtd_mutex);
+       ret = mtdchar_ioctl(file, cmd, arg);
+       mutex_unlock(&mtd_mutex);
 
        return ret;
 }
@@ -926,7 +1041,7 @@ struct mtd_oob_buf32 {
 #define MEMWRITEOOB32          _IOWR('M', 3, struct mtd_oob_buf32)
 #define MEMREADOOB32           _IOWR('M', 4, struct mtd_oob_buf32)
 
-static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
+static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
        unsigned long arg)
 {
        struct mtd_file_info *mfi = file->private_data;
@@ -934,7 +1049,7 @@ static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
        void __user *argp = compat_ptr(arg);
        int ret = 0;
 
-       lock_kernel();
+       mutex_lock(&mtd_mutex);
 
        switch (cmd) {
        case MEMWRITEOOB32:
@@ -945,7 +1060,7 @@ static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_writeoob(file, mtd, buf.start,
+                       ret = mtdchar_writeoob(file, mtd, buf.start,
                                buf.length, compat_ptr(buf.ptr),
                                &buf_user->length);
                break;
@@ -960,16 +1075,16 @@ static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
                if (copy_from_user(&buf, argp, sizeof(buf)))
                        ret = -EFAULT;
                else
-                       ret = mtd_do_readoob(mtd, buf.start,
+                       ret = mtdchar_readoob(file, mtd, buf.start,
                                buf.length, compat_ptr(buf.ptr),
                                &buf_user->start);
                break;
        }
        default:
-               ret = mtd_ioctl(file, cmd, (unsigned long)argp);
+               ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
        }
 
-       unlock_kernel();
+       mutex_unlock(&mtd_mutex);
 
        return ret;
 }
@@ -982,7 +1097,7 @@ static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
  *   mappings)
  */
 #ifndef CONFIG_MMU
-static unsigned long mtd_get_unmapped_area(struct file *file,
+static unsigned long mtdchar_get_unmapped_area(struct file *file,
                                           unsigned long addr,
                                           unsigned long len,
                                           unsigned long pgoff,
@@ -990,32 +1105,28 @@ static unsigned long mtd_get_unmapped_area(struct file *file,
 {
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
+       unsigned long offset;
+       int ret;
 
-       if (mtd->get_unmapped_area) {
-               unsigned long offset;
-
-               if (addr != 0)
-                       return (unsigned long) -EINVAL;
+       if (addr != 0)
+               return (unsigned long) -EINVAL;
 
-               if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
-                       return (unsigned long) -EINVAL;
+       if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
+               return (unsigned long) -EINVAL;
 
-               offset = pgoff << PAGE_SHIFT;
-               if (offset > mtd->size - len)
-                       return (unsigned long) -EINVAL;
+       offset = pgoff << PAGE_SHIFT;
+       if (offset > mtd->size - len)
+               return (unsigned long) -EINVAL;
 
-               return mtd->get_unmapped_area(mtd, len, offset, flags);
-       }
-
-       /* can't map directly */
-       return (unsigned long) -ENOSYS;
+       ret = mtd_get_unmapped_area(mtd, len, offset, flags);
+       return ret == -EOPNOTSUPP ? -ENOSYS : ret;
 }
 #endif
 
 /*
  * set up a mapping for shared memory segments
  */
-static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
+static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
 {
 #ifdef CONFIG_MMU
        struct mtd_file_info *mfi = file->private_data;
@@ -1056,55 +1167,38 @@ static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
 
 static const struct file_operations mtd_fops = {
        .owner          = THIS_MODULE,
-       .llseek         = mtd_lseek,
-       .read           = mtd_read,
-       .write          = mtd_write,
-       .unlocked_ioctl = mtd_unlocked_ioctl,
+       .llseek         = mtdchar_lseek,
+       .read           = mtdchar_read,
+       .write          = mtdchar_write,
+       .unlocked_ioctl = mtdchar_unlocked_ioctl,
 #ifdef CONFIG_COMPAT
-       .compat_ioctl   = mtd_compat_ioctl,
+       .compat_ioctl   = mtdchar_compat_ioctl,
 #endif
-       .open           = mtd_open,
-       .release        = mtd_close,
-       .mmap           = mtd_mmap,
+       .open           = mtdchar_open,
+       .release        = mtdchar_close,
+       .mmap           = mtdchar_mmap,
 #ifndef CONFIG_MMU
-       .get_unmapped_area = mtd_get_unmapped_area,
+       .get_unmapped_area = mtdchar_get_unmapped_area,
 #endif
 };
 
-static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
-                               const char *dev_name, void *data,
-                               struct vfsmount *mnt)
+static const struct super_operations mtd_ops = {
+       .drop_inode = generic_delete_inode,
+       .statfs = simple_statfs,
+};
+
+static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
+                               int flags, const char *dev_name, void *data)
 {
-        return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
-                             mnt);
+       return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
 }
 
 static struct file_system_type mtd_inodefs_type = {
        .name = "mtd_inodefs",
-       .get_sb = mtd_inodefs_get_sb,
+       .mount = mtd_inodefs_mount,
        .kill_sb = kill_anon_super,
 };
 
-static void mtdchar_notify_add(struct mtd_info *mtd)
-{
-}
-
-static void mtdchar_notify_remove(struct mtd_info *mtd)
-{
-       struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
-
-       if (mtd_ino) {
-               /* Destroy the inode if it exists */
-               mtd_ino->i_nlink = 0;
-               iput(mtd_ino);
-       }
-}
-
-static struct mtd_notifier mtdchar_notifier = {
-       .add = mtdchar_notify_add,
-       .remove = mtdchar_notify_remove,
-};
-
 static int __init init_mtdchar(void)
 {
        int ret;
@@ -1122,19 +1216,8 @@ static int __init init_mtdchar(void)
                pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
                goto err_unregister_chdev;
        }
-
-       mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
-       if (IS_ERR(mtd_inode_mnt)) {
-               ret = PTR_ERR(mtd_inode_mnt);
-               pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
-               goto err_unregister_filesystem;
-       }
-       register_mtd_user(&mtdchar_notifier);
-
        return ret;
 
-err_unregister_filesystem:
-       unregister_filesystem(&mtd_inodefs_type);
 err_unregister_chdev:
        __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
        return ret;
@@ -1142,8 +1225,6 @@ err_unregister_chdev:
 
 static void __exit cleanup_mtdchar(void)
 {
-       unregister_mtd_user(&mtdchar_notifier);
-       mntput(mtd_inode_mnt);
        unregister_filesystem(&mtd_inodefs_type);
        __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
 }