virtio_blk.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 bool 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. return true;
  58. }
  59. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  60. struct request *req)
  61. {
  62. unsigned long num, out, in;
  63. struct virtblk_req *vbr;
  64. vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  65. if (!vbr)
  66. /* When another request finishes we'll try again. */
  67. return false;
  68. vbr->req = req;
  69. if (blk_fs_request(vbr->req)) {
  70. vbr->out_hdr.type = 0;
  71. vbr->out_hdr.sector = vbr->req->sector;
  72. vbr->out_hdr.ioprio = vbr->req->ioprio;
  73. } else if (blk_pc_request(vbr->req)) {
  74. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  75. vbr->out_hdr.sector = 0;
  76. vbr->out_hdr.ioprio = vbr->req->ioprio;
  77. } else {
  78. /* We don't put anything else in the queue. */
  79. BUG();
  80. }
  81. if (blk_barrier_rq(vbr->req))
  82. vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
  83. /* This init could be done at vblk creation time */
  84. sg_init_table(vblk->sg, VIRTIO_MAX_SG);
  85. sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
  86. num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
  87. sg_set_buf(&vblk->sg[num+1], &vbr->in_hdr, sizeof(vbr->in_hdr));
  88. if (rq_data_dir(vbr->req) == WRITE) {
  89. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  90. out = 1 + num;
  91. in = 1;
  92. } else {
  93. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  94. out = 1;
  95. in = 1 + num;
  96. }
  97. if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
  98. mempool_free(vbr, vblk->pool);
  99. return false;
  100. }
  101. list_add_tail(&vbr->list, &vblk->reqs);
  102. return true;
  103. }
  104. static void do_virtblk_request(struct request_queue *q)
  105. {
  106. struct virtio_blk *vblk = NULL;
  107. struct request *req;
  108. unsigned int issued = 0;
  109. while ((req = elv_next_request(q)) != NULL) {
  110. vblk = req->rq_disk->private_data;
  111. BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg));
  112. /* If this request fails, stop queue and wait for something to
  113. finish to restart it. */
  114. if (!do_req(q, vblk, req)) {
  115. blk_stop_queue(q);
  116. break;
  117. }
  118. blkdev_dequeue_request(req);
  119. issued++;
  120. }
  121. if (issued)
  122. vblk->vq->vq_ops->kick(vblk->vq);
  123. }
  124. static int virtblk_ioctl(struct inode *inode, struct file *filp,
  125. unsigned cmd, unsigned long data)
  126. {
  127. return scsi_cmd_ioctl(filp, inode->i_bdev->bd_disk->queue,
  128. inode->i_bdev->bd_disk, cmd,
  129. (void __user *)data);
  130. }
  131. static struct block_device_operations virtblk_fops = {
  132. .ioctl = virtblk_ioctl,
  133. .owner = THIS_MODULE,
  134. };
  135. static int virtblk_probe(struct virtio_device *vdev)
  136. {
  137. struct virtio_blk *vblk;
  138. int err, major;
  139. void *token;
  140. unsigned int len;
  141. u64 cap;
  142. u32 v;
  143. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  144. if (!vblk) {
  145. err = -ENOMEM;
  146. goto out;
  147. }
  148. INIT_LIST_HEAD(&vblk->reqs);
  149. spin_lock_init(&vblk->lock);
  150. vblk->vdev = vdev;
  151. /* We expect one virtqueue, for output. */
  152. vblk->vq = vdev->config->find_vq(vdev, blk_done);
  153. if (IS_ERR(vblk->vq)) {
  154. err = PTR_ERR(vblk->vq);
  155. goto out_free_vblk;
  156. }
  157. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  158. if (!vblk->pool) {
  159. err = -ENOMEM;
  160. goto out_free_vq;
  161. }
  162. major = register_blkdev(0, "virtblk");
  163. if (major < 0) {
  164. err = major;
  165. goto out_mempool;
  166. }
  167. /* FIXME: How many partitions? How long is a piece of string? */
  168. vblk->disk = alloc_disk(1 << 4);
  169. if (!vblk->disk) {
  170. err = -ENOMEM;
  171. goto out_unregister_blkdev;
  172. }
  173. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  174. if (!vblk->disk->queue) {
  175. err = -ENOMEM;
  176. goto out_put_disk;
  177. }
  178. sprintf(vblk->disk->disk_name, "vd%c", virtblk_index++);
  179. vblk->disk->major = major;
  180. vblk->disk->first_minor = 0;
  181. vblk->disk->private_data = vblk;
  182. vblk->disk->fops = &virtblk_fops;
  183. /* If barriers are supported, tell block layer that queue is ordered */
  184. token = vdev->config->find(vdev, VIRTIO_CONFIG_BLK_F, &len);
  185. if (virtio_use_bit(vdev, token, len, VIRTIO_BLK_F_BARRIER))
  186. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  187. err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_CAPACITY, &cap);
  188. if (err) {
  189. dev_err(&vdev->dev, "Bad/missing capacity in config\n");
  190. goto out_cleanup_queue;
  191. }
  192. /* If capacity is too big, truncate with warning. */
  193. if ((sector_t)cap != cap) {
  194. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  195. (unsigned long long)cap);
  196. cap = (sector_t)-1;
  197. }
  198. set_capacity(vblk->disk, cap);
  199. err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SIZE_MAX, &v);
  200. if (!err)
  201. blk_queue_max_segment_size(vblk->disk->queue, v);
  202. else if (err != -ENOENT) {
  203. dev_err(&vdev->dev, "Bad SIZE_MAX in config\n");
  204. goto out_cleanup_queue;
  205. }
  206. err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SEG_MAX, &v);
  207. if (!err)
  208. blk_queue_max_hw_segments(vblk->disk->queue, v);
  209. else if (err != -ENOENT) {
  210. dev_err(&vdev->dev, "Bad SEG_MAX in config\n");
  211. goto out_cleanup_queue;
  212. }
  213. add_disk(vblk->disk);
  214. return 0;
  215. out_cleanup_queue:
  216. blk_cleanup_queue(vblk->disk->queue);
  217. out_put_disk:
  218. put_disk(vblk->disk);
  219. out_unregister_blkdev:
  220. unregister_blkdev(major, "virtblk");
  221. out_mempool:
  222. mempool_destroy(vblk->pool);
  223. out_free_vq:
  224. vdev->config->del_vq(vblk->vq);
  225. out_free_vblk:
  226. kfree(vblk);
  227. out:
  228. return err;
  229. }
  230. static void virtblk_remove(struct virtio_device *vdev)
  231. {
  232. struct virtio_blk *vblk = vdev->priv;
  233. int major = vblk->disk->major;
  234. BUG_ON(!list_empty(&vblk->reqs));
  235. blk_cleanup_queue(vblk->disk->queue);
  236. put_disk(vblk->disk);
  237. unregister_blkdev(major, "virtblk");
  238. mempool_destroy(vblk->pool);
  239. /* There should be nothing in the queue now, so no need to shutdown */
  240. vdev->config->del_vq(vblk->vq);
  241. kfree(vblk);
  242. }
  243. static struct virtio_device_id id_table[] = {
  244. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  245. { 0 },
  246. };
  247. static struct virtio_driver virtio_blk = {
  248. .driver.name = KBUILD_MODNAME,
  249. .driver.owner = THIS_MODULE,
  250. .id_table = id_table,
  251. .probe = virtblk_probe,
  252. .remove = __devexit_p(virtblk_remove),
  253. };
  254. static int __init init(void)
  255. {
  256. return register_virtio_driver(&virtio_blk);
  257. }
  258. static void __exit fini(void)
  259. {
  260. unregister_virtio_driver(&virtio_blk);
  261. }
  262. module_init(init);
  263. module_exit(fini);
  264. MODULE_DEVICE_TABLE(virtio, id_table);
  265. MODULE_DESCRIPTION("Virtio block driver");
  266. MODULE_LICENSE("GPL");