virtio_blk.c 12 KB

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