virtio_blk.c 10 KB

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