virtio_blk.c 24 KB

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