blkback.c 31 KB

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