blkback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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 <xen/events.h>
  42. #include <xen/page.h>
  43. #include <asm/xen/hypervisor.h>
  44. #include <asm/xen/hypercall.h>
  45. #include "common.h"
  46. /*
  47. * These are rather arbitrary. They are fairly large because adjacent requests
  48. * pulled from a communication ring are quite likely to end up being part of
  49. * the same scatter/gather request at the disc.
  50. *
  51. * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
  52. *
  53. * This will increase the chances of being able to write whole tracks.
  54. * 64 should be enough to keep us competitive with Linux.
  55. */
  56. static int xen_blkif_reqs = 64;
  57. module_param_named(reqs, xen_blkif_reqs, int, 0);
  58. MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
  59. /* Run-time switchable: /sys/module/blkback/parameters/ */
  60. static unsigned int log_stats;
  61. static unsigned int debug_lvl;
  62. module_param(log_stats, int, 0644);
  63. module_param(debug_lvl, int, 0644);
  64. /*
  65. * Each outstanding request that we've passed to the lower device layers has a
  66. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  67. * the pendcnt towards zero. When it hits zero, the specified domain has a
  68. * response queued for it, with the saved 'id' passed back.
  69. */
  70. struct pending_req {
  71. struct blkif_st *blkif;
  72. u64 id;
  73. int nr_pages;
  74. atomic_t pendcnt;
  75. unsigned short operation;
  76. int status;
  77. struct list_head free_list;
  78. };
  79. #define BLKBACK_INVALID_HANDLE (~0)
  80. struct xen_blkbk {
  81. struct pending_req *pending_reqs;
  82. /* List of all 'pending_req' available */
  83. struct list_head pending_free;
  84. /* And its spinlock. */
  85. spinlock_t pending_free_lock;
  86. wait_queue_head_t pending_free_wq;
  87. /* The list of all pages that are available. */
  88. struct page **pending_pages;
  89. /* And the grant handles that are available. */
  90. grant_handle_t *pending_grant_handles;
  91. };
  92. static struct xen_blkbk *blkbk;
  93. /*
  94. * Little helpful macro to figure out the index and virtual address of the
  95. * pending_pages[..]. For each 'pending_req' we have have up to
  96. * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
  97. * 10 and would index in the pending_pages[..]. */
  98. static inline int vaddr_pagenr(struct pending_req *req, int seg)
  99. {
  100. return (req - blkbk->pending_reqs) *
  101. BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
  102. }
  103. #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
  104. static inline unsigned long vaddr(struct pending_req *req, int seg)
  105. {
  106. unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
  107. return (unsigned long)pfn_to_kaddr(pfn);
  108. }
  109. #define pending_handle(_req, _seg) \
  110. (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
  111. static int do_block_io_op(struct blkif_st *blkif);
  112. static int dispatch_rw_block_io(struct blkif_st *blkif,
  113. struct blkif_request *req,
  114. struct pending_req *pending_req);
  115. static void make_response(struct blkif_st *blkif, u64 id,
  116. unsigned short op, int st);
  117. /*
  118. * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  119. */
  120. static struct pending_req *alloc_req(void)
  121. {
  122. struct pending_req *req = NULL;
  123. unsigned long flags;
  124. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  125. if (!list_empty(&blkbk->pending_free)) {
  126. req = list_entry(blkbk->pending_free.next, struct pending_req,
  127. free_list);
  128. list_del(&req->free_list);
  129. }
  130. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  131. return req;
  132. }
  133. /*
  134. * Return the 'pending_req' structure back to the freepool. We also
  135. * wake up the thread if it was waiting for a free page.
  136. */
  137. static void free_req(struct pending_req *req)
  138. {
  139. unsigned long flags;
  140. int was_empty;
  141. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  142. was_empty = list_empty(&blkbk->pending_free);
  143. list_add(&req->free_list, &blkbk->pending_free);
  144. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  145. if (was_empty)
  146. wake_up(&blkbk->pending_free_wq);
  147. }
  148. /*
  149. * Routines for managing virtual block devices (vbds).
  150. */
  151. static int vbd_translate(struct phys_req *req, struct blkif_st *blkif,
  152. int operation)
  153. {
  154. struct vbd *vbd = &blkif->vbd;
  155. int rc = -EACCES;
  156. if ((operation != READ) && vbd->readonly)
  157. goto out;
  158. if (unlikely((req->sector_number + req->nr_sects) > vbd_sz(vbd)))
  159. goto out;
  160. req->dev = vbd->pdevice;
  161. req->bdev = vbd->bdev;
  162. rc = 0;
  163. out:
  164. return rc;
  165. }
  166. static void vbd_resize(struct blkif_st *blkif)
  167. {
  168. struct vbd *vbd = &blkif->vbd;
  169. struct xenbus_transaction xbt;
  170. int err;
  171. struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
  172. unsigned long long new_size = vbd_sz(vbd);
  173. printk(KERN_INFO "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  174. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  175. printk(KERN_INFO "VBD Resize: new size %llu\n", new_size);
  176. vbd->size = new_size;
  177. again:
  178. err = xenbus_transaction_start(&xbt);
  179. if (err) {
  180. printk(KERN_WARNING "Error starting transaction");
  181. return;
  182. }
  183. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  184. (unsigned long long)vbd_sz(vbd));
  185. if (err) {
  186. printk(KERN_WARNING "Error writing new size");
  187. goto abort;
  188. }
  189. /*
  190. * Write the current state; we will use this to synchronize
  191. * the front-end. If the current state is "connected" the
  192. * front-end will get the new size information online.
  193. */
  194. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  195. if (err) {
  196. printk(KERN_WARNING "Error writing the state");
  197. goto abort;
  198. }
  199. err = xenbus_transaction_end(xbt, 0);
  200. if (err == -EAGAIN)
  201. goto again;
  202. if (err)
  203. printk(KERN_WARNING "Error ending transaction");
  204. abort:
  205. xenbus_transaction_end(xbt, 1);
  206. }
  207. /*
  208. * Notification from the guest OS.
  209. */
  210. static void blkif_notify_work(struct blkif_st *blkif)
  211. {
  212. blkif->waiting_reqs = 1;
  213. wake_up(&blkif->wq);
  214. }
  215. irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
  216. {
  217. blkif_notify_work(dev_id);
  218. return IRQ_HANDLED;
  219. }
  220. /*
  221. * SCHEDULER FUNCTIONS
  222. */
  223. static void print_stats(struct blkif_st *blkif)
  224. {
  225. printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | f %4d\n",
  226. current->comm, blkif->st_oo_req,
  227. blkif->st_rd_req, blkif->st_wr_req, blkif->st_f_req);
  228. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  229. blkif->st_rd_req = 0;
  230. blkif->st_wr_req = 0;
  231. blkif->st_oo_req = 0;
  232. }
  233. int xen_blkif_schedule(void *arg)
  234. {
  235. struct blkif_st *blkif = arg;
  236. struct vbd *vbd = &blkif->vbd;
  237. xen_blkif_get(blkif);
  238. if (debug_lvl)
  239. printk(KERN_DEBUG "%s: started\n", current->comm);
  240. while (!kthread_should_stop()) {
  241. if (try_to_freeze())
  242. continue;
  243. if (unlikely(vbd->size != vbd_sz(vbd)))
  244. vbd_resize(blkif);
  245. wait_event_interruptible(
  246. blkif->wq,
  247. blkif->waiting_reqs || kthread_should_stop());
  248. wait_event_interruptible(
  249. blkbk->pending_free_wq,
  250. !list_empty(&blkbk->pending_free) ||
  251. kthread_should_stop());
  252. blkif->waiting_reqs = 0;
  253. smp_mb(); /* clear flag *before* checking for work */
  254. if (do_block_io_op(blkif))
  255. blkif->waiting_reqs = 1;
  256. if (log_stats && time_after(jiffies, blkif->st_print))
  257. print_stats(blkif);
  258. }
  259. if (log_stats)
  260. print_stats(blkif);
  261. if (debug_lvl)
  262. printk(KERN_DEBUG "%s: exiting\n", current->comm);
  263. blkif->xenblkd = NULL;
  264. xen_blkif_put(blkif);
  265. return 0;
  266. }
  267. struct seg_buf {
  268. unsigned long buf;
  269. unsigned int nsec;
  270. };
  271. /*
  272. * Unmap the grant references, and also remove the M2P over-rides
  273. * used in the 'pending_req'.
  274. */
  275. static void xen_blkbk_unmap(struct pending_req *req)
  276. {
  277. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  278. unsigned int i, invcount = 0;
  279. grant_handle_t handle;
  280. int ret;
  281. for (i = 0; i < req->nr_pages; i++) {
  282. handle = pending_handle(req, i);
  283. if (handle == BLKBACK_INVALID_HANDLE)
  284. continue;
  285. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  286. GNTMAP_host_map, handle);
  287. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  288. invcount++;
  289. }
  290. ret = HYPERVISOR_grant_table_op(
  291. GNTTABOP_unmap_grant_ref, unmap, invcount);
  292. BUG_ON(ret);
  293. /* Note, we use invcount, so nr->pages, so we can't index
  294. * using vaddr(req, i).
  295. */
  296. for (i = 0; i < invcount; i++) {
  297. ret = m2p_remove_override(
  298. virt_to_page(unmap[i].host_addr), false);
  299. if (ret) {
  300. printk(KERN_ALERT "Failed to remove M2P override for " \
  301. "%lx\n", (unsigned long)unmap[i].host_addr);
  302. continue;
  303. }
  304. }
  305. }
  306. static int xen_blkbk_map(struct blkif_request *req, struct pending_req *pending_req,
  307. struct seg_buf seg[])
  308. {
  309. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  310. int i;
  311. int nseg = req->nr_segments;
  312. int ret = 0;
  313. /* Fill out preq.nr_sects with proper amount of sectors, and setup
  314. * assign map[..] with the PFN of the page in our domain with the
  315. * corresponding grant reference for each page.
  316. */
  317. for (i = 0; i < nseg; i++) {
  318. uint32_t flags;
  319. flags = GNTMAP_host_map;
  320. if (pending_req->operation != BLKIF_OP_READ)
  321. flags |= GNTMAP_readonly;
  322. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  323. req->u.rw.seg[i].gref, pending_req->blkif->domid);
  324. }
  325. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
  326. BUG_ON(ret);
  327. /* Now swizzel the MFN in our domain with the MFN from the other domain
  328. * so that when we access vaddr(pending_req,i) it has the contents of
  329. * the page from the other domain.
  330. */
  331. for (i = 0; i < nseg; i++) {
  332. if (unlikely(map[i].status != 0)) {
  333. DPRINTK("invalid buffer -- could not remap it\n");
  334. map[i].handle = BLKBACK_INVALID_HANDLE;
  335. ret |= 1;
  336. }
  337. pending_handle(pending_req, i) = map[i].handle;
  338. if (ret)
  339. continue;
  340. ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
  341. blkbk->pending_page(pending_req, i), false);
  342. if (ret) {
  343. printk(KERN_ALERT "Failed to install M2P override for"\
  344. " %lx (ret: %d)\n", (unsigned long)
  345. map[i].dev_bus_addr, ret);
  346. /* We could switch over to GNTTABOP_copy */
  347. continue;
  348. }
  349. seg[i].buf = map[i].dev_bus_addr |
  350. (req->u.rw.seg[i].first_sect << 9);
  351. }
  352. return ret;
  353. }
  354. /*
  355. * Completion callback on the bio's. Called as bh->b_end_io()
  356. */
  357. static void __end_block_io_op(struct pending_req *pending_req, int error)
  358. {
  359. /* An error fails the entire request. */
  360. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  361. (error == -EOPNOTSUPP)) {
  362. DPRINTK("blkback: flush diskcache op failed, not supported\n");
  363. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  364. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  365. } else if (error) {
  366. DPRINTK("Buffer not up-to-date at end of operation, "
  367. "error=%d\n", error);
  368. pending_req->status = BLKIF_RSP_ERROR;
  369. }
  370. /* If all of the bio's have completed it is time to unmap
  371. * the grant references associated with 'request' and provide
  372. * the proper response on the ring.
  373. */
  374. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  375. xen_blkbk_unmap(pending_req);
  376. make_response(pending_req->blkif, pending_req->id,
  377. pending_req->operation, pending_req->status);
  378. xen_blkif_put(pending_req->blkif);
  379. free_req(pending_req);
  380. }
  381. }
  382. /*
  383. * bio callback.
  384. */
  385. static void end_block_io_op(struct bio *bio, int error)
  386. {
  387. __end_block_io_op(bio->bi_private, error);
  388. bio_put(bio);
  389. }
  390. /*
  391. * Function to copy the from the ring buffer the 'struct blkif_request'
  392. * (which has the sectors we want, number of them, grant references, etc),
  393. * and transmute it to the block API to hand it over to the proper block disk.
  394. */
  395. static int do_block_io_op(struct blkif_st *blkif)
  396. {
  397. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  398. struct blkif_request req;
  399. struct pending_req *pending_req;
  400. RING_IDX rc, rp;
  401. int more_to_do = 0;
  402. rc = blk_rings->common.req_cons;
  403. rp = blk_rings->common.sring->req_prod;
  404. rmb(); /* Ensure we see queued requests up to 'rp'. */
  405. while (rc != rp) {
  406. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  407. break;
  408. if (kthread_should_stop()) {
  409. more_to_do = 1;
  410. break;
  411. }
  412. pending_req = alloc_req();
  413. if (NULL == pending_req) {
  414. blkif->st_oo_req++;
  415. more_to_do = 1;
  416. break;
  417. }
  418. switch (blkif->blk_protocol) {
  419. case BLKIF_PROTOCOL_NATIVE:
  420. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  421. break;
  422. case BLKIF_PROTOCOL_X86_32:
  423. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  424. break;
  425. case BLKIF_PROTOCOL_X86_64:
  426. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  427. break;
  428. default:
  429. BUG();
  430. }
  431. blk_rings->common.req_cons = ++rc; /* before make_response() */
  432. /* Apply all sanity checks to /private copy/ of request. */
  433. barrier();
  434. if (dispatch_rw_block_io(blkif, &req, pending_req))
  435. break;
  436. /* Yield point for this unbounded loop. */
  437. cond_resched();
  438. }
  439. return more_to_do;
  440. }
  441. /*
  442. * Transumation of the 'struct blkif_request' to a proper 'struct bio'
  443. * and call the 'submit_bio' to pass it to the underlaying storage.
  444. */
  445. static int dispatch_rw_block_io(struct blkif_st *blkif,
  446. struct blkif_request *req,
  447. struct pending_req *pending_req)
  448. {
  449. struct phys_req preq;
  450. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  451. unsigned int nseg;
  452. struct bio *bio = NULL;
  453. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  454. int i, nbio = 0;
  455. int operation;
  456. struct blk_plug plug;
  457. switch (req->operation) {
  458. case BLKIF_OP_READ:
  459. blkif->st_rd_req++;
  460. operation = READ;
  461. break;
  462. case BLKIF_OP_WRITE:
  463. blkif->st_wr_req++;
  464. operation = WRITE_ODIRECT;
  465. break;
  466. case BLKIF_OP_FLUSH_DISKCACHE:
  467. blkif->st_f_req++;
  468. operation = WRITE_FLUSH;
  469. /* The frontend likes to set this to -1, which vbd_translate
  470. * is alergic too. */
  471. req->u.rw.sector_number = 0;
  472. break;
  473. case BLKIF_OP_WRITE_BARRIER:
  474. default:
  475. operation = 0; /* make gcc happy */
  476. goto fail_response;
  477. break;
  478. }
  479. /* Check that the number of segments is sane. */
  480. nseg = req->nr_segments;
  481. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  482. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  483. DPRINTK("Bad number of segments in request (%d)\n", nseg);
  484. /* Haven't submitted any bio's yet. */
  485. goto fail_response;
  486. }
  487. preq.dev = req->handle;
  488. preq.sector_number = req->u.rw.sector_number;
  489. preq.nr_sects = 0;
  490. pending_req->blkif = blkif;
  491. pending_req->id = req->id;
  492. pending_req->operation = req->operation;
  493. pending_req->status = BLKIF_RSP_OKAY;
  494. pending_req->nr_pages = nseg;
  495. for (i = 0; i < nseg; i++) {
  496. seg[i].nsec = req->u.rw.seg[i].last_sect -
  497. req->u.rw.seg[i].first_sect + 1;
  498. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  499. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  500. goto fail_response;
  501. preq.nr_sects += seg[i].nsec;
  502. }
  503. if (vbd_translate(&preq, blkif, operation) != 0) {
  504. DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
  505. operation == READ ? "read" : "write",
  506. preq.sector_number,
  507. preq.sector_number + preq.nr_sects, preq.dev);
  508. goto fail_response;
  509. }
  510. /* This check _MUST_ be done after vbd_translate as the preq.bdev
  511. * is set there. */
  512. for (i = 0; i < nseg; i++) {
  513. if (((int)preq.sector_number|(int)seg[i].nsec) &
  514. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  515. DPRINTK("Misaligned I/O request from domain %d",
  516. blkif->domid);
  517. goto fail_response;
  518. }
  519. }
  520. /* If we have failed at this point, we need to undo the M2P override,
  521. * set gnttab_set_unmap_op on all of the grant references and perform
  522. * the hypercall to unmap the grants - that is all done in
  523. * xen_blkbk_unmap.
  524. */
  525. if (xen_blkbk_map(req, pending_req, seg))
  526. goto fail_flush;
  527. /* This corresponding blkif_put is done in __end_block_io_op */
  528. xen_blkif_get(blkif);
  529. for (i = 0; i < nseg; i++) {
  530. while ((bio == NULL) ||
  531. (bio_add_page(bio,
  532. blkbk->pending_page(pending_req, i),
  533. seg[i].nsec << 9,
  534. seg[i].buf & ~PAGE_MASK) == 0)) {
  535. bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, nseg-i);
  536. if (unlikely(bio == NULL))
  537. goto fail_put_bio;
  538. bio->bi_bdev = preq.bdev;
  539. bio->bi_private = pending_req;
  540. bio->bi_end_io = end_block_io_op;
  541. bio->bi_sector = preq.sector_number;
  542. }
  543. preq.sector_number += seg[i].nsec;
  544. }
  545. /* This will be hit if the operation was a barrier. */
  546. if (!bio) {
  547. BUG_ON(operation != WRITE_FLUSH);
  548. bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, 0);
  549. if (unlikely(bio == NULL))
  550. goto fail_put_bio;
  551. bio->bi_bdev = preq.bdev;
  552. bio->bi_private = pending_req;
  553. bio->bi_end_io = end_block_io_op;
  554. }
  555. /* We set it one so that the last submit_bio does not have to call
  556. * atomic_inc.
  557. */
  558. atomic_set(&pending_req->pendcnt, nbio);
  559. /* Get a reference count for the disk queue and start sending I/O */
  560. blk_start_plug(&plug);
  561. for (i = 0; i < nbio; i++)
  562. submit_bio(operation, biolist[i]);
  563. blk_finish_plug(&plug);
  564. /* Let the I/Os go.. */
  565. if (operation == READ)
  566. blkif->st_rd_sect += preq.nr_sects;
  567. else if (operation == WRITE || operation == WRITE_FLUSH)
  568. blkif->st_wr_sect += preq.nr_sects;
  569. return 0;
  570. fail_flush:
  571. xen_blkbk_unmap(pending_req);
  572. fail_response:
  573. /* Haven't submitted any bio's yet. */
  574. make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
  575. free_req(pending_req);
  576. msleep(1); /* back off a bit */
  577. return -EIO;
  578. fail_put_bio:
  579. for (i = 0; i < (nbio-1); i++)
  580. bio_put(biolist[i]);
  581. __end_block_io_op(pending_req, -EINVAL);
  582. msleep(1); /* back off a bit */
  583. return -EIO;
  584. }
  585. /*
  586. * Put a response on the ring on how the operation fared.
  587. */
  588. static void make_response(struct blkif_st *blkif, u64 id,
  589. unsigned short op, int st)
  590. {
  591. struct blkif_response resp;
  592. unsigned long flags;
  593. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  594. int more_to_do = 0;
  595. int notify;
  596. resp.id = id;
  597. resp.operation = op;
  598. resp.status = st;
  599. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  600. /* Place on the response ring for the relevant domain. */
  601. switch (blkif->blk_protocol) {
  602. case BLKIF_PROTOCOL_NATIVE:
  603. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  604. &resp, sizeof(resp));
  605. break;
  606. case BLKIF_PROTOCOL_X86_32:
  607. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  608. &resp, sizeof(resp));
  609. break;
  610. case BLKIF_PROTOCOL_X86_64:
  611. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  612. &resp, sizeof(resp));
  613. break;
  614. default:
  615. BUG();
  616. }
  617. blk_rings->common.rsp_prod_pvt++;
  618. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  619. if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
  620. /*
  621. * Tail check for pending requests. Allows frontend to avoid
  622. * notifications if requests are already in flight (lower
  623. * overheads and promotes batching).
  624. */
  625. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  626. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
  627. more_to_do = 1;
  628. }
  629. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  630. if (more_to_do)
  631. blkif_notify_work(blkif);
  632. if (notify)
  633. notify_remote_via_irq(blkif->irq);
  634. }
  635. static int __init xen_blkif_init(void)
  636. {
  637. int i, mmap_pages;
  638. int rc = 0;
  639. if (!xen_pv_domain())
  640. return -ENODEV;
  641. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  642. if (!blkbk) {
  643. printk(KERN_ALERT "%s: out of memory!\n", __func__);
  644. return -ENOMEM;
  645. }
  646. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  647. blkbk->pending_reqs = kmalloc(sizeof(blkbk->pending_reqs[0]) *
  648. xen_blkif_reqs, GFP_KERNEL);
  649. blkbk->pending_grant_handles = kzalloc(sizeof(blkbk->pending_grant_handles[0]) *
  650. mmap_pages, GFP_KERNEL);
  651. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  652. mmap_pages, GFP_KERNEL);
  653. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  654. !blkbk->pending_pages) {
  655. rc = -ENOMEM;
  656. goto out_of_memory;
  657. }
  658. for (i = 0; i < mmap_pages; i++) {
  659. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  660. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  661. if (blkbk->pending_pages[i] == NULL) {
  662. rc = -ENOMEM;
  663. goto out_of_memory;
  664. }
  665. }
  666. rc = xen_blkif_interface_init();
  667. if (rc)
  668. goto failed_init;
  669. memset(blkbk->pending_reqs, 0, sizeof(blkbk->pending_reqs));
  670. INIT_LIST_HEAD(&blkbk->pending_free);
  671. spin_lock_init(&blkbk->pending_free_lock);
  672. init_waitqueue_head(&blkbk->pending_free_wq);
  673. for (i = 0; i < xen_blkif_reqs; i++)
  674. list_add_tail(&blkbk->pending_reqs[i].free_list,
  675. &blkbk->pending_free);
  676. rc = xen_blkif_xenbus_init();
  677. if (rc)
  678. goto failed_init;
  679. return 0;
  680. out_of_memory:
  681. printk(KERN_ERR "%s: out of memory\n", __func__);
  682. failed_init:
  683. kfree(blkbk->pending_reqs);
  684. kfree(blkbk->pending_grant_handles);
  685. for (i = 0; i < mmap_pages; i++) {
  686. if (blkbk->pending_pages[i])
  687. __free_page(blkbk->pending_pages[i]);
  688. }
  689. kfree(blkbk->pending_pages);
  690. kfree(blkbk);
  691. blkbk = NULL;
  692. return rc;
  693. }
  694. module_init(xen_blkif_init);
  695. MODULE_LICENSE("Dual BSD/GPL");