virtio_blk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /* return ATA identify data
  143. */
  144. static int virtblk_identify(struct gendisk *disk, void *argp)
  145. {
  146. struct virtio_blk *vblk = disk->private_data;
  147. void *opaque;
  148. int err = -ENOMEM;
  149. opaque = kmalloc(VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
  150. if (!opaque)
  151. goto out;
  152. err = virtio_config_buf(vblk->vdev, VIRTIO_BLK_F_IDENTIFY,
  153. offsetof(struct virtio_blk_config, identify), opaque,
  154. VIRTIO_BLK_ID_BYTES);
  155. if (err)
  156. goto out_kfree;
  157. if (copy_to_user(argp, opaque, VIRTIO_BLK_ID_BYTES))
  158. err = -EFAULT;
  159. out_kfree:
  160. kfree(opaque);
  161. out:
  162. return err;
  163. }
  164. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  165. unsigned cmd, unsigned long data)
  166. {
  167. struct gendisk *disk = bdev->bd_disk;
  168. struct virtio_blk *vblk = disk->private_data;
  169. void __user *argp = (void __user *)data;
  170. if (cmd == HDIO_GET_IDENTITY)
  171. return virtblk_identify(disk, argp);
  172. /*
  173. * Only allow the generic SCSI ioctls if the host can support it.
  174. */
  175. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  176. return -ENOTTY;
  177. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp);
  178. }
  179. /* We provide getgeo only to please some old bootloader/partitioning tools */
  180. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  181. {
  182. struct virtio_blk *vblk = bd->bd_disk->private_data;
  183. struct virtio_blk_geometry vgeo;
  184. int err;
  185. /* see if the host passed in geometry config */
  186. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  187. offsetof(struct virtio_blk_config, geometry),
  188. &vgeo);
  189. if (!err) {
  190. geo->heads = vgeo.heads;
  191. geo->sectors = vgeo.sectors;
  192. geo->cylinders = vgeo.cylinders;
  193. } else {
  194. /* some standard values, similar to sd */
  195. geo->heads = 1 << 6;
  196. geo->sectors = 1 << 5;
  197. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  198. }
  199. return 0;
  200. }
  201. static const struct block_device_operations virtblk_fops = {
  202. .locked_ioctl = virtblk_ioctl,
  203. .owner = THIS_MODULE,
  204. .getgeo = virtblk_getgeo,
  205. };
  206. static int index_to_minor(int index)
  207. {
  208. return index << PART_BITS;
  209. }
  210. static int __devinit virtblk_probe(struct virtio_device *vdev)
  211. {
  212. struct virtio_blk *vblk;
  213. int err;
  214. u64 cap;
  215. u32 v;
  216. u32 blk_size, sg_elems;
  217. if (index_to_minor(index) >= 1 << MINORBITS)
  218. return -ENOSPC;
  219. /* We need to know how many segments before we allocate. */
  220. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  221. offsetof(struct virtio_blk_config, seg_max),
  222. &sg_elems);
  223. if (err)
  224. sg_elems = 1;
  225. /* We need an extra sg elements at head and tail. */
  226. sg_elems += 2;
  227. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  228. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  229. if (!vblk) {
  230. err = -ENOMEM;
  231. goto out;
  232. }
  233. INIT_LIST_HEAD(&vblk->reqs);
  234. spin_lock_init(&vblk->lock);
  235. vblk->vdev = vdev;
  236. vblk->sg_elems = sg_elems;
  237. sg_init_table(vblk->sg, vblk->sg_elems);
  238. /* We expect one virtqueue, for output. */
  239. vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests");
  240. if (IS_ERR(vblk->vq)) {
  241. err = PTR_ERR(vblk->vq);
  242. goto out_free_vblk;
  243. }
  244. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  245. if (!vblk->pool) {
  246. err = -ENOMEM;
  247. goto out_free_vq;
  248. }
  249. /* FIXME: How many partitions? How long is a piece of string? */
  250. vblk->disk = alloc_disk(1 << PART_BITS);
  251. if (!vblk->disk) {
  252. err = -ENOMEM;
  253. goto out_mempool;
  254. }
  255. vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  256. if (!vblk->disk->queue) {
  257. err = -ENOMEM;
  258. goto out_put_disk;
  259. }
  260. vblk->disk->queue->queuedata = vblk;
  261. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, vblk->disk->queue);
  262. if (index < 26) {
  263. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  264. } else if (index < (26 + 1) * 26) {
  265. sprintf(vblk->disk->disk_name, "vd%c%c",
  266. 'a' + index / 26 - 1, 'a' + index % 26);
  267. } else {
  268. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  269. const unsigned int m2 = (index / 26 - 1) % 26;
  270. const unsigned int m3 = index % 26;
  271. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  272. 'a' + m1, 'a' + m2, 'a' + m3);
  273. }
  274. vblk->disk->major = major;
  275. vblk->disk->first_minor = index_to_minor(index);
  276. vblk->disk->private_data = vblk;
  277. vblk->disk->fops = &virtblk_fops;
  278. vblk->disk->driverfs_dev = &vdev->dev;
  279. index++;
  280. /* If barriers are supported, tell block layer that queue is ordered */
  281. if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER))
  282. blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
  283. /* If disk is read-only in the host, the guest should obey */
  284. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  285. set_disk_ro(vblk->disk, 1);
  286. /* Host must always specify the capacity. */
  287. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  288. &cap, sizeof(cap));
  289. /* If capacity is too big, truncate with warning. */
  290. if ((sector_t)cap != cap) {
  291. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  292. (unsigned long long)cap);
  293. cap = (sector_t)-1;
  294. }
  295. set_capacity(vblk->disk, cap);
  296. /* We can handle whatever the host told us to handle. */
  297. blk_queue_max_phys_segments(vblk->disk->queue, vblk->sg_elems-2);
  298. blk_queue_max_hw_segments(vblk->disk->queue, vblk->sg_elems-2);
  299. /* No need to bounce any requests */
  300. blk_queue_bounce_limit(vblk->disk->queue, BLK_BOUNCE_ANY);
  301. /* No real sector limit. */
  302. blk_queue_max_sectors(vblk->disk->queue, -1U);
  303. /* Host can optionally specify maximum segment size and number of
  304. * segments. */
  305. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  306. offsetof(struct virtio_blk_config, size_max),
  307. &v);
  308. if (!err)
  309. blk_queue_max_segment_size(vblk->disk->queue, v);
  310. else
  311. blk_queue_max_segment_size(vblk->disk->queue, -1U);
  312. /* Host can optionally specify the block size of the device */
  313. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  314. offsetof(struct virtio_blk_config, blk_size),
  315. &blk_size);
  316. if (!err)
  317. blk_queue_logical_block_size(vblk->disk->queue, blk_size);
  318. add_disk(vblk->disk);
  319. return 0;
  320. out_put_disk:
  321. put_disk(vblk->disk);
  322. out_mempool:
  323. mempool_destroy(vblk->pool);
  324. out_free_vq:
  325. vdev->config->del_vqs(vdev);
  326. out_free_vblk:
  327. kfree(vblk);
  328. out:
  329. return err;
  330. }
  331. static void __devexit virtblk_remove(struct virtio_device *vdev)
  332. {
  333. struct virtio_blk *vblk = vdev->priv;
  334. /* Nothing should be pending. */
  335. BUG_ON(!list_empty(&vblk->reqs));
  336. /* Stop all the virtqueues. */
  337. vdev->config->reset(vdev);
  338. del_gendisk(vblk->disk);
  339. blk_cleanup_queue(vblk->disk->queue);
  340. put_disk(vblk->disk);
  341. mempool_destroy(vblk->pool);
  342. vdev->config->del_vqs(vdev);
  343. kfree(vblk);
  344. }
  345. static struct virtio_device_id id_table[] = {
  346. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  347. { 0 },
  348. };
  349. static unsigned int features[] = {
  350. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  351. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  352. VIRTIO_BLK_F_SCSI, VIRTIO_BLK_F_IDENTIFY
  353. };
  354. /*
  355. * virtio_blk causes spurious section mismatch warning by
  356. * simultaneously referring to a __devinit and a __devexit function.
  357. * Use __refdata to avoid this warning.
  358. */
  359. static struct virtio_driver __refdata virtio_blk = {
  360. .feature_table = features,
  361. .feature_table_size = ARRAY_SIZE(features),
  362. .driver.name = KBUILD_MODNAME,
  363. .driver.owner = THIS_MODULE,
  364. .id_table = id_table,
  365. .probe = virtblk_probe,
  366. .remove = __devexit_p(virtblk_remove),
  367. };
  368. static int __init init(void)
  369. {
  370. major = register_blkdev(0, "virtblk");
  371. if (major < 0)
  372. return major;
  373. return register_virtio_driver(&virtio_blk);
  374. }
  375. static void __exit fini(void)
  376. {
  377. unregister_blkdev(major, "virtblk");
  378. unregister_virtio_driver(&virtio_blk);
  379. }
  380. module_init(init);
  381. module_exit(fini);
  382. MODULE_DEVICE_TABLE(virtio, id_table);
  383. MODULE_DESCRIPTION("Virtio block driver");
  384. MODULE_LICENSE("GPL");