mtd: compat_ioctl cleanup
Subsystems (2)
PATCH 7/14 mtd: compat_ioctl cleanup
From: Kevin Cernekee <[email protected]>
1) Move the MEMREADOOB/MEMWRITEOOB compat_ioctl wrappers from fs/compat_ioctl.c into mtdchar.c . Original request was here:
http://lkml.org/lkml/2009/4/1/295
2) Add missing COMPATIBLE_IOCTL lines, so that mtd-utils does not error out when running in 64/32 compatibility mode.
Signed-off-by Kevin Cernekee <[email protected]>
Cc David Woodhouse <[email protected]>
Signed-off-by Andrew Morton <[email protected]>
---
drivers/mtd/mtdchar.c | 255 ++++++++++++++++++++++++++-------------- fs/compat_ioctl.c | 51 +------- 2 files changed, 180 insertions(+), 126 deletions(-)
drivers/mtd/mtdchar.c renamed from drivers/mtd/mtdchar.c~mtd-compat_ioctl-cleanup +171 -84
@@ -14,6 +14,7 @@#include <linux/sched.h> #include <linux/smp_lock.h> #include <linux/backing-dev.h> +#include <linux/compat.h> #include <linux/mtd/mtd.h> #include <linux/mtd/compatmac.h>
@@ -355,6 +356,100 @@ static int otp_select_filemode(struct mt# define otp_select_filemode (f,m) -EOPNOTSUPP#endif +static int mtd_do_writeoob (struct file *file,struct mtd_info *mtd, +uint64_t start,uint32_t length,void __user *ptr, +uint32_t __user *retp) +{ +struct mtd_oob_ops ops; +uint32_t retlen; +int ret =0 ; + +if (!(file->f_mode &FMODE_WRITE )) +return -EPERM ; + +if (length >4096 ) +return -EINVAL ; + +if (!mtd->write_oob) + ret = -EOPNOTSUPP ; +else + ret =access_ok (VERIFY_READ , ptr, length) ?0 :EFAULT ; + +if (ret) +return ret; + + ops.ooblen = length; + ops.ooboffs = start & (mtd->oobsize -1 ); + ops.datbuf =NULL ; + ops.mode =MTD_OOB_PLACE ; + +if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) +return -EINVAL ; + + ops.oobbuf =kmalloc (length,GFP_KERNEL ); +if (!ops.oobbuf) +return -ENOMEM ; + +if (copy_from_user (ops.oobbuf, ptr, length)) { +kfree (ops.oobbuf); +return -EFAULT ; + } + + start &= ~((uint64_t )mtd->oobsize -1 ); + ret = mtd->write_oob (mtd, start, &ops); + +if (ops.oobretlen >0xFFFFFFFFU ) + ret = -EOVERFLOW ; + retlen = ops.oobretlen; +if (copy_to_user (retp, &retlen,sizeof (length))) + ret = -EFAULT ; + +kfree (ops.oobbuf); +return ret; +} + +static int mtd_do_readoob (struct mtd_info *mtd,uint64_t start, +uint32_t length,void __user *ptr,uint32_t __user *retp) +{ +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; + + ops.ooblen = length; + ops.ooboffs = start & (mtd->oobsize -1 ); + ops.datbuf =NULL ; + ops.mode =MTD_OOB_PLACE ; + +if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) +return -EINVAL ; + + ops.oobbuf =kmalloc (length,GFP_KERNEL ); +if (!ops.oobbuf) +return -ENOMEM ; + + start &= ~((uint64_t )mtd->oobsize -1 ); + ret = mtd->read_oob (mtd, start, &ops); + +if (put_user (ops.oobretlen, retp)) + ret = -EFAULT ; +else if (ops.oobretlen &©_to_user (ptr, ops.oobbuf, + ops.oobretlen)) + ret = -EFAULT ; + +kfree (ops.oobbuf); +return ret; +} +static int mtd_ioctl (struct inode *inode,struct file *file,u_int cmd,u_long arg) {
@@ -487,100 +582,28 @@ static int mtd_ioctl(struct inode *inodecase MEMWRITEOOB : {struct mtd_oob_buf buf; -struct mtd_oob_ops ops; -struct mtd_oob_buf __user *user_buf = argp; -uint32_t retlen; - -if (!(file->f_mode &FMODE_WRITE )) -return -EPERM ; - -if (copy_from_user (&buf, argp,sizeof (struct mtd_oob_buf ))) -return -EFAULT ; - -if (buf.length >4096 ) -return -EINVAL ; - -if (!mtd->write_oob) - ret = -EOPNOTSUPP ; -else - ret =access_ok (VERIFY_READ , buf.ptr, - buf.length) ?0 :EFAULT ; - -if (ret) -return ret; - - ops.ooblen = buf.length; - ops.ooboffs = buf.start & (mtd->oobsize -1 ); - ops.datbuf =NULL ; - ops.mode =MTD_OOB_PLACE ; - -if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) -return -EINVAL ; - - ops.oobbuf =kmalloc (buf.length,GFP_KERNEL ); -if (!ops.oobbuf) -return -ENOMEM ; - -if (copy_from_user (ops.oobbuf, buf.ptr, buf.length)) { -kfree (ops.oobbuf); -return -EFAULT ; - } +struct mtd_oob_buf __user *buf_user = argp; - buf.start &= ~(mtd->oobsize -1 ); - ret = mtd->write_oob (mtd, buf.start, &ops); - -if (ops.oobretlen >0xFFFFFFFFU ) - ret = -EOVERFLOW ; - retlen = ops.oobretlen; -if (copy_to_user (&user_buf->length, &retlen,sizeof (buf.length))) +/* NOTE: writes return length to buf_user->length */ +if (copy_from_user (&buf, argp,sizeof (buf))) ret = -EFAULT ; - -kfree (ops.oobbuf); +else + ret =mtd_do_writeoob (file, mtd, buf.start, buf.length, + buf.ptr, &buf_user->length);break ; - }case MEMREADOOB : {struct mtd_oob_buf buf; -struct mtd_oob_ops ops; - -if (copy_from_user (&buf, argp,sizeof (struct mtd_oob_buf ))) -return -EFAULT ; - -if (buf.length >4096 ) -return -EINVAL ; - -if (!mtd->read_oob) - ret = -EOPNOTSUPP ; -else - ret =access_ok (VERIFY_WRITE , buf.ptr, - buf.length) ?0 : -EFAULT ; -if (ret) -return ret; - - ops.ooblen = buf.length; - ops.ooboffs = buf.start & (mtd->oobsize -1 ); - ops.datbuf =NULL ; - ops.mode =MTD_OOB_PLACE ; - -if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) -return -EINVAL ; - - ops.oobbuf =kmalloc (buf.length,GFP_KERNEL ); -if (!ops.oobbuf) -return -ENOMEM ; - - buf.start &= ~(mtd->oobsize -1 ); - ret = mtd->read_oob (mtd, buf.start, &ops); +struct mtd_oob_buf __user *buf_user = argp; -if (put_user (ops.oobretlen, (uint32_t __user *)argp)) +/* NOTE: writes return length to buf_user->start */ +if (copy_from_user (&buf, argp,sizeof (buf))) ret = -EFAULT ; -else if (ops.oobretlen &©_to_user (buf.ptr, ops.oobbuf, - ops.oobretlen)) - ret = -EFAULT ; - -kfree (ops.oobbuf); +else + ret =mtd_do_readoob (mtd, buf.start, buf.length, + buf.ptr, &buf_user->start);break ; }
@@ -771,6 +794,67 @@ static int mtd_ioctl(struct inode *inodereturn ret; }/* memory_ioctl */ +#ifdef CONFIG_COMPAT + +struct mtd_oob_buf32 { +u_int32_t start; +u_int32_t length; +compat_caddr_t ptr;/* unsigned char* */ +}; + +#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, +unsigned long arg) +{ +struct mtd_file_info *mfi = file->private_data; +struct mtd_info *mtd = mfi->mtd; +void __user *argp = (void __user *)arg; +int ret =0 ; + +lock_kernel (); + +switch (cmd) { +case MEMWRITEOOB32 : + { +struct mtd_oob_buf32 buf; +struct mtd_oob_buf32 __user *buf_user = argp; + +if (copy_from_user (&buf, argp,sizeof (buf))) + ret = -EFAULT ; +else + ret =mtd_do_writeoob (file, mtd, buf.start, + buf.length,compat_ptr (buf.ptr), + &buf_user->length); +break ; + } + +case MEMREADOOB32 : + { +struct mtd_oob_buf32 buf; +struct mtd_oob_buf32 __user *buf_user = argp; + +/* NOTE: writes return length to buf->start */ +if (copy_from_user (&buf, argp,sizeof (buf))) + ret = -EFAULT ; +else + ret =mtd_do_readoob (mtd, buf.start, + buf.length,compat_ptr (buf.ptr), + &buf_user->start); +break ; + } +default : + ret = -ENOIOCTLCMD ; + } + +unlock_kernel (); + +return ret; +} + +#endif /* CONFIG_COMPAT */ +/* * try to determine where a shared mapping can be made * - only supported for NOMMU at the moment (MMU can't doesn't copy private
@@ -830,6 +914,9 @@ static const struct file_operations mtd_ .read = mtd_read, .write = mtd_write, .ioctl = mtd_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = mtd_compat_ioctl, +#endif .open = mtd_open, .release = mtd_close, .mmap = mtd_mmap,
fs/compat_ioctl.c renamed from fs/compat_ioctl.c~mtd-compat_ioctl-cleanup +9 -42
71 lines not on this page · raw message
_