virtio_blk.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 PART_BITS 4
  9. static int major, index;
  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. /* What host tells us, plus 2 for header & tailer. */
  21. unsigned int sg_elems;
  22. /* Scatterlist: can be too big for stack. */
  23. struct scatterlist sg[/*sg_elems*/];
  24. };
  25. struct virtblk_req
  26. {
  27. struct list_head list;
  28. struct request *req;
  29. struct virtio_blk_outhdr out_hdr;
  30. u8 status;
  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 error;
  41. switch (vbr->status) {
  42. case VIRTIO_BLK_S_OK:
  43. error = 0;
  44. break;
  45. case VIRTIO_BLK_S_UNSUPP:
  46. error = -ENOTTY;
  47. break;
  48. default:
  49. error = -EIO;
  50. break;
  51. }
  52. __blk_end_request(vbr->req, error, blk_rq_bytes(vbr->req));
  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 = req_get_ioprio(vbr->req);
  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 = req_get_ioprio(vbr->req);
  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. sg_set_buf(&vblk->sg[0], &vbr->out_hdr, sizeof(vbr->out_hdr));
  85. num = blk_rq_map_sg(q, vbr->req, vblk->sg+1);
  86. sg_set_buf(&vblk->sg[num+1], &vbr->status, sizeof(vbr->status));
  87. if (rq_data_dir(vbr->req) == WRITE) {
  88. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  89. out = 1 + num;
  90. in = 1;
  91. } else {
  92. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  93. out = 1;
  94. in = 1 + num;
  95. }
  96. if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
  97. mempool_free(vbr, vblk->pool);
  98. return false;
  99. }
  100. list_add_tail(&vbr->list, &vblk->reqs);
  101. return true;
  102. }
  103. static void do_virtblk_request(struct request_queue *q)
  104. {
  105. struct virtio_blk *vblk = NULL;
  106. struct request *req;
  107. unsigned int issued = 0;
  108. while ((req = elv_next_request(q)) != NULL) {
  109. vblk = req->rq_disk->private_data;
  110. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  111. /* If this request fails, stop queue and wait for something to
  112. finish to restart it. */
  113. if (!do_req(q, vblk, req)) {
  114. blk_stop_queue(q);
  115. break;
  116. }
  117. blkdev_dequeue_request(req);
  118. issued++;
  119. }
  120. if (issued)
  121. vblk->vq->vq_ops->kick(vblk->vq);
  122. }
  123. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  124. unsigned cmd, unsigned long data)
  125. {
  126. return scsi_cmd_ioctl(bdev->bd_disk->queue,
  127. bdev->bd_disk, mode, cmd,
  128. (void __user *)data);
  129. }
  130. /* We provide getgeo only to please some old bootloader/partitioning tools */
  131. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  132. {
  133. struct virtio_blk *vblk = bd->bd_disk->private_data;
  134. struct virtio_blk_geometry vgeo;
  135. int err;
  136. /* see if the host passed in geometry config */
  137. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  138. offsetof(struct virtio_blk_config, geometry),
  139. &vgeo);
  140. if (!err) {
  141. geo->heads = vgeo.heads;
  142. geo->sectors = vgeo.sectors;
  143. geo->cylinders = vgeo.cylinders;
  144. } else {
  145. /* some standard values, similar to sd */
  146. geo->heads = 1 << 6;
  147. geo->sectors = 1 << 5;
  148. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  149. }
  150. return 0;
  151. }
  152. static struct block_device_operations virtblk_fops = {
  153. .locked_ioctl = virtblk_ioctl,
  154. .owner = THIS_MODULE,
  155. .getgeo = virtblk_getgeo,
  156. };
  157. static int index_to_minor(int index)
  158. {
  159. return index << PART_BITS;
  160. }
  161. static int virtblk_probe(struct virtio_device *vdev)
  162. {
  163. struct virtio_blk *vblk;
  164. int err;
  165. u64 cap;
  166. u32 v;
  167. u32 blk_size, sg_elems;
  168. if (index_to_minor(index) >= 1 << MINORBITS)
  169. return -ENOSPC;
  170. /* We need to know how many segments before we allocate. */
  171. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  172. offsetof(struct virtio_blk_config, seg_max),
  173. &sg_elems);
  174. if (err)
  175. sg_elems = 1;
  176. /* We need an extra sg elements at head and tail. */
  177. sg_elems += 2;
  178. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  179. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  180. if (!vblk) {
  181. err = -ENOMEM;
  182. goto out;
  183. }
  184. INIT_LIST_HEAD(&vblk->reqs);
  185. spin_lock_init(&vblk->lock);
  186. vblk->vdev = vdev;
  187. vblk->sg_elems = sg_elems;
  188. sg_init_table(vblk->sg, vblk->sg_elems);
  189. /* We expect one virtqueue, for output. */
  190. vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
  191. if (IS_ERR(vblk->vq)) {
  192. err = PTR_ERR(vblk->vq);
  193. goto out_free_vblk;
  194. }
  195. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  196. if (!vblk->pool) {
  197. err = -ENOMEM;
  198. goto out_free_vq;
  199. }
  200. /* FIXME: How many partitions? How long is a piece of string? */
  201. vblk->disk = alloc_disk(1 << PART_BITS);
  202. if (!vblk->disk) {
  203. err = -ENOMEM;
  204. goto out_mempool;
  205. }
  206. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  207. if (!vblk->disk->queue) {
  208. err = -ENOMEM;
  209. goto out_put_disk;
  210. }
  211. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue);
  212. if (index < 26) {
  213. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  214. } else if (index < (26 + 1) * 26) {
  215. sprintf(vblk->disk->disk_name, "vd%c%c",
  216. 'a' + index / 26 - 1, 'a' + index % 26);
  217. } else {
  218. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  219. const unsigned int m2 = (index / 26 - 1) % 26;
  220. const unsigned int m3 = index % 26;
  221. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  222. 'a' + m1, 'a' + m2, 'a' + m3);
  223. }
  224. vblk->disk->major = major;
  225. vblk->disk->first_minor = index_to_minor(index);
  226. vblk->disk->private_data = vblk;
  227. vblk->disk->fops = &virtblk_fops;
  228. vblk->disk->driverfs_dev = &vdev->dev;
  229. index++;
  230. /* If barriers are supported, tell block layer that queue is ordered */
  231. if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
  232. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  233. /* If disk is read-only in the host, the guest should obey */
  234. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  235. set_disk_ro(vblk->disk, 1);
  236. /* Host must always specify the capacity. */
  237. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  238. &cap, sizeof(cap));
  239. /* If capacity is too big, truncate with warning. */
  240. if ((sector_t)cap != cap) {
  241. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  242. (unsigned long long)cap);
  243. cap = (sector_t)-1;
  244. }
  245. set_capacity(vblk->disk, cap);
  246. /* We can handle whatever the host told us to handle. */
  247. blk_queue_max_phys_segments(vblk->disk->queue, vblk->sg_elems-2);
  248. blk_queue_max_hw_segments(vblk->disk->queue, vblk->sg_elems-2);
  249. /* No real sector limit. */
  250. blk_queue_max_sectors(vblk->disk->queue, -1U);
  251. /* Host can optionally specify maximum segment size and number of
  252. * segments. */
  253. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  254. offsetof(struct virtio_blk_config, size_max),
  255. &v);
  256. if (!err)
  257. blk_queue_max_segment_size(vblk->disk->queue, v);
  258. else
  259. blk_queue_max_segment_size(vblk->disk->queue, -1U);
  260. /* Host can optionally specify the block size of the device */
  261. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  262. offsetof(struct virtio_blk_config, blk_size),
  263. &blk_size);
  264. if (!err)
  265. blk_queue_hardsect_size(vblk->disk->queue, blk_size);
  266. add_disk(vblk->disk);
  267. return 0;
  268. out_put_disk:
  269. put_disk(vblk->disk);
  270. out_mempool:
  271. mempool_destroy(vblk->pool);
  272. out_free_vq:
  273. vdev->config->del_vq(vblk->vq);
  274. out_free_vblk:
  275. kfree(vblk);
  276. out:
  277. return err;
  278. }
  279. static void virtblk_remove(struct virtio_device *vdev)
  280. {
  281. struct virtio_blk *vblk = vdev->priv;
  282. /* Nothing should be pending. */
  283. BUG_ON(!list_empty(&vblk->reqs));
  284. /* Stop all the virtqueues. */
  285. vdev->config->reset(vdev);
  286. del_gendisk(vblk->disk);
  287. blk_cleanup_queue(vblk->disk->queue);
  288. put_disk(vblk->disk);
  289. mempool_destroy(vblk->pool);
  290. vdev->config->del_vq(vblk->vq);
  291. kfree(vblk);
  292. }
  293. static struct virtio_device_id id_table[] = {
  294. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  295. { 0 },
  296. };
  297. static unsigned int features[] = {
  298. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  299. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  300. };
  301. static struct virtio_driver virtio_blk = {
  302. .feature_table = features,
  303. .feature_table_size = ARRAY_SIZE(features),
  304. .driver.name = KBUILD_MODNAME,
  305. .driver.owner = THIS_MODULE,
  306. .id_table = id_table,
  307. .probe = virtblk_probe,
  308. .remove = __devexit_p(virtblk_remove),
  309. };
  310. static int __init init(void)
  311. {
  312. major = register_blkdev(0, "virtblk");
  313. if (major < 0)
  314. return major;
  315. return register_virtio_driver(&virtio_blk);
  316. }
  317. static void __exit fini(void)
  318. {
  319. unregister_blkdev(major, "virtblk");
  320. unregister_virtio_driver(&virtio_blk);
  321. }
  322. module_init(init);
  323. module_exit(fini);
  324. MODULE_DEVICE_TABLE(virtio, id_table);
  325. MODULE_DESCRIPTION("Virtio block driver");
  326. MODULE_LICENSE("GPL");