virtio_blk.c 14 KB

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