virtio_blk.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #define VIRTIO_MAX_SG (3+MAX_PHYS_SEGMENTS)
  9. static unsigned char virtblk_index = 'a';
  10. struct virtio_blk
  11. {
  12. spinlock_t lock;
  13. struct virtio_device *vdev;
  14. struct virtqueue *vq;
  15. /* The disk structure for the kernel. */
  16. struct gendisk *disk;
  17. /* Request tracking. */
  18. struct list_head reqs;
  19. mempool_t *pool;
  20. /* Scatterlist: can be too big for stack. */
  21. struct scatterlist sg[VIRTIO_MAX_SG];
  22. };
  23. struct virtblk_req
  24. {
  25. struct list_head list;
  26. struct request *req;
  27. struct virtio_blk_outhdr out_hdr;
  28. struct virtio_blk_inhdr in_hdr;
  29. };
  30. static void blk_done(struct virtqueue *vq)
  31. {
  32. struct virtio_blk *vblk = vq->vdev->priv;
  33. struct virtblk_req *vbr;
  34. unsigned int len;
  35. unsigned long flags;
  36. spin_lock_irqsave(&vblk->lock, flags);
  37. while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
  38. int uptodate;
  39. switch (vbr->in_hdr.status) {
  40. case VIRTIO_BLK_S_OK:
  41. uptodate = 1;
  42. break;
  43. case VIRTIO_BLK_S_UNSUPP:
  44. uptodate = -ENOTTY;
  45. break;
  46. default:
  47. uptodate = 0;
  48. break;
  49. }
  50. end_dequeued_request(vbr->req, uptodate);
  51. list_del(&vbr->list);
  52. mempool_free(vbr, vblk->pool);
  53. }
  54. /* In case queue is stopped waiting for more buffers. */
  55. blk_start_queue(vblk->disk->queue);
  56. spin_unlock_irqrestore(&vblk->lock, flags);
  57. }
  58. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  59. struct request *req)
  60. {
  61. unsigned long num, out, in;
  62. struct virtblk_req *vbr;
  63. vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  64. if (!vbr)
  65. /* When another request finishes we'll try again. */
  66. return false;
  67. vbr->req = req;
  68. if (blk_fs_request(vbr->req)) {
  69. vbr->out_hdr.type = 0;
  70. vbr->out_hdr.sector = vbr->req->sector;
  71. vbr->out_hdr.ioprio = vbr->req->ioprio;
  72. } else if (blk_pc_request(vbr->req)) {
  73. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  74. vbr->out_hdr.sector = 0;
  75. vbr->out_hdr.ioprio = vbr->req->ioprio;
  76. } else {
  77. /* We don't put anything else in the queue. */
  78. BUG();
  79. }
  80. if (blk_barrier_rq(vbr->req))
  81. vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
  82. /* This init could be done at vblk creation time */
  83. sg_init_table(vblk->sg, VIRTIO_MAX_SG);
  84. sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
  85. num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
  86. sg_set_buf(&vblk->sg[num+1], &vbr->in_hdr, sizeof(vbr->in_hdr));
  87. if (rq_data_dir(vbr->req) == WRITE) {
  88. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  89. out = 1 + num;
  90. in = 1;
  91. } else {
  92. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  93. out = 1;
  94. in = 1 + num;
  95. }
  96. if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
  97. mempool_free(vbr, vblk->pool);
  98. return false;
  99. }
  100. list_add_tail(&vbr->list, &vblk->reqs);
  101. return true;
  102. }
  103. static void do_virtblk_request(struct request_queue *q)
  104. {
  105. struct virtio_blk *vblk = NULL;
  106. struct request *req;
  107. unsigned int issued = 0;
  108. while ((req = elv_next_request(q)) != NULL) {
  109. vblk = req->rq_disk->private_data;
  110. BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
  111. /* If this request fails, stop queue and wait for something to
  112. finish to restart it. */
  113. if (!do_req(q, vblk, req)) {
  114. blk_stop_queue(q);
  115. break;
  116. }
  117. blkdev_dequeue_request(req);
  118. issued++;
  119. }
  120. if (issued)
  121. vblk->vq->vq_ops->kick(vblk->vq);
  122. }
  123. static int virtblk_ioctl(struct inode *inode, struct file *filp,
  124. unsigned cmd, unsigned long data)
  125. {
  126. return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue,
  127. inode->i_bdev->bd_disk, cmd,
  128. (void __user *)data);
  129. }
  130. static struct block_device_operations virtblk_fops = {
  131. .ioctl = virtblk_ioctl,
  132. .owner = THIS_MODULE,
  133. };
  134. static int virtblk_probe(struct virtio_device *vdev)
  135. {
  136. struct virtio_blk *vblk;
  137. int err, major;
  138. u64 cap;
  139. u32 v;
  140. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  141. if (!vblk) {
  142. err = -ENOMEM;
  143. goto out;
  144. }
  145. INIT_LIST_HEAD(&vblk->reqs);
  146. spin_lock_init(&vblk->lock);
  147. vblk->vdev = vdev;
  148. /* We expect one virtqueue, for output. */
  149. vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
  150. if (IS_ERR(vblk->vq)) {
  151. err = PTR_ERR(vblk->vq);
  152. goto out_free_vblk;
  153. }
  154. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  155. if (!vblk->pool) {
  156. err = -ENOMEM;
  157. goto out_free_vq;
  158. }
  159. major = register_blkdev(0, "virtblk");
  160. if (major < 0) {
  161. err = major;
  162. goto out_mempool;
  163. }
  164. /* FIXME: How many partitions? How long is a piece of string? */
  165. vblk->disk = alloc_disk(1 << 4);
  166. if (!vblk->disk) {
  167. err = -ENOMEM;
  168. goto out_unregister_blkdev;
  169. }
  170. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  171. if (!vblk->disk->queue) {
  172. err = -ENOMEM;
  173. goto out_put_disk;
  174. }
  175. sprintf(vblk->disk->disk_name, "vd%c", virtblk_index++);
  176. vblk->disk->major = major;
  177. vblk->disk->first_minor = 0;
  178. vblk->disk->private_data = vblk;
  179. vblk->disk->fops = &virtblk_fops;
  180. /* If barriers are supported, tell block layer that queue is ordered */
  181. if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER))
  182. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  183. /* Host must always specify the capacity. */
  184. __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity),
  185. &cap);
  186. /* If capacity is too big, truncate with warning. */
  187. if ((sector_t)cap != cap) {
  188. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  189. (unsigned long long)cap);
  190. cap = (sector_t)-1;
  191. }
  192. set_capacity(vblk->disk, cap);
  193. /* Host can optionally specify maximum segment size and number of
  194. * segments. */
  195. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  196. offsetof(struct virtio_blk_config, size_max),
  197. &v);
  198. if (!err)
  199. blk_queue_max_segment_size(vblk->disk->queue, v);
  200. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  201. offsetof(struct virtio_blk_config, seg_max),
  202. &v);
  203. if (!err)
  204. blk_queue_max_hw_segments(vblk->disk->queue, v);
  205. add_disk(vblk->disk);
  206. return 0;
  207. out_put_disk:
  208. put_disk(vblk->disk);
  209. out_unregister_blkdev:
  210. unregister_blkdev(major, "virtblk");
  211. out_mempool:
  212. mempool_destroy(vblk->pool);
  213. out_free_vq:
  214. vdev->config->del_vq(vblk->vq);
  215. out_free_vblk:
  216. kfree(vblk);
  217. out:
  218. return err;
  219. }
  220. static void virtblk_remove(struct virtio_device *vdev)
  221. {
  222. struct virtio_blk *vblk = vdev->priv;
  223. int major = vblk->disk->major;
  224. /* Nothing should be pending. */
  225. BUG_ON(!list_empty(&vblk->reqs));
  226. /* Stop all the virtqueues. */
  227. vdev->config->reset(vdev);
  228. blk_cleanup_queue(vblk->disk->queue);
  229. put_disk(vblk->disk);
  230. unregister_blkdev(major, "virtblk");
  231. mempool_destroy(vblk->pool);
  232. vdev->config->del_vq(vblk->vq);
  233. kfree(vblk);
  234. }
  235. static struct virtio_device_id id_table[] = {
  236. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  237. { 0 },
  238. };
  239. static struct virtio_driver virtio_blk = {
  240. .driver.name = KBUILD_MODNAME,
  241. .driver.owner = THIS_MODULE,
  242. .id_table = id_table,
  243. .probe = virtblk_probe,
  244. .remove = __devexit_p(virtblk_remove),
  245. };
  246. static int __init init(void)
  247. {
  248. return register_virtio_driver(&virtio_blk);
  249. }
  250. static void __exit fini(void)
  251. {
  252. unregister_virtio_driver(&virtio_blk);
  253. }
  254. module_init(init);
  255. module_exit(fini);
  256. MODULE_DEVICE_TABLE(virtio, id_table);
  257. MODULE_DESCRIPTION("Virtio block driver");
  258. MODULE_LICENSE("GPL");