virtio_blk.c 11 KB

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