virtio_blk.c 16 KB

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