blkback.c 21 KB

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