All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Elder <elder@inktank.com>
To: "ceph-devel@vger.kernel.org" <ceph-devel@vger.kernel.org>
Subject: [PATCH 2/2] rbd: prevent open for image being removed
Date: Mon, 14 Jan 2013 12:51:03 -0600	[thread overview]
Message-ID: <50F45397.2030905@inktank.com> (raw)
In-Reply-To: <50F4535A.6030601@inktank.com>

An open request for a mapped rbd image can arrive while removal of
that mapping is underway.  The control mutex and an open count is
protect a mapped device that's in use from being removed.  But it
is possible for the removal of the mapping to reach the point of no
return *after* a racing open has concluded it is OK to proceed.  The
result of this is not good.

Define and use a flag to indicate a mapping is getting removed to
avoid this problem.

This addresses http://tracker.newdream.net/issues/3427

Signed-off-by: Alex Elder <elder@inktank.com>
---
 drivers/block/rbd.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 9eb1631..760f7f7 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -264,6 +264,7 @@ struct rbd_device {

 enum rbd_dev_flags {
 	rbd_dev_flag_exists,	/* mapped snapshot has not been deleted */
+	rbd_dev_flag_removing,	/* this mapping is being removed */
 };

 static DEFINE_MUTEX(ctl_mutex);	  /* Serialize open/close/setup/teardown */
@@ -351,17 +352,22 @@ static int rbd_dev_v2_refresh(struct rbd_device
*rbd_dev, u64 *hver);
 static int rbd_open(struct block_device *bdev, fmode_t mode)
 {
 	struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
+	int ret = 0;

 	if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
 		return -EROFS;

 	mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
-	(void) get_device(&rbd_dev->dev);
-	set_device_ro(bdev, rbd_dev->mapping.read_only);
-	rbd_dev->open_count++;
+	if (!test_bit(rbd_dev_flag_removing, &rbd_dev->flags)) {
+		(void) get_device(&rbd_dev->dev);
+		set_device_ro(bdev, rbd_dev->mapping.read_only);
+		rbd_dev->open_count++;
+	} else {
+		ret = -ENOENT;
+	}
 	mutex_unlock(&ctl_mutex);

-	return 0;
+	return ret;
 }

 static int rbd_release(struct gendisk *disk, fmode_t mode)
@@ -3796,6 +3802,7 @@ static ssize_t rbd_remove(struct bus_type *bus,
 		ret = -EBUSY;
 		goto done;
 	}
+	set_bit(rbd_dev_flag_removing, &rbd_dev->flags);

 	rbd_remove_all_snaps(rbd_dev);
 	rbd_bus_del_dev(rbd_dev);
-- 
1.7.9.5


  parent reply	other threads:[~2013-01-14 18:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-14 18:50 [PATCH 0/2] rbd: prevent open of image being unmapped Alex Elder
2013-01-14 18:50 ` [PATCH 1/2] rbd: define flags field, use it for exists flag Alex Elder
2013-01-14 20:32   ` Dan Mick
2013-01-14 21:23     ` Alex Elder
2013-01-15  6:09       ` Dan Mick
2013-01-16  1:08       ` Josh Durgin
2013-01-17 23:16         ` Alex Elder
2013-01-15 21:00   ` Dan Mick
2013-01-14 18:51 ` Alex Elder [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-01-28 22:08 [PATCH 0/2] rbd: manage racing opens/removes Alex Elder
2013-01-28 22:09 ` [PATCH 2/2] rbd: prevent open for image being removed Alex Elder
2013-01-30 19:52   ` Josh Durgin
2013-01-30 21:25     ` Alex Elder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50F45397.2030905@inktank.com \
    --to=elder@inktank.com \
    --cc=ceph-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.