blkback.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 %3d | rd %4d | wr %4d | f %4d"
  338. " | ds %4d\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 void xen_blk_drain_io(struct xen_blkif *blkif)
  598. {
  599. atomic_set(&blkif->drain, 1);
  600. do {
  601. /* The initial value is one, and one refcnt taken at the
  602. * start of the xen_blkif_schedule thread. */
  603. if (atomic_read(&blkif->refcnt) <= 2)
  604. break;
  605. wait_for_completion_interruptible_timeout(
  606. &blkif->drain_complete, HZ);
  607. if (!atomic_read(&blkif->drain))
  608. break;
  609. } while (!kthread_should_stop());
  610. atomic_set(&blkif->drain, 0);
  611. }
  612. /*
  613. * Completion callback on the bio's. Called as bh->b_end_io()
  614. */
  615. static void __end_block_io_op(struct pending_req *pending_req, int error)
  616. {
  617. /* An error fails the entire request. */
  618. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  619. (error == -EOPNOTSUPP)) {
  620. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  621. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  622. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  623. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  624. (error == -EOPNOTSUPP)) {
  625. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  626. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  627. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  628. } else if (error) {
  629. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  630. " error=%d\n", error);
  631. pending_req->status = BLKIF_RSP_ERROR;
  632. }
  633. /*
  634. * If all of the bio's have completed it is time to unmap
  635. * the grant references associated with 'request' and provide
  636. * the proper response on the ring.
  637. */
  638. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  639. xen_blkbk_unmap(pending_req);
  640. make_response(pending_req->blkif, pending_req->id,
  641. pending_req->operation, pending_req->status);
  642. xen_blkif_put(pending_req->blkif);
  643. if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
  644. if (atomic_read(&pending_req->blkif->drain))
  645. complete(&pending_req->blkif->drain_complete);
  646. }
  647. free_req(pending_req);
  648. }
  649. }
  650. /*
  651. * bio callback.
  652. */
  653. static void end_block_io_op(struct bio *bio, int error)
  654. {
  655. __end_block_io_op(bio->bi_private, error);
  656. bio_put(bio);
  657. }
  658. /*
  659. * Function to copy the from the ring buffer the 'struct blkif_request'
  660. * (which has the sectors we want, number of them, grant references, etc),
  661. * and transmute it to the block API to hand it over to the proper block disk.
  662. */
  663. static int
  664. __do_block_io_op(struct xen_blkif *blkif)
  665. {
  666. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  667. struct blkif_request req;
  668. struct pending_req *pending_req;
  669. RING_IDX rc, rp;
  670. int more_to_do = 0;
  671. rc = blk_rings->common.req_cons;
  672. rp = blk_rings->common.sring->req_prod;
  673. rmb(); /* Ensure we see queued requests up to 'rp'. */
  674. while (rc != rp) {
  675. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  676. break;
  677. if (kthread_should_stop()) {
  678. more_to_do = 1;
  679. break;
  680. }
  681. pending_req = alloc_req();
  682. if (NULL == pending_req) {
  683. blkif->st_oo_req++;
  684. more_to_do = 1;
  685. break;
  686. }
  687. switch (blkif->blk_protocol) {
  688. case BLKIF_PROTOCOL_NATIVE:
  689. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  690. break;
  691. case BLKIF_PROTOCOL_X86_32:
  692. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  693. break;
  694. case BLKIF_PROTOCOL_X86_64:
  695. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  696. break;
  697. default:
  698. BUG();
  699. }
  700. blk_rings->common.req_cons = ++rc; /* before make_response() */
  701. /* Apply all sanity checks to /private copy/ of request. */
  702. barrier();
  703. if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
  704. free_req(pending_req);
  705. if (dispatch_discard_io(blkif, &req))
  706. break;
  707. } else if (dispatch_rw_block_io(blkif, &req, pending_req))
  708. break;
  709. /* Yield point for this unbounded loop. */
  710. cond_resched();
  711. }
  712. return more_to_do;
  713. }
  714. static int
  715. do_block_io_op(struct xen_blkif *blkif)
  716. {
  717. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  718. int more_to_do;
  719. do {
  720. more_to_do = __do_block_io_op(blkif);
  721. if (more_to_do)
  722. break;
  723. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  724. } while (more_to_do);
  725. return more_to_do;
  726. }
  727. /*
  728. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  729. * and call the 'submit_bio' to pass it to the underlying storage.
  730. */
  731. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  732. struct blkif_request *req,
  733. struct pending_req *pending_req)
  734. {
  735. struct phys_req preq;
  736. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  737. unsigned int nseg;
  738. struct bio *bio = NULL;
  739. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  740. int i, nbio = 0;
  741. int operation;
  742. struct blk_plug plug;
  743. bool drain = false;
  744. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  745. switch (req->operation) {
  746. case BLKIF_OP_READ:
  747. blkif->st_rd_req++;
  748. operation = READ;
  749. break;
  750. case BLKIF_OP_WRITE:
  751. blkif->st_wr_req++;
  752. operation = WRITE_ODIRECT;
  753. break;
  754. case BLKIF_OP_WRITE_BARRIER:
  755. drain = true;
  756. case BLKIF_OP_FLUSH_DISKCACHE:
  757. blkif->st_f_req++;
  758. operation = WRITE_FLUSH;
  759. break;
  760. default:
  761. operation = 0; /* make gcc happy */
  762. goto fail_response;
  763. break;
  764. }
  765. /* Check that the number of segments is sane. */
  766. nseg = req->u.rw.nr_segments;
  767. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  768. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  769. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  770. nseg);
  771. /* Haven't submitted any bio's yet. */
  772. goto fail_response;
  773. }
  774. preq.sector_number = req->u.rw.sector_number;
  775. preq.nr_sects = 0;
  776. pending_req->blkif = blkif;
  777. pending_req->id = req->u.rw.id;
  778. pending_req->operation = req->operation;
  779. pending_req->status = BLKIF_RSP_OKAY;
  780. pending_req->nr_pages = nseg;
  781. for (i = 0; i < nseg; i++) {
  782. seg[i].nsec = req->u.rw.seg[i].last_sect -
  783. req->u.rw.seg[i].first_sect + 1;
  784. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  785. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  786. goto fail_response;
  787. preq.nr_sects += seg[i].nsec;
  788. }
  789. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  790. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  791. operation == READ ? "read" : "write",
  792. preq.sector_number,
  793. preq.sector_number + preq.nr_sects, preq.dev);
  794. goto fail_response;
  795. }
  796. /*
  797. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  798. * is set there.
  799. */
  800. for (i = 0; i < nseg; i++) {
  801. if (((int)preq.sector_number|(int)seg[i].nsec) &
  802. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  803. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  804. blkif->domid);
  805. goto fail_response;
  806. }
  807. }
  808. /* Wait on all outstanding I/O's and once that has been completed
  809. * issue the WRITE_FLUSH.
  810. */
  811. if (drain)
  812. xen_blk_drain_io(pending_req->blkif);
  813. /*
  814. * If we have failed at this point, we need to undo the M2P override,
  815. * set gnttab_set_unmap_op on all of the grant references and perform
  816. * the hypercall to unmap the grants - that is all done in
  817. * xen_blkbk_unmap.
  818. */
  819. if (xen_blkbk_map(req, pending_req, seg, pages))
  820. goto fail_flush;
  821. /*
  822. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  823. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  824. */
  825. xen_blkif_get(blkif);
  826. for (i = 0; i < nseg; i++) {
  827. while ((bio == NULL) ||
  828. (bio_add_page(bio,
  829. pages[i],
  830. seg[i].nsec << 9,
  831. seg[i].buf & ~PAGE_MASK) == 0)) {
  832. bio = bio_alloc(GFP_KERNEL, nseg-i);
  833. if (unlikely(bio == NULL))
  834. goto fail_put_bio;
  835. biolist[nbio++] = bio;
  836. bio->bi_bdev = preq.bdev;
  837. bio->bi_private = pending_req;
  838. bio->bi_end_io = end_block_io_op;
  839. bio->bi_sector = preq.sector_number;
  840. }
  841. preq.sector_number += seg[i].nsec;
  842. }
  843. /* This will be hit if the operation was a flush or discard. */
  844. if (!bio) {
  845. BUG_ON(operation != WRITE_FLUSH);
  846. bio = bio_alloc(GFP_KERNEL, 0);
  847. if (unlikely(bio == NULL))
  848. goto fail_put_bio;
  849. biolist[nbio++] = bio;
  850. bio->bi_bdev = preq.bdev;
  851. bio->bi_private = pending_req;
  852. bio->bi_end_io = end_block_io_op;
  853. }
  854. /*
  855. * We set it one so that the last submit_bio does not have to call
  856. * atomic_inc.
  857. */
  858. atomic_set(&pending_req->pendcnt, nbio);
  859. /* Get a reference count for the disk queue and start sending I/O */
  860. blk_start_plug(&plug);
  861. for (i = 0; i < nbio; i++)
  862. submit_bio(operation, biolist[i]);
  863. /* Let the I/Os go.. */
  864. blk_finish_plug(&plug);
  865. if (operation == READ)
  866. blkif->st_rd_sect += preq.nr_sects;
  867. else if (operation & WRITE)
  868. blkif->st_wr_sect += preq.nr_sects;
  869. return 0;
  870. fail_flush:
  871. xen_blkbk_unmap(pending_req);
  872. fail_response:
  873. /* Haven't submitted any bio's yet. */
  874. make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
  875. free_req(pending_req);
  876. msleep(1); /* back off a bit */
  877. return -EIO;
  878. fail_put_bio:
  879. for (i = 0; i < nbio; i++)
  880. bio_put(biolist[i]);
  881. __end_block_io_op(pending_req, -EINVAL);
  882. msleep(1); /* back off a bit */
  883. return -EIO;
  884. }
  885. /*
  886. * Put a response on the ring on how the operation fared.
  887. */
  888. static void make_response(struct xen_blkif *blkif, u64 id,
  889. unsigned short op, int st)
  890. {
  891. struct blkif_response resp;
  892. unsigned long flags;
  893. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  894. int notify;
  895. resp.id = id;
  896. resp.operation = op;
  897. resp.status = st;
  898. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  899. /* Place on the response ring for the relevant domain. */
  900. switch (blkif->blk_protocol) {
  901. case BLKIF_PROTOCOL_NATIVE:
  902. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  903. &resp, sizeof(resp));
  904. break;
  905. case BLKIF_PROTOCOL_X86_32:
  906. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  907. &resp, sizeof(resp));
  908. break;
  909. case BLKIF_PROTOCOL_X86_64:
  910. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  911. &resp, sizeof(resp));
  912. break;
  913. default:
  914. BUG();
  915. }
  916. blk_rings->common.rsp_prod_pvt++;
  917. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  918. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  919. if (notify)
  920. notify_remote_via_irq(blkif->irq);
  921. }
  922. static int __init xen_blkif_init(void)
  923. {
  924. int i, mmap_pages;
  925. int rc = 0;
  926. if (!xen_domain())
  927. return -ENODEV;
  928. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  929. if (!blkbk) {
  930. pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
  931. return -ENOMEM;
  932. }
  933. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  934. blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
  935. xen_blkif_reqs, GFP_KERNEL);
  936. blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
  937. mmap_pages, GFP_KERNEL);
  938. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  939. mmap_pages, GFP_KERNEL);
  940. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  941. !blkbk->pending_pages) {
  942. rc = -ENOMEM;
  943. goto out_of_memory;
  944. }
  945. for (i = 0; i < mmap_pages; i++) {
  946. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  947. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  948. if (blkbk->pending_pages[i] == NULL) {
  949. rc = -ENOMEM;
  950. goto out_of_memory;
  951. }
  952. }
  953. rc = xen_blkif_interface_init();
  954. if (rc)
  955. goto failed_init;
  956. INIT_LIST_HEAD(&blkbk->pending_free);
  957. spin_lock_init(&blkbk->pending_free_lock);
  958. init_waitqueue_head(&blkbk->pending_free_wq);
  959. for (i = 0; i < xen_blkif_reqs; i++)
  960. list_add_tail(&blkbk->pending_reqs[i].free_list,
  961. &blkbk->pending_free);
  962. rc = xen_blkif_xenbus_init();
  963. if (rc)
  964. goto failed_init;
  965. return 0;
  966. out_of_memory:
  967. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  968. failed_init:
  969. kfree(blkbk->pending_reqs);
  970. kfree(blkbk->pending_grant_handles);
  971. if (blkbk->pending_pages) {
  972. for (i = 0; i < mmap_pages; i++) {
  973. if (blkbk->pending_pages[i])
  974. __free_page(blkbk->pending_pages[i]);
  975. }
  976. kfree(blkbk->pending_pages);
  977. }
  978. kfree(blkbk);
  979. blkbk = NULL;
  980. return rc;
  981. }
  982. module_init(xen_blkif_init);
  983. MODULE_LICENSE("Dual BSD/GPL");
  984. MODULE_ALIAS("xen-backend:vbd");