virtio_blk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 error;
  40. switch (vbr->status) {
  41. case VIRTIO_BLK_S_OK:
  42. error = 0;
  43. break;
  44. case VIRTIO_BLK_S_UNSUPP:
  45. error = -ENOTTY;
  46. break;
  47. default:
  48. error = -EIO;
  49. break;
  50. }
  51. __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req));
  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 = req_get_ioprio(vbr->req);
  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 = req_get_ioprio(vbr->req);
  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 block_device *bdev, fmode_t mode,
  125. unsigned cmd, unsigned long data)
  126. {
  127. return scsi_cmd_ioctl(bdev->bd_disk->queue,
  128. bdev->bd_disk, mode, 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. struct virtio_blk *vblk = bd->bd_disk->private_data;
  135. struct virtio_blk_geometry vgeo;
  136. int err;
  137. /* see if the host passed in geometry config */
  138. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  139. offsetof(struct virtio_blk_config, geometry),
  140. &vgeo);
  141. if (!err) {
  142. geo->heads = vgeo.heads;
  143. geo->sectors = vgeo.sectors;
  144. geo->cylinders = vgeo.cylinders;
  145. } else {
  146. /* some standard values, similar to sd */
  147. geo->heads = 1 << 6;
  148. geo->sectors = 1 << 5;
  149. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  150. }
  151. return 0;
  152. }
  153. static struct block_device_operations virtblk_fops = {
  154. .locked_ioctl = virtblk_ioctl,
  155. .owner = THIS_MODULE,
  156. .getgeo = virtblk_getgeo,
  157. };
  158. static int index_to_minor(int index)
  159. {
  160. return index << PART_BITS;
  161. }
  162. static int virtblk_probe(struct virtio_device *vdev)
  163. {
  164. struct virtio_blk *vblk;
  165. int err;
  166. u64 cap;
  167. u32 v;
  168. u32 blk_size;
  169. if (index_to_minor(index) >= 1 << MINORBITS)
  170. return -ENOSPC;
  171. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  172. if (!vblk) {
  173. err = -ENOMEM;
  174. goto out;
  175. }
  176. INIT_LIST_HEAD(&vblk->reqs);
  177. spin_lock_init(&vblk->lock);
  178. vblk->vdev = vdev;
  179. /* We expect one virtqueue, for output. */
  180. vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
  181. if (IS_ERR(vblk->vq)) {
  182. err = PTR_ERR(vblk->vq);
  183. goto out_free_vblk;
  184. }
  185. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  186. if (!vblk->pool) {
  187. err = -ENOMEM;
  188. goto out_free_vq;
  189. }
  190. /* FIXME: How many partitions? How long is a piece of string? */
  191. vblk->disk = alloc_disk(1 << PART_BITS);
  192. if (!vblk->disk) {
  193. err = -ENOMEM;
  194. goto out_mempool;
  195. }
  196. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  197. if (!vblk->disk->queue) {
  198. err = -ENOMEM;
  199. goto out_put_disk;
  200. }
  201. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue);
  202. if (index < 26) {
  203. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  204. } else if (index < (26 + 1) * 26) {
  205. sprintf(vblk->disk->disk_name, "vd%c%c",
  206. 'a' + index / 26 - 1, 'a' + index % 26);
  207. } else {
  208. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  209. const unsigned int m2 = (index / 26 - 1) % 26;
  210. const unsigned int m3 = index % 26;
  211. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  212. 'a' + m1, 'a' + m2, 'a' + m3);
  213. }
  214. vblk->disk->major = major;
  215. vblk->disk->first_minor = index_to_minor(index);
  216. vblk->disk->private_data = vblk;
  217. vblk->disk->fops = &virtblk_fops;
  218. vblk->disk->driverfs_dev = &vdev->dev;
  219. index++;
  220. /* If barriers are supported, tell block layer that queue is ordered */
  221. if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
  222. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  223. /* If disk is read-only in the host, the guest should obey */
  224. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  225. set_disk_ro(vblk->disk, 1);
  226. /* Host must always specify the capacity. */
  227. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  228. &cap, sizeof(cap));
  229. /* If capacity is too big, truncate with warning. */
  230. if ((sector_t)cap != cap) {
  231. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  232. (unsigned long long)cap);
  233. cap = (sector_t)-1;
  234. }
  235. set_capacity(vblk->disk, cap);
  236. /* Host can optionally specify maximum segment size and number of
  237. * segments. */
  238. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  239. offsetof(struct virtio_blk_config, size_max),
  240. &v);
  241. if (!err)
  242. blk_queue_max_segment_size(vblk->disk->queue, v);
  243. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  244. offsetof(struct virtio_blk_config, seg_max),
  245. &v);
  246. if (!err)
  247. blk_queue_max_hw_segments(vblk->disk->queue, v);
  248. /* Host can optionally specify the block size of the device */
  249. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  250. offsetof(struct virtio_blk_config, blk_size),
  251. &blk_size);
  252. if (!err)
  253. blk_queue_hardsect_size(vblk->disk->queue, blk_size);
  254. add_disk(vblk->disk);
  255. return 0;
  256. out_put_disk:
  257. put_disk(vblk->disk);
  258. out_mempool:
  259. mempool_destroy(vblk->pool);
  260. out_free_vq:
  261. vdev->config->del_vq(vblk->vq);
  262. out_free_vblk:
  263. kfree(vblk);
  264. out:
  265. return err;
  266. }
  267. static void virtblk_remove(struct virtio_device *vdev)
  268. {
  269. struct virtio_blk *vblk = vdev->priv;
  270. /* Nothing should be pending. */
  271. BUG_ON(!list_empty(&vblk->reqs));
  272. /* Stop all the virtqueues. */
  273. vdev->config->reset(vdev);
  274. del_gendisk(vblk->disk);
  275. blk_cleanup_queue(vblk->disk->queue);
  276. put_disk(vblk->disk);
  277. mempool_destroy(vblk->pool);
  278. vdev->config->del_vq(vblk->vq);
  279. kfree(vblk);
  280. }
  281. static struct virtio_device_id id_table[] = {
  282. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  283. { 0 },
  284. };
  285. static unsigned int features[] = {
  286. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  287. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  288. };
  289. static struct virtio_driver virtio_blk = {
  290. .feature_table = features,
  291. .feature_table_size = ARRAY_SIZE(features),
  292. .driver.name = KBUILD_MODNAME,
  293. .driver.owner = THIS_MODULE,
  294. .id_table = id_table,
  295. .probe = virtblk_probe,
  296. .remove = __devexit_p(virtblk_remove),
  297. };
  298. static int __init init(void)
  299. {
  300. major = register_blkdev(0, "virtblk");
  301. if (major < 0)
  302. return major;
  303. return register_virtio_driver(&virtio_blk);
  304. }
  305. static void __exit fini(void)
  306. {
  307. unregister_blkdev(major, "virtblk");
  308. unregister_virtio_driver(&virtio_blk);
  309. }
  310. module_init(init);
  311. module_exit(fini);
  312. MODULE_DEVICE_TABLE(virtio, id_table);
  313. MODULE_DESCRIPTION("Virtio block driver");
  314. MODULE_LICENSE("GPL");