virtio_blk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 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. 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. .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. if (index < 26) {
  202. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  203. } else if (index < (26 + 1) * 26) {
  204. sprintf(vblk->disk->disk_name, "vd%c%c",
  205. 'a' + index / 26 - 1, 'a' + index % 26);
  206. } else {
  207. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  208. const unsigned int m2 = (index / 26 - 1) % 26;
  209. const unsigned int m3 = index % 26;
  210. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  211. 'a' + m1, 'a' + m2, 'a' + m3);
  212. }
  213. vblk->disk->major = major;
  214. vblk->disk->first_minor = index_to_minor(index);
  215. vblk->disk->private_data = vblk;
  216. vblk->disk->fops = &virtblk_fops;
  217. vblk->disk->driverfs_dev = &vdev->dev;
  218. index++;
  219. /* If barriers are supported, tell block layer that queue is ordered */
  220. if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
  221. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  222. /* If disk is read-only in the host, the guest should obey */
  223. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  224. set_disk_ro(vblk->disk, 1);
  225. /* Host must always specify the capacity. */
  226. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  227. &cap, sizeof(cap));
  228. /* If capacity is too big, truncate with warning. */
  229. if ((sector_t)cap != cap) {
  230. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  231. (unsigned long long)cap);
  232. cap = (sector_t)-1;
  233. }
  234. set_capacity(vblk->disk, cap);
  235. /* Host can optionally specify maximum segment size and number of
  236. * segments. */
  237. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  238. offsetof(struct virtio_blk_config, size_max),
  239. &v);
  240. if (!err)
  241. blk_queue_max_segment_size(vblk->disk->queue, v);
  242. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  243. offsetof(struct virtio_blk_config, seg_max),
  244. &v);
  245. if (!err)
  246. blk_queue_max_hw_segments(vblk->disk->queue, v);
  247. /* Host can optionally specify the block size of the device */
  248. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  249. offsetof(struct virtio_blk_config, blk_size),
  250. &blk_size);
  251. if (!err)
  252. blk_queue_hardsect_size(vblk->disk->queue, blk_size);
  253. add_disk(vblk->disk);
  254. return 0;
  255. out_put_disk:
  256. put_disk(vblk->disk);
  257. out_mempool:
  258. mempool_destroy(vblk->pool);
  259. out_free_vq:
  260. vdev->config->del_vq(vblk->vq);
  261. out_free_vblk:
  262. kfree(vblk);
  263. out:
  264. return err;
  265. }
  266. static void virtblk_remove(struct virtio_device *vdev)
  267. {
  268. struct virtio_blk *vblk = vdev->priv;
  269. /* Nothing should be pending. */
  270. BUG_ON(!list_empty(&vblk->reqs));
  271. /* Stop all the virtqueues. */
  272. vdev->config->reset(vdev);
  273. del_gendisk(vblk->disk);
  274. blk_cleanup_queue(vblk->disk->queue);
  275. put_disk(vblk->disk);
  276. mempool_destroy(vblk->pool);
  277. vdev->config->del_vq(vblk->vq);
  278. kfree(vblk);
  279. }
  280. static struct virtio_device_id id_table[] = {
  281. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  282. { 0 },
  283. };
  284. static unsigned int features[] = {
  285. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  286. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  287. };
  288. static struct virtio_driver virtio_blk = {
  289. .feature_table = features,
  290. .feature_table_size = ARRAY_SIZE(features),
  291. .driver.name = KBUILD_MODNAME,
  292. .driver.owner = THIS_MODULE,
  293. .id_table = id_table,
  294. .probe = virtblk_probe,
  295. .remove = __devexit_p(virtblk_remove),
  296. };
  297. static int __init init(void)
  298. {
  299. major = register_blkdev(0, "virtblk");
  300. if (major < 0)
  301. return major;
  302. return register_virtio_driver(&virtio_blk);
  303. }
  304. static void __exit fini(void)
  305. {
  306. unregister_blkdev(major, "virtblk");
  307. unregister_virtio_driver(&virtio_blk);
  308. }
  309. module_init(init);
  310. module_exit(fini);
  311. MODULE_DEVICE_TABLE(virtio, id_table);
  312. MODULE_DESCRIPTION("Virtio block driver");
  313. MODULE_LICENSE("GPL");