Skip to contentSkip to conversation

UBI: add ioctl compatibility

PATCH In review Artem Bityutskiy 2 participants 6 messages Jan 15, 2009 – Jan 16, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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

---
 fs/compat_ioctl.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
fs/compat_ioctl.c +14 -0
index 5235c67..f1e00e1 100644
@@ -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)
Signature · 5 lines
1.6.0.6

-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#1 lore raw
Quoted 1 line · Artem Bityutskiy

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.

Quoted 6 lines

COMPATIBLE_IOCTL(MEMGETREGIONINFO)
COMPATIBLE_IOCTL(MEMGETBADBLOCK)
COMPATIBLE_IOCTL(MEMSETBADBLOCK)

fs/compat_ioctl.c +3 -0
+/* UBI */
+COMPATIBLE_IOCTL(UBI_IOCMKVOL)
+ULONG_IOCTL(UBI_IOCRMVOL)

fs/compat_ioctl.c COMPATIBLE_IOCTL(MEMGETREGIONCOUNT) · line 2439

Quoted 10 lines
fs/compat_ioctl.c +10 -0
+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 <><
#2 lore raw
Quoted 6 lines · Arnd Bergmann
Quoted 1 line · Artem Bityutskiy

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.

Quoted 1 line

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).

---
 drivers/mtd/ubi/cdev.c |   41 ++++++++++++++++++++++-------------------
 1 files changed, 22 insertions(+), 19 deletions(-)
drivers/mtd/ubi/cdev.c +22 -19
index a4a6e9a..c5f8a91 100644
@@ -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,
 };
Signature · 7 lines
1.6.0.6



-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)
#3 lore raw
Quoted 3 lines · Artem Bityutskiy

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 <><
#4 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 3 lines · Artem Bityutskiy

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).

---
 drivers/mtd/ubi/cdev.c |   66 ++++++++++++++++++++++++++++++++++--------------
 1 files changed, 47 insertions(+), 19 deletions(-)
drivers/mtd/ubi/cdev.c +47 -19
index d99935c..820f50e 100644
@@ -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,
 };
Signature · 5 lines
1.6.0.6

-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)
Reviewed-by
#5 lore raw
Quoted 6 lines · Artem Bityutskiy

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.

Quoted 6 lines

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).

Looks good now, thanks!