virtio_blk.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 int major, index;
  11. struct virtio_blk
  12. {
  13. spinlock_t lock;
  14. struct virtio_device *vdev;
  15. struct virtqueue *vq;
  16. /* The disk structure for the kernel. */
  17. struct gendisk *disk;
  18. /* Request tracking. */
  19. struct list_head reqs;
  20. mempool_t *pool;
  21. /* Scatterlist: can be too big for stack. */
  22. struct scatterlist sg[VIRTIO_MAX_SG];
  23. };
  24. struct virtblk_req
  25. {
  26. struct list_head list;
  27. struct request *req;
  28. struct virtio_blk_outhdr out_hdr;
  29. u8 status;
  30. };
  31. static void blk_done(struct virtqueue *vq)
  32. {
  33. struct virtio_blk *vblk = vq->vdev->priv;
  34. struct virtblk_req *vbr;
  35. unsigned int len;
  36. unsigned long flags;
  37. spin_lock_irqsave(&vblk->lock, flags);
  38. while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
  39. int uptodate;
  40. switch (vbr->status) {
  41. case VIRTIO_BLK_S_OK:
  42. uptodate = 1;
  43. break;
  44. case VIRTIO_BLK_S_UNSUPP:
  45. uptodate = -ENOTTY;
  46. break;
  47. default:
  48. uptodate = 0;
  49. break;
  50. }
  51. end_dequeued_request(vbr->req, uptodate);
  52. list_del(&vbr->list);
  53. mempool_free(vbr, vblk->pool);
  54. }
  55. /* In case queue is stopped waiting for more buffers. */
  56. blk_start_queue(vblk->disk->queue);
  57. spin_unlock_irqrestore(&vblk->lock, flags);
  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->status, sizeof(vbr->status));
  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. /* We provide getgeo only to please some old bootloader/partitioning tools */
  132. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  133. {
  134. /* some standard values, similar to sd */
  135. geo->heads = 1 << 6;
  136. geo->sectors = 1 << 5;
  137. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  138. return 0;
  139. }
  140. static struct block_device_operations virtblk_fops = {
  141. .ioctl = virtblk_ioctl,
  142. .owner = THIS_MODULE,
  143. .getgeo = virtblk_getgeo,
  144. };
  145. static int index_to_minor(int index)
  146. {
  147. return index << PART_BITS;
  148. }
  149. static int virtblk_probe(struct virtio_device *vdev)
  150. {
  151. struct virtio_blk *vblk;
  152. int err;
  153. u64 cap;
  154. u32 v;
  155. if (index_to_minor(index) >= 1 << MINORBITS)
  156. return -ENOSPC;
  157. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  158. if (!vblk) {
  159. err = -ENOMEM;
  160. goto out;
  161. }
  162. INIT_LIST_HEAD(&vblk->reqs);
  163. spin_lock_init(&vblk->lock);
  164. vblk->vdev = vdev;
  165. /* We expect one virtqueue, for output. */
  166. vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
  167. if (IS_ERR(vblk->vq)) {
  168. err = PTR_ERR(vblk->vq);
  169. goto out_free_vblk;
  170. }
  171. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  172. if (!vblk->pool) {
  173. err = -ENOMEM;
  174. goto out_free_vq;
  175. }
  176. /* FIXME: How many partitions? How long is a piece of string? */
  177. vblk->disk = alloc_disk(1 << PART_BITS);
  178. if (!vblk->disk) {
  179. err = -ENOMEM;
  180. goto out_mempool;
  181. }
  182. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  183. if (!vblk->disk->queue) {
  184. err = -ENOMEM;
  185. goto out_put_disk;
  186. }
  187. if (index < 26) {
  188. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  189. } else if (index < (26 + 1) * 26) {
  190. sprintf(vblk->disk->disk_name, "vd%c%c",
  191. 'a' + index / 26 - 1, 'a' + index % 26);
  192. } else {
  193. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  194. const unsigned int m2 = (index / 26 - 1) % 26;
  195. const unsigned int m3 = index % 26;
  196. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  197. 'a' + m1, 'a' + m2, 'a' + m3);
  198. }
  199. vblk->disk->major = major;
  200. vblk->disk->first_minor = index_to_minor(index);
  201. vblk->disk->private_data = vblk;
  202. vblk->disk->fops = &virtblk_fops;
  203. vblk->disk->driverfs_dev = &vdev->dev;
  204. index++;
  205. /* If barriers are supported, tell block layer that queue is ordered */
  206. if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER))
  207. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  208. /* Host must always specify the capacity. */
  209. __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity),
  210. &cap);
  211. /* If capacity is too big, truncate with warning. */
  212. if ((sector_t)cap != cap) {
  213. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  214. (unsigned long long)cap);
  215. cap = (sector_t)-1;
  216. }
  217. set_capacity(vblk->disk, cap);
  218. /* Host can optionally specify maximum segment size and number of
  219. * segments. */
  220. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  221. offsetof(struct virtio_blk_config, size_max),
  222. &v);
  223. if (!err)
  224. blk_queue_max_segment_size(vblk->disk->queue, v);
  225. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  226. offsetof(struct virtio_blk_config, seg_max),
  227. &v);
  228. if (!err)
  229. blk_queue_max_hw_segments(vblk->disk->queue, v);
  230. add_disk(vblk->disk);
  231. return 0;
  232. out_put_disk:
  233. put_disk(vblk->disk);
  234. out_mempool:
  235. mempool_destroy(vblk->pool);
  236. out_free_vq:
  237. vdev->config->del_vq(vblk->vq);
  238. out_free_vblk:
  239. kfree(vblk);
  240. out:
  241. return err;
  242. }
  243. static void virtblk_remove(struct virtio_device *vdev)
  244. {
  245. struct virtio_blk *vblk = vdev->priv;
  246. int major = vblk->disk->major;
  247. /* Nothing should be pending. */
  248. BUG_ON(!list_empty(&vblk->reqs));
  249. /* Stop all the virtqueues. */
  250. vdev->config->reset(vdev);
  251. blk_cleanup_queue(vblk->disk->queue);
  252. put_disk(vblk->disk);
  253. unregister_blkdev(major, "virtblk");
  254. mempool_destroy(vblk->pool);
  255. vdev->config->del_vq(vblk->vq);
  256. kfree(vblk);
  257. }
  258. static struct virtio_device_id id_table[] = {
  259. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  260. { 0 },
  261. };
  262. static struct virtio_driver virtio_blk = {
  263. .driver.name = KBUILD_MODNAME,
  264. .driver.owner = THIS_MODULE,
  265. .id_table = id_table,
  266. .probe = virtblk_probe,
  267. .remove = __devexit_p(virtblk_remove),
  268. };
  269. static int __init init(void)
  270. {
  271. major = register_blkdev(0, "virtblk");
  272. if (major < 0)
  273. return major;
  274. return register_virtio_driver(&virtio_blk);
  275. }
  276. static void __exit fini(void)
  277. {
  278. unregister_blkdev(major, "virtblk");
  279. unregister_virtio_driver(&virtio_blk);
  280. }
  281. module_init(init);
  282. module_exit(fini);
  283. MODULE_DEVICE_TABLE(virtio, id_table);
  284. MODULE_DESCRIPTION("Virtio block driver");
  285. MODULE_LICENSE("GPL");