summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Elder <elder@inktank.com>2012-11-16 09:29:16 -0600
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-01-17 08:51:21 -0800
commit3317122449a2ce5c0bba77e1f05b1df2ff716447 (patch)
tree56de6b861daee00e1f6399e46c94f0fde7ddfdc1
parent1e411c66732294f5fb983323dafa7559796481f5 (diff)
downloadlinux-3.10-3317122449a2ce5c0bba77e1f05b1df2ff716447.tar.gz
linux-3.10-3317122449a2ce5c0bba77e1f05b1df2ff716447.tar.bz2
linux-3.10-3317122449a2ce5c0bba77e1f05b1df2ff716447.zip
rbd: do not allow remove of mounted-on image
There is no check in rbd_remove() to see if anybody holds open the image being removed. That's not cool. Add a simple open count that goes up and down with opens and closes (releases) of the device, and don't allow an rbd image to be removed if the count is non-zero. Protect the updates of the open count value with ctl_mutex to ensure the underlying rbd device doesn't get removed while concurrently being opened. Signed-off-by: Alex Elder <elder@inktank.com> Reviewed-by: Sage Weil <sage@inktank.com> (based on commit 42382b709bd1d143b9f0fa93e0a3a1f2f4210707)
-rw-r--r--drivers/block/rbd.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index d9ebe6844da..cba3d0278b8 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -189,6 +189,7 @@ struct rbd_device {
/* sysfs related */
struct device dev;
+ unsigned long open_count;
};
static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
@@ -249,8 +250,11 @@ static int rbd_open(struct block_device *bdev, fmode_t mode)
if ((mode & FMODE_WRITE) && rbd_dev->read_only)
return -EROFS;
+ mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
rbd_get_dev(rbd_dev);
set_device_ro(bdev, rbd_dev->read_only);
+ rbd_dev->open_count++;
+ mutex_unlock(&ctl_mutex);
return 0;
}
@@ -259,7 +263,11 @@ static int rbd_release(struct gendisk *disk, fmode_t mode)
{
struct rbd_device *rbd_dev = disk->private_data;
+ mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
+ BUG_ON(!rbd_dev->open_count);
+ rbd_dev->open_count--;
rbd_put_dev(rbd_dev);
+ mutex_unlock(&ctl_mutex);
return 0;
}
@@ -2448,6 +2456,11 @@ static ssize_t rbd_remove(struct bus_type *bus,
goto done;
}
+ if (rbd_dev->open_count) {
+ ret = -EBUSY;
+ goto done;
+ }
+
__rbd_remove_all_snaps(rbd_dev);
rbd_bus_del_dev(rbd_dev);