blkback.c 21 KB

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