virtio_blk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. struct virtio_scsi_inhdr in_hdr;
  31. u8 status;
  32. };
  33. static void blk_done(struct virtqueue *vq)
  34. {
  35. struct virtio_blk *vblk = vq->vdev->priv;
  36. struct virtblk_req *vbr;
  37. unsigned int len;
  38. unsigned long flags;
  39. spin_lock_irqsave(&vblk->lock, flags);
  40. while ((vbr = vblk->vq->vq_ops->get_buf(vblk->vq, &len)) != NULL) {
  41. int error;
  42. switch (vbr->status) {
  43. case VIRTIO_BLK_S_OK:
  44. error = 0;
  45. break;
  46. case VIRTIO_BLK_S_UNSUPP:
  47. error = -ENOTTY;
  48. break;
  49. default:
  50. error = -EIO;
  51. break;
  52. }
  53. if (blk_pc_request(vbr->req)) {
  54. vbr->req->resid_len = vbr->in_hdr.residual;
  55. vbr->req->sense_len = vbr->in_hdr.sense_len;
  56. vbr->req->errors = vbr->in_hdr.errors;
  57. }
  58. __blk_end_request_all(vbr->req, error);
  59. list_del(&vbr->list);
  60. mempool_free(vbr, vblk->pool);
  61. }
  62. /* In case queue is stopped waiting for more buffers. */
  63. blk_start_queue(vblk->disk->queue);
  64. spin_unlock_irqrestore(&vblk->lock, flags);
  65. }
  66. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  67. struct request *req)
  68. {
  69. unsigned long num, out = 0, in = 0;
  70. struct virtblk_req *vbr;
  71. vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  72. if (!vbr)
  73. /* When another request finishes we'll try again. */
  74. return false;
  75. vbr->req = req;
  76. if (blk_fs_request(vbr->req)) {
  77. vbr->out_hdr.type = 0;
  78. vbr->out_hdr.sector = blk_rq_pos(vbr->req);
  79. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  80. } else if (blk_pc_request(vbr->req)) {
  81. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  82. vbr->out_hdr.sector = 0;
  83. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  84. } else {
  85. /* We don't put anything else in the queue. */
  86. BUG();
  87. }
  88. if (blk_barrier_rq(vbr->req))
  89. vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
  90. sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
  91. /*
  92. * If this is a packet command we need a couple of additional headers.
  93. * Behind the normal outhdr we put a segment with the scsi command
  94. * block, and before the normal inhdr we put the sense data and the
  95. * inhdr with additional status information before the normal inhdr.
  96. */
  97. if (blk_pc_request(vbr->req))
  98. sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
  99. num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
  100. if (blk_pc_request(vbr->req)) {
  101. sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, 96);
  102. sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
  103. sizeof(vbr->in_hdr));
  104. }
  105. sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
  106. sizeof(vbr->status));
  107. if (num) {
  108. if (rq_data_dir(vbr->req) == WRITE) {
  109. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  110. out += num;
  111. } else {
  112. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  113. in += num;
  114. }
  115. }
  116. if (vblk->vq->vq_ops->add_buf(vblk->vq, vblk->sg, out, in, vbr)) {
  117. mempool_free(vbr, vblk->pool);
  118. return false;
  119. }
  120. list_add_tail(&vbr->list, &vblk->reqs);
  121. return true;
  122. }
  123. static void do_virtblk_request(struct request_queue *q)
  124. {
  125. struct virtio_blk *vblk = q->queuedata;
  126. struct request *req;
  127. unsigned int issued = 0;
  128. while ((req = blk_peek_request(q)) != NULL) {
  129. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  130. /* If this request fails, stop queue and wait for something to
  131. finish to restart it. */
  132. if (!do_req(q, vblk, req)) {
  133. blk_stop_queue(q);
  134. break;
  135. }
  136. blk_start_request(req);
  137. issued++;
  138. }
  139. if (issued)
  140. vblk->vq->vq_ops->kick(vblk->vq);
  141. }
  142. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  143. unsigned cmd, unsigned long data)
  144. {
  145. struct gendisk *disk = bdev->bd_disk;
  146. struct virtio_blk *vblk = disk->private_data;
  147. /*
  148. * Only allow the generic SCSI ioctls if the host can support it.
  149. */
  150. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  151. return -ENOIOCTLCMD;
  152. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd,
  153. (void __user *)data);
  154. }
  155. /* We provide getgeo only to please some old bootloader/partitioning tools */
  156. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  157. {
  158. struct virtio_blk *vblk = bd->bd_disk->private_data;
  159. struct virtio_blk_geometry vgeo;
  160. int err;
  161. /* see if the host passed in geometry config */
  162. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  163. offsetof(struct virtio_blk_config, geometry),
  164. &vgeo);
  165. if (!err) {
  166. geo->heads = vgeo.heads;
  167. geo->sectors = vgeo.sectors;
  168. geo->cylinders = vgeo.cylinders;
  169. } else {
  170. /* some standard values, similar to sd */
  171. geo->heads = 1 << 6;
  172. geo->sectors = 1 << 5;
  173. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  174. }
  175. return 0;
  176. }
  177. static struct block_device_operations virtblk_fops = {
  178. .locked_ioctl = virtblk_ioctl,
  179. .owner = THIS_MODULE,
  180. .getgeo = virtblk_getgeo,
  181. };
  182. static int index_to_minor(int index)
  183. {
  184. return index << PART_BITS;
  185. }
  186. static int virtblk_probe(struct virtio_device *vdev)
  187. {
  188. struct virtio_blk *vblk;
  189. int err;
  190. u64 cap;
  191. u32 v;
  192. u32 blk_size, sg_elems;
  193. if (index_to_minor(index) >= 1 << MINORBITS)
  194. return -ENOSPC;
  195. /* We need to know how many segments before we allocate. */
  196. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  197. offsetof(struct virtio_blk_config, seg_max),
  198. &sg_elems);
  199. if (err)
  200. sg_elems = 1;
  201. /* We need an extra sg elements at head and tail. */
  202. sg_elems += 2;
  203. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  204. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  205. if (!vblk) {
  206. err = -ENOMEM;
  207. goto out;
  208. }
  209. INIT_LIST_HEAD(&vblk->reqs);
  210. spin_lock_init(&vblk->lock);
  211. vblk->vdev = vdev;
  212. vblk->sg_elems = sg_elems;
  213. sg_init_table(vblk->sg, vblk->sg_elems);
  214. /* We expect one virtqueue, for output. */
  215. vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
  216. if (IS_ERR(vblk->vq)) {
  217. err = PTR_ERR(vblk->vq);
  218. goto out_free_vblk;
  219. }
  220. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  221. if (!vblk->pool) {
  222. err = -ENOMEM;
  223. goto out_free_vq;
  224. }
  225. /* FIXME: How many partitions? How long is a piece of string? */
  226. vblk->disk = alloc_disk(1 << PART_BITS);
  227. if (!vblk->disk) {
  228. err = -ENOMEM;
  229. goto out_mempool;
  230. }
  231. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  232. if (!vblk->disk->queue) {
  233. err = -ENOMEM;
  234. goto out_put_disk;
  235. }
  236. vblk->disk->queue->queuedata = vblk;
  237. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue);
  238. if (index < 26) {
  239. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  240. } else if (index < (26 + 1) * 26) {
  241. sprintf(vblk->disk->disk_name, "vd%c%c",
  242. 'a' + index / 26 - 1, 'a' + index % 26);
  243. } else {
  244. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  245. const unsigned int m2 = (index / 26 - 1) % 26;
  246. const unsigned int m3 = index % 26;
  247. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  248. 'a' + m1, 'a' + m2, 'a' + m3);
  249. }
  250. vblk->disk->major = major;
  251. vblk->disk->first_minor = index_to_minor(index);
  252. vblk->disk->private_data = vblk;
  253. vblk->disk->fops = &virtblk_fops;
  254. vblk->disk->driverfs_dev = &vdev->dev;
  255. index++;
  256. /* If barriers are supported, tell block layer that queue is ordered */
  257. if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
  258. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  259. /* If disk is read-only in the host, the guest should obey */
  260. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  261. set_disk_ro(vblk->disk, 1);
  262. /* Host must always specify the capacity. */
  263. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  264. &cap, sizeof(cap));
  265. /* If capacity is too big, truncate with warning. */
  266. if ((sector_t)cap != cap) {
  267. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  268. (unsigned long long)cap);
  269. cap = (sector_t)-1;
  270. }
  271. set_capacity(vblk->disk, cap);
  272. /* We can handle whatever the host told us to handle. */
  273. blk_queue_max_phys_segments(vblk->disk->queue, vblk->sg_elems-2);
  274. blk_queue_max_hw_segments(vblk->disk->queue, vblk->sg_elems-2);
  275. /* No real sector limit. */
  276. blk_queue_max_sectors(vblk->disk->queue, -1U);
  277. /* Host can optionally specify maximum segment size and number of
  278. * segments. */
  279. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  280. offsetof(struct virtio_blk_config, size_max),
  281. &v);
  282. if (!err)
  283. blk_queue_max_segment_size(vblk->disk->queue, v);
  284. else
  285. blk_queue_max_segment_size(vblk->disk->queue, -1U);
  286. /* Host can optionally specify the block size of the device */
  287. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  288. offsetof(struct virtio_blk_config, blk_size),
  289. &blk_size);
  290. if (!err)
  291. blk_queue_logical_block_size(vblk->disk->queue, blk_size);
  292. add_disk(vblk->disk);
  293. return 0;
  294. out_put_disk:
  295. put_disk(vblk->disk);
  296. out_mempool:
  297. mempool_destroy(vblk->pool);
  298. out_free_vq:
  299. vdev->config->del_vq(vblk->vq);
  300. out_free_vblk:
  301. kfree(vblk);
  302. out:
  303. return err;
  304. }
  305. static void virtblk_remove(struct virtio_device *vdev)
  306. {
  307. struct virtio_blk *vblk = vdev->priv;
  308. /* Nothing should be pending. */
  309. BUG_ON(!list_empty(&vblk->reqs));
  310. /* Stop all the virtqueues. */
  311. vdev->config->reset(vdev);
  312. del_gendisk(vblk->disk);
  313. blk_cleanup_queue(vblk->disk->queue);
  314. put_disk(vblk->disk);
  315. mempool_destroy(vblk->pool);
  316. vdev->config->del_vq(vblk->vq);
  317. kfree(vblk);
  318. }
  319. static struct virtio_device_id id_table[] = {
  320. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  321. { 0 },
  322. };
  323. static unsigned int features[] = {
  324. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  325. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  326. VIRTIO_BLK_F_SCSI,
  327. };
  328. static struct virtio_driver virtio_blk = {
  329. .feature_table = features,
  330. .feature_table_size = ARRAY_SIZE(features),
  331. .driver.name = KBUILD_MODNAME,
  332. .driver.owner = THIS_MODULE,
  333. .id_table = id_table,
  334. .probe = virtblk_probe,
  335. .remove = __devexit_p(virtblk_remove),
  336. };
  337. static int __init init(void)
  338. {
  339. major = register_blkdev(0, "virtblk");
  340. if (major < 0)
  341. return major;
  342. return register_virtio_driver(&virtio_blk);
  343. }
  344. static void __exit fini(void)
  345. {
  346. unregister_blkdev(major, "virtblk");
  347. unregister_virtio_driver(&virtio_blk);
  348. }
  349. module_init(init);
  350. module_exit(fini);
  351. MODULE_DEVICE_TABLE(virtio, id_table);
  352. MODULE_DESCRIPTION("Virtio block driver");
  353. MODULE_LICENSE("GPL");