mtd: fix bdev exclusive open bugs in block2mtd::add_device()
authorTejun Heo <tj@kernel.org>
Sat, 13 Nov 2010 10:55:16 +0000 (11:55 +0100)
committerTejun Heo <tj@kernel.org>
Sat, 13 Nov 2010 10:55:16 +0000 (11:55 +0100)
There are two bdev exclusive open bugs.

* open_bdev_exclusive() must not be called with NULL holder.  Use dev
  as the holder.

* open_by_devnum() doesn't open the bdev exclusively but
  block2mtd_free_device() always assumes it.  Explicitly claim the
  bdev.

The latter is rather clumsy but will be simplified with future
blkdev_get/put() cleanups.

- Updated to use local variable @mode to cache FMODE_* masks as
  suggested by Artem Bityutskiy.

Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: linux-mtd@lists.infradead.org
Cc: Artem Bityutskiy <dedekind1@gmail.com>
drivers/mtd/devices/block2mtd.c

index 2cf0cc6a418931897d7149978125041172522e9c..a9e2d3b38aebc6cde5bfc36a80361d6c459e2091 100644 (file)
@@ -234,6 +234,7 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
 /* FIXME: ensure that mtd->size % erase_size == 0 */
 static struct block2mtd_dev *add_device(char *devname, int erase_size)
 {
+       const fmode_t mode = FMODE_READ | FMODE_WRITE;
        struct block_device *bdev;
        struct block2mtd_dev *dev;
        char *name;
@@ -246,7 +247,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
                return NULL;
 
        /* Get a handle on the device */
-       bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
+       bdev = open_bdev_exclusive(devname, mode, dev);
 #ifndef MODULE
        if (IS_ERR(bdev)) {
 
@@ -255,7 +256,15 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
 
                dev_t devt = name_to_dev_t(devname);
                if (devt) {
-                       bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
+                       bdev = open_by_devnum(devt, mode);
+                       if (!IS_ERR(bdev)) {
+                               int ret;
+                               ret = bd_claim(bdev, dev);
+                               if (ret) {
+                                       blkdev_put(bdev, mode);
+                                       bdev = ERR_PTR(ret);
+                               }
+                       }
                }
        }
 #endif