virtio_blk.c 13 KB

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