virtio_blk.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/virtio.h>
  9. #include <linux/virtio_blk.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/string_helpers.h>
  12. #include <scsi/scsi_cmnd.h>
  13. #include <linux/idr.h>
  14. #define PART_BITS 4
  15. static bool use_bio;
  16. module_param(use_bio, bool, S_IRUGO);
  17. static int major;
  18. static DEFINE_IDA(vd_index_ida);
  19. static struct workqueue_struct *virtblk_wq;
  20. struct virtio_blk
  21. {
  22. struct virtio_device *vdev;
  23. struct virtqueue *vq;
  24. wait_queue_head_t queue_wait;
  25. /* The disk structure for the kernel. */
  26. struct gendisk *disk;
  27. mempool_t *pool;
  28. /* Process context for config space updates */
  29. struct work_struct config_work;
  30. /* Lock for config space updates */
  31. struct mutex config_lock;
  32. /* enable config space updates */
  33. bool config_enable;
  34. /* What host tells us, plus 2 for header & tailer. */
  35. unsigned int sg_elems;
  36. /* Ida index - used to track minor number allocations. */
  37. int index;
  38. /* Scatterlist: can be too big for stack. */
  39. struct scatterlist sg[/*sg_elems*/];
  40. };
  41. struct virtblk_req
  42. {
  43. struct request *req;
  44. struct bio *bio;
  45. struct virtio_blk_outhdr out_hdr;
  46. struct virtio_scsi_inhdr in_hdr;
  47. struct work_struct work;
  48. struct virtio_blk *vblk;
  49. int flags;
  50. u8 status;
  51. struct scatterlist sg[];
  52. };
  53. enum {
  54. VBLK_IS_FLUSH = 1,
  55. VBLK_REQ_FLUSH = 2,
  56. VBLK_REQ_DATA = 4,
  57. VBLK_REQ_FUA = 8,
  58. };
  59. static inline int virtblk_result(struct virtblk_req *vbr)
  60. {
  61. switch (vbr->status) {
  62. case VIRTIO_BLK_S_OK:
  63. return 0;
  64. case VIRTIO_BLK_S_UNSUPP:
  65. return -ENOTTY;
  66. default:
  67. return -EIO;
  68. }
  69. }
  70. static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
  71. gfp_t gfp_mask)
  72. {
  73. struct virtblk_req *vbr;
  74. vbr = mempool_alloc(vblk->pool, gfp_mask);
  75. if (!vbr)
  76. return NULL;
  77. vbr->vblk = vblk;
  78. if (use_bio)
  79. sg_init_table(vbr->sg, vblk->sg_elems);
  80. return vbr;
  81. }
  82. static int __virtblk_add_req(struct virtqueue *vq,
  83. struct virtblk_req *vbr,
  84. struct scatterlist *data_sg,
  85. bool have_data)
  86. {
  87. struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
  88. unsigned int num_out = 0, num_in = 0;
  89. int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT;
  90. sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
  91. sgs[num_out++] = &hdr;
  92. /*
  93. * If this is a packet command we need a couple of additional headers.
  94. * Behind the normal outhdr we put a segment with the scsi command
  95. * block, and before the normal inhdr we put the sense data and the
  96. * inhdr with additional status information.
  97. */
  98. if (type == VIRTIO_BLK_T_SCSI_CMD) {
  99. sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
  100. sgs[num_out++] = &cmd;
  101. }
  102. if (have_data) {
  103. if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT)
  104. sgs[num_out++] = data_sg;
  105. else
  106. sgs[num_out + num_in++] = data_sg;
  107. }
  108. if (type == VIRTIO_BLK_T_SCSI_CMD) {
  109. sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
  110. sgs[num_out + num_in++] = &sense;
  111. sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
  112. sgs[num_out + num_in++] = &inhdr;
  113. }
  114. sg_init_one(&status, &vbr->status, sizeof(vbr->status));
  115. sgs[num_out + num_in++] = &status;
  116. return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
  117. }
  118. static void virtblk_add_req(struct virtblk_req *vbr, bool have_data)
  119. {
  120. struct virtio_blk *vblk = vbr->vblk;
  121. DEFINE_WAIT(wait);
  122. int ret;
  123. spin_lock_irq(vblk->disk->queue->queue_lock);
  124. while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr, vbr->sg,
  125. have_data)) < 0)) {
  126. prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
  127. TASK_UNINTERRUPTIBLE);
  128. spin_unlock_irq(vblk->disk->queue->queue_lock);
  129. io_schedule();
  130. spin_lock_irq(vblk->disk->queue->queue_lock);
  131. finish_wait(&vblk->queue_wait, &wait);
  132. }
  133. virtqueue_kick(vblk->vq);
  134. spin_unlock_irq(vblk->disk->queue->queue_lock);
  135. }
  136. static void virtblk_bio_send_flush(struct virtblk_req *vbr)
  137. {
  138. vbr->flags |= VBLK_IS_FLUSH;
  139. vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
  140. vbr->out_hdr.sector = 0;
  141. vbr->out_hdr.ioprio = 0;
  142. virtblk_add_req(vbr, false);
  143. }
  144. static void virtblk_bio_send_data(struct virtblk_req *vbr)
  145. {
  146. struct virtio_blk *vblk = vbr->vblk;
  147. struct bio *bio = vbr->bio;
  148. bool have_data;
  149. vbr->flags &= ~VBLK_IS_FLUSH;
  150. vbr->out_hdr.type = 0;
  151. vbr->out_hdr.sector = bio->bi_sector;
  152. vbr->out_hdr.ioprio = bio_prio(bio);
  153. if (blk_bio_map_sg(vblk->disk->queue, bio, vbr->sg)) {
  154. have_data = true;
  155. if (bio->bi_rw & REQ_WRITE)
  156. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  157. else
  158. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  159. } else
  160. have_data = false;
  161. virtblk_add_req(vbr, have_data);
  162. }
  163. static void virtblk_bio_send_data_work(struct work_struct *work)
  164. {
  165. struct virtblk_req *vbr;
  166. vbr = container_of(work, struct virtblk_req, work);
  167. virtblk_bio_send_data(vbr);
  168. }
  169. static void virtblk_bio_send_flush_work(struct work_struct *work)
  170. {
  171. struct virtblk_req *vbr;
  172. vbr = container_of(work, struct virtblk_req, work);
  173. virtblk_bio_send_flush(vbr);
  174. }
  175. static inline void virtblk_request_done(struct virtblk_req *vbr)
  176. {
  177. struct virtio_blk *vblk = vbr->vblk;
  178. struct request *req = vbr->req;
  179. int error = virtblk_result(vbr);
  180. if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
  181. req->resid_len = vbr->in_hdr.residual;
  182. req->sense_len = vbr->in_hdr.sense_len;
  183. req->errors = vbr->in_hdr.errors;
  184. } else if (req->cmd_type == REQ_TYPE_SPECIAL) {
  185. req->errors = (error != 0);
  186. }
  187. __blk_end_request_all(req, error);
  188. mempool_free(vbr, vblk->pool);
  189. }
  190. static inline void virtblk_bio_flush_done(struct virtblk_req *vbr)
  191. {
  192. struct virtio_blk *vblk = vbr->vblk;
  193. if (vbr->flags & VBLK_REQ_DATA) {
  194. /* Send out the actual write data */
  195. INIT_WORK(&vbr->work, virtblk_bio_send_data_work);
  196. queue_work(virtblk_wq, &vbr->work);
  197. } else {
  198. bio_endio(vbr->bio, virtblk_result(vbr));
  199. mempool_free(vbr, vblk->pool);
  200. }
  201. }
  202. static inline void virtblk_bio_data_done(struct virtblk_req *vbr)
  203. {
  204. struct virtio_blk *vblk = vbr->vblk;
  205. if (unlikely(vbr->flags & VBLK_REQ_FUA)) {
  206. /* Send out a flush before end the bio */
  207. vbr->flags &= ~VBLK_REQ_DATA;
  208. INIT_WORK(&vbr->work, virtblk_bio_send_flush_work);
  209. queue_work(virtblk_wq, &vbr->work);
  210. } else {
  211. bio_endio(vbr->bio, virtblk_result(vbr));
  212. mempool_free(vbr, vblk->pool);
  213. }
  214. }
  215. static inline void virtblk_bio_done(struct virtblk_req *vbr)
  216. {
  217. if (unlikely(vbr->flags & VBLK_IS_FLUSH))
  218. virtblk_bio_flush_done(vbr);
  219. else
  220. virtblk_bio_data_done(vbr);
  221. }
  222. static void virtblk_done(struct virtqueue *vq)
  223. {
  224. struct virtio_blk *vblk = vq->vdev->priv;
  225. bool bio_done = false, req_done = false;
  226. struct virtblk_req *vbr;
  227. unsigned long flags;
  228. unsigned int len;
  229. spin_lock_irqsave(vblk->disk->queue->queue_lock, flags);
  230. do {
  231. virtqueue_disable_cb(vq);
  232. while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
  233. if (vbr->bio) {
  234. virtblk_bio_done(vbr);
  235. bio_done = true;
  236. } else {
  237. virtblk_request_done(vbr);
  238. req_done = true;
  239. }
  240. }
  241. if (unlikely(virtqueue_is_broken(vq)))
  242. break;
  243. } while (!virtqueue_enable_cb(vq));
  244. /* In case queue is stopped waiting for more buffers. */
  245. if (req_done)
  246. blk_start_queue(vblk->disk->queue);
  247. spin_unlock_irqrestore(vblk->disk->queue->queue_lock, flags);
  248. if (bio_done)
  249. wake_up(&vblk->queue_wait);
  250. }
  251. static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
  252. struct request *req)
  253. {
  254. unsigned int num;
  255. struct virtblk_req *vbr;
  256. vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
  257. if (!vbr)
  258. /* When another request finishes we'll try again. */
  259. return false;
  260. vbr->req = req;
  261. vbr->bio = NULL;
  262. if (req->cmd_flags & REQ_FLUSH) {
  263. vbr->out_hdr.type = VIRTIO_BLK_T_FLUSH;
  264. vbr->out_hdr.sector = 0;
  265. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  266. } else {
  267. switch (req->cmd_type) {
  268. case REQ_TYPE_FS:
  269. vbr->out_hdr.type = 0;
  270. vbr->out_hdr.sector = blk_rq_pos(vbr->req);
  271. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  272. break;
  273. case REQ_TYPE_BLOCK_PC:
  274. vbr->out_hdr.type = VIRTIO_BLK_T_SCSI_CMD;
  275. vbr->out_hdr.sector = 0;
  276. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  277. break;
  278. case REQ_TYPE_SPECIAL:
  279. vbr->out_hdr.type = VIRTIO_BLK_T_GET_ID;
  280. vbr->out_hdr.sector = 0;
  281. vbr->out_hdr.ioprio = req_get_ioprio(vbr->req);
  282. break;
  283. default:
  284. /* We don't put anything else in the queue. */
  285. BUG();
  286. }
  287. }
  288. num = blk_rq_map_sg(q, vbr->req, vblk->sg);
  289. if (num) {
  290. if (rq_data_dir(vbr->req) == WRITE)
  291. vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
  292. else
  293. vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
  294. }
  295. if (__virtblk_add_req(vblk->vq, vbr, vblk->sg, num) < 0) {
  296. mempool_free(vbr, vblk->pool);
  297. return false;
  298. }
  299. return true;
  300. }
  301. static void virtblk_request(struct request_queue *q)
  302. {
  303. struct virtio_blk *vblk = q->queuedata;
  304. struct request *req;
  305. unsigned int issued = 0;
  306. while ((req = blk_peek_request(q)) != NULL) {
  307. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  308. /* If this request fails, stop queue and wait for something to
  309. finish to restart it. */
  310. if (!do_req(q, vblk, req)) {
  311. blk_stop_queue(q);
  312. break;
  313. }
  314. blk_start_request(req);
  315. issued++;
  316. }
  317. if (issued)
  318. virtqueue_kick(vblk->vq);
  319. }
  320. static void virtblk_make_request(struct request_queue *q, struct bio *bio)
  321. {
  322. struct virtio_blk *vblk = q->queuedata;
  323. struct virtblk_req *vbr;
  324. BUG_ON(bio->bi_phys_segments + 2 > vblk->sg_elems);
  325. vbr = virtblk_alloc_req(vblk, GFP_NOIO);
  326. if (!vbr) {
  327. bio_endio(bio, -ENOMEM);
  328. return;
  329. }
  330. vbr->bio = bio;
  331. vbr->flags = 0;
  332. if (bio->bi_rw & REQ_FLUSH)
  333. vbr->flags |= VBLK_REQ_FLUSH;
  334. if (bio->bi_rw & REQ_FUA)
  335. vbr->flags |= VBLK_REQ_FUA;
  336. if (bio->bi_size)
  337. vbr->flags |= VBLK_REQ_DATA;
  338. if (unlikely(vbr->flags & VBLK_REQ_FLUSH))
  339. virtblk_bio_send_flush(vbr);
  340. else
  341. virtblk_bio_send_data(vbr);
  342. }
  343. /* return id (s/n) string for *disk to *id_str
  344. */
  345. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  346. {
  347. struct virtio_blk *vblk = disk->private_data;
  348. struct request *req;
  349. struct bio *bio;
  350. int err;
  351. bio = bio_map_kern(vblk->disk->queue, id_str, VIRTIO_BLK_ID_BYTES,
  352. GFP_KERNEL);
  353. if (IS_ERR(bio))
  354. return PTR_ERR(bio);
  355. req = blk_make_request(vblk->disk->queue, bio, GFP_KERNEL);
  356. if (IS_ERR(req)) {
  357. bio_put(bio);
  358. return PTR_ERR(req);
  359. }
  360. req->cmd_type = REQ_TYPE_SPECIAL;
  361. err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  362. blk_put_request(req);
  363. return err;
  364. }
  365. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  366. unsigned int cmd, unsigned long data)
  367. {
  368. struct gendisk *disk = bdev->bd_disk;
  369. struct virtio_blk *vblk = disk->private_data;
  370. /*
  371. * Only allow the generic SCSI ioctls if the host can support it.
  372. */
  373. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  374. return -ENOTTY;
  375. return scsi_cmd_blk_ioctl(bdev, mode, cmd,
  376. (void __user *)data);
  377. }
  378. /* We provide getgeo only to please some old bootloader/partitioning tools */
  379. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  380. {
  381. struct virtio_blk *vblk = bd->bd_disk->private_data;
  382. /* see if the host passed in geometry config */
  383. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
  384. virtio_cread(vblk->vdev, struct virtio_blk_config,
  385. geometry.cylinders, &geo->cylinders);
  386. virtio_cread(vblk->vdev, struct virtio_blk_config,
  387. geometry.heads, &geo->heads);
  388. virtio_cread(vblk->vdev, struct virtio_blk_config,
  389. geometry.sectors, &geo->sectors);
  390. } else {
  391. /* some standard values, similar to sd */
  392. geo->heads = 1 << 6;
  393. geo->sectors = 1 << 5;
  394. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  395. }
  396. return 0;
  397. }
  398. static const struct block_device_operations virtblk_fops = {
  399. .ioctl = virtblk_ioctl,
  400. .owner = THIS_MODULE,
  401. .getgeo = virtblk_getgeo,
  402. };
  403. static int index_to_minor(int index)
  404. {
  405. return index << PART_BITS;
  406. }
  407. static int minor_to_index(int minor)
  408. {
  409. return minor >> PART_BITS;
  410. }
  411. static ssize_t virtblk_serial_show(struct device *dev,
  412. struct device_attribute *attr, char *buf)
  413. {
  414. struct gendisk *disk = dev_to_disk(dev);
  415. int err;
  416. /* sysfs gives us a PAGE_SIZE buffer */
  417. BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
  418. buf[VIRTIO_BLK_ID_BYTES] = '\0';
  419. err = virtblk_get_id(disk, buf);
  420. if (!err)
  421. return strlen(buf);
  422. if (err == -EIO) /* Unsupported? Make it empty. */
  423. return 0;
  424. return err;
  425. }
  426. DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
  427. static void virtblk_config_changed_work(struct work_struct *work)
  428. {
  429. struct virtio_blk *vblk =
  430. container_of(work, struct virtio_blk, config_work);
  431. struct virtio_device *vdev = vblk->vdev;
  432. struct request_queue *q = vblk->disk->queue;
  433. char cap_str_2[10], cap_str_10[10];
  434. char *envp[] = { "RESIZE=1", NULL };
  435. u64 capacity, size;
  436. mutex_lock(&vblk->config_lock);
  437. if (!vblk->config_enable)
  438. goto done;
  439. /* Host must always specify the capacity. */
  440. virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
  441. /* If capacity is too big, truncate with warning. */
  442. if ((sector_t)capacity != capacity) {
  443. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  444. (unsigned long long)capacity);
  445. capacity = (sector_t)-1;
  446. }
  447. size = capacity * queue_logical_block_size(q);
  448. string_get_size(size, STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  449. string_get_size(size, STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  450. dev_notice(&vdev->dev,
  451. "new size: %llu %d-byte logical blocks (%s/%s)\n",
  452. (unsigned long long)capacity,
  453. queue_logical_block_size(q),
  454. cap_str_10, cap_str_2);
  455. set_capacity(vblk->disk, capacity);
  456. revalidate_disk(vblk->disk);
  457. kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
  458. done:
  459. mutex_unlock(&vblk->config_lock);
  460. }
  461. static void virtblk_config_changed(struct virtio_device *vdev)
  462. {
  463. struct virtio_blk *vblk = vdev->priv;
  464. queue_work(virtblk_wq, &vblk->config_work);
  465. }
  466. static int init_vq(struct virtio_blk *vblk)
  467. {
  468. int err = 0;
  469. /* We expect one virtqueue, for output. */
  470. vblk->vq = virtio_find_single_vq(vblk->vdev, virtblk_done, "requests");
  471. if (IS_ERR(vblk->vq))
  472. err = PTR_ERR(vblk->vq);
  473. return err;
  474. }
  475. /*
  476. * Legacy naming scheme used for virtio devices. We are stuck with it for
  477. * virtio blk but don't ever use it for any new driver.
  478. */
  479. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  480. {
  481. const int base = 'z' - 'a' + 1;
  482. char *begin = buf + strlen(prefix);
  483. char *end = buf + buflen;
  484. char *p;
  485. int unit;
  486. p = end - 1;
  487. *p = '\0';
  488. unit = base;
  489. do {
  490. if (p == begin)
  491. return -EINVAL;
  492. *--p = 'a' + (index % unit);
  493. index = (index / unit) - 1;
  494. } while (index >= 0);
  495. memmove(begin, p, end - p);
  496. memcpy(buf, prefix, strlen(prefix));
  497. return 0;
  498. }
  499. static int virtblk_get_cache_mode(struct virtio_device *vdev)
  500. {
  501. u8 writeback;
  502. int err;
  503. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
  504. struct virtio_blk_config, wce,
  505. &writeback);
  506. if (err)
  507. writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_WCE);
  508. return writeback;
  509. }
  510. static void virtblk_update_cache_mode(struct virtio_device *vdev)
  511. {
  512. u8 writeback = virtblk_get_cache_mode(vdev);
  513. struct virtio_blk *vblk = vdev->priv;
  514. if (writeback)
  515. blk_queue_flush(vblk->disk->queue, REQ_FLUSH);
  516. else
  517. blk_queue_flush(vblk->disk->queue, 0);
  518. revalidate_disk(vblk->disk);
  519. }
  520. static const char *const virtblk_cache_types[] = {
  521. "write through", "write back"
  522. };
  523. static ssize_t
  524. virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
  525. const char *buf, size_t count)
  526. {
  527. struct gendisk *disk = dev_to_disk(dev);
  528. struct virtio_blk *vblk = disk->private_data;
  529. struct virtio_device *vdev = vblk->vdev;
  530. int i;
  531. BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
  532. for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
  533. if (sysfs_streq(buf, virtblk_cache_types[i]))
  534. break;
  535. if (i < 0)
  536. return -EINVAL;
  537. virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
  538. virtblk_update_cache_mode(vdev);
  539. return count;
  540. }
  541. static ssize_t
  542. virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
  543. char *buf)
  544. {
  545. struct gendisk *disk = dev_to_disk(dev);
  546. struct virtio_blk *vblk = disk->private_data;
  547. u8 writeback = virtblk_get_cache_mode(vblk->vdev);
  548. BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
  549. return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
  550. }
  551. static const struct device_attribute dev_attr_cache_type_ro =
  552. __ATTR(cache_type, S_IRUGO,
  553. virtblk_cache_type_show, NULL);
  554. static const struct device_attribute dev_attr_cache_type_rw =
  555. __ATTR(cache_type, S_IRUGO|S_IWUSR,
  556. virtblk_cache_type_show, virtblk_cache_type_store);
  557. static int virtblk_probe(struct virtio_device *vdev)
  558. {
  559. struct virtio_blk *vblk;
  560. struct request_queue *q;
  561. int err, index;
  562. int pool_size;
  563. u64 cap;
  564. u32 v, blk_size, sg_elems, opt_io_size;
  565. u16 min_io_size;
  566. u8 physical_block_exp, alignment_offset;
  567. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  568. GFP_KERNEL);
  569. if (err < 0)
  570. goto out;
  571. index = err;
  572. /* We need to know how many segments before we allocate. */
  573. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  574. struct virtio_blk_config, seg_max,
  575. &sg_elems);
  576. /* We need at least one SG element, whatever they say. */
  577. if (err || !sg_elems)
  578. sg_elems = 1;
  579. /* We need an extra sg elements at head and tail. */
  580. sg_elems += 2;
  581. vdev->priv = vblk = kmalloc(sizeof(*vblk) +
  582. sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
  583. if (!vblk) {
  584. err = -ENOMEM;
  585. goto out_free_index;
  586. }
  587. init_waitqueue_head(&vblk->queue_wait);
  588. vblk->vdev = vdev;
  589. vblk->sg_elems = sg_elems;
  590. sg_init_table(vblk->sg, vblk->sg_elems);
  591. mutex_init(&vblk->config_lock);
  592. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  593. vblk->config_enable = true;
  594. err = init_vq(vblk);
  595. if (err)
  596. goto out_free_vblk;
  597. pool_size = sizeof(struct virtblk_req);
  598. if (use_bio)
  599. pool_size += sizeof(struct scatterlist) * sg_elems;
  600. vblk->pool = mempool_create_kmalloc_pool(1, pool_size);
  601. if (!vblk->pool) {
  602. err = -ENOMEM;
  603. goto out_free_vq;
  604. }
  605. /* FIXME: How many partitions? How long is a piece of string? */
  606. vblk->disk = alloc_disk(1 << PART_BITS);
  607. if (!vblk->disk) {
  608. err = -ENOMEM;
  609. goto out_mempool;
  610. }
  611. q = vblk->disk->queue = blk_init_queue(virtblk_request, NULL);
  612. if (!q) {
  613. err = -ENOMEM;
  614. goto out_put_disk;
  615. }
  616. if (use_bio)
  617. blk_queue_make_request(q, virtblk_make_request);
  618. q->queuedata = vblk;
  619. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  620. vblk->disk->major = major;
  621. vblk->disk->first_minor = index_to_minor(index);
  622. vblk->disk->private_data = vblk;
  623. vblk->disk->fops = &virtblk_fops;
  624. vblk->disk->driverfs_dev = &vdev->dev;
  625. vblk->index = index;
  626. /* configure queue flush support */
  627. virtblk_update_cache_mode(vdev);
  628. /* If disk is read-only in the host, the guest should obey */
  629. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  630. set_disk_ro(vblk->disk, 1);
  631. /* Host must always specify the capacity. */
  632. virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
  633. /* If capacity is too big, truncate with warning. */
  634. if ((sector_t)cap != cap) {
  635. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  636. (unsigned long long)cap);
  637. cap = (sector_t)-1;
  638. }
  639. set_capacity(vblk->disk, cap);
  640. /* We can handle whatever the host told us to handle. */
  641. blk_queue_max_segments(q, vblk->sg_elems-2);
  642. /* No need to bounce any requests */
  643. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  644. /* No real sector limit. */
  645. blk_queue_max_hw_sectors(q, -1U);
  646. /* Host can optionally specify maximum segment size and number of
  647. * segments. */
  648. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
  649. struct virtio_blk_config, size_max, &v);
  650. if (!err)
  651. blk_queue_max_segment_size(q, v);
  652. else
  653. blk_queue_max_segment_size(q, -1U);
  654. /* Host can optionally specify the block size of the device */
  655. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
  656. struct virtio_blk_config, blk_size,
  657. &blk_size);
  658. if (!err)
  659. blk_queue_logical_block_size(q, blk_size);
  660. else
  661. blk_size = queue_logical_block_size(q);
  662. /* Use topology information if available */
  663. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  664. struct virtio_blk_config, physical_block_exp,
  665. &physical_block_exp);
  666. if (!err && physical_block_exp)
  667. blk_queue_physical_block_size(q,
  668. blk_size * (1 << physical_block_exp));
  669. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  670. struct virtio_blk_config, alignment_offset,
  671. &alignment_offset);
  672. if (!err && alignment_offset)
  673. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  674. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  675. struct virtio_blk_config, min_io_size,
  676. &min_io_size);
  677. if (!err && min_io_size)
  678. blk_queue_io_min(q, blk_size * min_io_size);
  679. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  680. struct virtio_blk_config, opt_io_size,
  681. &opt_io_size);
  682. if (!err && opt_io_size)
  683. blk_queue_io_opt(q, blk_size * opt_io_size);
  684. add_disk(vblk->disk);
  685. err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
  686. if (err)
  687. goto out_del_disk;
  688. if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
  689. err = device_create_file(disk_to_dev(vblk->disk),
  690. &dev_attr_cache_type_rw);
  691. else
  692. err = device_create_file(disk_to_dev(vblk->disk),
  693. &dev_attr_cache_type_ro);
  694. if (err)
  695. goto out_del_disk;
  696. return 0;
  697. out_del_disk:
  698. del_gendisk(vblk->disk);
  699. blk_cleanup_queue(vblk->disk->queue);
  700. out_put_disk:
  701. put_disk(vblk->disk);
  702. out_mempool:
  703. mempool_destroy(vblk->pool);
  704. out_free_vq:
  705. vdev->config->del_vqs(vdev);
  706. out_free_vblk:
  707. kfree(vblk);
  708. out_free_index:
  709. ida_simple_remove(&vd_index_ida, index);
  710. out:
  711. return err;
  712. }
  713. static void virtblk_remove(struct virtio_device *vdev)
  714. {
  715. struct virtio_blk *vblk = vdev->priv;
  716. int index = vblk->index;
  717. int refc;
  718. /* Prevent config work handler from accessing the device. */
  719. mutex_lock(&vblk->config_lock);
  720. vblk->config_enable = false;
  721. mutex_unlock(&vblk->config_lock);
  722. del_gendisk(vblk->disk);
  723. blk_cleanup_queue(vblk->disk->queue);
  724. /* Stop all the virtqueues. */
  725. vdev->config->reset(vdev);
  726. flush_work(&vblk->config_work);
  727. refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
  728. put_disk(vblk->disk);
  729. mempool_destroy(vblk->pool);
  730. vdev->config->del_vqs(vdev);
  731. kfree(vblk);
  732. /* Only free device id if we don't have any users */
  733. if (refc == 1)
  734. ida_simple_remove(&vd_index_ida, index);
  735. }
  736. #ifdef CONFIG_PM_SLEEP
  737. static int virtblk_freeze(struct virtio_device *vdev)
  738. {
  739. struct virtio_blk *vblk = vdev->priv;
  740. /* Ensure we don't receive any more interrupts */
  741. vdev->config->reset(vdev);
  742. /* Prevent config work handler from accessing the device. */
  743. mutex_lock(&vblk->config_lock);
  744. vblk->config_enable = false;
  745. mutex_unlock(&vblk->config_lock);
  746. flush_work(&vblk->config_work);
  747. spin_lock_irq(vblk->disk->queue->queue_lock);
  748. blk_stop_queue(vblk->disk->queue);
  749. spin_unlock_irq(vblk->disk->queue->queue_lock);
  750. blk_sync_queue(vblk->disk->queue);
  751. vdev->config->del_vqs(vdev);
  752. return 0;
  753. }
  754. static int virtblk_restore(struct virtio_device *vdev)
  755. {
  756. struct virtio_blk *vblk = vdev->priv;
  757. int ret;
  758. vblk->config_enable = true;
  759. ret = init_vq(vdev->priv);
  760. if (!ret) {
  761. spin_lock_irq(vblk->disk->queue->queue_lock);
  762. blk_start_queue(vblk->disk->queue);
  763. spin_unlock_irq(vblk->disk->queue->queue_lock);
  764. }
  765. return ret;
  766. }
  767. #endif
  768. static const struct virtio_device_id id_table[] = {
  769. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  770. { 0 },
  771. };
  772. static unsigned int features[] = {
  773. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  774. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  775. VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
  776. };
  777. static struct virtio_driver virtio_blk = {
  778. .feature_table = features,
  779. .feature_table_size = ARRAY_SIZE(features),
  780. .driver.name = KBUILD_MODNAME,
  781. .driver.owner = THIS_MODULE,
  782. .id_table = id_table,
  783. .probe = virtblk_probe,
  784. .remove = virtblk_remove,
  785. .config_changed = virtblk_config_changed,
  786. #ifdef CONFIG_PM_SLEEP
  787. .freeze = virtblk_freeze,
  788. .restore = virtblk_restore,
  789. #endif
  790. };
  791. static int __init init(void)
  792. {
  793. int error;
  794. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  795. if (!virtblk_wq)
  796. return -ENOMEM;
  797. major = register_blkdev(0, "virtblk");
  798. if (major < 0) {
  799. error = major;
  800. goto out_destroy_workqueue;
  801. }
  802. error = register_virtio_driver(&virtio_blk);
  803. if (error)
  804. goto out_unregister_blkdev;
  805. return 0;
  806. out_unregister_blkdev:
  807. unregister_blkdev(major, "virtblk");
  808. out_destroy_workqueue:
  809. destroy_workqueue(virtblk_wq);
  810. return error;
  811. }
  812. static void __exit fini(void)
  813. {
  814. unregister_blkdev(major, "virtblk");
  815. unregister_virtio_driver(&virtio_blk);
  816. destroy_workqueue(virtblk_wq);
  817. }
  818. module_init(init);
  819. module_exit(fini);
  820. MODULE_DEVICE_TABLE(virtio, id_table);
  821. MODULE_DESCRIPTION("Virtio block driver");
  822. MODULE_LICENSE("GPL");