blkback.c 18 KB

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