virtio_blk.c 19 KB

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