virtio_blk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. //#define DEBUG
  2. #include <linux/spinlock.h>
  3. #include <linux/slab.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/virtio.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 = virtqueue_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. if (blk_special_request(vbr->req))
  60. vbr->req->errors = (error != 0);
  61. __blk_end_request_all(vbr->req, error);
  62. list_del(&vbr->list);
  63. mempool_free(vbr, vblk->pool);
  64. }
  65. /* In case queue is stopped waiting for more buffers. */
  66. blk_start_queue(vblk->disk->queue);
  67. spin_unlock_irqrestore(&vblk->lock, flags);
  68. }
  69. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  70. struct request *req)
  71. {
  72. unsigned long num, out = 0, in = 0;
  73. struct virtblk_req *vbr;
  74. vbr = mempool_alloc(vblk->pool, GFP_ATOMIC);
  75. if (!vbr)
  76. /* When another request finishes we'll try again. */
  77. return false;
  78. vbr->req = req;
  79. switch (req->cmd_type) {
  80. case REQ_TYPE_FS:
  81. vbr->out_hdr.type = 0;
  82. vbr->out_hdr.sector = blk_rq_pos(vbr->req);
  83. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  84. break;
  85. case REQ_TYPE_BLOCK_PC:
  86. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  87. vbr->out_hdr.sector = 0;
  88. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  89. break;
  90. case REQ_TYPE_SPECIAL:
  91. vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
  92. vbr->out_hdr.sector = 0;
  93. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  94. break;
  95. case REQ_TYPE_LINUX_BLOCK:
  96. if (req->cmd[0] == REQ_LB_OP_FLUSH) {
  97. vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
  98. vbr->out_hdr.sector = 0;
  99. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  100. break;
  101. }
  102. /*FALLTHRU*/
  103. default:
  104. /* We don't put anything else in the queue. */
  105. BUG();
  106. }
  107. if (blk_barrier_rq(vbr->req))
  108. vbr->out_hdr.type |= VIRTIO_BLK_T_BARRIER;
  109. sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
  110. /*
  111. * If this is a packet command we need a couple of additional headers.
  112. * Behind the normal outhdr we put a segment with the scsi command
  113. * block, and before the normal inhdr we put the sense data and the
  114. * inhdr with additional status information before the normal inhdr.
  115. */
  116. if (blk_pc_request(vbr->req))
  117. sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
  118. num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
  119. if (blk_pc_request(vbr->req)) {
  120. sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, 96);
  121. sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
  122. sizeof(vbr->in_hdr));
  123. }
  124. sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
  125. sizeof(vbr->status));
  126. if (num) {
  127. if (rq_data_dir(vbr->req) == WRITE) {
  128. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  129. out += num;
  130. } else {
  131. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  132. in += num;
  133. }
  134. }
  135. if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) {
  136. mempool_free(vbr, vblk->pool);
  137. return false;
  138. }
  139. list_add_tail(&vbr->list, &vblk->reqs);
  140. return true;
  141. }
  142. static void do_virtblk_request(struct request_queue *q)
  143. {
  144. struct virtio_blk *vblk = q->queuedata;
  145. struct request *req;
  146. unsigned int issued = 0;
  147. while ((req = blk_peek_request(q)) != NULL) {
  148. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  149. /* If this request fails, stop queue and wait for something to
  150. finish to restart it. */
  151. if (!do_req(q, vblk, req)) {
  152. blk_stop_queue(q);
  153. break;
  154. }
  155. blk_start_request(req);
  156. issued++;
  157. }
  158. if (issued)
  159. virtqueue_kick(vblk->vq);
  160. }
  161. static void virtblk_prepare_flush(struct request_queue *q, struct request *req)
  162. {
  163. req->cmd_type = REQ_TYPE_LINUX_BLOCK;
  164. req->cmd[0] = REQ_LB_OP_FLUSH;
  165. }
  166. /* return id (s/n) string for *disk to *id_str
  167. */
  168. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  169. {
  170. struct virtio_blk *vblk = disk->private_data;
  171. struct request *req;
  172. struct bio *bio;
  173. bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
  174. GFP_KERNEL);
  175. if (IS_ERR(bio))
  176. return PTR_ERR(bio);
  177. req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
  178. if (IS_ERR(req)) {
  179. bio_put(bio);
  180. return PTR_ERR(req);
  181. }
  182. req->cmd_type = REQ_TYPE_SPECIAL;
  183. return blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  184. }
  185. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  186. unsigned cmd, unsigned long data)
  187. {
  188. struct gendisk *disk = bdev->bd_disk;
  189. struct virtio_blk *vblk = disk->private_data;
  190. if (cmd == 0x56424944) { /* 'VBID' */
  191. void __user *usr_data = (void __user *)data;
  192. char id_str[VIRTIO_BLK_ID_BYTES];
  193. int err;
  194. err = virtblk_get_id(disk, id_str);
  195. if (!err && copy_to_user(usr_data, id_str, VIRTIO_BLK_ID_BYTES))
  196. err = -EFAULT;
  197. return err;
  198. }
  199. /*
  200. * Only allow the generic SCSI ioctls if the host can support it.
  201. */
  202. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  203. return -ENOTTY;
  204. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd,
  205. (void __user *)data);
  206. }
  207. /* We provide getgeo only to please some old bootloader/partitioning tools */
  208. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  209. {
  210. struct virtio_blk *vblk = bd->bd_disk->private_data;
  211. struct virtio_blk_geometry vgeo;
  212. int err;
  213. /* see if the host passed in geometry config */
  214. err = virtio_config_val(vblk->vdev, VIRTIO_BLK_F_GEOMETRY,
  215. offsetof(struct virtio_blk_config, geometry),
  216. &vgeo);
  217. if (!err) {
  218. geo->heads = vgeo.heads;
  219. geo->sectors = vgeo.sectors;
  220. geo->cylinders = vgeo.cylinders;
  221. } else {
  222. /* some standard values, similar to sd */
  223. geo->heads = 1 << 6;
  224. geo->sectors = 1 << 5;
  225. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  226. }
  227. return 0;
  228. }
  229. static const struct block_device_operations virtblk_fops = {
  230. .locked_ioctl = virtblk_ioctl,
  231. .owner = THIS_MODULE,
  232. .getgeo = virtblk_getgeo,
  233. };
  234. static int index_to_minor(int index)
  235. {
  236. return index << PART_BITS;
  237. }
  238. static int __devinit virtblk_probe(struct virtio_device *vdev)
  239. {
  240. struct virtio_blk *vblk;
  241. struct request_queue *q;
  242. int err;
  243. u64 cap;
  244. u32 v, blk_size, sg_elems, opt_io_size;
  245. u16 min_io_size;
  246. u8 physical_block_exp, alignment_offset;
  247. if (index_to_minor(index) >= 1 << MINORBITS)
  248. return -ENOSPC;
  249. /* We need to know how many segments before we allocate. */
  250. err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
  251. offsetof(struct virtio_blk_config, seg_max),
  252. &sg_elems);
  253. /* We need at least one SG element, whatever they say. */
  254. if (err || !sg_elems)
  255. sg_elems = 1;
  256. /* We need an extra sg elements at head and tail. */
  257. sg_elems += 2;
  258. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  259. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  260. if (!vblk) {
  261. err = -ENOMEM;
  262. goto out;
  263. }
  264. INIT_LIST_HEAD(&vblk->reqs);
  265. spin_lock_init(&vblk->lock);
  266. vblk->vdev = vdev;
  267. vblk->sg_elems = sg_elems;
  268. sg_init_table(vblk->sg, vblk->sg_elems);
  269. /* We expect one virtqueue, for output. */
  270. vblk->vq = virtio_find_single_vq(vdev, blk_done, "requests");
  271. if (IS_ERR(vblk->vq)) {
  272. err = PTR_ERR(vblk->vq);
  273. goto out_free_vblk;
  274. }
  275. vblk->pool = mempool_create_kmalloc_pool(1,sizeof(struct virtblk_req));
  276. if (!vblk->pool) {
  277. err = -ENOMEM;
  278. goto out_free_vq;
  279. }
  280. /* FIXME: How many partitions? How long is a piece of string? */
  281. vblk->disk = alloc_disk(1 << PART_BITS);
  282. if (!vblk->disk) {
  283. err = -ENOMEM;
  284. goto out_mempool;
  285. }
  286. q = vblk->disk->queue = blk_init_queue(do_virtblk_request, &vblk->lock);
  287. if (!q) {
  288. err = -ENOMEM;
  289. goto out_put_disk;
  290. }
  291. q->queuedata = vblk;
  292. if (index < 26) {
  293. sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
  294. } else if (index < (26 + 1) * 26) {
  295. sprintf(vblk->disk->disk_name, "vd%c%c",
  296. 'a' + index / 26 - 1, 'a' + index % 26);
  297. } else {
  298. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  299. const unsigned int m2 = (index / 26 - 1) % 26;
  300. const unsigned int m3 = index % 26;
  301. sprintf(vblk->disk->disk_name, "vd%c%c%c",
  302. 'a' + m1, 'a' + m2, 'a' + m3);
  303. }
  304. vblk->disk->major = major;
  305. vblk->disk->first_minor = index_to_minor(index);
  306. vblk->disk->private_data = vblk;
  307. vblk->disk->fops = &virtblk_fops;
  308. vblk->disk->driverfs_dev = &vdev->dev;
  309. index++;
  310. if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH)) {
  311. /*
  312. * If the FLUSH feature is supported we do have support for
  313. * flushing a volatile write cache on the host. Use that
  314. * to implement write barrier support.
  315. */
  316. blk_queue_ordered(q, QUEUE_ORDERED_DRAIN_FLUSH,
  317. virtblk_prepare_flush);
  318. } else if (virtio_has_feature(vdev, VIRTIO_BLK_F_BARRIER)) {
  319. /*
  320. * If the BARRIER feature is supported the host expects us
  321. * to order request by tags. This implies there is not
  322. * volatile write cache on the host, and that the host
  323. * never re-orders outstanding I/O. This feature is not
  324. * useful for real life scenarious and deprecated.
  325. */
  326. blk_queue_ordered(q, QUEUE_ORDERED_TAG, NULL);
  327. } else {
  328. /*
  329. * If the FLUSH feature is not supported we must assume that
  330. * the host does not perform any kind of volatile write
  331. * caching. We still need to drain the queue to provider
  332. * proper barrier semantics.
  333. */
  334. blk_queue_ordered(q, QUEUE_ORDERED_DRAIN, NULL);
  335. }
  336. /* If disk is read-only in the host, the guest should obey */
  337. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  338. set_disk_ro(vblk->disk, 1);
  339. /* Host must always specify the capacity. */
  340. vdev->config->get(vdev, offsetof(struct virtio_blk_config, capacity),
  341. &cap, sizeof(cap));
  342. /* If capacity is too big, truncate with warning. */
  343. if ((sector_t)cap != cap) {
  344. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  345. (unsigned long long)cap);
  346. cap = (sector_t)-1;
  347. }
  348. set_capacity(vblk->disk, cap);
  349. /* We can handle whatever the host told us to handle. */
  350. blk_queue_max_segments(q, vblk->sg_elems-2);
  351. /* No need to bounce any requests */
  352. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  353. /* No real sector limit. */
  354. blk_queue_max_hw_sectors(q, -1U);
  355. /* Host can optionally specify maximum segment size and number of
  356. * segments. */
  357. err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
  358. offsetof(struct virtio_blk_config, size_max),
  359. &v);
  360. if (!err)
  361. blk_queue_max_segment_size(q, v);
  362. else
  363. blk_queue_max_segment_size(q, -1U);
  364. /* Host can optionally specify the block size of the device */
  365. err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
  366. offsetof(struct virtio_blk_config, blk_size),
  367. &blk_size);
  368. if (!err)
  369. blk_queue_logical_block_size(q, blk_size);
  370. else
  371. blk_size = queue_logical_block_size(q);
  372. /* Use topology information if available */
  373. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  374. offsetof(struct virtio_blk_config, physical_block_exp),
  375. &physical_block_exp);
  376. if (!err && physical_block_exp)
  377. blk_queue_physical_block_size(q,
  378. blk_size * (1 << physical_block_exp));
  379. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  380. offsetof(struct virtio_blk_config, alignment_offset),
  381. &alignment_offset);
  382. if (!err && alignment_offset)
  383. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  384. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  385. offsetof(struct virtio_blk_config, min_io_size),
  386. &min_io_size);
  387. if (!err && min_io_size)
  388. blk_queue_io_min(q, blk_size * min_io_size);
  389. err = virtio_config_val(vdev, VIRTIO_BLK_F_TOPOLOGY,
  390. offsetof(struct virtio_blk_config, opt_io_size),
  391. &opt_io_size);
  392. if (!err && opt_io_size)
  393. blk_queue_io_opt(q, blk_size * opt_io_size);
  394. add_disk(vblk->disk);
  395. return 0;
  396. out_put_disk:
  397. put_disk(vblk->disk);
  398. out_mempool:
  399. mempool_destroy(vblk->pool);
  400. out_free_vq:
  401. vdev->config->del_vqs(vdev);
  402. out_free_vblk:
  403. kfree(vblk);
  404. out:
  405. return err;
  406. }
  407. static void __devexit virtblk_remove(struct virtio_device *vdev)
  408. {
  409. struct virtio_blk *vblk = vdev->priv;
  410. /* Nothing should be pending. */
  411. BUG_ON(!list_empty(&vblk->reqs));
  412. /* Stop all the virtqueues. */
  413. vdev->config->reset(vdev);
  414. del_gendisk(vblk->disk);
  415. blk_cleanup_queue(vblk->disk->queue);
  416. put_disk(vblk->disk);
  417. mempool_destroy(vblk->pool);
  418. vdev->config->del_vqs(vdev);
  419. kfree(vblk);
  420. }
  421. static const struct virtio_device_id id_table[] = {
  422. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  423. { 0 },
  424. };
  425. static unsigned int features[] = {
  426. VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
  427. VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  428. VIRTIO_BLK_F_SCSI, VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY
  429. };
  430. /*
  431. * virtio_blk causes spurious section mismatch warning by
  432. * simultaneously referring to a __devinit and a __devexit function.
  433. * Use __refdata to avoid this warning.
  434. */
  435. static struct virtio_driver __refdata virtio_blk = {
  436. .feature_table = features,
  437. .feature_table_size = ARRAY_SIZE(features),
  438. .driver.name = KBUILD_MODNAME,
  439. .driver.owner = THIS_MODULE,
  440. .id_table = id_table,
  441. .probe = virtblk_probe,
  442. .remove = __devexit_p(virtblk_remove),
  443. };
  444. static int __init init(void)
  445. {
  446. major = register_blkdev(0, "virtblk");
  447. if (major < 0)
  448. return major;
  449. return register_virtio_driver(&virtio_blk);
  450. }
  451. static void __exit fini(void)
  452. {
  453. unregister_blkdev(major, "virtblk");
  454. unregister_virtio_driver(&virtio_blk);
  455. }
  456. module_init(init);
  457. module_exit(fini);
  458. MODULE_DEVICE_TABLE(virtio, id_table);
  459. MODULE_DESCRIPTION("Virtio block driver");
  460. MODULE_LICENSE("GPL");