blkfront: Fix blkfront backend switch race (bdev release)
authorDaniel Stodden <daniel.stodden@citrix.com>
Sat, 7 Aug 2010 16:45:12 +0000 (18:45 +0200)
committerJens Axboe <jaxboe@fusionio.com>
Sat, 7 Aug 2010 16:45:12 +0000 (18:45 +0200)
We cannot read backend state within bdev operations, because it risks
grabbing the state change before xenbus gets to do it.

Fixed by tracking deferral with a frontend switch to Closing. State
exposure isn't strictly necessary, but the backends won't mind.

For a 'clean' deferral this seems actually a more decent protocol than
raising errors.

Signed-off-by: Daniel Stodden <daniel.stodden@citrix.com>
Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
drivers/block/xen-blkfront.c

index 763a315712cc9a506e81832f6d53eba90d7be161..49862993f31e1e282387ccc909f312ef7109617a 100644 (file)
@@ -1142,31 +1142,48 @@ static int blkif_open(struct block_device *bdev, fmode_t mode)
        if (!err)
                ++info->users;
 
-       unlock_kernel();
 out:
+       unlock_kernel();
        return err;
 }
 
 static int blkif_release(struct gendisk *disk, fmode_t mode)
 {
        struct blkfront_info *info = disk->private_data;
+       struct block_device *bdev;
+       struct xenbus_device *xbdev;
+
        lock_kernel();
-       info->users--;
-       if (info->users == 0) {
-               /* Check whether we have been instructed to close.  We will
-                  have ignored this request initially, as the device was
-                  still mounted. */
-               struct xenbus_device *dev = info->xbdev;
-
-               if (!dev) {
-                       xlvbd_release_gendisk(info);
-                       kfree(info);
-               } else if (xenbus_read_driver_state(dev->otherend)
-                          == XenbusStateClosing && info->is_ready) {
-                       xlvbd_release_gendisk(info);
-                       xenbus_frontend_closed(dev);
-               }
+       if (--info->users)
+               goto out;
+
+       bdev = bdget_disk(disk, 0);
+       bdput(bdev);
+
+       /*
+        * Check if we have been instructed to close. We will have
+        * deferred this request, because the bdev was still open.
+        */
+
+       mutex_lock(&info->mutex);
+       xbdev = info->xbdev;
+
+       if (xbdev && xbdev->state == XenbusStateClosing) {
+               /* pending switch to state closed */
+               xlvbd_release_gendisk(info);
+               xenbus_frontend_closed(info->xbdev);
+       }
+
+       mutex_unlock(&info->mutex);
+
+       if (!xbdev) {
+               /* sudden device removal */
+               xlvbd_release_gendisk(info);
+               disk->private_data = NULL;
+               kfree(info);
        }
+
+out:
        unlock_kernel();
        return 0;
 }