virtio_blk.c 15 KB

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