blkback.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
  93. static inline unsigned long vaddr(pending_req_t *req, int seg)
  94. {
  95. unsigned long pfn = page_to_pfn(pending_page(req, seg));
  96. return (unsigned long)pfn_to_kaddr(pfn);
  97. }
  98. #define pending_handle(_req, _seg) \
  99. (pending_grant_handles[vaddr_pagenr(_req, _seg)])
  100. static int do_block_io_op(blkif_t *blkif);
  101. static void dispatch_rw_block_io(blkif_t *blkif,
  102. struct blkif_request *req,
  103. pending_req_t *pending_req);
  104. static void make_response(blkif_t *blkif, u64 id,
  105. unsigned short op, int st);
  106. /******************************************************************
  107. * misc small helpers
  108. */
  109. static pending_req_t* alloc_req(void)
  110. {
  111. pending_req_t *req = NULL;
  112. unsigned long flags;
  113. spin_lock_irqsave(&pending_free_lock, flags);
  114. if (!list_empty(&pending_free)) {
  115. req = list_entry(pending_free.next, pending_req_t, free_list);
  116. list_del(&req->free_list);
  117. }
  118. spin_unlock_irqrestore(&pending_free_lock, flags);
  119. return req;
  120. }
  121. static void free_req(pending_req_t *req)
  122. {
  123. unsigned long flags;
  124. int was_empty;
  125. spin_lock_irqsave(&pending_free_lock, flags);
  126. was_empty = list_empty(&pending_free);
  127. list_add(&req->free_list, &pending_free);
  128. spin_unlock_irqrestore(&pending_free_lock, flags);
  129. if (was_empty)
  130. wake_up(&pending_free_wq);
  131. }
  132. static void unplug_queue(blkif_t *blkif)
  133. {
  134. if (blkif->plug == NULL)
  135. return;
  136. if (blkif->plug->unplug_fn)
  137. blkif->plug->unplug_fn(blkif->plug);
  138. blk_put_queue(blkif->plug);
  139. blkif->plug = NULL;
  140. }
  141. static void plug_queue(blkif_t *blkif, struct block_device *bdev)
  142. {
  143. struct request_queue *q = bdev_get_queue(bdev);
  144. if (q == blkif->plug)
  145. return;
  146. unplug_queue(blkif);
  147. blk_get_queue(q);
  148. blkif->plug = q;
  149. }
  150. static void fast_flush_area(pending_req_t *req)
  151. {
  152. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  153. unsigned int i, invcount = 0;
  154. grant_handle_t handle;
  155. int ret;
  156. for (i = 0; i < req->nr_pages; i++) {
  157. handle = pending_handle(req, i);
  158. if (handle == BLKBACK_INVALID_HANDLE)
  159. continue;
  160. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  161. GNTMAP_host_map, handle);
  162. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  163. invcount++;
  164. }
  165. ret = HYPERVISOR_grant_table_op(
  166. GNTTABOP_unmap_grant_ref, unmap, invcount);
  167. BUG_ON(ret);
  168. }
  169. /******************************************************************
  170. * SCHEDULER FUNCTIONS
  171. */
  172. static void print_stats(blkif_t *blkif)
  173. {
  174. printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n",
  175. current->comm, blkif->st_oo_req,
  176. blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
  177. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  178. blkif->st_rd_req = 0;
  179. blkif->st_wr_req = 0;
  180. blkif->st_oo_req = 0;
  181. }
  182. int blkif_schedule(void *arg)
  183. {
  184. blkif_t *blkif = arg;
  185. struct vbd *vbd = &blkif->vbd;
  186. blkif_get(blkif);
  187. if (debug_lvl)
  188. printk(KERN_DEBUG "%s: started\n", current->comm);
  189. while (!kthread_should_stop()) {
  190. if (try_to_freeze())
  191. continue;
  192. if (unlikely(vbd->size != vbd_size(vbd)))
  193. vbd_resize(blkif);
  194. wait_event_interruptible(
  195. blkif->wq,
  196. blkif->waiting_reqs || kthread_should_stop());
  197. wait_event_interruptible(
  198. pending_free_wq,
  199. !list_empty(&pending_free) || kthread_should_stop());
  200. blkif->waiting_reqs = 0;
  201. smp_mb(); /* clear flag *before* checking for work */
  202. if (do_block_io_op(blkif))
  203. blkif->waiting_reqs = 1;
  204. unplug_queue(blkif);
  205. if (log_stats && time_after(jiffies, blkif->st_print))
  206. print_stats(blkif);
  207. }
  208. if (log_stats)
  209. print_stats(blkif);
  210. if (debug_lvl)
  211. printk(KERN_DEBUG "%s: exiting\n", current->comm);
  212. blkif->xenblkd = NULL;
  213. blkif_put(blkif);
  214. return 0;
  215. }
  216. /******************************************************************
  217. * COMPLETION CALLBACK -- Called as bh->b_end_io()
  218. */
  219. static void __end_block_io_op(pending_req_t *pending_req, int error)
  220. {
  221. /* An error fails the entire request. */
  222. if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  223. (error == -EOPNOTSUPP)) {
  224. DPRINTK("blkback: write barrier op failed, not supported\n");
  225. blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
  226. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  227. } else if (error) {
  228. DPRINTK("Buffer not up-to-date at end of operation, "
  229. "error=%d\n", error);
  230. pending_req->status = BLKIF_RSP_ERROR;
  231. }
  232. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  233. fast_flush_area(pending_req);
  234. make_response(pending_req->blkif, pending_req->id,
  235. pending_req->operation, pending_req->status);
  236. blkif_put(pending_req->blkif);
  237. free_req(pending_req);
  238. }
  239. }
  240. static void end_block_io_op(struct bio *bio, int error)
  241. {
  242. __end_block_io_op(bio->bi_private, error);
  243. bio_put(bio);
  244. }
  245. /******************************************************************************
  246. * NOTIFICATION FROM GUEST OS.
  247. */
  248. static void blkif_notify_work(blkif_t *blkif)
  249. {
  250. blkif->waiting_reqs = 1;
  251. wake_up(&blkif->wq);
  252. }
  253. irqreturn_t blkif_be_int(int irq, void *dev_id)
  254. {
  255. blkif_notify_work(dev_id);
  256. return IRQ_HANDLED;
  257. }
  258. /******************************************************************
  259. * DOWNWARD CALLS -- These interface with the block-device layer proper.
  260. */
  261. static int do_block_io_op(blkif_t *blkif)
  262. {
  263. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  264. struct blkif_request req;
  265. pending_req_t *pending_req;
  266. RING_IDX rc, rp;
  267. int more_to_do = 0;
  268. rc = blk_rings->common.req_cons;
  269. rp = blk_rings->common.sring->req_prod;
  270. rmb(); /* Ensure we see queued requests up to 'rp'. */
  271. while (rc != rp) {
  272. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  273. break;
  274. if (kthread_should_stop()) {
  275. more_to_do = 1;
  276. break;
  277. }
  278. pending_req = alloc_req();
  279. if (NULL == pending_req) {
  280. blkif->st_oo_req++;
  281. more_to_do = 1;
  282. break;
  283. }
  284. switch (blkif->blk_protocol) {
  285. case BLKIF_PROTOCOL_NATIVE:
  286. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  287. break;
  288. case BLKIF_PROTOCOL_X86_32:
  289. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  290. break;
  291. case BLKIF_PROTOCOL_X86_64:
  292. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  293. break;
  294. default:
  295. BUG();
  296. }
  297. blk_rings->common.req_cons = ++rc; /* before make_response() */
  298. /* Apply all sanity checks to /private copy/ of request. */
  299. barrier();
  300. switch (req.operation) {
  301. case BLKIF_OP_READ:
  302. blkif->st_rd_req++;
  303. dispatch_rw_block_io(blkif, &req, pending_req);
  304. break;
  305. case BLKIF_OP_WRITE_BARRIER:
  306. blkif->st_br_req++;
  307. /* fall through */
  308. case BLKIF_OP_WRITE:
  309. blkif->st_wr_req++;
  310. dispatch_rw_block_io(blkif, &req, pending_req);
  311. break;
  312. default:
  313. /* A good sign something is wrong: sleep for a while to
  314. * avoid excessive CPU consumption by a bad guest. */
  315. msleep(1);
  316. DPRINTK("error: unknown block io operation [%d]\n",
  317. req.operation);
  318. make_response(blkif, req.id, req.operation,
  319. BLKIF_RSP_ERROR);
  320. free_req(pending_req);
  321. break;
  322. }
  323. /* Yield point for this unbounded loop. */
  324. cond_resched();
  325. }
  326. return more_to_do;
  327. }
  328. static void dispatch_rw_block_io(blkif_t *blkif,
  329. struct blkif_request *req,
  330. pending_req_t *pending_req)
  331. {
  332. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  333. struct phys_req preq;
  334. struct {
  335. unsigned long buf; unsigned int nsec;
  336. } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  337. unsigned int nseg;
  338. struct bio *bio = NULL;
  339. int ret, i;
  340. int operation;
  341. switch (req->operation) {
  342. case BLKIF_OP_READ:
  343. operation = READ;
  344. break;
  345. case BLKIF_OP_WRITE:
  346. operation = WRITE;
  347. break;
  348. case BLKIF_OP_WRITE_BARRIER:
  349. operation = REQ_FLUSH | REQ_FUA;
  350. break;
  351. default:
  352. operation = 0; /* make gcc happy */
  353. BUG();
  354. }
  355. /* Check that number of segments is sane. */
  356. nseg = req->nr_segments;
  357. if (unlikely(nseg == 0 && operation != (REQ_FLUSH | REQ_FUA)) ||
  358. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  359. DPRINTK("Bad number of segments in request (%d)\n", nseg);
  360. goto fail_response;
  361. }
  362. preq.dev = req->handle;
  363. preq.sector_number = req->sector_number;
  364. preq.nr_sects = 0;
  365. pending_req->blkif = blkif;
  366. pending_req->id = req->id;
  367. pending_req->operation = req->operation;
  368. pending_req->status = BLKIF_RSP_OKAY;
  369. pending_req->nr_pages = nseg;
  370. for (i = 0; i < nseg; i++) {
  371. uint32_t flags;
  372. seg[i].nsec = req->seg[i].last_sect -
  373. req->seg[i].first_sect + 1;
  374. if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  375. (req->seg[i].last_sect < req->seg[i].first_sect))
  376. goto fail_response;
  377. preq.nr_sects += seg[i].nsec;
  378. flags = GNTMAP_host_map;
  379. if (operation != READ)
  380. flags |= GNTMAP_readonly;
  381. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  382. req->seg[i].gref, blkif->domid);
  383. }
  384. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
  385. BUG_ON(ret);
  386. for (i = 0; i < nseg; i++) {
  387. if (unlikely(map[i].status != 0)) {
  388. DPRINTK("invalid buffer -- could not remap it\n");
  389. map[i].handle = BLKBACK_INVALID_HANDLE;
  390. ret |= 1;
  391. }
  392. pending_handle(pending_req, i) = map[i].handle;
  393. if (ret)
  394. continue;
  395. set_phys_to_machine(
  396. page_to_pfn(pending_page(pending_req, i)),
  397. FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
  398. seg[i].buf = map[i].dev_bus_addr |
  399. (req->seg[i].first_sect << 9);
  400. }
  401. if (ret)
  402. goto fail_flush;
  403. if (vbd_translate(&preq, blkif, operation) != 0) {
  404. DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
  405. operation == READ ? "read" : "write",
  406. preq.sector_number,
  407. preq.sector_number + preq.nr_sects, preq.dev);
  408. goto fail_flush;
  409. }
  410. plug_queue(blkif, preq.bdev);
  411. atomic_set(&pending_req->pendcnt, 1);
  412. blkif_get(blkif);
  413. for (i = 0; i < nseg; i++) {
  414. if (((int)preq.sector_number|(int)seg[i].nsec) &
  415. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  416. DPRINTK("Misaligned I/O request from domain %d",
  417. blkif->domid);
  418. goto fail_put_bio;
  419. }
  420. while ((bio == NULL) ||
  421. (bio_add_page(bio,
  422. pending_page(pending_req, i),
  423. seg[i].nsec << 9,
  424. seg[i].buf & ~PAGE_MASK) == 0)) {
  425. if (bio) {
  426. atomic_inc(&pending_req->pendcnt);
  427. submit_bio(operation, bio);
  428. }
  429. bio = bio_alloc(GFP_KERNEL, nseg-i);
  430. if (unlikely(bio == NULL))
  431. goto fail_put_bio;
  432. bio->bi_bdev = preq.bdev;
  433. bio->bi_private = pending_req;
  434. bio->bi_end_io = end_block_io_op;
  435. bio->bi_sector = preq.sector_number;
  436. }
  437. preq.sector_number += seg[i].nsec;
  438. }
  439. if (!bio) {
  440. BUG_ON(operation != (REQ_FLUSH | REQ_FUA));
  441. bio = bio_alloc(GFP_KERNEL, 0);
  442. if (unlikely(bio == NULL))
  443. goto fail_put_bio;
  444. bio->bi_bdev = preq.bdev;
  445. bio->bi_private = pending_req;
  446. bio->bi_end_io = end_block_io_op;
  447. bio->bi_sector = -1;
  448. }
  449. submit_bio(operation, bio);
  450. if (operation == READ)
  451. blkif->st_rd_sect += preq.nr_sects;
  452. else if (operation == WRITE || operation == (REQ_FLUSH | REQ_FUA))
  453. blkif->st_wr_sect += preq.nr_sects;
  454. return;
  455. fail_flush:
  456. fast_flush_area(pending_req);
  457. fail_response:
  458. make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
  459. free_req(pending_req);
  460. msleep(1); /* back off a bit */
  461. return;
  462. fail_put_bio:
  463. __end_block_io_op(pending_req, -EINVAL);
  464. if (bio)
  465. bio_put(bio);
  466. unplug_queue(blkif);
  467. msleep(1); /* back off a bit */
  468. return;
  469. }
  470. /******************************************************************
  471. * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
  472. */
  473. static void make_response(blkif_t *blkif, u64 id,
  474. unsigned short op, int st)
  475. {
  476. struct blkif_response resp;
  477. unsigned long flags;
  478. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  479. int more_to_do = 0;
  480. int notify;
  481. resp.id = id;
  482. resp.operation = op;
  483. resp.status = st;
  484. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  485. /* Place on the response ring for the relevant domain. */
  486. switch (blkif->blk_protocol) {
  487. case BLKIF_PROTOCOL_NATIVE:
  488. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  489. &resp, sizeof(resp));
  490. break;
  491. case BLKIF_PROTOCOL_X86_32:
  492. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  493. &resp, sizeof(resp));
  494. break;
  495. case BLKIF_PROTOCOL_X86_64:
  496. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  497. &resp, sizeof(resp));
  498. break;
  499. default:
  500. BUG();
  501. }
  502. blk_rings->common.rsp_prod_pvt++;
  503. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  504. if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
  505. /*
  506. * Tail check for pending requests. Allows frontend to avoid
  507. * notifications if requests are already in flight (lower
  508. * overheads and promotes batching).
  509. */
  510. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  511. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
  512. more_to_do = 1;
  513. }
  514. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  515. if (more_to_do)
  516. blkif_notify_work(blkif);
  517. if (notify)
  518. notify_remote_via_irq(blkif->irq);
  519. }
  520. static int __init blkif_init(void)
  521. {
  522. int i, mmap_pages;
  523. int rc = 0;
  524. if (!xen_pv_domain())
  525. return -ENODEV;
  526. mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  527. pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
  528. blkif_reqs, GFP_KERNEL);
  529. pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
  530. mmap_pages, GFP_KERNEL);
  531. pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
  532. if (!pending_reqs || !pending_grant_handles || !pending_pages) {
  533. rc = -ENOMEM;
  534. goto out_of_memory;
  535. }
  536. for (i = 0; i < mmap_pages; i++)
  537. pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  538. rc = blkif_interface_init();
  539. if (rc)
  540. goto failed_init;
  541. memset(pending_reqs, 0, sizeof(pending_reqs));
  542. INIT_LIST_HEAD(&pending_free);
  543. for (i = 0; i < blkif_reqs; i++)
  544. list_add_tail(&pending_reqs[i].free_list, &pending_free);
  545. rc = blkif_xenbus_init();
  546. if (rc)
  547. goto failed_init;
  548. return 0;
  549. out_of_memory:
  550. printk(KERN_ERR "%s: out of memory\n", __func__);
  551. failed_init:
  552. kfree(pending_reqs);
  553. kfree(pending_grant_handles);
  554. free_empty_pages_and_pagevec(pending_pages, mmap_pages);
  555. return rc;
  556. }
  557. module_init(blkif_init);
  558. MODULE_LICENSE("Dual BSD/GPL");