Commit 5087a50e authored by Michael S. Tsirkin's avatar Michael S. Tsirkin Committed by Rusty Russell

virtio-blk: use ida to allocate disk index

Based on a patch by Mark Wu <dwu@redhat.com>

Current index allocation in virtio-blk is based on a monotonically
increasing variable "index". This means we'll run out of numbers
after a while.  It also could cause confusion about the disk
name in the case of hot-plugging disks.
Change virtio-blk to use ida to allocate index, instead.
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent edfd52e6
...@@ -8,10 +8,13 @@ ...@@ -8,10 +8,13 @@
#include <linux/scatterlist.h> #include <linux/scatterlist.h>
#include <linux/string_helpers.h> #include <linux/string_helpers.h>
#include <scsi/scsi_cmnd.h> #include <scsi/scsi_cmnd.h>
#include <linux/idr.h>
#define PART_BITS 4 #define PART_BITS 4
static int major, index; static int major;
static DEFINE_IDA(vd_index_ida);
struct workqueue_struct *virtblk_wq; struct workqueue_struct *virtblk_wq;
struct virtio_blk struct virtio_blk
...@@ -35,6 +38,9 @@ struct virtio_blk ...@@ -35,6 +38,9 @@ struct virtio_blk
/* What host tells us, plus 2 for header & tailer. */ /* What host tells us, plus 2 for header & tailer. */
unsigned int sg_elems; unsigned int sg_elems;
/* Ida index - used to track minor number allocations. */
int index;
/* Scatterlist: can be too big for stack. */ /* Scatterlist: can be too big for stack. */
struct scatterlist sg[/*sg_elems*/]; struct scatterlist sg[/*sg_elems*/];
}; };
...@@ -276,6 +282,11 @@ static int index_to_minor(int index) ...@@ -276,6 +282,11 @@ static int index_to_minor(int index)
return index << PART_BITS; return index << PART_BITS;
} }
static int minor_to_index(int minor)
{
return minor >> PART_BITS;
}
static ssize_t virtblk_serial_show(struct device *dev, static ssize_t virtblk_serial_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
...@@ -341,14 +352,17 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) ...@@ -341,14 +352,17 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
{ {
struct virtio_blk *vblk; struct virtio_blk *vblk;
struct request_queue *q; struct request_queue *q;
int err; int err, index;
u64 cap; u64 cap;
u32 v, blk_size, sg_elems, opt_io_size; u32 v, blk_size, sg_elems, opt_io_size;
u16 min_io_size; u16 min_io_size;
u8 physical_block_exp, alignment_offset; u8 physical_block_exp, alignment_offset;
if (index_to_minor(index) >= 1 << MINORBITS) err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
return -ENOSPC; GFP_KERNEL);
if (err < 0)
goto out;
index = err;
/* We need to know how many segments before we allocate. */ /* We need to know how many segments before we allocate. */
err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX, err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
...@@ -365,7 +379,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) ...@@ -365,7 +379,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL); sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
if (!vblk) { if (!vblk) {
err = -ENOMEM; err = -ENOMEM;
goto out; goto out_free_index;
} }
INIT_LIST_HEAD(&vblk->reqs); INIT_LIST_HEAD(&vblk->reqs);
...@@ -421,7 +435,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) ...@@ -421,7 +435,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
vblk->disk->private_data = vblk; vblk->disk->private_data = vblk;
vblk->disk->fops = &virtblk_fops; vblk->disk->fops = &virtblk_fops;
vblk->disk->driverfs_dev = &vdev->dev; vblk->disk->driverfs_dev = &vdev->dev;
index++; vblk->index = index;
/* configure queue flush support */ /* configure queue flush support */
if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH)) if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
...@@ -516,6 +530,8 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) ...@@ -516,6 +530,8 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
vdev->config->del_vqs(vdev); vdev->config->del_vqs(vdev);
out_free_vblk: out_free_vblk:
kfree(vblk); kfree(vblk);
out_free_index:
ida_simple_remove(&vd_index_ida, index);
out: out:
return err; return err;
} }
...@@ -523,6 +539,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) ...@@ -523,6 +539,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
static void __devexit virtblk_remove(struct virtio_device *vdev) static void __devexit virtblk_remove(struct virtio_device *vdev)
{ {
struct virtio_blk *vblk = vdev->priv; struct virtio_blk *vblk = vdev->priv;
int index = vblk->index;
flush_work(&vblk->config_work); flush_work(&vblk->config_work);
...@@ -538,6 +555,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev) ...@@ -538,6 +555,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
mempool_destroy(vblk->pool); mempool_destroy(vblk->pool);
vdev->config->del_vqs(vdev); vdev->config->del_vqs(vdev);
kfree(vblk); kfree(vblk);
ida_simple_remove(&vd_index_ida, index);
} }
static const struct virtio_device_id id_table[] = { static const struct virtio_device_id id_table[] = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment