Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / mtdchar.c
index fe09cd2..f2f482b 100644 (file)
 #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 DEFINE_MUTEX(mtd_mutex);
-static struct vfsmount *mtd_inode_mnt __read_mostly;
 
 /*
  * Data structure to hold the pointer to the mtd device as well
@@ -75,7 +74,9 @@ static loff_t mtdchar_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 mtdchar_open(struct inode *inode, struct file *file)
 {
@@ -92,6 +93,10 @@ static int mtdchar_open(struct inode *inode, struct file *file)
        if ((file->f_mode & FMODE_WRITE) && (minor & 1))
                return -EACCES;
 
+       ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
+       if (ret)
+               return ret;
+
        mutex_lock(&mtd_mutex);
        mtd = get_mtd_device(NULL, devnum);
 
@@ -101,16 +106,14 @@ static int mtdchar_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;
@@ -122,25 +125,28 @@ static int mtdchar_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:
        mutex_unlock(&mtd_mutex);
+       simple_release_fs(&mnt, &count);
        return ret;
 } /* mtdchar_open */
 
@@ -154,7 +160,7 @@ static int mtdchar_close(struct inode *inode, struct file *file)
        pr_debug("MTD_close\n");
 
        /* Only sync if opened RW */
-       if ((file->f_mode & FMODE_WRITE) && mtd->sync)
+       if ((file->f_mode & FMODE_WRITE))
                mtd_sync(mtd);
 
        iput(mfi->ino);
@@ -162,6 +168,7 @@ static int mtdchar_close(struct inode *inode, struct file *file)
        put_mtd_device(mtd);
        file->private_data = NULL;
        kfree(mfi);
+       simple_release_fs(&mnt, &count);
 
        return 0;
 } /* mtdchar_close */
@@ -189,7 +196,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 {
        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;
@@ -310,10 +317,6 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
                        ret = -EROFS;
                        break;
                case MTD_FILE_MODE_OTP_USER:
-                       if (!mtd->write_user_prot_reg) {
-                               ret = -EOPNOTSUPP;
-                               break;
-                       }
                        ret = mtd_write_user_prot_reg(mtd, *ppos, len,
                                                      &retlen, kbuf);
                        break;
@@ -366,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_FILE_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_FILE_MODE_OTP_USER;
+               mfi->mode = MTD_FILE_MODE_OTP_USER;
                break;
        default:
                ret = -EINVAL;
@@ -407,7 +412,7 @@ static int mtdchar_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;
@@ -452,13 +457,8 @@ static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
        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->writesize - 1);
@@ -583,7 +583,7 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,
                        !access_ok(VERIFY_READ, req.usr_data, req.len) ||
                        !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
                return -EFAULT;
-       if (!mtd->write_oob)
+       if (!mtd->_write_oob)
                return -EOPNOTSUPP;
 
        ops.mode = req.mode;
@@ -821,10 +821,7 @@ static int mtdchar_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;
        }
 
@@ -835,10 +832,7 @@ static int mtdchar_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;
        }
 
@@ -849,10 +843,7 @@ static int mtdchar_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;
        }
 
@@ -883,10 +874,7 @@ static int mtdchar_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;
        }
 
@@ -896,10 +884,7 @@ static int mtdchar_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;
        }
 
@@ -924,17 +909,15 @@ static int mtdchar_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_FILE_MODE_OTP_FACTORY:
-                       if (mtd->get_fact_prot_info)
-                               ret = mtd_get_fact_prot_info(mtd, buf, 4096);
+                       ret = mtd_get_fact_prot_info(mtd, buf, 4096);
                        break;
                case MTD_FILE_MODE_OTP_USER:
-                       if (mtd->get_user_prot_info)
-                               ret = mtd_get_user_prot_info(mtd, buf, 4096);
+                       ret = mtd_get_user_prot_info(mtd, buf, 4096);
                        break;
                default:
+                       ret = -EINVAL;
                        break;
                }
                if (ret >= 0) {
@@ -958,8 +941,6 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
                        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);
                break;
        }
@@ -1004,7 +985,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
                        break;
 
                case MTD_FILE_MODE_RAW:
-                       if (!mtd->read_oob || !mtd->write_oob)
+                       if (!mtd_has_oob(mtd))
                                return -EOPNOTSUPP;
                        mfi->mode = arg;
 
@@ -1124,25 +1105,21 @@ static unsigned long mtdchar_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 (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
-                       return (unsigned long) -EINVAL;
+       if (addr != 0)
+               return (unsigned long) -EINVAL;
 
-               offset = pgoff << PAGE_SHIFT;
-               if (offset > mtd->size - len)
-                       return (unsigned long) -EINVAL;
+       if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
+               return (unsigned long) -EINVAL;
 
-               return mtd_get_unmapped_area(mtd, len, offset, flags);
-       }
+       offset = pgoff << PAGE_SHIFT;
+       if (offset > mtd->size - len)
+               return (unsigned long) -EINVAL;
 
-       /* can't map directly */
-       return (unsigned long) -ENOSYS;
+       ret = mtd_get_unmapped_area(mtd, len, offset, flags);
+       return ret == -EOPNOTSUPP ? -ENOSYS : ret;
 }
 #endif
 
@@ -1205,10 +1182,15 @@ static const struct file_operations mtd_fops = {
 #endif
 };
 
+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 mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
+       return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
 }
 
 static struct file_system_type mtd_inodefs_type = {
@@ -1217,26 +1199,6 @@ static struct file_system_type mtd_inodefs_type = {
        .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 */
-               clear_nlink(mtd_ino);
-               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;
@@ -1254,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;
@@ -1274,8 +1225,6 @@ err_unregister_chdev:
 
 static void __exit cleanup_mtdchar(void)
 {
-       unregister_mtd_user(&mtdchar_notifier);
-       kern_unmount(mtd_inode_mnt);
        unregister_filesystem(&mtd_inodefs_type);
        __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
 }