2009-01-15 16:20:25

by Artem Bityutskiy

[permalink] [raw]
Subject: [PATCH] UBI: add ioctl compatibility

Hi Arnd,

would you please glance if this patch all-right?



From: Artem Bityutskiy <[email protected]>
Subject: [PATCH] UBI: add ioctl compatibility

UBI ioctl's do not work when running 64-bit kernel and 32-bit
user-land. Fix this by adding ioctl translation etries to
fs/compat_ioctl.c

Reported-by: Geert Uytterhoeven <[email protected]>
Signed-off-by: Artem Bityutskiy <[email protected]>
---
fs/compat_ioctl.c | 14 ++++++++++++++
1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/fs/compat_ioctl.c b/fs/compat_ioctl.c
index 5235c67..f1e00e1 100644
--- a/fs/compat_ioctl.c
+++ b/fs/compat_ioctl.c
@@ -95,6 +95,7 @@
#include <linux/sonet.h>
#include <linux/atm_suni.h>
#include <linux/mtd/mtd.h>
+#include <mtd/ubi-user.h>

#include <linux/usb.h>
#include <linux/usbdevice_fs.h>
@@ -2423,6 +2424,19 @@ COMPATIBLE_IOCTL(MEMGETREGIONCOUNT)
COMPATIBLE_IOCTL(MEMGETREGIONINFO)
COMPATIBLE_IOCTL(MEMGETBADBLOCK)
COMPATIBLE_IOCTL(MEMSETBADBLOCK)
+/* UBI */
+COMPATIBLE_IOCTL(UBI_IOCMKVOL)
+ULONG_IOCTL(UBI_IOCRMVOL)
+COMPATIBLE_IOCTL(UBI_IOCRSVOL)
+COMPATIBLE_IOCTL(UBI_IOCRNVOL)
+COMPATIBLE_IOCTL(UBI_IOCATT)
+ULONG_IOCTL(UBI_IOCDET)
+ULONG_IOCTL(UBI_IOCVOLUP)
+ULONG_IOCTL(UBI_IOCEBER)
+ULONG_IOCTL(UBI_IOCEBCH)
+ULONG_IOCTL(UBI_IOCEBMAP)
+ULONG_IOCTL(UBI_IOCEBUNMAP)
+ULONG_IOCTL(UBI_IOCEBISMAP)
/* NBD */
ULONG_IOCTL(NBD_SET_SOCK)
ULONG_IOCTL(NBD_SET_BLKSIZE)
--
1.6.0.6

--
Best regards,
Artem Bityutskiy (Битюцкий Артём)


2009-01-15 16:34:51

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH] UBI: add ioctl compatibility

On Thursday 15 January 2009, Artem Bityutskiy wrote:

> would you please glance if this patch all-right?

No, it's not. New ioctl numbers should not be added to fs/compat_ioctl.c
but rather to the file that implements the file operations (ubi/cdev.c).

The best way to do it would be to add functions that do

static long compat_vol_cdev_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
int ret;

arg = (unsigned long)compat_ptr(arg);

lock_kernel();
ret = vol_cdev_locked_ioctl(file->f_inode, file, cmd, arg);
unlock_kernel();

return ret;
}

static long vol_cdev_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
int ret;

lock_kernel();
ret = vol_cdev_locked_ioctl(file->f_inode, file, cmd, arg);
unlock_kernel();

return ret;
}

and then use these two functions as your unlocked_ioctl and compat_ioctl
methods in file_operations.

If you can prove that you don't rely on the BKL, you can also drop the
{un,}lock_kernel() calls.


> COMPATIBLE_IOCTL(MEMGETREGIONINFO)
> COMPATIBLE_IOCTL(MEMGETBADBLOCK)
> COMPATIBLE_IOCTL(MEMSETBADBLOCK)
> +/* UBI */
> +COMPATIBLE_IOCTL(UBI_IOCMKVOL)
> +ULONG_IOCTL(UBI_IOCRMVOL)
> +COMPATIBLE_IOCTL(UBI_IOCRSVOL)
> +COMPATIBLE_IOCTL(UBI_IOCRNVOL)
> +COMPATIBLE_IOCTL(UBI_IOCATT)
> +ULONG_IOCTL(UBI_IOCDET)
> +ULONG_IOCTL(UBI_IOCVOLUP)
> +ULONG_IOCTL(UBI_IOCEBER)
> +ULONG_IOCTL(UBI_IOCEBCH)
> +ULONG_IOCTL(UBI_IOCEBMAP)
> +ULONG_IOCTL(UBI_IOCEBUNMAP)
> +ULONG_IOCTL(UBI_IOCEBISMAP)

ULONG_IOCTL() would be wrong here, all your ioctl handlers expect
a pointer, not an unsigned long

Arnd <><

2009-01-16 11:09:21

by Artem Bityutskiy

[permalink] [raw]
Subject: Re: [PATCH] UBI: add ioctl compatibility

On Thu, 2009-01-15 at 17:34 +0100, Arnd Bergmann wrote:
> On Thursday 15 January 2009, Artem Bityutskiy wrote:
>
> > would you please glance if this patch all-right?
>
> No, it's not. New ioctl numbers should not be added to fs/compat_ioctl.c
> but rather to the file that implements the file operations (ubi/cdev.c).

Thank you Arnd. Here is the corrected patch. Tested on x68_64 + 32-bit
UBI tools.

>From 8d1e92e1e277e3f9fb51491b248f3c6cbfc86083 Mon Sep 17 00:00:00 2001
From: Artem Bityutskiy <[email protected]>
Date: Fri, 16 Jan 2009 14:08:35 +0200
Subject: [PATCH] UBI: add ioctl compatibility

UBI ioctl's do not work when running 64-bit kernel and 32-bit
user-land. Fix this by adding the compat_ioctl method.

Also, UBI serializes all ioctls, so more than one ioctl at a time,
and UBI does not seem to depend on anything else, so use
unlocked_ioctl instead of ioctl (no BKL needed).

Reported-by: Geert Uytterhoeven <[email protected]>

Signed-off-by: Artem Bityutskiy <[email protected]>
---
drivers/mtd/ubi/cdev.c | 41 ++++++++++++++++++++++-------------------
1 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index a4a6e9a..c5f8a91 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -402,8 +402,8 @@ static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
return count;
}

-static int vol_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
struct ubi_volume_desc *desc = file->private_data;
@@ -803,8 +803,8 @@ out_free:
return err;
}

-static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
struct ubi_device *ubi;
@@ -814,7 +814,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

- ubi = ubi_get_by_major(imajor(inode));
+ ubi = ubi_get_by_major(imajor(file->f_mapping->host));
if (!ubi)
return -ENODEV;

@@ -950,8 +950,8 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
return err;
}

-static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
void __user *argp = (void __user *)arg;
@@ -1029,24 +1029,27 @@ static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,

/* UBI control character device operations */
const struct file_operations ubi_ctrl_cdev_operations = {
- .ioctl = ctrl_cdev_ioctl,
- .owner = THIS_MODULE,
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = ctrl_cdev_ioctl,
+ .compat_ioctl = ctrl_cdev_ioctl,
};

/* UBI character device operations */
const struct file_operations ubi_cdev_operations = {
- .owner = THIS_MODULE,
- .ioctl = ubi_cdev_ioctl,
- .llseek = no_llseek,
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .unlocked_ioctl = ubi_cdev_ioctl,
+ .compat_ioctl = ubi_cdev_ioctl,
};

/* UBI volume character device operations */
const struct file_operations ubi_vol_cdev_operations = {
- .owner = THIS_MODULE,
- .open = vol_cdev_open,
- .release = vol_cdev_release,
- .llseek = vol_cdev_llseek,
- .read = vol_cdev_read,
- .write = vol_cdev_write,
- .ioctl = vol_cdev_ioctl,
+ .owner = THIS_MODULE,
+ .open = vol_cdev_open,
+ .release = vol_cdev_release,
+ .llseek = vol_cdev_llseek,
+ .read = vol_cdev_read,
+ .write = vol_cdev_write,
+ .unlocked_ioctl = vol_cdev_ioctl,
+ .compat_ioctl = vol_cdev_ioctl,
};
--
1.6.0.6



--
Best regards,
Artem Bityutskiy (Битюцкий Артём)

2009-01-16 12:24:53

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH] UBI: add ioctl compatibility

On Friday 16 January 2009, Artem Bityutskiy wrote:
> Thank you Arnd. Here is the corrected patch. Tested on x68_64 + 32-bit
> UBI tools.
>

Looks better now, but the compat_ versions are still missing the
compat_ptr() conversions. Those only make a difference on s390,
so you wouldn't see the problem on x86_64.

Arnd <><

2009-01-16 14:34:21

by Artem Bityutskiy

[permalink] [raw]
Subject: Re: [PATCH] UBI: add ioctl compatibility

On Fri, 2009-01-16 at 13:24 +0100, Arnd Bergmann wrote:
> On Friday 16 January 2009, Artem Bityutskiy wrote:
> > Thank you Arnd. Here is the corrected patch. Tested on x68_64 + 32-bit
> > UBI tools.
> >
>
> Looks better now, but the compat_ versions are still missing the
> compat_ptr() conversions. Those only make a difference on s390,
> so you wouldn't see the problem on x86_64.

Ok, here is the new patch.

From: Artem Bityutskiy <[email protected]>
Subject: [PATCH] UBI: add ioctl compatibility

UBI ioctl's do not work when running 64-bit kernel and 32-bit
user-land. Fix this by adding the compat_ioctl method.

Also, UBI serializes all ioctls, so more than one ioctl at a time
is not a problem. Amd UBI does not seem to depend on anything else,
so use unlocked_ioctl instead of ioctl (no BKL needed).

Reported-by: Geert Uytterhoeven <[email protected]>
Signed-off-by: Artem Bityutskiy <[email protected]>
---
drivers/mtd/ubi/cdev.c | 66 ++++++++++++++++++++++++++++++++++--------------
1 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index d99935c..820f50e 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -40,6 +40,7 @@
#include <linux/ioctl.h>
#include <linux/capability.h>
#include <linux/uaccess.h>
+#include <linux/compat.h>
#include <mtd/ubi-user.h>
#include <asm/div64.h>
#include "ubi.h"
@@ -401,8 +402,8 @@ static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
return count;
}

-static int vol_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
struct ubi_volume_desc *desc = file->private_data;
@@ -566,6 +567,14 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
return err;
}

+static long vol_cdev_compat_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ unsigned long translated_arg = (unsigned long)compat_ptr(arg);
+
+ return vol_cdev_ioctl(file, cmd, translated_arg);
+}
+
/**
* verify_mkvol_req - verify volume creation request.
* @ubi: UBI device description object
@@ -800,8 +809,8 @@ out_free:
return err;
}

-static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
struct ubi_device *ubi;
@@ -811,7 +820,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

- ubi = ubi_get_by_major(imajor(inode));
+ ubi = ubi_get_by_major(imajor(file->f_mapping->host));
if (!ubi)
return -ENODEV;

@@ -947,8 +956,16 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
return err;
}

-static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long ubi_cdev_compat_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ unsigned long translated_arg = (unsigned long)compat_ptr(arg);
+
+ return ubi_cdev_ioctl(file, cmd, translated_arg);
+}
+
+static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
{
int err = 0;
void __user *argp = (void __user *)arg;
@@ -1024,26 +1041,37 @@ static int ctrl_cdev_ioctl(struct inode *inode, struct file *file,
return err;
}

+static long ctrl_cdev_compat_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ unsigned long translated_arg = (unsigned long)compat_ptr(arg);
+
+ return ctrl_cdev_ioctl(file, cmd, translated_arg);
+}
+
/* UBI control character device operations */
const struct file_operations ubi_ctrl_cdev_operations = {
- .ioctl = ctrl_cdev_ioctl,
- .owner = THIS_MODULE,
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = ctrl_cdev_ioctl,
+ .compat_ioctl = ctrl_cdev_compat_ioctl,
};

/* UBI character device operations */
const struct file_operations ubi_cdev_operations = {
- .owner = THIS_MODULE,
- .ioctl = ubi_cdev_ioctl,
- .llseek = no_llseek,
+ .owner = THIS_MODULE,
+ .llseek = no_llseek,
+ .unlocked_ioctl = ubi_cdev_ioctl,
+ .compat_ioctl = ubi_cdev_compat_ioctl,
};

/* UBI volume character device operations */
const struct file_operations ubi_vol_cdev_operations = {
- .owner = THIS_MODULE,
- .open = vol_cdev_open,
- .release = vol_cdev_release,
- .llseek = vol_cdev_llseek,
- .read = vol_cdev_read,
- .write = vol_cdev_write,
- .ioctl = vol_cdev_ioctl,
+ .owner = THIS_MODULE,
+ .open = vol_cdev_open,
+ .release = vol_cdev_release,
+ .llseek = vol_cdev_llseek,
+ .read = vol_cdev_read,
+ .write = vol_cdev_write,
+ .unlocked_ioctl = vol_cdev_ioctl,
+ .compat_ioctl = vol_cdev_compat_ioctl,
};
--
1.6.0.6

--
Best regards,
Artem Bityutskiy (Битюцкий Артём)

2009-01-16 17:26:09

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH] UBI: add ioctl compatibility

On Friday 16 January 2009, Artem Bityutskiy wrote:
> From: Artem Bityutskiy <[email protected]>
> Subject: [PATCH] UBI: add ioctl compatibility
>
> UBI ioctl's do not work when running 64-bit kernel and 32-bit
> user-land. Fix this by adding the compat_ioctl method.
>
> Also, UBI serializes all ioctls, so more than one ioctl at a time
> is not a problem. Amd UBI does not seem to depend on anything else,
> so use unlocked_ioctl instead of ioctl (no BKL needed).
>
> Reported-by: Geert Uytterhoeven <[email protected]>
> Signed-off-by: Artem Bityutskiy <[email protected]>

Looks good now, thanks!

Reviewed-by: Arnd Bergmann <[email protected]>