virtio_blk.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. /* Prevent config work handler from accessing the device. */
  735. mutex_lock(&vblk->config_lock);
  736. vblk->config_enable = false;
  737. mutex_unlock(&vblk->config_lock);
  738. del_gendisk(vblk->disk);
  739. blk_cleanup_queue(vblk->disk->queue);
  740. /* Stop all the virtqueues. */
  741. vdev->config->reset(vdev);
  742. flush_work(&vblk->config_work);
  743. put_disk(vblk->disk);
  744. mempool_destroy(vblk->pool);
  745. vdev->config->del_vqs(vdev);
  746. kfree(vblk);
  747. ida_simple_remove(&vd_index_ida, index);
  748. }
  749. #ifdef CONFIG_PM
  750. static int virtblk_freeze(struct virtio_device *vdev)
  751. {
  752. struct virtio_blk *vblk = vdev->priv;
  753. /* Ensure we don't receive any more interrupts */
  754. vdev->config->reset(vdev);
  755. /* Prevent config work handler from accessing the device. */
  756. mutex_lock(&vblk->config_lock);
  757. vblk->config_enable = false;
  758. mutex_unlock(&vblk->config_lock);
  759. flush_work(&vblk->config_work);
  760. spin_lock_irq(vblk->disk->queue->queue_lock);
  761. blk_stop_queue(vblk->disk->queue);
  762. spin_unlock_irq(vblk->disk->queue->queue_lock);
  763. blk_sync_queue(vblk->disk->queue);
  764. vdev->config->del_vqs(vdev);
  765. return 0;
  766. }
  767. static int virtblk_restore(struct virtio_device *vdev)
  768. {
  769. struct virtio_blk *vblk = vdev->priv;
  770. int ret;
  771. vblk->config_enable = true;
  772. ret = init_vq(vdev->priv);
  773. if (!ret) {
  774. spin_lock_irq(vblk->disk->queue->queue_lock);
  775. blk_start_queue(vblk->disk->queue);
  776. spin_unlock_irq(vblk->disk->queue->queue_lock);
  777. }
  778. return ret;
  779. }
  780. #endif
  781. static const struct virtio_device_id id_table[] = {
  782. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  783. { 0 },
  784. };
  785. static unsigned int features[] = {
  786. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  787. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  788. VIRTIO_BLK_F_WCE, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE
  789. };
  790. static struct virtio_driver virtio_blk = {
  791. .feature_table = features,
  792. .feature_table_size = ARRAY_SIZE(features),
  793. .driver.name = KBUILD_MODNAME,
  794. .driver.owner = THIS_MODULE,
  795. .id_table = id_table,
  796. .probe = virtblk_probe,
  797. .remove = virtblk_remove,
  798. .config_changed = virtblk_config_changed,
  799. #ifdef CONFIG_PM
  800. .freeze = virtblk_freeze,
  801. .restore = virtblk_restore,
  802. #endif
  803. };
  804. static int __init init(void)
  805. {
  806. int error;
  807. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  808. if (!virtblk_wq)
  809. return -ENOMEM;
  810. major = register_blkdev(0, "virtblk");
  811. if (major < 0) {
  812. error = major;
  813. goto out_destroy_workqueue;
  814. }
  815. error = register_virtio_driver(&virtio_blk);
  816. if (error)
  817. goto out_unregister_blkdev;
  818. return 0;
  819. out_unregister_blkdev:
  820. unregister_blkdev(major, "virtblk");
  821. out_destroy_workqueue:
  822. destroy_workqueue(virtblk_wq);
  823. return error;
  824. }
  825. static void __exit fini(void)
  826. {
  827. unregister_blkdev(major, "virtblk");
  828. unregister_virtio_driver(&virtio_blk);
  829. destroy_workqueue(virtblk_wq);
  830. }
  831. module_init(init);
  832. module_exit(fini);
  833. MODULE_DEVICE_TABLE(virtio, id_table);
  834. MODULE_DESCRIPTION("Virtio block driver");
  835. MODULE_LICENSE("GPL");