blkback.c 30 KB

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