blkback.c 20 KB

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