blkback.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. /*
  47. * These are rather arbitrary. They are fairly large because adjacent requests
  48. * pulled from a communication ring are quite likely to end up being part of
  49. * the same scatter/gather request at the disc.
  50. *
  51. * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
  52. *
  53. * This will increase the chances of being able to write whole tracks.
  54. * 64 should be enough to keep us competitive with Linux.
  55. */
  56. static int xen_blkif_reqs = 64;
  57. module_param_named(reqs, xen_blkif_reqs, int, 0);
  58. MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
  59. /* Run-time switchable: /sys/module/blkback/parameters/ */
  60. static unsigned int log_stats;
  61. module_param(log_stats, int, 0644);
  62. /*
  63. * Each outstanding request that we've passed to the lower device layers has a
  64. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  65. * the pendcnt towards zero. When it hits zero, the specified domain has a
  66. * response queued for it, with the saved 'id' passed back.
  67. */
  68. struct pending_req {
  69. struct xen_blkif *blkif;
  70. u64 id;
  71. int nr_pages;
  72. atomic_t pendcnt;
  73. unsigned short operation;
  74. int status;
  75. struct list_head free_list;
  76. };
  77. #define BLKBACK_INVALID_HANDLE (~0)
  78. struct xen_blkbk {
  79. struct pending_req *pending_reqs;
  80. /* List of all 'pending_req' available */
  81. struct list_head pending_free;
  82. /* And its spinlock. */
  83. spinlock_t pending_free_lock;
  84. wait_queue_head_t pending_free_wq;
  85. /* The list of all pages that are available. */
  86. struct page **pending_pages;
  87. /* And the grant handles that are available. */
  88. grant_handle_t *pending_grant_handles;
  89. };
  90. static struct xen_blkbk *blkbk;
  91. /*
  92. * Little helpful macro to figure out the index and virtual address of the
  93. * pending_pages[..]. For each 'pending_req' we have have up to
  94. * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
  95. * 10 and would index in the pending_pages[..].
  96. */
  97. static inline int vaddr_pagenr(struct pending_req *req, int seg)
  98. {
  99. return (req - blkbk->pending_reqs) *
  100. BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
  101. }
  102. #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
  103. static inline unsigned long vaddr(struct pending_req *req, int seg)
  104. {
  105. unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
  106. return (unsigned long)pfn_to_kaddr(pfn);
  107. }
  108. #define pending_handle(_req, _seg) \
  109. (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
  110. static int do_block_io_op(struct xen_blkif *blkif);
  111. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  112. struct blkif_request *req,
  113. struct pending_req *pending_req);
  114. static void make_response(struct xen_blkif *blkif, u64 id,
  115. unsigned short op, int st);
  116. /*
  117. * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  118. */
  119. static struct pending_req *alloc_req(void)
  120. {
  121. struct pending_req *req = NULL;
  122. unsigned long flags;
  123. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  124. if (!list_empty(&blkbk->pending_free)) {
  125. req = list_entry(blkbk->pending_free.next, struct pending_req,
  126. free_list);
  127. list_del(&req->free_list);
  128. }
  129. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  130. return req;
  131. }
  132. /*
  133. * Return the 'pending_req' structure back to the freepool. We also
  134. * wake up the thread if it was waiting for a free page.
  135. */
  136. static void free_req(struct pending_req *req)
  137. {
  138. unsigned long flags;
  139. int was_empty;
  140. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  141. was_empty = list_empty(&blkbk->pending_free);
  142. list_add(&req->free_list, &blkbk->pending_free);
  143. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  144. if (was_empty)
  145. wake_up(&blkbk->pending_free_wq);
  146. }
  147. /*
  148. * Routines for managing virtual block devices (vbds).
  149. */
  150. static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
  151. int operation)
  152. {
  153. struct xen_vbd *vbd = &blkif->vbd;
  154. int rc = -EACCES;
  155. if ((operation != READ) && vbd->readonly)
  156. goto out;
  157. if (likely(req->nr_sects)) {
  158. blkif_sector_t end = req->sector_number + req->nr_sects;
  159. if (unlikely(end < req->sector_number))
  160. goto out;
  161. if (unlikely(end > vbd_sz(vbd)))
  162. goto out;
  163. }
  164. req->dev = vbd->pdevice;
  165. req->bdev = vbd->bdev;
  166. rc = 0;
  167. out:
  168. return rc;
  169. }
  170. static void xen_vbd_resize(struct xen_blkif *blkif)
  171. {
  172. struct xen_vbd *vbd = &blkif->vbd;
  173. struct xenbus_transaction xbt;
  174. int err;
  175. struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
  176. unsigned long long new_size = vbd_sz(vbd);
  177. pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  178. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  179. pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
  180. vbd->size = new_size;
  181. again:
  182. err = xenbus_transaction_start(&xbt);
  183. if (err) {
  184. pr_warn(DRV_PFX "Error starting transaction");
  185. return;
  186. }
  187. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  188. (unsigned long long)vbd_sz(vbd));
  189. if (err) {
  190. pr_warn(DRV_PFX "Error writing new size");
  191. goto abort;
  192. }
  193. /*
  194. * Write the current state; we will use this to synchronize
  195. * the front-end. If the current state is "connected" the
  196. * front-end will get the new size information online.
  197. */
  198. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  199. if (err) {
  200. pr_warn(DRV_PFX "Error writing the state");
  201. goto abort;
  202. }
  203. err = xenbus_transaction_end(xbt, 0);
  204. if (err == -EAGAIN)
  205. goto again;
  206. if (err)
  207. pr_warn(DRV_PFX "Error ending transaction");
  208. return;
  209. abort:
  210. xenbus_transaction_end(xbt, 1);
  211. }
  212. /*
  213. * Notification from the guest OS.
  214. */
  215. static void blkif_notify_work(struct xen_blkif *blkif)
  216. {
  217. blkif->waiting_reqs = 1;
  218. wake_up(&blkif->wq);
  219. }
  220. irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
  221. {
  222. blkif_notify_work(dev_id);
  223. return IRQ_HANDLED;
  224. }
  225. /*
  226. * SCHEDULER FUNCTIONS
  227. */
  228. static void print_stats(struct xen_blkif *blkif)
  229. {
  230. pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
  231. " | ds %4d\n",
  232. current->comm, blkif->st_oo_req,
  233. blkif->st_rd_req, blkif->st_wr_req,
  234. blkif->st_f_req, blkif->st_ds_req);
  235. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  236. blkif->st_rd_req = 0;
  237. blkif->st_wr_req = 0;
  238. blkif->st_oo_req = 0;
  239. blkif->st_ds_req = 0;
  240. }
  241. int xen_blkif_schedule(void *arg)
  242. {
  243. struct xen_blkif *blkif = arg;
  244. struct xen_vbd *vbd = &blkif->vbd;
  245. int ret;
  246. xen_blkif_get(blkif);
  247. while (!kthread_should_stop()) {
  248. if (try_to_freeze())
  249. continue;
  250. if (unlikely(vbd->size != vbd_sz(vbd)))
  251. xen_vbd_resize(blkif);
  252. wait_event_interruptible(
  253. blkif->wq,
  254. blkif->waiting_reqs || kthread_should_stop());
  255. wait_event_interruptible(
  256. blkbk->pending_free_wq,
  257. !list_empty(&blkbk->pending_free) ||
  258. kthread_should_stop());
  259. blkif->waiting_reqs = 0;
  260. smp_mb(); /* clear flag *before* checking for work */
  261. ret = do_block_io_op(blkif);
  262. if (ret > 0)
  263. blkif->waiting_reqs = 1;
  264. if (ret == -EACCES)
  265. wait_event_interruptible(blkif->shutdown_wq,
  266. kthread_should_stop());
  267. if (log_stats && time_after(jiffies, blkif->st_print))
  268. print_stats(blkif);
  269. }
  270. if (log_stats)
  271. print_stats(blkif);
  272. blkif->xenblkd = NULL;
  273. xen_blkif_put(blkif);
  274. return 0;
  275. }
  276. struct seg_buf {
  277. unsigned long buf;
  278. unsigned int nsec;
  279. };
  280. /*
  281. * Unmap the grant references, and also remove the M2P over-rides
  282. * used in the 'pending_req'.
  283. */
  284. static void xen_blkbk_unmap(struct pending_req *req)
  285. {
  286. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  287. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  288. unsigned int i, invcount = 0;
  289. grant_handle_t handle;
  290. int ret;
  291. for (i = 0; i < req->nr_pages; i++) {
  292. handle = pending_handle(req, i);
  293. if (handle == BLKBACK_INVALID_HANDLE)
  294. continue;
  295. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  296. GNTMAP_host_map, handle);
  297. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  298. pages[invcount] = virt_to_page(vaddr(req, i));
  299. invcount++;
  300. }
  301. ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  302. BUG_ON(ret);
  303. }
  304. static int xen_blkbk_map(struct blkif_request *req,
  305. struct pending_req *pending_req,
  306. struct seg_buf seg[])
  307. {
  308. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  309. int i;
  310. int nseg = req->u.rw.nr_segments;
  311. int ret = 0;
  312. /*
  313. * Fill out preq.nr_sects with proper amount of sectors, and setup
  314. * assign map[..] with the PFN of the page in our domain with the
  315. * corresponding grant reference for each page.
  316. */
  317. for (i = 0; i < nseg; i++) {
  318. uint32_t flags;
  319. flags = GNTMAP_host_map;
  320. if (pending_req->operation != BLKIF_OP_READ)
  321. flags |= GNTMAP_readonly;
  322. gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
  323. req->u.rw.seg[i].gref,
  324. pending_req->blkif->domid);
  325. }
  326. ret = gnttab_map_refs(map, NULL, &blkbk->pending_page(pending_req, 0), nseg);
  327. BUG_ON(ret);
  328. /*
  329. * Now swizzle the MFN in our domain with the MFN from the other domain
  330. * so that when we access vaddr(pending_req,i) it has the contents of
  331. * the page from the other domain.
  332. */
  333. for (i = 0; i < nseg; i++) {
  334. if (unlikely(map[i].status != 0)) {
  335. pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
  336. map[i].handle = BLKBACK_INVALID_HANDLE;
  337. ret |= 1;
  338. }
  339. pending_handle(pending_req, i) = map[i].handle;
  340. if (ret)
  341. continue;
  342. seg[i].buf = map[i].dev_bus_addr |
  343. (req->u.rw.seg[i].first_sect << 9);
  344. }
  345. return ret;
  346. }
  347. static int dispatch_discard_io(struct xen_blkif *blkif,
  348. struct blkif_request *req)
  349. {
  350. int err = 0;
  351. int status = BLKIF_RSP_OKAY;
  352. struct block_device *bdev = blkif->vbd.bdev;
  353. unsigned long secure;
  354. struct phys_req preq;
  355. xen_blkif_get(blkif);
  356. preq.sector_number = req->u.discard.sector_number;
  357. preq.nr_sects = req->u.discard.nr_sectors;
  358. err = xen_vbd_translate(&preq, blkif, WRITE);
  359. if (err) {
  360. pr_warn(DRV_PFX "access denied: DISCARD [%llu->%llu] on dev=%04x\n",
  361. preq.sector_number,
  362. preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
  363. goto fail_response;
  364. }
  365. blkif->st_ds_req++;
  366. secure = (blkif->vbd.discard_secure &&
  367. (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
  368. BLKDEV_DISCARD_SECURE : 0;
  369. err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
  370. req->u.discard.nr_sectors,
  371. GFP_KERNEL, secure);
  372. fail_response:
  373. if (err == -EOPNOTSUPP) {
  374. pr_debug(DRV_PFX "discard op failed, not supported\n");
  375. status = BLKIF_RSP_EOPNOTSUPP;
  376. } else if (err)
  377. status = BLKIF_RSP_ERROR;
  378. make_response(blkif, req->u.discard.id, req->operation, status);
  379. xen_blkif_put(blkif);
  380. return err;
  381. }
  382. static int dispatch_other_io(struct xen_blkif *blkif,
  383. struct blkif_request *req,
  384. struct pending_req *pending_req)
  385. {
  386. free_req(pending_req);
  387. make_response(blkif, req->u.other.id, req->operation,
  388. BLKIF_RSP_EOPNOTSUPP);
  389. return -EIO;
  390. }
  391. static void xen_blk_drain_io(struct xen_blkif *blkif)
  392. {
  393. atomic_set(&blkif->drain, 1);
  394. do {
  395. /* The initial value is one, and one refcnt taken at the
  396. * start of the xen_blkif_schedule thread. */
  397. if (atomic_read(&blkif->refcnt) <= 2)
  398. break;
  399. wait_for_completion_interruptible_timeout(
  400. &blkif->drain_complete, HZ);
  401. if (!atomic_read(&blkif->drain))
  402. break;
  403. } while (!kthread_should_stop());
  404. atomic_set(&blkif->drain, 0);
  405. }
  406. /*
  407. * Completion callback on the bio's. Called as bh->b_end_io()
  408. */
  409. static void __end_block_io_op(struct pending_req *pending_req, int error)
  410. {
  411. /* An error fails the entire request. */
  412. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  413. (error == -EOPNOTSUPP)) {
  414. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  415. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  416. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  417. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  418. (error == -EOPNOTSUPP)) {
  419. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  420. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  421. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  422. } else if (error) {
  423. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  424. " error=%d\n", error);
  425. pending_req->status = BLKIF_RSP_ERROR;
  426. }
  427. /*
  428. * If all of the bio's have completed it is time to unmap
  429. * the grant references associated with 'request' and provide
  430. * the proper response on the ring.
  431. */
  432. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  433. xen_blkbk_unmap(pending_req);
  434. make_response(pending_req->blkif, pending_req->id,
  435. pending_req->operation, pending_req->status);
  436. xen_blkif_put(pending_req->blkif);
  437. if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
  438. if (atomic_read(&pending_req->blkif->drain))
  439. complete(&pending_req->blkif->drain_complete);
  440. }
  441. free_req(pending_req);
  442. }
  443. }
  444. /*
  445. * bio callback.
  446. */
  447. static void end_block_io_op(struct bio *bio, int error)
  448. {
  449. __end_block_io_op(bio->bi_private, error);
  450. bio_put(bio);
  451. }
  452. /*
  453. * Function to copy the from the ring buffer the 'struct blkif_request'
  454. * (which has the sectors we want, number of them, grant references, etc),
  455. * and transmute it to the block API to hand it over to the proper block disk.
  456. */
  457. static int
  458. __do_block_io_op(struct xen_blkif *blkif)
  459. {
  460. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  461. struct blkif_request req;
  462. struct pending_req *pending_req;
  463. RING_IDX rc, rp;
  464. int more_to_do = 0;
  465. rc = blk_rings->common.req_cons;
  466. rp = blk_rings->common.sring->req_prod;
  467. rmb(); /* Ensure we see queued requests up to 'rp'. */
  468. if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
  469. rc = blk_rings->common.rsp_prod_pvt;
  470. pr_warn(DRV_PFX "Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
  471. rp, rc, rp - rc, blkif->vbd.pdevice);
  472. return -EACCES;
  473. }
  474. while (rc != rp) {
  475. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  476. break;
  477. if (kthread_should_stop()) {
  478. more_to_do = 1;
  479. break;
  480. }
  481. pending_req = alloc_req();
  482. if (NULL == pending_req) {
  483. blkif->st_oo_req++;
  484. more_to_do = 1;
  485. break;
  486. }
  487. switch (blkif->blk_protocol) {
  488. case BLKIF_PROTOCOL_NATIVE:
  489. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  490. break;
  491. case BLKIF_PROTOCOL_X86_32:
  492. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  493. break;
  494. case BLKIF_PROTOCOL_X86_64:
  495. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  496. break;
  497. default:
  498. BUG();
  499. }
  500. blk_rings->common.req_cons = ++rc; /* before make_response() */
  501. /* Apply all sanity checks to /private copy/ of request. */
  502. barrier();
  503. switch (req.operation) {
  504. case BLKIF_OP_READ:
  505. case BLKIF_OP_WRITE:
  506. case BLKIF_OP_WRITE_BARRIER:
  507. case BLKIF_OP_FLUSH_DISKCACHE:
  508. if (dispatch_rw_block_io(blkif, &req, pending_req))
  509. goto done;
  510. break;
  511. case BLKIF_OP_DISCARD:
  512. free_req(pending_req);
  513. if (dispatch_discard_io(blkif, &req))
  514. goto done;
  515. break;
  516. default:
  517. if (dispatch_other_io(blkif, &req, pending_req))
  518. goto done;
  519. break;
  520. }
  521. /* Yield point for this unbounded loop. */
  522. cond_resched();
  523. }
  524. done:
  525. return more_to_do;
  526. }
  527. static int
  528. do_block_io_op(struct xen_blkif *blkif)
  529. {
  530. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  531. int more_to_do;
  532. do {
  533. more_to_do = __do_block_io_op(blkif);
  534. if (more_to_do)
  535. break;
  536. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  537. } while (more_to_do);
  538. return more_to_do;
  539. }
  540. /*
  541. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  542. * and call the 'submit_bio' to pass it to the underlying storage.
  543. */
  544. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  545. struct blkif_request *req,
  546. struct pending_req *pending_req)
  547. {
  548. struct phys_req preq;
  549. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  550. unsigned int nseg;
  551. struct bio *bio = NULL;
  552. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  553. int i, nbio = 0;
  554. int operation;
  555. struct blk_plug plug;
  556. bool drain = false;
  557. switch (req->operation) {
  558. case BLKIF_OP_READ:
  559. blkif->st_rd_req++;
  560. operation = READ;
  561. break;
  562. case BLKIF_OP_WRITE:
  563. blkif->st_wr_req++;
  564. operation = WRITE_ODIRECT;
  565. break;
  566. case BLKIF_OP_WRITE_BARRIER:
  567. drain = true;
  568. case BLKIF_OP_FLUSH_DISKCACHE:
  569. blkif->st_f_req++;
  570. operation = WRITE_FLUSH;
  571. break;
  572. default:
  573. operation = 0; /* make gcc happy */
  574. goto fail_response;
  575. break;
  576. }
  577. /* Check that the number of segments is sane. */
  578. nseg = req->u.rw.nr_segments;
  579. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  580. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  581. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  582. nseg);
  583. /* Haven't submitted any bio's yet. */
  584. goto fail_response;
  585. }
  586. preq.dev = req->u.rw.handle;
  587. preq.sector_number = req->u.rw.sector_number;
  588. preq.nr_sects = 0;
  589. pending_req->blkif = blkif;
  590. pending_req->id = req->u.rw.id;
  591. pending_req->operation = req->operation;
  592. pending_req->status = BLKIF_RSP_OKAY;
  593. pending_req->nr_pages = nseg;
  594. for (i = 0; i < nseg; i++) {
  595. seg[i].nsec = req->u.rw.seg[i].last_sect -
  596. req->u.rw.seg[i].first_sect + 1;
  597. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  598. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  599. goto fail_response;
  600. preq.nr_sects += seg[i].nsec;
  601. }
  602. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  603. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  604. operation == READ ? "read" : "write",
  605. preq.sector_number,
  606. preq.sector_number + preq.nr_sects, preq.dev);
  607. goto fail_response;
  608. }
  609. /*
  610. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  611. * is set there.
  612. */
  613. for (i = 0; i < nseg; i++) {
  614. if (((int)preq.sector_number|(int)seg[i].nsec) &
  615. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  616. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  617. blkif->domid);
  618. goto fail_response;
  619. }
  620. }
  621. /* Wait on all outstanding I/O's and once that has been completed
  622. * issue the WRITE_FLUSH.
  623. */
  624. if (drain)
  625. xen_blk_drain_io(pending_req->blkif);
  626. /*
  627. * If we have failed at this point, we need to undo the M2P override,
  628. * set gnttab_set_unmap_op on all of the grant references and perform
  629. * the hypercall to unmap the grants - that is all done in
  630. * xen_blkbk_unmap.
  631. */
  632. if (xen_blkbk_map(req, pending_req, seg))
  633. goto fail_flush;
  634. /*
  635. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  636. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  637. */
  638. xen_blkif_get(blkif);
  639. for (i = 0; i < nseg; i++) {
  640. while ((bio == NULL) ||
  641. (bio_add_page(bio,
  642. blkbk->pending_page(pending_req, i),
  643. seg[i].nsec << 9,
  644. seg[i].buf & ~PAGE_MASK) == 0)) {
  645. bio = bio_alloc(GFP_KERNEL, nseg-i);
  646. if (unlikely(bio == NULL))
  647. goto fail_put_bio;
  648. biolist[nbio++] = bio;
  649. bio->bi_bdev = preq.bdev;
  650. bio->bi_private = pending_req;
  651. bio->bi_end_io = end_block_io_op;
  652. bio->bi_sector = preq.sector_number;
  653. }
  654. preq.sector_number += seg[i].nsec;
  655. }
  656. /* This will be hit if the operation was a flush or discard. */
  657. if (!bio) {
  658. BUG_ON(operation != WRITE_FLUSH);
  659. bio = bio_alloc(GFP_KERNEL, 0);
  660. if (unlikely(bio == NULL))
  661. goto fail_put_bio;
  662. biolist[nbio++] = bio;
  663. bio->bi_bdev = preq.bdev;
  664. bio->bi_private = pending_req;
  665. bio->bi_end_io = end_block_io_op;
  666. }
  667. atomic_set(&pending_req->pendcnt, nbio);
  668. blk_start_plug(&plug);
  669. for (i = 0; i < nbio; i++)
  670. submit_bio(operation, biolist[i]);
  671. /* Let the I/Os go.. */
  672. blk_finish_plug(&plug);
  673. if (operation == READ)
  674. blkif->st_rd_sect += preq.nr_sects;
  675. else if (operation & WRITE)
  676. blkif->st_wr_sect += preq.nr_sects;
  677. return 0;
  678. fail_flush:
  679. xen_blkbk_unmap(pending_req);
  680. fail_response:
  681. /* Haven't submitted any bio's yet. */
  682. make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
  683. free_req(pending_req);
  684. msleep(1); /* back off a bit */
  685. return -EIO;
  686. fail_put_bio:
  687. for (i = 0; i < nbio; i++)
  688. bio_put(biolist[i]);
  689. atomic_set(&pending_req->pendcnt, 1);
  690. __end_block_io_op(pending_req, -EINVAL);
  691. msleep(1); /* back off a bit */
  692. return -EIO;
  693. }
  694. /*
  695. * Put a response on the ring on how the operation fared.
  696. */
  697. static void make_response(struct xen_blkif *blkif, u64 id,
  698. unsigned short op, int st)
  699. {
  700. struct blkif_response resp;
  701. unsigned long flags;
  702. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  703. int notify;
  704. resp.id = id;
  705. resp.operation = op;
  706. resp.status = st;
  707. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  708. /* Place on the response ring for the relevant domain. */
  709. switch (blkif->blk_protocol) {
  710. case BLKIF_PROTOCOL_NATIVE:
  711. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  712. &resp, sizeof(resp));
  713. break;
  714. case BLKIF_PROTOCOL_X86_32:
  715. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  716. &resp, sizeof(resp));
  717. break;
  718. case BLKIF_PROTOCOL_X86_64:
  719. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  720. &resp, sizeof(resp));
  721. break;
  722. default:
  723. BUG();
  724. }
  725. blk_rings->common.rsp_prod_pvt++;
  726. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  727. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  728. if (notify)
  729. notify_remote_via_irq(blkif->irq);
  730. }
  731. static int __init xen_blkif_init(void)
  732. {
  733. int i, mmap_pages;
  734. int rc = 0;
  735. if (!xen_domain())
  736. return -ENODEV;
  737. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  738. if (!blkbk) {
  739. pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
  740. return -ENOMEM;
  741. }
  742. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  743. blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
  744. xen_blkif_reqs, GFP_KERNEL);
  745. blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
  746. mmap_pages, GFP_KERNEL);
  747. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  748. mmap_pages, GFP_KERNEL);
  749. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  750. !blkbk->pending_pages) {
  751. rc = -ENOMEM;
  752. goto out_of_memory;
  753. }
  754. for (i = 0; i < mmap_pages; i++) {
  755. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  756. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  757. if (blkbk->pending_pages[i] == NULL) {
  758. rc = -ENOMEM;
  759. goto out_of_memory;
  760. }
  761. }
  762. rc = xen_blkif_interface_init();
  763. if (rc)
  764. goto failed_init;
  765. INIT_LIST_HEAD(&blkbk->pending_free);
  766. spin_lock_init(&blkbk->pending_free_lock);
  767. init_waitqueue_head(&blkbk->pending_free_wq);
  768. for (i = 0; i < xen_blkif_reqs; i++)
  769. list_add_tail(&blkbk->pending_reqs[i].free_list,
  770. &blkbk->pending_free);
  771. rc = xen_blkif_xenbus_init();
  772. if (rc)
  773. goto failed_init;
  774. return 0;
  775. out_of_memory:
  776. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  777. failed_init:
  778. kfree(blkbk->pending_reqs);
  779. kfree(blkbk->pending_grant_handles);
  780. if (blkbk->pending_pages) {
  781. for (i = 0; i < mmap_pages; i++) {
  782. if (blkbk->pending_pages[i])
  783. __free_page(blkbk->pending_pages[i]);
  784. }
  785. kfree(blkbk->pending_pages);
  786. }
  787. kfree(blkbk);
  788. blkbk = NULL;
  789. return rc;
  790. }
  791. module_init(xen_blkif_init);
  792. MODULE_LICENSE("Dual BSD/GPL");
  793. MODULE_ALIAS("xen-backend:vbd");