virtio_blk.c 7.4 KB

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