blkback.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /******************************************************************************
  2. *
  3. * Back-end of the driver for virtual block devices. This portion of the
  4. * driver exports a 'unified' block-device interface that can be accessed
  5. * by any operating system that implements a compatible front end. A
  6. * reference front-end implementation can be found in:
  7. * drivers/block/xen-blkfront.c
  8. *
  9. * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
  10. * Copyright (c) 2005, Christopher Clark
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License version 2
  14. * as published by the Free Software Foundation; or, when distributed
  15. * separately from the Linux kernel or incorporated into other
  16. * software packages, subject to the following license:
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this source file (the "Software"), to deal in the Software without
  20. * restriction, including without limitation the rights to use, copy, modify,
  21. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  22. * and to permit persons to whom the Software is furnished to do so, subject to
  23. * the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in
  26. * all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  33. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  34. * IN THE SOFTWARE.
  35. */
  36. #include <linux/spinlock.h>
  37. #include <linux/kthread.h>
  38. #include <linux/list.h>
  39. #include <linux/delay.h>
  40. #include <linux/freezer.h>
  41. #include <linux/loop.h>
  42. #include <linux/falloc.h>
  43. #include <linux/fs.h>
  44. #include <xen/events.h>
  45. #include <xen/page.h>
  46. #include <asm/xen/hypervisor.h>
  47. #include <asm/xen/hypercall.h>
  48. #include "common.h"
  49. /*
  50. * These are rather arbitrary. They are fairly large because adjacent requests
  51. * pulled from a communication ring are quite likely to end up being part of
  52. * the same scatter/gather request at the disc.
  53. *
  54. * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
  55. *
  56. * This will increase the chances of being able to write whole tracks.
  57. * 64 should be enough to keep us competitive with Linux.
  58. */
  59. static int xen_blkif_reqs = 64;
  60. module_param_named(reqs, xen_blkif_reqs, int, 0);
  61. MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
  62. /* Run-time switchable: /sys/module/blkback/parameters/ */
  63. static unsigned int log_stats;
  64. module_param(log_stats, int, 0644);
  65. /*
  66. * Each outstanding request that we've passed to the lower device layers has a
  67. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  68. * the pendcnt towards zero. When it hits zero, the specified domain has a
  69. * response queued for it, with the saved 'id' passed back.
  70. */
  71. struct pending_req {
  72. struct xen_blkif *blkif;
  73. u64 id;
  74. int nr_pages;
  75. atomic_t pendcnt;
  76. unsigned short operation;
  77. int status;
  78. struct list_head free_list;
  79. };
  80. #define BLKBACK_INVALID_HANDLE (~0)
  81. struct xen_blkbk {
  82. struct pending_req *pending_reqs;
  83. /* List of all 'pending_req' available */
  84. struct list_head pending_free;
  85. /* And its spinlock. */
  86. spinlock_t pending_free_lock;
  87. wait_queue_head_t pending_free_wq;
  88. /* The list of all pages that are available. */
  89. struct page **pending_pages;
  90. /* And the grant handles that are available. */
  91. grant_handle_t *pending_grant_handles;
  92. };
  93. static struct xen_blkbk *blkbk;
  94. /*
  95. * Little helpful macro to figure out the index and virtual address of the
  96. * pending_pages[..]. For each 'pending_req' we have have up to
  97. * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
  98. * 10 and would index in the pending_pages[..].
  99. */
  100. static inline int vaddr_pagenr(struct pending_req *req, int seg)
  101. {
  102. return (req - blkbk->pending_reqs) *
  103. BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
  104. }
  105. #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
  106. static inline unsigned long vaddr(struct pending_req *req, int seg)
  107. {
  108. unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
  109. return (unsigned long)pfn_to_kaddr(pfn);
  110. }
  111. #define pending_handle(_req, _seg) \
  112. (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
  113. static int do_block_io_op(struct xen_blkif *blkif);
  114. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  115. struct blkif_request *req,
  116. struct pending_req *pending_req);
  117. static void make_response(struct xen_blkif *blkif, u64 id,
  118. unsigned short op, int st);
  119. /*
  120. * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  121. */
  122. static struct pending_req *alloc_req(void)
  123. {
  124. struct pending_req *req = NULL;
  125. unsigned long flags;
  126. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  127. if (!list_empty(&blkbk->pending_free)) {
  128. req = list_entry(blkbk->pending_free.next, struct pending_req,
  129. free_list);
  130. list_del(&req->free_list);
  131. }
  132. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  133. return req;
  134. }
  135. /*
  136. * Return the 'pending_req' structure back to the freepool. We also
  137. * wake up the thread if it was waiting for a free page.
  138. */
  139. static void free_req(struct pending_req *req)
  140. {
  141. unsigned long flags;
  142. int was_empty;
  143. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  144. was_empty = list_empty(&blkbk->pending_free);
  145. list_add(&req->free_list, &blkbk->pending_free);
  146. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  147. if (was_empty)
  148. wake_up(&blkbk->pending_free_wq);
  149. }
  150. /*
  151. * Routines for managing virtual block devices (vbds).
  152. */
  153. static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
  154. int operation)
  155. {
  156. struct xen_vbd *vbd = &blkif->vbd;
  157. int rc = -EACCES;
  158. if ((operation != READ) && vbd->readonly)
  159. goto out;
  160. if (likely(req->nr_sects)) {
  161. blkif_sector_t end = req->sector_number + req->nr_sects;
  162. if (unlikely(end < req->sector_number))
  163. goto out;
  164. if (unlikely(end > vbd_sz(vbd)))
  165. goto out;
  166. }
  167. req->dev = vbd->pdevice;
  168. req->bdev = vbd->bdev;
  169. rc = 0;
  170. out:
  171. return rc;
  172. }
  173. static void xen_vbd_resize(struct xen_blkif *blkif)
  174. {
  175. struct xen_vbd *vbd = &blkif->vbd;
  176. struct xenbus_transaction xbt;
  177. int err;
  178. struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
  179. unsigned long long new_size = vbd_sz(vbd);
  180. pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  181. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  182. pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
  183. vbd->size = new_size;
  184. again:
  185. err = xenbus_transaction_start(&xbt);
  186. if (err) {
  187. pr_warn(DRV_PFX "Error starting transaction");
  188. return;
  189. }
  190. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  191. (unsigned long long)vbd_sz(vbd));
  192. if (err) {
  193. pr_warn(DRV_PFX "Error writing new size");
  194. goto abort;
  195. }
  196. /*
  197. * Write the current state; we will use this to synchronize
  198. * the front-end. If the current state is "connected" the
  199. * front-end will get the new size information online.
  200. */
  201. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  202. if (err) {
  203. pr_warn(DRV_PFX "Error writing the state");
  204. goto abort;
  205. }
  206. err = xenbus_transaction_end(xbt, 0);
  207. if (err == -EAGAIN)
  208. goto again;
  209. if (err)
  210. pr_warn(DRV_PFX "Error ending transaction");
  211. return;
  212. abort:
  213. xenbus_transaction_end(xbt, 1);
  214. }
  215. /*
  216. * Notification from the guest OS.
  217. */
  218. static void blkif_notify_work(struct xen_blkif *blkif)
  219. {
  220. blkif->waiting_reqs = 1;
  221. wake_up(&blkif->wq);
  222. }
  223. irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
  224. {
  225. blkif_notify_work(dev_id);
  226. return IRQ_HANDLED;
  227. }
  228. /*
  229. * SCHEDULER FUNCTIONS
  230. */
  231. static void print_stats(struct xen_blkif *blkif)
  232. {
  233. pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
  234. " | ds %4d\n",
  235. current->comm, blkif->st_oo_req,
  236. blkif->st_rd_req, blkif->st_wr_req,
  237. blkif->st_f_req, blkif->st_ds_req);
  238. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  239. blkif->st_rd_req = 0;
  240. blkif->st_wr_req = 0;
  241. blkif->st_oo_req = 0;
  242. blkif->st_ds_req = 0;
  243. }
  244. int xen_blkif_schedule(void *arg)
  245. {
  246. struct xen_blkif *blkif = arg;
  247. struct xen_vbd *vbd = &blkif->vbd;
  248. xen_blkif_get(blkif);
  249. while (!kthread_should_stop()) {
  250. if (try_to_freeze())
  251. continue;
  252. if (unlikely(vbd->size != vbd_sz(vbd)))
  253. xen_vbd_resize(blkif);
  254. wait_event_interruptible(
  255. blkif->wq,
  256. blkif->waiting_reqs || kthread_should_stop());
  257. wait_event_interruptible(
  258. blkbk->pending_free_wq,
  259. !list_empty(&blkbk->pending_free) ||
  260. kthread_should_stop());
  261. blkif->waiting_reqs = 0;
  262. smp_mb(); /* clear flag *before* checking for work */
  263. if (do_block_io_op(blkif))
  264. blkif->waiting_reqs = 1;
  265. if (log_stats && time_after(jiffies, blkif->st_print))
  266. print_stats(blkif);
  267. }
  268. if (log_stats)
  269. print_stats(blkif);
  270. blkif->xenblkd = NULL;
  271. xen_blkif_put(blkif);
  272. return 0;
  273. }
  274. struct seg_buf {
  275. unsigned long buf;
  276. unsigned int nsec;
  277. };
  278. /*
  279. * Unmap the grant references, and also remove the M2P over-rides
  280. * used in the 'pending_req'.
  281. */
  282. static void xen_blkbk_unmap(struct pending_req *req)
  283. {
  284. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  285. unsigned int i, invcount = 0;
  286. grant_handle_t handle;
  287. int ret;
  288. for (i = 0; i < req->nr_pages; i++) {
  289. handle = pending_handle(req, i);
  290. if (handle == BLKBACK_INVALID_HANDLE)
  291. continue;
  292. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  293. GNTMAP_host_map, handle);
  294. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  295. invcount++;
  296. }
  297. ret = HYPERVISOR_grant_table_op(
  298. GNTTABOP_unmap_grant_ref, unmap, invcount);
  299. BUG_ON(ret);
  300. /*
  301. * Note, we use invcount, so nr->pages, so we can't index
  302. * using vaddr(req, i).
  303. */
  304. for (i = 0; i < invcount; i++) {
  305. ret = m2p_remove_override(
  306. virt_to_page(unmap[i].host_addr), false);
  307. if (ret) {
  308. pr_alert(DRV_PFX "Failed to remove M2P override for %lx\n",
  309. (unsigned long)unmap[i].host_addr);
  310. continue;
  311. }
  312. }
  313. }
  314. static int xen_blkbk_map(struct blkif_request *req,
  315. struct pending_req *pending_req,
  316. struct seg_buf seg[])
  317. {
  318. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  319. int i;
  320. int nseg = req->nr_segments;
  321. int ret = 0;
  322. /*
  323. * Fill out preq.nr_sects with proper amount of sectors, and setup
  324. * assign map[..] with the PFN of the page in our domain with the
  325. * corresponding grant reference for each page.
  326. */
  327. for (i = 0; i < nseg; i++) {
  328. uint32_t flags;
  329. flags = GNTMAP_host_map;
  330. if (pending_req->operation != BLKIF_OP_READ)
  331. flags |= GNTMAP_readonly;
  332. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  333. req->u.rw.seg[i].gref,
  334. pending_req->blkif->domid);
  335. }
  336. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
  337. BUG_ON(ret);
  338. /*
  339. * Now swizzle the MFN in our domain with the MFN from the other domain
  340. * so that when we access vaddr(pending_req,i) it has the contents of
  341. * the page from the other domain.
  342. */
  343. for (i = 0; i < nseg; i++) {
  344. if (unlikely(map[i].status != 0)) {
  345. pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
  346. map[i].handle = BLKBACK_INVALID_HANDLE;
  347. ret |= 1;
  348. }
  349. pending_handle(pending_req, i) = map[i].handle;
  350. if (ret)
  351. continue;
  352. ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
  353. blkbk->pending_page(pending_req, i), false);
  354. if (ret) {
  355. pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n",
  356. (unsigned long)map[i].dev_bus_addr, ret);
  357. /* We could switch over to GNTTABOP_copy */
  358. continue;
  359. }
  360. seg[i].buf = map[i].dev_bus_addr |
  361. (req->u.rw.seg[i].first_sect << 9);
  362. }
  363. return ret;
  364. }
  365. static void xen_blk_discard(struct xen_blkif *blkif, struct blkif_request *req)
  366. {
  367. int err = 0;
  368. int status = BLKIF_RSP_OKAY;
  369. struct block_device *bdev = blkif->vbd.bdev;
  370. if (blkif->blk_backend_type == BLKIF_BACKEND_PHY)
  371. /* just forward the discard request */
  372. err = blkdev_issue_discard(bdev,
  373. req->u.discard.sector_number,
  374. req->u.discard.nr_sectors,
  375. GFP_KERNEL, 0);
  376. else if (blkif->blk_backend_type == BLKIF_BACKEND_FILE) {
  377. /* punch a hole in the backing file */
  378. struct loop_device *lo = bdev->bd_disk->private_data;
  379. struct file *file = lo->lo_backing_file;
  380. if (file->f_op->fallocate)
  381. err = file->f_op->fallocate(file,
  382. FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
  383. req->u.discard.sector_number << 9,
  384. req->u.discard.nr_sectors << 9);
  385. else
  386. err = -EOPNOTSUPP;
  387. } else
  388. err = -EOPNOTSUPP;
  389. if (err == -EOPNOTSUPP) {
  390. pr_debug(DRV_PFX "discard op failed, not supported\n");
  391. status = BLKIF_RSP_EOPNOTSUPP;
  392. } else if (err)
  393. status = BLKIF_RSP_ERROR;
  394. make_response(blkif, req->id, req->operation, status);
  395. }
  396. static void xen_blk_drain_io(struct xen_blkif *blkif)
  397. {
  398. atomic_set(&blkif->drain, 1);
  399. do {
  400. /* The initial value is one, and one refcnt taken at the
  401. * start of the xen_blkif_schedule thread. */
  402. if (atomic_read(&blkif->refcnt) <= 2)
  403. break;
  404. wait_for_completion_interruptible_timeout(
  405. &blkif->drain_complete, HZ);
  406. if (!atomic_read(&blkif->drain))
  407. break;
  408. } while (!kthread_should_stop());
  409. atomic_set(&blkif->drain, 0);
  410. }
  411. /*
  412. * Completion callback on the bio's. Called as bh->b_end_io()
  413. */
  414. static void __end_block_io_op(struct pending_req *pending_req, int error)
  415. {
  416. /* An error fails the entire request. */
  417. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  418. (error == -EOPNOTSUPP)) {
  419. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  420. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  421. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  422. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  423. (error == -EOPNOTSUPP)) {
  424. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  425. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  426. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  427. } else if (error) {
  428. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  429. " error=%d\n", error);
  430. pending_req->status = BLKIF_RSP_ERROR;
  431. }
  432. /*
  433. * If all of the bio's have completed it is time to unmap
  434. * the grant references associated with 'request' and provide
  435. * the proper response on the ring.
  436. */
  437. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  438. xen_blkbk_unmap(pending_req);
  439. make_response(pending_req->blkif, pending_req->id,
  440. pending_req->operation, pending_req->status);
  441. xen_blkif_put(pending_req->blkif);
  442. if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
  443. if (atomic_read(&pending_req->blkif->drain))
  444. complete(&pending_req->blkif->drain_complete);
  445. }
  446. free_req(pending_req);
  447. }
  448. }
  449. /*
  450. * bio callback.
  451. */
  452. static void end_block_io_op(struct bio *bio, int error)
  453. {
  454. __end_block_io_op(bio->bi_private, error);
  455. bio_put(bio);
  456. }
  457. /*
  458. * Function to copy the from the ring buffer the 'struct blkif_request'
  459. * (which has the sectors we want, number of them, grant references, etc),
  460. * and transmute it to the block API to hand it over to the proper block disk.
  461. */
  462. static int
  463. __do_block_io_op(struct xen_blkif *blkif)
  464. {
  465. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  466. struct blkif_request req;
  467. struct pending_req *pending_req;
  468. RING_IDX rc, rp;
  469. int more_to_do = 0;
  470. rc = blk_rings->common.req_cons;
  471. rp = blk_rings->common.sring->req_prod;
  472. rmb(); /* Ensure we see queued requests up to 'rp'. */
  473. while (rc != rp) {
  474. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  475. break;
  476. if (kthread_should_stop()) {
  477. more_to_do = 1;
  478. break;
  479. }
  480. pending_req = alloc_req();
  481. if (NULL == pending_req) {
  482. blkif->st_oo_req++;
  483. more_to_do = 1;
  484. break;
  485. }
  486. switch (blkif->blk_protocol) {
  487. case BLKIF_PROTOCOL_NATIVE:
  488. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  489. break;
  490. case BLKIF_PROTOCOL_X86_32:
  491. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  492. break;
  493. case BLKIF_PROTOCOL_X86_64:
  494. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  495. break;
  496. default:
  497. BUG();
  498. }
  499. blk_rings->common.req_cons = ++rc; /* before make_response() */
  500. /* Apply all sanity checks to /private copy/ of request. */
  501. barrier();
  502. if (dispatch_rw_block_io(blkif, &req, pending_req))
  503. break;
  504. /* Yield point for this unbounded loop. */
  505. cond_resched();
  506. }
  507. return more_to_do;
  508. }
  509. static int
  510. do_block_io_op(struct xen_blkif *blkif)
  511. {
  512. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  513. int more_to_do;
  514. do {
  515. more_to_do = __do_block_io_op(blkif);
  516. if (more_to_do)
  517. break;
  518. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  519. } while (more_to_do);
  520. return more_to_do;
  521. }
  522. /*
  523. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  524. * and call the 'submit_bio' to pass it to the underlying storage.
  525. */
  526. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  527. struct blkif_request *req,
  528. struct pending_req *pending_req)
  529. {
  530. struct phys_req preq;
  531. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  532. unsigned int nseg;
  533. struct bio *bio = NULL;
  534. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  535. int i, nbio = 0;
  536. int operation;
  537. struct blk_plug plug;
  538. bool drain = false;
  539. switch (req->operation) {
  540. case BLKIF_OP_READ:
  541. blkif->st_rd_req++;
  542. operation = READ;
  543. break;
  544. case BLKIF_OP_WRITE:
  545. blkif->st_wr_req++;
  546. operation = WRITE_ODIRECT;
  547. break;
  548. case BLKIF_OP_WRITE_BARRIER:
  549. drain = true;
  550. case BLKIF_OP_FLUSH_DISKCACHE:
  551. blkif->st_f_req++;
  552. operation = WRITE_FLUSH;
  553. break;
  554. case BLKIF_OP_DISCARD:
  555. blkif->st_ds_req++;
  556. operation = REQ_DISCARD;
  557. break;
  558. default:
  559. operation = 0; /* make gcc happy */
  560. goto fail_response;
  561. break;
  562. }
  563. /* Check that the number of segments is sane. */
  564. nseg = req->nr_segments;
  565. if (unlikely(nseg == 0 && operation != WRITE_FLUSH &&
  566. operation != REQ_DISCARD) ||
  567. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  568. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  569. nseg);
  570. /* Haven't submitted any bio's yet. */
  571. goto fail_response;
  572. }
  573. preq.dev = req->handle;
  574. preq.sector_number = req->u.rw.sector_number;
  575. preq.nr_sects = 0;
  576. pending_req->blkif = blkif;
  577. pending_req->id = req->id;
  578. pending_req->operation = req->operation;
  579. pending_req->status = BLKIF_RSP_OKAY;
  580. pending_req->nr_pages = nseg;
  581. for (i = 0; i < nseg; i++) {
  582. seg[i].nsec = req->u.rw.seg[i].last_sect -
  583. req->u.rw.seg[i].first_sect + 1;
  584. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  585. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  586. goto fail_response;
  587. preq.nr_sects += seg[i].nsec;
  588. }
  589. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  590. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  591. operation == READ ? "read" : "write",
  592. preq.sector_number,
  593. preq.sector_number + preq.nr_sects, preq.dev);
  594. goto fail_response;
  595. }
  596. /*
  597. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  598. * is set there.
  599. */
  600. for (i = 0; i < nseg; i++) {
  601. if (((int)preq.sector_number|(int)seg[i].nsec) &
  602. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  603. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  604. blkif->domid);
  605. goto fail_response;
  606. }
  607. }
  608. /* Wait on all outstanding I/O's and once that has been completed
  609. * issue the WRITE_FLUSH.
  610. */
  611. if (drain)
  612. xen_blk_drain_io(pending_req->blkif);
  613. /*
  614. * If we have failed at this point, we need to undo the M2P override,
  615. * set gnttab_set_unmap_op on all of the grant references and perform
  616. * the hypercall to unmap the grants - that is all done in
  617. * xen_blkbk_unmap.
  618. */
  619. if (operation != REQ_DISCARD && xen_blkbk_map(req, pending_req, seg))
  620. goto fail_flush;
  621. /*
  622. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  623. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  624. */
  625. xen_blkif_get(blkif);
  626. for (i = 0; i < nseg; i++) {
  627. while ((bio == NULL) ||
  628. (bio_add_page(bio,
  629. blkbk->pending_page(pending_req, i),
  630. seg[i].nsec << 9,
  631. seg[i].buf & ~PAGE_MASK) == 0)) {
  632. bio = bio_alloc(GFP_KERNEL, nseg-i);
  633. if (unlikely(bio == NULL))
  634. goto fail_put_bio;
  635. biolist[nbio++] = bio;
  636. bio->bi_bdev = preq.bdev;
  637. bio->bi_private = pending_req;
  638. bio->bi_end_io = end_block_io_op;
  639. bio->bi_sector = preq.sector_number;
  640. }
  641. preq.sector_number += seg[i].nsec;
  642. }
  643. /* This will be hit if the operation was a flush or discard. */
  644. if (!bio) {
  645. BUG_ON(operation != WRITE_FLUSH && operation != REQ_DISCARD);
  646. if (operation == WRITE_FLUSH) {
  647. bio = bio_alloc(GFP_KERNEL, 0);
  648. if (unlikely(bio == NULL))
  649. goto fail_put_bio;
  650. biolist[nbio++] = bio;
  651. bio->bi_bdev = preq.bdev;
  652. bio->bi_private = pending_req;
  653. bio->bi_end_io = end_block_io_op;
  654. } else if (operation == REQ_DISCARD) {
  655. xen_blk_discard(blkif, req);
  656. xen_blkif_put(blkif);
  657. free_req(pending_req);
  658. return 0;
  659. }
  660. }
  661. /*
  662. * We set it one so that the last submit_bio does not have to call
  663. * atomic_inc.
  664. */
  665. atomic_set(&pending_req->pendcnt, nbio);
  666. /* Get a reference count for the disk queue and start sending I/O */
  667. blk_start_plug(&plug);
  668. for (i = 0; i < nbio; i++)
  669. submit_bio(operation, biolist[i]);
  670. /* Let the I/Os go.. */
  671. blk_finish_plug(&plug);
  672. if (operation == READ)
  673. blkif->st_rd_sect += preq.nr_sects;
  674. else if (operation & WRITE)
  675. blkif->st_wr_sect += preq.nr_sects;
  676. return 0;
  677. fail_flush:
  678. xen_blkbk_unmap(pending_req);
  679. fail_response:
  680. /* Haven't submitted any bio's yet. */
  681. make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
  682. free_req(pending_req);
  683. msleep(1); /* back off a bit */
  684. return -EIO;
  685. fail_put_bio:
  686. for (i = 0; i < nbio; i++)
  687. bio_put(biolist[i]);
  688. __end_block_io_op(pending_req, -EINVAL);
  689. msleep(1); /* back off a bit */
  690. return -EIO;
  691. }
  692. /*
  693. * Put a response on the ring on how the operation fared.
  694. */
  695. static void make_response(struct xen_blkif *blkif, u64 id,
  696. unsigned short op, int st)
  697. {
  698. struct blkif_response resp;
  699. unsigned long flags;
  700. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  701. int notify;
  702. resp.id = id;
  703. resp.operation = op;
  704. resp.status = st;
  705. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  706. /* Place on the response ring for the relevant domain. */
  707. switch (blkif->blk_protocol) {
  708. case BLKIF_PROTOCOL_NATIVE:
  709. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  710. &resp, sizeof(resp));
  711. break;
  712. case BLKIF_PROTOCOL_X86_32:
  713. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  714. &resp, sizeof(resp));
  715. break;
  716. case BLKIF_PROTOCOL_X86_64:
  717. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  718. &resp, sizeof(resp));
  719. break;
  720. default:
  721. BUG();
  722. }
  723. blk_rings->common.rsp_prod_pvt++;
  724. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  725. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  726. if (notify)
  727. notify_remote_via_irq(blkif->irq);
  728. }
  729. static int __init xen_blkif_init(void)
  730. {
  731. int i, mmap_pages;
  732. int rc = 0;
  733. if (!xen_pv_domain())
  734. return -ENODEV;
  735. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  736. if (!blkbk) {
  737. pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
  738. return -ENOMEM;
  739. }
  740. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  741. blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
  742. xen_blkif_reqs, GFP_KERNEL);
  743. blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
  744. mmap_pages, GFP_KERNEL);
  745. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  746. mmap_pages, GFP_KERNEL);
  747. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  748. !blkbk->pending_pages) {
  749. rc = -ENOMEM;
  750. goto out_of_memory;
  751. }
  752. for (i = 0; i < mmap_pages; i++) {
  753. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  754. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  755. if (blkbk->pending_pages[i] == NULL) {
  756. rc = -ENOMEM;
  757. goto out_of_memory;
  758. }
  759. }
  760. rc = xen_blkif_interface_init();
  761. if (rc)
  762. goto failed_init;
  763. INIT_LIST_HEAD(&blkbk->pending_free);
  764. spin_lock_init(&blkbk->pending_free_lock);
  765. init_waitqueue_head(&blkbk->pending_free_wq);
  766. for (i = 0; i < xen_blkif_reqs; i++)
  767. list_add_tail(&blkbk->pending_reqs[i].free_list,
  768. &blkbk->pending_free);
  769. rc = xen_blkif_xenbus_init();
  770. if (rc)
  771. goto failed_init;
  772. return 0;
  773. out_of_memory:
  774. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  775. failed_init:
  776. kfree(blkbk->pending_reqs);
  777. kfree(blkbk->pending_grant_handles);
  778. if (blkbk->pending_pages) {
  779. for (i = 0; i < mmap_pages; i++) {
  780. if (blkbk->pending_pages[i])
  781. __free_page(blkbk->pending_pages[i]);
  782. }
  783. kfree(blkbk->pending_pages);
  784. }
  785. kfree(blkbk);
  786. blkbk = NULL;
  787. return rc;
  788. }
  789. module_init(xen_blkif_init);
  790. MODULE_LICENSE("Dual BSD/GPL");
  791. MODULE_ALIAS("xen-backend:vbd");