virtio_blk.c 25 KB

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