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