blkback.c 24 KB

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