linux/drivers/block/virtio_blk.c
<<
>>
Prefs
   1//#define DEBUG
   2#include <linux/spinlock.h>
   3#include <linux/blkdev.h>
   4#include <linux/hdreg.h>
   5#include <linux/virtio.h>
   6#include <linux/virtio_blk.h>
   7#include <linux/scatterlist.h>
   8
   9#define VIRTIO_MAX_SG   (3+MAX_PHYS_SEGMENTS)
  10#define PART_BITS 4
  11
  12static int major, index;
  13
  14struct virtio_blk
  15{
  16        spinlock_t lock;
  17
  18        struct virtio_device *vdev;
  19        struct virtqueue *vq;
  20
  21        /* The disk structure for the kernel. */
  22        struct gendisk *disk;
  23
  24        /* Request tracking. */
  25        struct list_head reqs;
  26
  27        mempool_t *pool;
  28
  29        /* Scatterlist: can be too big for stack. */
  30        struct scatterlist sg[VIRTIO_MAX_SG];
  31};
  32
  33struct virtblk_req
  34{
  35        struct list_head list;
  36        struct request *req;
  37        struct virtio_blk_outhdr out_hdr;
  38        u8 status;
  39};
  40
  41static void blk_done(struct virtqueue *vq)
  42{
  43        struct virtio_blk *vblk = vq->vdev->priv;
  44        struct virtblk_req *vbr;
  45        unsigned int len;
  46        unsigned long flags;
  47
  48        spin_lock_irqsave(&vblk->lock, flags);
  49        while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
  50                int uptodate;
  51                switch (vbr->status) {
  52                case VIRTIO_BLK_S_OK:
  53                        uptodate = 1;
  54                        break;
  55                case VIRTIO_BLK_S_UNSUPP:
  56                        uptodate = -ENOTTY;
  57                        break;
  58                default:
  59                        uptodate = 0;
  60                        break;
  61                }
  62
  63                end_dequeued_request(vbr->req, uptodate);
  64                list_del(&vbr->list);
  65                mempool_free(vbr, vblk->pool);
  66        }
  67        /* In case queue is stopped waiting for more buffers. */
  68        blk_start_queue(vblk->disk->queue);
  69        spin_unlock_irqrestore(&vblk->lock, flags);
  70}
  71
  72static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  73                   struct request *req)
  74{
  75        unsigned long num, out, in;
  76        struct virtblk_req *vbr;
  77
  78        vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  79        if (!vbr)
  80                /* When another request finishes we'll try again. */
  81                return false;
  82
  83        vbr->req = req;
  84        if (blk_fs_request(vbr->req)) {
  85                vbr->out_hdr.type = 0;
  86                vbr->out_hdr.sector = vbr->req->sector;
  87                vbr->out_hdr.ioprio = vbr->req->ioprio;
  88        } else if (blk_pc_request(vbr->req)) {
  89                vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  90                vbr->out_hdr.sector = 0;
  91                vbr->out_hdr.ioprio = vbr->req->ioprio;
  92        } else {
  93                /* We don't put anything else in the queue. */
  94                BUG();
  95        }
  96
  97        if (blk_barrier_rq(vbr->req))
  98                vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
  99
 100        /* This init could be done at vblk creation time */
 101        sg_init_table(vblk->sg, VIRTIO_MAX_SG);
 102        sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
 103        num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
 104        sg_set_buf(&vblk->sg[num+1], &vbr->status, sizeof(vbr->status));
 105
 106        if (rq_data_dir(vbr->req) == WRITE) {
 107                vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
 108                out = 1 + num;
 109                in = 1;
 110        } else {
 111                vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
 112                out = 1;
 113                in = 1 + num;
 114        }
 115
 116        if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
 117                mempool_free(vbr, vblk->pool);
 118                return false;
 119        }
 120
 121        list_add_tail(&vbr->list, &vblk->reqs);
 122        return true;
 123}
 124
 125static void do_virtblk_request(struct request_queue *q)
 126{
 127        struct virtio_blk *vblk = NULL;
 128        struct request *req;
 129        unsigned int issued = 0;
 130
 131        while ((req = elv_next_request(q)) != NULL) {
 132                vblk = req->rq_disk->private_data;
 133                BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
 134
 135                /* If this request fails, stop queue and wait for something to
 136                   finish to restart it. */
 137                if (!do_req(q, vblk, req)) {
 138                        blk_stop_queue(q);
 139                        break;
 140                }
 141                blkdev_dequeue_request(req);
 142                issued++;
 143        }
 144
 145        if (issued)
 146                vblk->vq->vq_ops->kick(vblk->vq);
 147}
 148
 149static int virtblk_ioctl(struct inode *inode, struct file *filp,
 150                         unsigned cmd, unsigned long data)
 151{
 152        return scsi_cmd_blk_ioctl(filp, inode->i_bdev, cmd,
 153                              (void __user *)data);
 154}
 155
 156/* We provide getgeo only to please some old bootloader/partitioning tools */
 157static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
 158{
 159        struct virtio_blk *vblk = bd->bd_disk->private_data;
 160        struct virtio_blk_geometry vgeo;
 161        int err;
 162
 163        /* see if the host passed in geometry config */
 164        err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
 165                                offsetof(struct virtio_blk_config, geometry),
 166                                &vgeo);
 167
 168        if (!err) {
 169                geo->heads = vgeo.heads;
 170                geo->sectors = vgeo.sectors;
 171                geo->cylinders = vgeo.cylinders;
 172        } else {
 173                /* some standard values, similar to sd */
 174                geo->heads = 1 << 6;
 175                geo->sectors = 1 << 5;
 176                geo->cylinders = get_capacity(bd->bd_disk) >> 11;
 177        }
 178        return 0;
 179}
 180
 181static struct block_device_operations virtblk_fops = {
 182        .ioctl  = virtblk_ioctl,
 183        .owner  = THIS_MODULE,
 184        .getgeo = virtblk_getgeo,
 185};
 186
 187static int index_to_minor(int index)
 188{
 189        return index << PART_BITS;
 190}
 191
 192static int virtblk_probe(struct virtio_device *vdev)
 193{
 194        struct virtio_blk *vblk;
 195        int err;
 196        u64 cap;
 197        u32 v;
 198        u32 blk_size;
 199
 200        if (index_to_minor(index) >= 1 << MINORBITS)
 201                return -ENOSPC;
 202
 203        vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
 204        if (!vblk) {
 205                err = -ENOMEM;
 206                goto out;
 207        }
 208
 209        INIT_LIST_HEAD(&vblk->reqs);
 210        spin_lock_init(&vblk->lock);
 211        vblk->vdev = vdev;
 212
 213        /* We expect one virtqueue, for output. */
 214        vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
 215        if (IS_ERR(vblk->vq)) {
 216                err = PTR_ERR(vblk->vq);
 217                goto out_free_vblk;
 218        }
 219
 220        vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
 221        if (!vblk->pool) {
 222                err = -ENOMEM;
 223                goto out_free_vq;
 224        }
 225
 226        /* FIXME: How many partitions?  How long is a piece of string? */
 227        vblk->disk = alloc_disk(1 << PART_BITS);
 228        if (!vblk->disk) {
 229                err = -ENOMEM;
 230                goto out_mempool;
 231        }
 232
 233        vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
 234        if (!vblk->disk->queue) {
 235                err = -ENOMEM;
 236                goto out_put_disk;
 237        }
 238
 239        if (index < 26) {
 240                sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
 241        } else if (index < (26 + 1) * 26) {
 242                sprintf(vblk->disk->disk_name, "vd%c%c",
 243                        'a' + index / 26 - 1, 'a' + index % 26);
 244        } else {
 245                const unsigned int m1 = (index / 26 - 1) / 26 - 1;
 246                const unsigned int m2 = (index / 26 - 1) % 26;
 247                const unsigned int m3 =  index % 26;
 248                sprintf(vblk->disk->disk_name, "vd%c%c%c",
 249                        'a' + m1, 'a' + m2, 'a' + m3);
 250        }
 251
 252        vblk->disk->major = major;
 253        vblk->disk->first_minor = index_to_minor(index);
 254        vblk->disk->private_data = vblk;
 255        vblk->disk->fops = &virtblk_fops;
 256        vblk->disk->driverfs_dev = &vdev->dev;
 257        index++;
 258
 259        /* If barriers are supported, tell block layer that queue is ordered */
 260        if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
 261                blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
 262
 263        /* If disk is read-only in the host, the guest should obey */
 264        if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
 265                set_disk_ro(vblk->disk, 1);
 266
 267        /* Host must always specify the capacity. */
 268        vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
 269                          &cap, sizeof(cap));
 270
 271        /* If capacity is too big, truncate with warning. */
 272        if ((sector_t)cap != cap) {
 273                dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
 274                         (unsigned long long)cap);
 275                cap = (sector_t)-1;
 276        }
 277        set_capacity(vblk->disk, cap);
 278
 279        /* Host can optionally specify maximum segment size and number of
 280         * segments. */
 281        err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
 282                                offsetof(struct virtio_blk_config, size_max),
 283                                &v);
 284        if (!err)
 285                blk_queue_max_segment_size(vblk->disk->queue, v);
 286
 287        err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
 288                                offsetof(struct virtio_blk_config, seg_max),
 289                                &v);
 290        if (!err)
 291                blk_queue_max_hw_segments(vblk->disk->queue, v);
 292
 293        /* Host can optionally specify the block size of the device */
 294        err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
 295                                offsetof(struct virtio_blk_config, blk_size),
 296                                &blk_size);
 297        if (!err)
 298                blk_queue_hardsect_size(vblk->disk->queue, blk_size);
 299
 300        add_disk(vblk->disk);
 301        return 0;
 302
 303out_put_disk:
 304        put_disk(vblk->disk);
 305out_mempool:
 306        mempool_destroy(vblk->pool);
 307out_free_vq:
 308        vdev->config->del_vq(vblk->vq);
 309out_free_vblk:
 310        kfree(vblk);
 311out:
 312        return err;
 313}
 314
 315static void virtblk_remove(struct virtio_device *vdev)
 316{
 317        struct virtio_blk *vblk = vdev->priv;
 318
 319        /* Nothing should be pending. */
 320        BUG_ON(!list_empty(&vblk->reqs));
 321
 322        /* Stop all the virtqueues. */
 323        vdev->config->reset(vdev);
 324
 325        del_gendisk(vblk->disk);
 326        blk_cleanup_queue(vblk->disk->queue);
 327        put_disk(vblk->disk);
 328        mempool_destroy(vblk->pool);
 329        vdev->config->del_vq(vblk->vq);
 330        kfree(vblk);
 331}
 332
 333static struct virtio_device_id id_table[] = {
 334        { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
 335        { 0 },
 336};
 337
 338static unsigned int features[] = {
 339        VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
 340        VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 341};
 342
 343static struct virtio_driver virtio_blk = {
 344        .feature_table = features,
 345        .feature_table_size = ARRAY_SIZE(features),
 346        .driver.name =  KBUILD_MODNAME,
 347        .driver.owner = THIS_MODULE,
 348        .id_table =     id_table,
 349        .probe =        virtblk_probe,
 350        .remove =       __devexit_p(virtblk_remove),
 351};
 352
 353static int __init init(void)
 354{
 355        major = register_blkdev(0, "virtblk");
 356        if (major < 0)
 357                return major;
 358        return register_virtio_driver(&virtio_blk);
 359}
 360
 361static void __exit fini(void)
 362{
 363        unregister_blkdev(major, "virtblk");
 364        unregister_virtio_driver(&virtio_blk);
 365}
 366module_init(init);
 367module_exit(fini);
 368
 369MODULE_DEVICE_TABLE(virtio, id_table);
 370MODULE_DESCRIPTION("Virtio block driver");
 371MODULE_LICENSE("GPL");
 372