blkback.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  329. struct phys_req preq;
  330. struct {
  331. unsigned long buf; unsigned int nsec;
  332. } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  333. unsigned int nseg;
  334. struct bio *bio = NULL;
  335. int ret, i;
  336. int operation;
  337. switch (req->operation) {
  338. case BLKIF_OP_READ:
  339. operation = READ;
  340. break;
  341. case BLKIF_OP_WRITE:
  342. operation = WRITE;
  343. break;
  344. case BLKIF_OP_WRITE_BARRIER:
  345. operation = WRITE_BARRIER;
  346. break;
  347. default:
  348. operation = 0; /* make gcc happy */
  349. BUG();
  350. }
  351. /* Check that number of segments is sane. */
  352. nseg = req->nr_segments;
  353. if (unlikely(nseg == 0 && operation != WRITE_BARRIER) ||
  354. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  355. DPRINTK("Bad number of segments in request (%d)\n", nseg);
  356. goto fail_response;
  357. }
  358. preq.dev = req->handle;
  359. preq.sector_number = req->sector_number;
  360. preq.nr_sects = 0;
  361. pending_req->blkif = blkif;
  362. pending_req->id = req->id;
  363. pending_req->operation = req->operation;
  364. pending_req->status = BLKIF_RSP_OKAY;
  365. pending_req->nr_pages = nseg;
  366. for (i = 0; i < nseg; i++) {
  367. uint32_t flags;
  368. seg[i].nsec = req->seg[i].last_sect -
  369. req->seg[i].first_sect + 1;
  370. if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  371. (req->seg[i].last_sect < req->seg[i].first_sect))
  372. goto fail_response;
  373. preq.nr_sects += seg[i].nsec;
  374. flags = GNTMAP_host_map;
  375. if (operation != READ)
  376. flags |= GNTMAP_readonly;
  377. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  378. req->seg[i].gref, blkif->domid);
  379. }
  380. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
  381. BUG_ON(ret);
  382. for (i = 0; i < nseg; i++) {
  383. if (unlikely(map[i].status != 0)) {
  384. DPRINTK("invalid buffer -- could not remap it\n");
  385. map[i].handle = BLKBACK_INVALID_HANDLE;
  386. ret |= 1;
  387. }
  388. pending_handle(pending_req, i) = map[i].handle;
  389. if (ret)
  390. continue;
  391. set_phys_to_machine(__pa(vaddr(
  392. pending_req, i)) >> PAGE_SHIFT,
  393. FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
  394. seg[i].buf = map[i].dev_bus_addr |
  395. (req->seg[i].first_sect << 9);
  396. }
  397. if (ret)
  398. goto fail_flush;
  399. if (vbd_translate(&preq, blkif, operation) != 0) {
  400. DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
  401. operation == READ ? "read" : "write",
  402. preq.sector_number,
  403. preq.sector_number + preq.nr_sects, preq.dev);
  404. goto fail_flush;
  405. }
  406. plug_queue(blkif, preq.bdev);
  407. atomic_set(&pending_req->pendcnt, 1);
  408. blkif_get(blkif);
  409. for (i = 0; i < nseg; i++) {
  410. if (((int)preq.sector_number|(int)seg[i].nsec) &
  411. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  412. DPRINTK("Misaligned I/O request from domain %d",
  413. blkif->domid);
  414. goto fail_put_bio;
  415. }
  416. while ((bio == NULL) ||
  417. (bio_add_page(bio,
  418. virt_to_page(vaddr(pending_req, i)),
  419. seg[i].nsec << 9,
  420. seg[i].buf & ~PAGE_MASK) == 0)) {
  421. if (bio) {
  422. atomic_inc(&pending_req->pendcnt);
  423. submit_bio(operation, bio);
  424. }
  425. bio = bio_alloc(GFP_KERNEL, nseg-i);
  426. if (unlikely(bio == NULL))
  427. goto fail_put_bio;
  428. bio->bi_bdev = preq.bdev;
  429. bio->bi_private = pending_req;
  430. bio->bi_end_io = end_block_io_op;
  431. bio->bi_sector = preq.sector_number;
  432. }
  433. preq.sector_number += seg[i].nsec;
  434. }
  435. if (!bio) {
  436. BUG_ON(operation != WRITE_BARRIER);
  437. bio = bio_alloc(GFP_KERNEL, 0);
  438. if (unlikely(bio == NULL))
  439. goto fail_put_bio;
  440. bio->bi_bdev = preq.bdev;
  441. bio->bi_private = pending_req;
  442. bio->bi_end_io = end_block_io_op;
  443. bio->bi_sector = -1;
  444. }
  445. submit_bio(operation, bio);
  446. if (operation == READ)
  447. blkif->st_rd_sect += preq.nr_sects;
  448. else if (operation == WRITE || operation == WRITE_BARRIER)
  449. blkif->st_wr_sect += preq.nr_sects;
  450. return;
  451. fail_flush:
  452. fast_flush_area(pending_req);
  453. fail_response:
  454. make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
  455. free_req(pending_req);
  456. msleep(1); /* back off a bit */
  457. return;
  458. fail_put_bio:
  459. __end_block_io_op(pending_req, -EINVAL);
  460. if (bio)
  461. bio_put(bio);
  462. unplug_queue(blkif);
  463. msleep(1); /* back off a bit */
  464. return;
  465. }
  466. /******************************************************************
  467. * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
  468. */
  469. static void make_response(blkif_t *blkif, u64 id,
  470. unsigned short op, int st)
  471. {
  472. struct blkif_response resp;
  473. unsigned long flags;
  474. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  475. int more_to_do = 0;
  476. int notify;
  477. resp.id = id;
  478. resp.operation = op;
  479. resp.status = st;
  480. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  481. /* Place on the response ring for the relevant domain. */
  482. switch (blkif->blk_protocol) {
  483. case BLKIF_PROTOCOL_NATIVE:
  484. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  485. &resp, sizeof(resp));
  486. break;
  487. case BLKIF_PROTOCOL_X86_32:
  488. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  489. &resp, sizeof(resp));
  490. break;
  491. case BLKIF_PROTOCOL_X86_64:
  492. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  493. &resp, sizeof(resp));
  494. break;
  495. default:
  496. BUG();
  497. }
  498. blk_rings->common.rsp_prod_pvt++;
  499. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  500. if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
  501. /*
  502. * Tail check for pending requests. Allows frontend to avoid
  503. * notifications if requests are already in flight (lower
  504. * overheads and promotes batching).
  505. */
  506. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  507. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
  508. more_to_do = 1;
  509. }
  510. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  511. if (more_to_do)
  512. blkif_notify_work(blkif);
  513. if (notify)
  514. notify_remote_via_irq(blkif->irq);
  515. }
  516. static int __init blkif_init(void)
  517. {
  518. int i, mmap_pages;
  519. int rc = 0;
  520. if (!xen_pv_domain())
  521. return -ENODEV;
  522. mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  523. pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
  524. blkif_reqs, GFP_KERNEL);
  525. pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
  526. mmap_pages, GFP_KERNEL);
  527. pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
  528. if (!pending_reqs || !pending_grant_handles || !pending_pages) {
  529. rc = -ENOMEM;
  530. goto out_of_memory;
  531. }
  532. for (i = 0; i < mmap_pages; i++)
  533. pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  534. rc = blkif_interface_init();
  535. if (rc)
  536. goto failed_init;
  537. memset(pending_reqs, 0, sizeof(pending_reqs));
  538. INIT_LIST_HEAD(&pending_free);
  539. for (i = 0; i < blkif_reqs; i++)
  540. list_add_tail(&pending_reqs[i].free_list, &pending_free);
  541. rc = blkif_xenbus_init();
  542. if (rc)
  543. goto failed_init;
  544. return 0;
  545. out_of_memory:
  546. printk(KERN_ERR "%s: out of memory\n", __func__);
  547. failed_init:
  548. kfree(pending_reqs);
  549. kfree(pending_grant_handles);
  550. free_empty_pages_and_pagevec(pending_pages, mmap_pages);
  551. return rc;
  552. }
  553. module_init(blkif_init);
  554. MODULE_LICENSE("Dual BSD/GPL");