[PATCH v3] mtd: Do not corrupt backing device of device node inode

Previous thread: [PATCH V2] ubi: init even if mtd device cannot be attached, if built into kernel by Marc Kleine-Budde on Wednesday, May 5, 2010 - 1:17 am. (2 messages)

Next thread: [GIT]: Networking by David Miller on Wednesday, May 5, 2010 - 1:27 am. (5 messages)
From: Kirill A. Shutemov
Date: Wednesday, May 5, 2010 - 1:21 am

We cannot modify file->f_mapping->backing_dev_info, because it will corrupt
backing device of device node inode, since file->f_mapping is equal to
inode->i_mapping (see __dentry_open() in fs/open.c).

Let's introduce separate inode for MTD device with appropriate backing
device.

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
Acked-by: Jan Kara <jack@suse.cz>

---
Changelog
 v2 -> v3:
  - Rebase to mtd.git.
 v1 -> v2:
  - Fix error handling based on comments by Jan Kara.

---
 drivers/mtd/mtdchar.c   |   77 ++++++++++++++++++++++++++++++++++++++++++-----
 drivers/mtd/mtdcore.c   |    3 ++
 include/linux/mtd/mtd.h |    3 ++
 3 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index c355491..a2bb3ce 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -15,12 +15,15 @@
 #include <linux/smp_lock.h>
 #include <linux/backing-dev.h>
 #include <linux/compat.h>
+#include <linux/mount.h>
 
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/compatmac.h>
 
 #include <asm/uaccess.h>
 
+#define MTD_INODE_FS_MAGIC 0x11307854
+static struct vfsmount *mtd_inode_mnt __read_mostly;
 
 /*
  * Data structure to hold the pointer to the mtd device as well
@@ -85,11 +88,30 @@ static int mtd_open(struct inode *inode, struct file *file)
 		goto out;
 	}
 
-	if (mtd->backing_dev_info)
-		file->f_mapping->backing_dev_info = mtd->backing_dev_info;
+	if (!mtd->inode) {
+		mtd->inode = new_inode(mtd_inode_mnt->mnt_sb);
+		if (!mtd->inode) {
+			put_mtd_device(mtd);
+			ret = -ENOMEM;
+			goto out;
+		}
+		mtd->inode->i_mode = S_IFCHR;
+		mtd->inode->i_rdev = inode->i_rdev;
+		if (mtd->backing_dev_info) {
+			mtd->inode->i_data.backing_dev_info =
+				mtd->backing_dev_info;
+		}
+	}
+
+	spin_lock(&inode_lock);
+	__iget(mtd->inode);
+	spin_unlock(&inode_lock);
+
+	file->f_mapping = mtd->inode->i_mapping;
 
 	/* You can't open it RW if it's not a writeable device */
 	if ((file->f_mode & ...
From: Artem Bityutskiy
Date: Wednesday, May 5, 2010 - 7:17 am

I get the following build error:

[dedekind@eru l2-mtd-2.6]$ make -j8
O=/home/dedekind/tmp/l2-mtd-2.6-x86_64/
  GEN     /home/dedekind/tmp/l2-mtd-2.6-x86_64/Makefile
  CHK     include/linux/version.h
  CHK     include/generated/utsrelease.h
  Using /home/dedekind/git/l2-mtd-2.6 as source for kernel
  CALL    /home/dedekind/git/l2-mtd-2.6/scripts/checksyscalls.sh
  CHK     include/generated/compile.h
  CHK     include/linux/version.h
make[3]: `scripts/unifdef' is up to date.
  Building modules, stage 2.
Kernel: arch/x86/boot/bzImage is ready  (#2)
  MODPOST 146 modules
ERROR: "__iget" [drivers/mtd/mtdchar.ko] undefined!
ERROR: "inode_lock" [drivers/mtd/mtdchar.ko] undefined!
make[2]: *** [__modpost] Error 1
make[1]: *** [modules] Error 2
make: *** [sub-make] Error 2

__iget and inode_lock are not exported...

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

--

From: Jan Kara
Date: Wednesday, May 5, 2010 - 7:27 am

Ah, right. igrab() should be used instead of __iget inside the
inode_lock.

								Honza

-- 
Jan Kara <jack@suse.cz>
SUSE Labs, CR
--

Previous thread: [PATCH V2] ubi: init even if mtd device cannot be attached, if built into kernel by Marc Kleine-Budde on Wednesday, May 5, 2010 - 1:17 am. (2 messages)

Next thread: [GIT]: Networking by David Miller on Wednesday, May 5, 2010 - 1:27 am. (5 messages)