blkback.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /******************************************************************************
  2. * arch/xen/drivers/blkif/backend/main.c
  3. *
  4. * Back-end of the driver for virtual block devices. This portion of the
  5. * driver exports a 'unified' block-device interface that can be accessed
  6. * by any operating system that implements a compatible front end. A
  7. * reference front-end implementation can be found in:
  8. * arch/xen/drivers/blkif/frontend
  9. *
  10. * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
  11. * Copyright (c) 2005, Christopher Clark
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation; or, when distributed
  16. * separately from the Linux kernel or incorporated into other
  17. * software packages, subject to the following license:
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this source file (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use, copy, modify,
  22. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so, subject to
  24. * the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  35. * IN THE SOFTWARE.
  36. */
  37. #include <linux/spinlock.h>
  38. #include <linux/kthread.h>
  39. #include <linux/list.h>
  40. #include <linux/delay.h>
  41. #include <linux/freezer.h>
  42. #include <xen/balloon.h>
  43. #include <xen/events.h>
  44. #include <xen/page.h>
  45. #include <asm/xen/hypervisor.h>
  46. #include <asm/xen/hypercall.h>
  47. #include "common.h"
  48. /*
  49. * These are rather arbitrary. They are fairly large because adjacent requests
  50. * pulled from a communication ring are quite likely to end up being part of
  51. * the same scatter/gather request at the disc.
  52. *
  53. * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
  54. *
  55. * This will increase the chances of being able to write whole tracks.
  56. * 64 should be enough to keep us competitive with Linux.
  57. */
  58. static int blkif_reqs = 64;
  59. module_param_named(reqs, blkif_reqs, int, 0);
  60. MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
  61. /* Run-time switchable: /sys/module/blkback/parameters/ */
  62. static unsigned int log_stats = 0;
  63. static unsigned int debug_lvl = 0;
  64. module_param(log_stats, int, 0644);
  65. module_param(debug_lvl, int, 0644);
  66. /*
  67. * Each outstanding request that we've passed to the lower device layers has a
  68. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  69. * the pendcnt towards zero. When it hits zero, the specified domain has a
  70. * response queued for it, with the saved 'id' passed back.
  71. */
  72. typedef struct {
  73. blkif_t *blkif;
  74. u64 id;
  75. int nr_pages;
  76. atomic_t pendcnt;
  77. unsigned short operation;
  78. int status;
  79. struct list_head free_list;
  80. } pending_req_t;
  81. static pending_req_t *pending_reqs;
  82. static struct list_head pending_free;
  83. static DEFINE_SPINLOCK(pending_free_lock);
  84. static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
  85. #define BLKBACK_INVALID_HANDLE (~0)
  86. static struct page **pending_pages;
  87. static grant_handle_t *pending_grant_handles;
  88. static inline int vaddr_pagenr(pending_req_t *req, int seg)
  89. {
  90. return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
  91. }
  92. static inline unsigned long vaddr(pending_req_t *req, int seg)
  93. {
  94. unsigned long pfn = page_to_pfn(pending_pages[vaddr_pagenr(req, seg)]);
  95. return (unsigned long)pfn_to_kaddr(pfn);
  96. }
  97. #define pending_handle(_req, _seg) \
  98. (pending_grant_handles[vaddr_pagenr(_req, _seg)])
  99. static int do_block_io_op(blkif_t *blkif);
  100. static void dispatch_rw_block_io(blkif_t *blkif,
  101. struct blkif_request *req,
  102. pending_req_t *pending_req);
  103. static void make_response(blkif_t *blkif, u64 id,
  104. unsigned short op, int st);
  105. /******************************************************************
  106. * misc small helpers
  107. */
  108. static pending_req_t* alloc_req(void)
  109. {
  110. pending_req_t *req = NULL;
  111. unsigned long flags;
  112. spin_lock_irqsave(&pending_free_lock, flags);
  113. if (!list_empty(&pending_free)) {
  114. req = list_entry(pending_free.next, pending_req_t, free_list);
  115. list_del(&req->free_list);
  116. }
  117. spin_unlock_irqrestore(&pending_free_lock, flags);
  118. return req;
  119. }
  120. static void free_req(pending_req_t *req)
  121. {
  122. unsigned long flags;
  123. int was_empty;
  124. spin_lock_irqsave(&pending_free_lock, flags);
  125. was_empty = list_empty(&pending_free);
  126. list_add(&req->free_list, &pending_free);
  127. spin_unlock_irqrestore(&pending_free_lock, flags);
  128. if (was_empty)
  129. wake_up(&pending_free_wq);
  130. }
  131. static void unplug_queue(blkif_t *blkif)
  132. {
  133. if (blkif->plug == NULL)
  134. return;
  135. if (blkif->plug->unplug_fn)
  136. blkif->plug->unplug_fn(blkif->plug);
  137. blk_put_queue(blkif->plug);
  138. blkif->plug = NULL;
  139. }
  140. static void plug_queue(blkif_t *blkif, struct block_device *bdev)
  141. {
  142. struct request_queue *q = bdev_get_queue(bdev);
  143. if (q == blkif->plug)
  144. return;
  145. unplug_queue(blkif);
  146. blk_get_queue(q);
  147. blkif->plug = q;
  148. }
  149. static void fast_flush_area(pending_req_t *req)
  150. {
  151. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  152. unsigned int i, invcount = 0;
  153. grant_handle_t handle;
  154. int ret;
  155. for (i = 0; i < req->nr_pages; i++) {
  156. handle = pending_handle(req, i);
  157. if (handle == BLKBACK_INVALID_HANDLE)
  158. continue;
  159. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  160. GNTMAP_host_map, handle);
  161. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  162. invcount++;
  163. }
  164. ret = HYPERVISOR_grant_table_op(
  165. GNTTABOP_unmap_grant_ref, unmap, invcount);
  166. BUG_ON(ret);
  167. }
  168. /******************************************************************
  169. * SCHEDULER FUNCTIONS
  170. */
  171. static void print_stats(blkif_t *blkif)
  172. {
  173. printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n",
  174. current->comm, blkif->st_oo_req,
  175. blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
  176. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  177. blkif->st_rd_req = 0;
  178. blkif->st_wr_req = 0;
  179. blkif->st_oo_req = 0;
  180. }
  181. int blkif_schedule(void *arg)
  182. {
  183. blkif_t *blkif = arg;
  184. blkif_get(blkif);
  185. if (debug_lvl)
  186. printk(KERN_DEBUG "%s: started\n", current->comm);
  187. while (!kthread_should_stop()) {
  188. if (try_to_freeze())
  189. continue;
  190. wait_event_interruptible(
  191. blkif->wq,
  192. blkif->waiting_reqs || kthread_should_stop());
  193. wait_event_interruptible(
  194. pending_free_wq,
  195. !list_empty(&pending_free) || kthread_should_stop());
  196. blkif->waiting_reqs = 0;
  197. smp_mb(); /* clear flag *before* checking for work */
  198. if (do_block_io_op(blkif))
  199. blkif->waiting_reqs = 1;
  200. unplug_queue(blkif);
  201. if (log_stats && time_after(jiffies, blkif->st_print))
  202. print_stats(blkif);
  203. }
  204. if (log_stats)
  205. print_stats(blkif);
  206. if (debug_lvl)
  207. printk(KERN_DEBUG "%s: exiting\n", current->comm);
  208. blkif->xenblkd = NULL;
  209. blkif_put(blkif);
  210. return 0;
  211. }
  212. /******************************************************************
  213. * COMPLETION CALLBACK -- Called as bh->b_end_io()
  214. */
  215. static void __end_block_io_op(pending_req_t *pending_req, int error)
  216. {
  217. /* An error fails the entire request. */
  218. if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  219. (error == -EOPNOTSUPP)) {
  220. DPRINTK("blkback: write barrier op failed, not supported\n");
  221. blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
  222. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  223. } else if (error) {
  224. DPRINTK("Buffer not up-to-date at end of operation, "
  225. "error=%d\n", error);
  226. pending_req->status = BLKIF_RSP_ERROR;
  227. }
  228. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  229. fast_flush_area(pending_req);
  230. make_response(pending_req->blkif, pending_req->id,
  231. pending_req->operation, pending_req->status);
  232. blkif_put(pending_req->blkif);
  233. free_req(pending_req);
  234. }
  235. }
  236. static void end_block_io_op(struct bio *bio, int error)
  237. {
  238. __end_block_io_op(bio->bi_private, error);
  239. bio_put(bio);
  240. }
  241. /******************************************************************************
  242. * NOTIFICATION FROM GUEST OS.
  243. */
  244. static void blkif_notify_work(blkif_t *blkif)
  245. {
  246. blkif->waiting_reqs = 1;
  247. wake_up(&blkif->wq);
  248. }
  249. irqreturn_t blkif_be_int(int irq, void *dev_id)
  250. {
  251. blkif_notify_work(dev_id);
  252. return IRQ_HANDLED;
  253. }
  254. /******************************************************************
  255. * DOWNWARD CALLS -- These interface with the block-device layer proper.
  256. */
  257. static int do_block_io_op(blkif_t *blkif)
  258. {
  259. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  260. struct blkif_request req;
  261. pending_req_t *pending_req;
  262. RING_IDX rc, rp;
  263. int more_to_do = 0;
  264. rc = blk_rings->common.req_cons;
  265. rp = blk_rings->common.sring->req_prod;
  266. rmb(); /* Ensure we see queued requests up to 'rp'. */
  267. while (rc != rp) {
  268. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  269. break;
  270. if (kthread_should_stop()) {
  271. more_to_do = 1;
  272. break;
  273. }
  274. pending_req = alloc_req();
  275. if (NULL == pending_req) {
  276. blkif->st_oo_req++;
  277. more_to_do = 1;
  278. break;
  279. }
  280. switch (blkif->blk_protocol) {
  281. case BLKIF_PROTOCOL_NATIVE:
  282. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  283. break;
  284. case BLKIF_PROTOCOL_X86_32:
  285. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  286. break;
  287. case BLKIF_PROTOCOL_X86_64:
  288. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  289. break;
  290. default:
  291. BUG();
  292. }
  293. blk_rings->common.req_cons = ++rc; /* before make_response() */
  294. /* Apply all sanity checks to /private copy/ of request. */
  295. barrier();
  296. switch (req.operation) {
  297. case BLKIF_OP_READ:
  298. blkif->st_rd_req++;
  299. dispatch_rw_block_io(blkif, &req, pending_req);
  300. break;
  301. case BLKIF_OP_WRITE_BARRIER:
  302. blkif->st_br_req++;
  303. /* fall through */
  304. case BLKIF_OP_WRITE:
  305. blkif->st_wr_req++;
  306. dispatch_rw_block_io(blkif, &req, pending_req);
  307. break;
  308. default:
  309. /* A good sign something is wrong: sleep for a while to
  310. * avoid excessive CPU consumption by a bad guest. */
  311. msleep(1);
  312. DPRINTK("error: unknown block io operation [%d]\n",
  313. req.operation);
  314. make_response(blkif, req.id, req.operation,
  315. BLKIF_RSP_ERROR);
  316. free_req(pending_req);
  317. break;
  318. }
  319. /* Yield point for this unbounded loop. */
  320. cond_resched();
  321. }
  322. return more_to_do;
  323. }
  324. static void dispatch_rw_block_io(blkif_t *blkif,
  325. struct blkif_request *req,
  326. pending_req_t *pending_req)
  327. {
  328. extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
  329. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  330. struct phys_req preq;
  331. struct {
  332. unsigned long buf; unsigned int nsec;
  333. } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  334. unsigned int nseg;
  335. struct bio *bio = NULL;
  336. int ret, i;
  337. int operation;
  338. switch (req->operation) {
  339. case BLKIF_OP_READ:
  340. operation = READ;
  341. break;
  342. case BLKIF_OP_WRITE:
  343. operation = WRITE;
  344. break;
  345. case BLKIF_OP_WRITE_BARRIER:
  346. operation = WRITE_BARRIER;
  347. break;
  348. default:
  349. operation = 0; /* make gcc happy */
  350. BUG();
  351. }
  352. /* Check that number of segments is sane. */
  353. nseg = req->nr_segments;
  354. if (unlikely(nseg == 0 && operation != WRITE_BARRIER) ||
  355. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  356. DPRINTK("Bad number of segments in request (%d)\n", nseg);
  357. goto fail_response;
  358. }
  359. preq.dev = req->handle;
  360. preq.sector_number = req->sector_number;
  361. preq.nr_sects = 0;
  362. pending_req->blkif = blkif;
  363. pending_req->id = req->id;
  364. pending_req->operation = req->operation;
  365. pending_req->status = BLKIF_RSP_OKAY;
  366. pending_req->nr_pages = nseg;
  367. for (i = 0; i < nseg; i++) {
  368. uint32_t flags;
  369. seg[i].nsec = req->seg[i].last_sect -
  370. req->seg[i].first_sect + 1;
  371. if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  372. (req->seg[i].last_sect < req->seg[i].first_sect))
  373. goto fail_response;
  374. preq.nr_sects += seg[i].nsec;
  375. flags = GNTMAP_host_map;
  376. if (operation != READ)
  377. flags |= GNTMAP_readonly;
  378. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  379. req->seg[i].gref, blkif->domid);
  380. }
  381. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
  382. BUG_ON(ret);
  383. for (i = 0; i < nseg; i++) {
  384. if (unlikely(map[i].status != 0)) {
  385. DPRINTK("invalid buffer -- could not remap it\n");
  386. map[i].handle = BLKBACK_INVALID_HANDLE;
  387. ret |= 1;
  388. }
  389. pending_handle(pending_req, i) = map[i].handle;
  390. if (ret)
  391. continue;
  392. set_phys_to_machine(__pa(vaddr(
  393. pending_req, i)) >> PAGE_SHIFT,
  394. FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
  395. seg[i].buf = map[i].dev_bus_addr |
  396. (req->seg[i].first_sect << 9);
  397. }
  398. if (ret)
  399. goto fail_flush;
  400. if (vbd_translate(&preq, blkif, operation) != 0) {
  401. DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
  402. operation == READ ? "read" : "write",
  403. preq.sector_number,
  404. preq.sector_number + preq.nr_sects, preq.dev);
  405. goto fail_flush;
  406. }
  407. plug_queue(blkif, preq.bdev);
  408. atomic_set(&pending_req->pendcnt, 1);
  409. blkif_get(blkif);
  410. for (i = 0; i < nseg; i++) {
  411. if (((int)preq.sector_number|(int)seg[i].nsec) &
  412. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  413. DPRINTK("Misaligned I/O request from domain %d",
  414. blkif->domid);
  415. goto fail_put_bio;
  416. }
  417. while ((bio == NULL) ||
  418. (bio_add_page(bio,
  419. virt_to_page(vaddr(pending_req, i)),
  420. seg[i].nsec << 9,
  421. seg[i].buf & ~PAGE_MASK) == 0)) {
  422. if (bio) {
  423. atomic_inc(&pending_req->pendcnt);
  424. submit_bio(operation, bio);
  425. }
  426. bio = bio_alloc(GFP_KERNEL, nseg-i);
  427. if (unlikely(bio == NULL))
  428. goto fail_put_bio;
  429. bio->bi_bdev = preq.bdev;
  430. bio->bi_private = pending_req;
  431. bio->bi_end_io = end_block_io_op;
  432. bio->bi_sector = preq.sector_number;
  433. }
  434. preq.sector_number += seg[i].nsec;
  435. }
  436. if (!bio) {
  437. BUG_ON(operation != WRITE_BARRIER);
  438. bio = bio_alloc(GFP_KERNEL, 0);
  439. if (unlikely(bio == NULL))
  440. goto fail_put_bio;
  441. bio->bi_bdev = preq.bdev;
  442. bio->bi_private = pending_req;
  443. bio->bi_end_io = end_block_io_op;
  444. bio->bi_sector = -1;
  445. }
  446. submit_bio(operation, bio);
  447. if (operation == READ)
  448. blkif->st_rd_sect += preq.nr_sects;
  449. else if (operation == WRITE || operation == WRITE_BARRIER)
  450. blkif->st_wr_sect += preq.nr_sects;
  451. return;
  452. fail_flush:
  453. fast_flush_area(pending_req);
  454. fail_response:
  455. make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
  456. free_req(pending_req);
  457. msleep(1); /* back off a bit */
  458. return;
  459. fail_put_bio:
  460. __end_block_io_op(pending_req, -EINVAL);
  461. if (bio)
  462. bio_put(bio);
  463. unplug_queue(blkif);
  464. msleep(1); /* back off a bit */
  465. return;
  466. }
  467. /******************************************************************
  468. * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
  469. */
  470. static void make_response(blkif_t *blkif, u64 id,
  471. unsigned short op, int st)
  472. {
  473. struct blkif_response resp;
  474. unsigned long flags;
  475. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  476. int more_to_do = 0;
  477. int notify;
  478. resp.id = id;
  479. resp.operation = op;
  480. resp.status = st;
  481. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  482. /* Place on the response ring for the relevant domain. */
  483. switch (blkif->blk_protocol) {
  484. case BLKIF_PROTOCOL_NATIVE:
  485. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  486. &resp, sizeof(resp));
  487. break;
  488. case BLKIF_PROTOCOL_X86_32:
  489. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  490. &resp, sizeof(resp));
  491. break;
  492. case BLKIF_PROTOCOL_X86_64:
  493. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  494. &resp, sizeof(resp));
  495. break;
  496. default:
  497. BUG();
  498. }
  499. blk_rings->common.rsp_prod_pvt++;
  500. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  501. if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
  502. /*
  503. * Tail check for pending requests. Allows frontend to avoid
  504. * notifications if requests are already in flight (lower
  505. * overheads and promotes batching).
  506. */
  507. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  508. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
  509. more_to_do = 1;
  510. }
  511. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  512. if (more_to_do)
  513. blkif_notify_work(blkif);
  514. if (notify)
  515. notify_remote_via_irq(blkif->irq);
  516. }
  517. static int __init blkif_init(void)
  518. {
  519. int i, mmap_pages;
  520. printk(KERN_CRIT "***blkif_init\n");
  521. if (!xen_pv_domain())
  522. return -ENODEV;
  523. mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  524. pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
  525. blkif_reqs, GFP_KERNEL);
  526. pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
  527. mmap_pages, GFP_KERNEL);
  528. pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
  529. if (!pending_reqs || !pending_grant_handles || !pending_pages)
  530. goto out_of_memory;
  531. for (i = 0; i < mmap_pages; i++)
  532. pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  533. blkif_interface_init();
  534. memset(pending_reqs, 0, sizeof(pending_reqs));
  535. INIT_LIST_HEAD(&pending_free);
  536. for (i = 0; i < blkif_reqs; i++)
  537. list_add_tail(&pending_reqs[i].free_list, &pending_free);
  538. blkif_xenbus_init();
  539. return 0;
  540. out_of_memory:
  541. kfree(pending_reqs);
  542. kfree(pending_grant_handles);
  543. free_empty_pages_and_pagevec(pending_pages, mmap_pages);
  544. printk("%s: out of memory\n", __FUNCTION__);
  545. return -ENOMEM;
  546. }
  547. module_init(blkif_init);
  548. MODULE_LICENSE("Dual BSD/GPL");