blkback.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 <linux/bitmap.h>
  42. #include <xen/events.h>
  43. #include <xen/page.h>
  44. #include <xen/xen.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 'xen_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 xen_blkif_reqs = 64;
  59. module_param_named(reqs, xen_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;
  63. module_param(log_stats, int, 0644);
  64. /*
  65. * Each outstanding request that we've passed to the lower device layers has a
  66. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  67. * the pendcnt towards zero. When it hits zero, the specified domain has a
  68. * response queued for it, with the saved 'id' passed back.
  69. */
  70. struct pending_req {
  71. struct xen_blkif *blkif;
  72. u64 id;
  73. int nr_pages;
  74. atomic_t pendcnt;
  75. unsigned short operation;
  76. int status;
  77. struct list_head free_list;
  78. DECLARE_BITMAP(unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  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. * Maximum number of grant pages that can be mapped in blkback.
  96. * BLKIF_MAX_SEGMENTS_PER_REQUEST * RING_SIZE is the maximum number of
  97. * pages that blkback will persistently map.
  98. * Currently, this is:
  99. * RING_SIZE = 32 (for all known ring types)
  100. * BLKIF_MAX_SEGMENTS_PER_REQUEST = 11
  101. * sizeof(struct persistent_gnt) = 48
  102. * So the maximum memory used to store the grants is:
  103. * 32 * 11 * 48 = 16896 bytes
  104. */
  105. static inline unsigned int max_mapped_grant_pages(enum blkif_protocol protocol)
  106. {
  107. switch (protocol) {
  108. case BLKIF_PROTOCOL_NATIVE:
  109. return __CONST_RING_SIZE(blkif, PAGE_SIZE) *
  110. BLKIF_MAX_SEGMENTS_PER_REQUEST;
  111. case BLKIF_PROTOCOL_X86_32:
  112. return __CONST_RING_SIZE(blkif_x86_32, PAGE_SIZE) *
  113. BLKIF_MAX_SEGMENTS_PER_REQUEST;
  114. case BLKIF_PROTOCOL_X86_64:
  115. return __CONST_RING_SIZE(blkif_x86_64, PAGE_SIZE) *
  116. BLKIF_MAX_SEGMENTS_PER_REQUEST;
  117. default:
  118. BUG();
  119. }
  120. return 0;
  121. }
  122. /*
  123. * Little helpful macro to figure out the index and virtual address of the
  124. * pending_pages[..]. For each 'pending_req' we have have up to
  125. * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
  126. * 10 and would index in the pending_pages[..].
  127. */
  128. static inline int vaddr_pagenr(struct pending_req *req, int seg)
  129. {
  130. return (req - blkbk->pending_reqs) *
  131. BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
  132. }
  133. #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
  134. static inline unsigned long vaddr(struct pending_req *req, int seg)
  135. {
  136. unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
  137. return (unsigned long)pfn_to_kaddr(pfn);
  138. }
  139. #define pending_handle(_req, _seg) \
  140. (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
  141. static int do_block_io_op(struct xen_blkif *blkif);
  142. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  143. struct blkif_request *req,
  144. struct pending_req *pending_req);
  145. static void make_response(struct xen_blkif *blkif, u64 id,
  146. unsigned short op, int st);
  147. #define foreach_grant(pos, rbtree, node) \
  148. for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node); \
  149. &(pos)->node != NULL; \
  150. (pos) = container_of(rb_next(&(pos)->node), typeof(*(pos)), node))
  151. static void add_persistent_gnt(struct rb_root *root,
  152. struct persistent_gnt *persistent_gnt)
  153. {
  154. struct rb_node **new = &(root->rb_node), *parent = NULL;
  155. struct persistent_gnt *this;
  156. /* Figure out where to put new node */
  157. while (*new) {
  158. this = container_of(*new, struct persistent_gnt, node);
  159. parent = *new;
  160. if (persistent_gnt->gnt < this->gnt)
  161. new = &((*new)->rb_left);
  162. else if (persistent_gnt->gnt > this->gnt)
  163. new = &((*new)->rb_right);
  164. else {
  165. pr_alert(DRV_PFX " trying to add a gref that's already in the tree\n");
  166. BUG();
  167. }
  168. }
  169. /* Add new node and rebalance tree. */
  170. rb_link_node(&(persistent_gnt->node), parent, new);
  171. rb_insert_color(&(persistent_gnt->node), root);
  172. }
  173. static struct persistent_gnt *get_persistent_gnt(struct rb_root *root,
  174. grant_ref_t gref)
  175. {
  176. struct persistent_gnt *data;
  177. struct rb_node *node = root->rb_node;
  178. while (node) {
  179. data = container_of(node, struct persistent_gnt, node);
  180. if (gref < data->gnt)
  181. node = node->rb_left;
  182. else if (gref > data->gnt)
  183. node = node->rb_right;
  184. else
  185. return data;
  186. }
  187. return NULL;
  188. }
  189. /*
  190. * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  191. */
  192. static struct pending_req *alloc_req(void)
  193. {
  194. struct pending_req *req = NULL;
  195. unsigned long flags;
  196. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  197. if (!list_empty(&blkbk->pending_free)) {
  198. req = list_entry(blkbk->pending_free.next, struct pending_req,
  199. free_list);
  200. list_del(&req->free_list);
  201. }
  202. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  203. return req;
  204. }
  205. /*
  206. * Return the 'pending_req' structure back to the freepool. We also
  207. * wake up the thread if it was waiting for a free page.
  208. */
  209. static void free_req(struct pending_req *req)
  210. {
  211. unsigned long flags;
  212. int was_empty;
  213. spin_lock_irqsave(&blkbk->pending_free_lock, flags);
  214. was_empty = list_empty(&blkbk->pending_free);
  215. list_add(&req->free_list, &blkbk->pending_free);
  216. spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
  217. if (was_empty)
  218. wake_up(&blkbk->pending_free_wq);
  219. }
  220. /*
  221. * Routines for managing virtual block devices (vbds).
  222. */
  223. static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
  224. int operation)
  225. {
  226. struct xen_vbd *vbd = &blkif->vbd;
  227. int rc = -EACCES;
  228. if ((operation != READ) && vbd->readonly)
  229. goto out;
  230. if (likely(req->nr_sects)) {
  231. blkif_sector_t end = req->sector_number + req->nr_sects;
  232. if (unlikely(end < req->sector_number))
  233. goto out;
  234. if (unlikely(end > vbd_sz(vbd)))
  235. goto out;
  236. }
  237. req->dev = vbd->pdevice;
  238. req->bdev = vbd->bdev;
  239. rc = 0;
  240. out:
  241. return rc;
  242. }
  243. static void xen_vbd_resize(struct xen_blkif *blkif)
  244. {
  245. struct xen_vbd *vbd = &blkif->vbd;
  246. struct xenbus_transaction xbt;
  247. int err;
  248. struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
  249. unsigned long long new_size = vbd_sz(vbd);
  250. pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  251. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  252. pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
  253. vbd->size = new_size;
  254. again:
  255. err = xenbus_transaction_start(&xbt);
  256. if (err) {
  257. pr_warn(DRV_PFX "Error starting transaction");
  258. return;
  259. }
  260. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  261. (unsigned long long)vbd_sz(vbd));
  262. if (err) {
  263. pr_warn(DRV_PFX "Error writing new size");
  264. goto abort;
  265. }
  266. /*
  267. * Write the current state; we will use this to synchronize
  268. * the front-end. If the current state is "connected" the
  269. * front-end will get the new size information online.
  270. */
  271. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  272. if (err) {
  273. pr_warn(DRV_PFX "Error writing the state");
  274. goto abort;
  275. }
  276. err = xenbus_transaction_end(xbt, 0);
  277. if (err == -EAGAIN)
  278. goto again;
  279. if (err)
  280. pr_warn(DRV_PFX "Error ending transaction");
  281. return;
  282. abort:
  283. xenbus_transaction_end(xbt, 1);
  284. }
  285. /*
  286. * Notification from the guest OS.
  287. */
  288. static void blkif_notify_work(struct xen_blkif *blkif)
  289. {
  290. blkif->waiting_reqs = 1;
  291. wake_up(&blkif->wq);
  292. }
  293. irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
  294. {
  295. blkif_notify_work(dev_id);
  296. return IRQ_HANDLED;
  297. }
  298. /*
  299. * SCHEDULER FUNCTIONS
  300. */
  301. static void print_stats(struct xen_blkif *blkif)
  302. {
  303. pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
  304. " | ds %4d\n",
  305. current->comm, blkif->st_oo_req,
  306. blkif->st_rd_req, blkif->st_wr_req,
  307. blkif->st_f_req, blkif->st_ds_req);
  308. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  309. blkif->st_rd_req = 0;
  310. blkif->st_wr_req = 0;
  311. blkif->st_oo_req = 0;
  312. blkif->st_ds_req = 0;
  313. }
  314. int xen_blkif_schedule(void *arg)
  315. {
  316. struct xen_blkif *blkif = arg;
  317. struct xen_vbd *vbd = &blkif->vbd;
  318. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  319. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  320. struct persistent_gnt *persistent_gnt;
  321. int ret = 0;
  322. int segs_to_unmap = 0;
  323. xen_blkif_get(blkif);
  324. while (!kthread_should_stop()) {
  325. if (try_to_freeze())
  326. continue;
  327. if (unlikely(vbd->size != vbd_sz(vbd)))
  328. xen_vbd_resize(blkif);
  329. wait_event_interruptible(
  330. blkif->wq,
  331. blkif->waiting_reqs || kthread_should_stop());
  332. wait_event_interruptible(
  333. blkbk->pending_free_wq,
  334. !list_empty(&blkbk->pending_free) ||
  335. kthread_should_stop());
  336. blkif->waiting_reqs = 0;
  337. smp_mb(); /* clear flag *before* checking for work */
  338. if (do_block_io_op(blkif))
  339. blkif->waiting_reqs = 1;
  340. if (log_stats && time_after(jiffies, blkif->st_print))
  341. print_stats(blkif);
  342. }
  343. /* Free all persistent grant pages */
  344. if (!RB_EMPTY_ROOT(&blkif->persistent_gnts)) {
  345. foreach_grant(persistent_gnt, &blkif->persistent_gnts, node) {
  346. BUG_ON(persistent_gnt->handle ==
  347. BLKBACK_INVALID_HANDLE);
  348. gnttab_set_unmap_op(&unmap[segs_to_unmap],
  349. (unsigned long) pfn_to_kaddr(page_to_pfn(
  350. persistent_gnt->page)),
  351. GNTMAP_host_map,
  352. persistent_gnt->handle);
  353. pages[segs_to_unmap] = persistent_gnt->page;
  354. rb_erase(&persistent_gnt->node,
  355. &blkif->persistent_gnts);
  356. kfree(persistent_gnt);
  357. blkif->persistent_gnt_c--;
  358. if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
  359. !rb_next(&persistent_gnt->node)) {
  360. ret = gnttab_unmap_refs(unmap, NULL, pages,
  361. segs_to_unmap);
  362. BUG_ON(ret);
  363. segs_to_unmap = 0;
  364. }
  365. }
  366. }
  367. BUG_ON(blkif->persistent_gnt_c != 0);
  368. BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
  369. if (log_stats)
  370. print_stats(blkif);
  371. blkif->xenblkd = NULL;
  372. xen_blkif_put(blkif);
  373. return 0;
  374. }
  375. struct seg_buf {
  376. unsigned long buf;
  377. unsigned int nsec;
  378. };
  379. /*
  380. * Unmap the grant references, and also remove the M2P over-rides
  381. * used in the 'pending_req'.
  382. */
  383. static void xen_blkbk_unmap(struct pending_req *req)
  384. {
  385. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  386. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  387. unsigned int i, invcount = 0;
  388. grant_handle_t handle;
  389. int ret;
  390. for (i = 0; i < req->nr_pages; i++) {
  391. if (!test_bit(i, req->unmap_seg))
  392. continue;
  393. handle = pending_handle(req, i);
  394. if (handle == BLKBACK_INVALID_HANDLE)
  395. continue;
  396. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  397. GNTMAP_host_map, handle);
  398. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  399. pages[invcount] = virt_to_page(vaddr(req, i));
  400. invcount++;
  401. }
  402. ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  403. BUG_ON(ret);
  404. }
  405. static int xen_blkbk_map(struct blkif_request *req,
  406. struct pending_req *pending_req,
  407. struct seg_buf seg[],
  408. struct page *pages[])
  409. {
  410. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  411. struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  412. struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  413. struct persistent_gnt *persistent_gnt = NULL;
  414. struct xen_blkif *blkif = pending_req->blkif;
  415. phys_addr_t addr = 0;
  416. int i, j;
  417. bool new_map;
  418. int nseg = req->u.rw.nr_segments;
  419. int segs_to_map = 0;
  420. int ret = 0;
  421. int use_persistent_gnts;
  422. use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
  423. BUG_ON(blkif->persistent_gnt_c >
  424. max_mapped_grant_pages(pending_req->blkif->blk_protocol));
  425. /*
  426. * Fill out preq.nr_sects with proper amount of sectors, and setup
  427. * assign map[..] with the PFN of the page in our domain with the
  428. * corresponding grant reference for each page.
  429. */
  430. for (i = 0; i < nseg; i++) {
  431. uint32_t flags;
  432. if (use_persistent_gnts)
  433. persistent_gnt = get_persistent_gnt(
  434. &blkif->persistent_gnts,
  435. req->u.rw.seg[i].gref);
  436. if (persistent_gnt) {
  437. /*
  438. * We are using persistent grants and
  439. * the grant is already mapped
  440. */
  441. new_map = false;
  442. } else if (use_persistent_gnts &&
  443. blkif->persistent_gnt_c <
  444. max_mapped_grant_pages(blkif->blk_protocol)) {
  445. /*
  446. * We are using persistent grants, the grant is
  447. * not mapped but we have room for it
  448. */
  449. new_map = true;
  450. persistent_gnt = kmalloc(
  451. sizeof(struct persistent_gnt),
  452. GFP_KERNEL);
  453. if (!persistent_gnt)
  454. return -ENOMEM;
  455. persistent_gnt->page = alloc_page(GFP_KERNEL);
  456. if (!persistent_gnt->page) {
  457. kfree(persistent_gnt);
  458. return -ENOMEM;
  459. }
  460. persistent_gnt->gnt = req->u.rw.seg[i].gref;
  461. persistent_gnt->handle = BLKBACK_INVALID_HANDLE;
  462. pages_to_gnt[segs_to_map] =
  463. persistent_gnt->page;
  464. addr = (unsigned long) pfn_to_kaddr(
  465. page_to_pfn(persistent_gnt->page));
  466. add_persistent_gnt(&blkif->persistent_gnts,
  467. persistent_gnt);
  468. blkif->persistent_gnt_c++;
  469. pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
  470. persistent_gnt->gnt, blkif->persistent_gnt_c,
  471. max_mapped_grant_pages(blkif->blk_protocol));
  472. } else {
  473. /*
  474. * We are either using persistent grants and
  475. * hit the maximum limit of grants mapped,
  476. * or we are not using persistent grants.
  477. */
  478. if (use_persistent_gnts &&
  479. !blkif->vbd.overflow_max_grants) {
  480. blkif->vbd.overflow_max_grants = 1;
  481. pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
  482. blkif->domid, blkif->vbd.handle);
  483. }
  484. new_map = true;
  485. pages[i] = blkbk->pending_page(pending_req, i);
  486. addr = vaddr(pending_req, i);
  487. pages_to_gnt[segs_to_map] =
  488. blkbk->pending_page(pending_req, i);
  489. }
  490. if (persistent_gnt) {
  491. pages[i] = persistent_gnt->page;
  492. persistent_gnts[i] = persistent_gnt;
  493. } else {
  494. persistent_gnts[i] = NULL;
  495. }
  496. if (new_map) {
  497. flags = GNTMAP_host_map;
  498. if (!persistent_gnt &&
  499. (pending_req->operation != BLKIF_OP_READ))
  500. flags |= GNTMAP_readonly;
  501. gnttab_set_map_op(&map[segs_to_map++], addr,
  502. flags, req->u.rw.seg[i].gref,
  503. blkif->domid);
  504. }
  505. }
  506. if (segs_to_map) {
  507. ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
  508. BUG_ON(ret);
  509. }
  510. /*
  511. * Now swizzle the MFN in our domain with the MFN from the other domain
  512. * so that when we access vaddr(pending_req,i) it has the contents of
  513. * the page from the other domain.
  514. */
  515. bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  516. for (i = 0, j = 0; i < nseg; i++) {
  517. if (!persistent_gnts[i] ||
  518. persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) {
  519. /* This is a newly mapped grant */
  520. BUG_ON(j >= segs_to_map);
  521. if (unlikely(map[j].status != 0)) {
  522. pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
  523. map[j].handle = BLKBACK_INVALID_HANDLE;
  524. ret |= 1;
  525. if (persistent_gnts[i]) {
  526. rb_erase(&persistent_gnts[i]->node,
  527. &blkif->persistent_gnts);
  528. blkif->persistent_gnt_c--;
  529. kfree(persistent_gnts[i]);
  530. persistent_gnts[i] = NULL;
  531. }
  532. }
  533. }
  534. if (persistent_gnts[i]) {
  535. if (persistent_gnts[i]->handle ==
  536. BLKBACK_INVALID_HANDLE) {
  537. /*
  538. * If this is a new persistent grant
  539. * save the handler
  540. */
  541. persistent_gnts[i]->handle = map[j].handle;
  542. persistent_gnts[i]->dev_bus_addr =
  543. map[j++].dev_bus_addr;
  544. }
  545. pending_handle(pending_req, i) =
  546. persistent_gnts[i]->handle;
  547. if (ret)
  548. continue;
  549. seg[i].buf = persistent_gnts[i]->dev_bus_addr |
  550. (req->u.rw.seg[i].first_sect << 9);
  551. } else {
  552. pending_handle(pending_req, i) = map[j].handle;
  553. bitmap_set(pending_req->unmap_seg, i, 1);
  554. if (ret) {
  555. j++;
  556. continue;
  557. }
  558. seg[i].buf = map[j++].dev_bus_addr |
  559. (req->u.rw.seg[i].first_sect << 9);
  560. }
  561. }
  562. return ret;
  563. }
  564. static int dispatch_discard_io(struct xen_blkif *blkif,
  565. struct blkif_request *req)
  566. {
  567. int err = 0;
  568. int status = BLKIF_RSP_OKAY;
  569. struct block_device *bdev = blkif->vbd.bdev;
  570. unsigned long secure;
  571. blkif->st_ds_req++;
  572. xen_blkif_get(blkif);
  573. secure = (blkif->vbd.discard_secure &&
  574. (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
  575. BLKDEV_DISCARD_SECURE : 0;
  576. err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
  577. req->u.discard.nr_sectors,
  578. GFP_KERNEL, secure);
  579. if (err == -EOPNOTSUPP) {
  580. pr_debug(DRV_PFX "discard op failed, not supported\n");
  581. status = BLKIF_RSP_EOPNOTSUPP;
  582. } else if (err)
  583. status = BLKIF_RSP_ERROR;
  584. make_response(blkif, req->u.discard.id, req->operation, status);
  585. xen_blkif_put(blkif);
  586. return err;
  587. }
  588. static void xen_blk_drain_io(struct xen_blkif *blkif)
  589. {
  590. atomic_set(&blkif->drain, 1);
  591. do {
  592. /* The initial value is one, and one refcnt taken at the
  593. * start of the xen_blkif_schedule thread. */
  594. if (atomic_read(&blkif->refcnt) <= 2)
  595. break;
  596. wait_for_completion_interruptible_timeout(
  597. &blkif->drain_complete, HZ);
  598. if (!atomic_read(&blkif->drain))
  599. break;
  600. } while (!kthread_should_stop());
  601. atomic_set(&blkif->drain, 0);
  602. }
  603. /*
  604. * Completion callback on the bio's. Called as bh->b_end_io()
  605. */
  606. static void __end_block_io_op(struct pending_req *pending_req, int error)
  607. {
  608. /* An error fails the entire request. */
  609. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  610. (error == -EOPNOTSUPP)) {
  611. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  612. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  613. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  614. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  615. (error == -EOPNOTSUPP)) {
  616. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  617. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  618. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  619. } else if (error) {
  620. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  621. " error=%d\n", error);
  622. pending_req->status = BLKIF_RSP_ERROR;
  623. }
  624. /*
  625. * If all of the bio's have completed it is time to unmap
  626. * the grant references associated with 'request' and provide
  627. * the proper response on the ring.
  628. */
  629. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  630. xen_blkbk_unmap(pending_req);
  631. make_response(pending_req->blkif, pending_req->id,
  632. pending_req->operation, pending_req->status);
  633. xen_blkif_put(pending_req->blkif);
  634. if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
  635. if (atomic_read(&pending_req->blkif->drain))
  636. complete(&pending_req->blkif->drain_complete);
  637. }
  638. free_req(pending_req);
  639. }
  640. }
  641. /*
  642. * bio callback.
  643. */
  644. static void end_block_io_op(struct bio *bio, int error)
  645. {
  646. __end_block_io_op(bio->bi_private, error);
  647. bio_put(bio);
  648. }
  649. /*
  650. * Function to copy the from the ring buffer the 'struct blkif_request'
  651. * (which has the sectors we want, number of them, grant references, etc),
  652. * and transmute it to the block API to hand it over to the proper block disk.
  653. */
  654. static int
  655. __do_block_io_op(struct xen_blkif *blkif)
  656. {
  657. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  658. struct blkif_request req;
  659. struct pending_req *pending_req;
  660. RING_IDX rc, rp;
  661. int more_to_do = 0;
  662. rc = blk_rings->common.req_cons;
  663. rp = blk_rings->common.sring->req_prod;
  664. rmb(); /* Ensure we see queued requests up to 'rp'. */
  665. while (rc != rp) {
  666. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  667. break;
  668. if (kthread_should_stop()) {
  669. more_to_do = 1;
  670. break;
  671. }
  672. pending_req = alloc_req();
  673. if (NULL == pending_req) {
  674. blkif->st_oo_req++;
  675. more_to_do = 1;
  676. break;
  677. }
  678. switch (blkif->blk_protocol) {
  679. case BLKIF_PROTOCOL_NATIVE:
  680. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  681. break;
  682. case BLKIF_PROTOCOL_X86_32:
  683. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  684. break;
  685. case BLKIF_PROTOCOL_X86_64:
  686. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  687. break;
  688. default:
  689. BUG();
  690. }
  691. blk_rings->common.req_cons = ++rc; /* before make_response() */
  692. /* Apply all sanity checks to /private copy/ of request. */
  693. barrier();
  694. if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
  695. free_req(pending_req);
  696. if (dispatch_discard_io(blkif, &req))
  697. break;
  698. } else if (dispatch_rw_block_io(blkif, &req, pending_req))
  699. break;
  700. /* Yield point for this unbounded loop. */
  701. cond_resched();
  702. }
  703. return more_to_do;
  704. }
  705. static int
  706. do_block_io_op(struct xen_blkif *blkif)
  707. {
  708. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  709. int more_to_do;
  710. do {
  711. more_to_do = __do_block_io_op(blkif);
  712. if (more_to_do)
  713. break;
  714. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  715. } while (more_to_do);
  716. return more_to_do;
  717. }
  718. /*
  719. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  720. * and call the 'submit_bio' to pass it to the underlying storage.
  721. */
  722. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  723. struct blkif_request *req,
  724. struct pending_req *pending_req)
  725. {
  726. struct phys_req preq;
  727. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  728. unsigned int nseg;
  729. struct bio *bio = NULL;
  730. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  731. int i, nbio = 0;
  732. int operation;
  733. struct blk_plug plug;
  734. bool drain = false;
  735. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  736. switch (req->operation) {
  737. case BLKIF_OP_READ:
  738. blkif->st_rd_req++;
  739. operation = READ;
  740. break;
  741. case BLKIF_OP_WRITE:
  742. blkif->st_wr_req++;
  743. operation = WRITE_ODIRECT;
  744. break;
  745. case BLKIF_OP_WRITE_BARRIER:
  746. drain = true;
  747. case BLKIF_OP_FLUSH_DISKCACHE:
  748. blkif->st_f_req++;
  749. operation = WRITE_FLUSH;
  750. break;
  751. default:
  752. operation = 0; /* make gcc happy */
  753. goto fail_response;
  754. break;
  755. }
  756. /* Check that the number of segments is sane. */
  757. nseg = req->u.rw.nr_segments;
  758. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  759. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  760. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  761. nseg);
  762. /* Haven't submitted any bio's yet. */
  763. goto fail_response;
  764. }
  765. preq.dev = req->u.rw.handle;
  766. preq.sector_number = req->u.rw.sector_number;
  767. preq.nr_sects = 0;
  768. pending_req->blkif = blkif;
  769. pending_req->id = req->u.rw.id;
  770. pending_req->operation = req->operation;
  771. pending_req->status = BLKIF_RSP_OKAY;
  772. pending_req->nr_pages = nseg;
  773. for (i = 0; i < nseg; i++) {
  774. seg[i].nsec = req->u.rw.seg[i].last_sect -
  775. req->u.rw.seg[i].first_sect + 1;
  776. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  777. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  778. goto fail_response;
  779. preq.nr_sects += seg[i].nsec;
  780. }
  781. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  782. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  783. operation == READ ? "read" : "write",
  784. preq.sector_number,
  785. preq.sector_number + preq.nr_sects, preq.dev);
  786. goto fail_response;
  787. }
  788. /*
  789. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  790. * is set there.
  791. */
  792. for (i = 0; i < nseg; i++) {
  793. if (((int)preq.sector_number|(int)seg[i].nsec) &
  794. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  795. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  796. blkif->domid);
  797. goto fail_response;
  798. }
  799. }
  800. /* Wait on all outstanding I/O's and once that has been completed
  801. * issue the WRITE_FLUSH.
  802. */
  803. if (drain)
  804. xen_blk_drain_io(pending_req->blkif);
  805. /*
  806. * If we have failed at this point, we need to undo the M2P override,
  807. * set gnttab_set_unmap_op on all of the grant references and perform
  808. * the hypercall to unmap the grants - that is all done in
  809. * xen_blkbk_unmap.
  810. */
  811. if (xen_blkbk_map(req, pending_req, seg, pages))
  812. goto fail_flush;
  813. /*
  814. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  815. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  816. */
  817. xen_blkif_get(blkif);
  818. for (i = 0; i < nseg; i++) {
  819. while ((bio == NULL) ||
  820. (bio_add_page(bio,
  821. pages[i],
  822. seg[i].nsec << 9,
  823. seg[i].buf & ~PAGE_MASK) == 0)) {
  824. bio = bio_alloc(GFP_KERNEL, nseg-i);
  825. if (unlikely(bio == NULL))
  826. goto fail_put_bio;
  827. biolist[nbio++] = bio;
  828. bio->bi_bdev = preq.bdev;
  829. bio->bi_private = pending_req;
  830. bio->bi_end_io = end_block_io_op;
  831. bio->bi_sector = preq.sector_number;
  832. }
  833. preq.sector_number += seg[i].nsec;
  834. }
  835. /* This will be hit if the operation was a flush or discard. */
  836. if (!bio) {
  837. BUG_ON(operation != WRITE_FLUSH);
  838. bio = bio_alloc(GFP_KERNEL, 0);
  839. if (unlikely(bio == NULL))
  840. goto fail_put_bio;
  841. biolist[nbio++] = bio;
  842. bio->bi_bdev = preq.bdev;
  843. bio->bi_private = pending_req;
  844. bio->bi_end_io = end_block_io_op;
  845. }
  846. /*
  847. * We set it one so that the last submit_bio does not have to call
  848. * atomic_inc.
  849. */
  850. atomic_set(&pending_req->pendcnt, nbio);
  851. /* Get a reference count for the disk queue and start sending I/O */
  852. blk_start_plug(&plug);
  853. for (i = 0; i < nbio; i++)
  854. submit_bio(operation, biolist[i]);
  855. /* Let the I/Os go.. */
  856. blk_finish_plug(&plug);
  857. if (operation == READ)
  858. blkif->st_rd_sect += preq.nr_sects;
  859. else if (operation & WRITE)
  860. blkif->st_wr_sect += preq.nr_sects;
  861. return 0;
  862. fail_flush:
  863. xen_blkbk_unmap(pending_req);
  864. fail_response:
  865. /* Haven't submitted any bio's yet. */
  866. make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
  867. free_req(pending_req);
  868. msleep(1); /* back off a bit */
  869. return -EIO;
  870. fail_put_bio:
  871. for (i = 0; i < nbio; i++)
  872. bio_put(biolist[i]);
  873. __end_block_io_op(pending_req, -EINVAL);
  874. msleep(1); /* back off a bit */
  875. return -EIO;
  876. }
  877. /*
  878. * Put a response on the ring on how the operation fared.
  879. */
  880. static void make_response(struct xen_blkif *blkif, u64 id,
  881. unsigned short op, int st)
  882. {
  883. struct blkif_response resp;
  884. unsigned long flags;
  885. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  886. int notify;
  887. resp.id = id;
  888. resp.operation = op;
  889. resp.status = st;
  890. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  891. /* Place on the response ring for the relevant domain. */
  892. switch (blkif->blk_protocol) {
  893. case BLKIF_PROTOCOL_NATIVE:
  894. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  895. &resp, sizeof(resp));
  896. break;
  897. case BLKIF_PROTOCOL_X86_32:
  898. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  899. &resp, sizeof(resp));
  900. break;
  901. case BLKIF_PROTOCOL_X86_64:
  902. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  903. &resp, sizeof(resp));
  904. break;
  905. default:
  906. BUG();
  907. }
  908. blk_rings->common.rsp_prod_pvt++;
  909. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  910. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  911. if (notify)
  912. notify_remote_via_irq(blkif->irq);
  913. }
  914. static int __init xen_blkif_init(void)
  915. {
  916. int i, mmap_pages;
  917. int rc = 0;
  918. if (!xen_domain())
  919. return -ENODEV;
  920. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  921. if (!blkbk) {
  922. pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
  923. return -ENOMEM;
  924. }
  925. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  926. blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
  927. xen_blkif_reqs, GFP_KERNEL);
  928. blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
  929. mmap_pages, GFP_KERNEL);
  930. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  931. mmap_pages, GFP_KERNEL);
  932. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  933. !blkbk->pending_pages) {
  934. rc = -ENOMEM;
  935. goto out_of_memory;
  936. }
  937. for (i = 0; i < mmap_pages; i++) {
  938. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  939. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  940. if (blkbk->pending_pages[i] == NULL) {
  941. rc = -ENOMEM;
  942. goto out_of_memory;
  943. }
  944. }
  945. rc = xen_blkif_interface_init();
  946. if (rc)
  947. goto failed_init;
  948. INIT_LIST_HEAD(&blkbk->pending_free);
  949. spin_lock_init(&blkbk->pending_free_lock);
  950. init_waitqueue_head(&blkbk->pending_free_wq);
  951. for (i = 0; i < xen_blkif_reqs; i++)
  952. list_add_tail(&blkbk->pending_reqs[i].free_list,
  953. &blkbk->pending_free);
  954. rc = xen_blkif_xenbus_init();
  955. if (rc)
  956. goto failed_init;
  957. return 0;
  958. out_of_memory:
  959. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  960. failed_init:
  961. kfree(blkbk->pending_reqs);
  962. kfree(blkbk->pending_grant_handles);
  963. if (blkbk->pending_pages) {
  964. for (i = 0; i < mmap_pages; i++) {
  965. if (blkbk->pending_pages[i])
  966. __free_page(blkbk->pending_pages[i]);
  967. }
  968. kfree(blkbk->pending_pages);
  969. }
  970. kfree(blkbk);
  971. blkbk = NULL;
  972. return rc;
  973. }
  974. module_init(xen_blkif_init);
  975. MODULE_LICENSE("Dual BSD/GPL");
  976. MODULE_ALIAS("xen-backend:vbd");