blkback.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
  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 | pg: %4u/%4u\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->persistent_gnt_c,
  343. max_mapped_grant_pages(blkif->blk_protocol));
  344. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  345. blkif->st_rd_req = 0;
  346. blkif->st_wr_req = 0;
  347. blkif->st_oo_req = 0;
  348. blkif->st_ds_req = 0;
  349. }
  350. int xen_blkif_schedule(void *arg)
  351. {
  352. struct xen_blkif *blkif = arg;
  353. struct xen_vbd *vbd = &blkif->vbd;
  354. xen_blkif_get(blkif);
  355. while (!kthread_should_stop()) {
  356. if (try_to_freeze())
  357. continue;
  358. if (unlikely(vbd->size != vbd_sz(vbd)))
  359. xen_vbd_resize(blkif);
  360. wait_event_interruptible(
  361. blkif->wq,
  362. blkif->waiting_reqs || kthread_should_stop());
  363. wait_event_interruptible(
  364. blkbk->pending_free_wq,
  365. !list_empty(&blkbk->pending_free) ||
  366. kthread_should_stop());
  367. blkif->waiting_reqs = 0;
  368. smp_mb(); /* clear flag *before* checking for work */
  369. if (do_block_io_op(blkif))
  370. blkif->waiting_reqs = 1;
  371. if (log_stats && time_after(jiffies, blkif->st_print))
  372. print_stats(blkif);
  373. }
  374. /* Free all persistent grant pages */
  375. if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
  376. free_persistent_gnts(&blkif->persistent_gnts,
  377. blkif->persistent_gnt_c);
  378. BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
  379. blkif->persistent_gnt_c = 0;
  380. if (log_stats)
  381. print_stats(blkif);
  382. blkif->xenblkd = NULL;
  383. xen_blkif_put(blkif);
  384. return 0;
  385. }
  386. struct seg_buf {
  387. unsigned int offset;
  388. unsigned int nsec;
  389. };
  390. /*
  391. * Unmap the grant references, and also remove the M2P over-rides
  392. * used in the 'pending_req'.
  393. */
  394. static void xen_blkbk_unmap(struct pending_req *req)
  395. {
  396. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  397. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  398. unsigned int i, invcount = 0;
  399. grant_handle_t handle;
  400. int ret;
  401. for (i = 0; i < req->nr_pages; i++) {
  402. if (!test_bit(i, req->unmap_seg))
  403. continue;
  404. handle = pending_handle(req, i);
  405. if (handle == BLKBACK_INVALID_HANDLE)
  406. continue;
  407. gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
  408. GNTMAP_host_map, handle);
  409. pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
  410. pages[invcount] = virt_to_page(vaddr(req, i));
  411. invcount++;
  412. }
  413. ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
  414. BUG_ON(ret);
  415. }
  416. static int xen_blkbk_map(struct blkif_request *req,
  417. struct pending_req *pending_req,
  418. struct seg_buf seg[],
  419. struct page *pages[])
  420. {
  421. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  422. struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  423. struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  424. struct persistent_gnt *persistent_gnt = NULL;
  425. struct xen_blkif *blkif = pending_req->blkif;
  426. phys_addr_t addr = 0;
  427. int i, j;
  428. bool new_map;
  429. int nseg = req->u.rw.nr_segments;
  430. int segs_to_map = 0;
  431. int ret = 0;
  432. int use_persistent_gnts;
  433. use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
  434. BUG_ON(blkif->persistent_gnt_c >
  435. max_mapped_grant_pages(pending_req->blkif->blk_protocol));
  436. /*
  437. * Fill out preq.nr_sects with proper amount of sectors, and setup
  438. * assign map[..] with the PFN of the page in our domain with the
  439. * corresponding grant reference for each page.
  440. */
  441. for (i = 0; i < nseg; i++) {
  442. uint32_t flags;
  443. if (use_persistent_gnts)
  444. persistent_gnt = get_persistent_gnt(
  445. &blkif->persistent_gnts,
  446. req->u.rw.seg[i].gref);
  447. if (persistent_gnt) {
  448. /*
  449. * We are using persistent grants and
  450. * the grant is already mapped
  451. */
  452. new_map = false;
  453. } else if (use_persistent_gnts &&
  454. blkif->persistent_gnt_c <
  455. max_mapped_grant_pages(blkif->blk_protocol)) {
  456. /*
  457. * We are using persistent grants, the grant is
  458. * not mapped but we have room for it
  459. */
  460. new_map = true;
  461. persistent_gnt = kmalloc(
  462. sizeof(struct persistent_gnt),
  463. GFP_KERNEL);
  464. if (!persistent_gnt)
  465. return -ENOMEM;
  466. if (alloc_xenballooned_pages(1, &persistent_gnt->page,
  467. false)) {
  468. kfree(persistent_gnt);
  469. return -ENOMEM;
  470. }
  471. persistent_gnt->gnt = req->u.rw.seg[i].gref;
  472. persistent_gnt->handle = BLKBACK_INVALID_HANDLE;
  473. pages_to_gnt[segs_to_map] =
  474. persistent_gnt->page;
  475. addr = (unsigned long) pfn_to_kaddr(
  476. page_to_pfn(persistent_gnt->page));
  477. add_persistent_gnt(&blkif->persistent_gnts,
  478. persistent_gnt);
  479. blkif->persistent_gnt_c++;
  480. pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
  481. persistent_gnt->gnt, blkif->persistent_gnt_c,
  482. max_mapped_grant_pages(blkif->blk_protocol));
  483. } else {
  484. /*
  485. * We are either using persistent grants and
  486. * hit the maximum limit of grants mapped,
  487. * or we are not using persistent grants.
  488. */
  489. if (use_persistent_gnts &&
  490. !blkif->vbd.overflow_max_grants) {
  491. blkif->vbd.overflow_max_grants = 1;
  492. pr_alert(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
  493. blkif->domid, blkif->vbd.handle);
  494. }
  495. new_map = true;
  496. pages[i] = blkbk->pending_page(pending_req, i);
  497. addr = vaddr(pending_req, i);
  498. pages_to_gnt[segs_to_map] =
  499. blkbk->pending_page(pending_req, i);
  500. }
  501. if (persistent_gnt) {
  502. pages[i] = persistent_gnt->page;
  503. persistent_gnts[i] = persistent_gnt;
  504. } else {
  505. persistent_gnts[i] = NULL;
  506. }
  507. if (new_map) {
  508. flags = GNTMAP_host_map;
  509. if (!persistent_gnt &&
  510. (pending_req->operation != BLKIF_OP_READ))
  511. flags |= GNTMAP_readonly;
  512. gnttab_set_map_op(&map[segs_to_map++], addr,
  513. flags, req->u.rw.seg[i].gref,
  514. blkif->domid);
  515. }
  516. }
  517. if (segs_to_map) {
  518. ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
  519. BUG_ON(ret);
  520. }
  521. /*
  522. * Now swizzle the MFN in our domain with the MFN from the other domain
  523. * so that when we access vaddr(pending_req,i) it has the contents of
  524. * the page from the other domain.
  525. */
  526. bitmap_zero(pending_req->unmap_seg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  527. for (i = 0, j = 0; i < nseg; i++) {
  528. if (!persistent_gnts[i] ||
  529. persistent_gnts[i]->handle == BLKBACK_INVALID_HANDLE) {
  530. /* This is a newly mapped grant */
  531. BUG_ON(j >= segs_to_map);
  532. if (unlikely(map[j].status != 0)) {
  533. pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
  534. map[j].handle = BLKBACK_INVALID_HANDLE;
  535. ret |= 1;
  536. if (persistent_gnts[i]) {
  537. rb_erase(&persistent_gnts[i]->node,
  538. &blkif->persistent_gnts);
  539. blkif->persistent_gnt_c--;
  540. kfree(persistent_gnts[i]);
  541. persistent_gnts[i] = NULL;
  542. }
  543. }
  544. }
  545. if (persistent_gnts[i]) {
  546. if (persistent_gnts[i]->handle ==
  547. BLKBACK_INVALID_HANDLE) {
  548. /*
  549. * If this is a new persistent grant
  550. * save the handler
  551. */
  552. persistent_gnts[i]->handle = map[j++].handle;
  553. }
  554. pending_handle(pending_req, i) =
  555. persistent_gnts[i]->handle;
  556. if (ret)
  557. continue;
  558. } else {
  559. pending_handle(pending_req, i) = map[j++].handle;
  560. bitmap_set(pending_req->unmap_seg, i, 1);
  561. if (ret)
  562. continue;
  563. }
  564. seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
  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 int dispatch_other_io(struct xen_blkif *blkif,
  593. struct blkif_request *req,
  594. struct pending_req *pending_req)
  595. {
  596. free_req(pending_req);
  597. make_response(blkif, req->u.other.id, req->operation,
  598. BLKIF_RSP_EOPNOTSUPP);
  599. return -EIO;
  600. }
  601. static void xen_blk_drain_io(struct xen_blkif *blkif)
  602. {
  603. atomic_set(&blkif->drain, 1);
  604. do {
  605. /* The initial value is one, and one refcnt taken at the
  606. * start of the xen_blkif_schedule thread. */
  607. if (atomic_read(&blkif->refcnt) <= 2)
  608. break;
  609. wait_for_completion_interruptible_timeout(
  610. &blkif->drain_complete, HZ);
  611. if (!atomic_read(&blkif->drain))
  612. break;
  613. } while (!kthread_should_stop());
  614. atomic_set(&blkif->drain, 0);
  615. }
  616. /*
  617. * Completion callback on the bio's. Called as bh->b_end_io()
  618. */
  619. static void __end_block_io_op(struct pending_req *pending_req, int error)
  620. {
  621. /* An error fails the entire request. */
  622. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  623. (error == -EOPNOTSUPP)) {
  624. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  625. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  626. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  627. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  628. (error == -EOPNOTSUPP)) {
  629. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  630. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  631. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  632. } else if (error) {
  633. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  634. " error=%d\n", error);
  635. pending_req->status = BLKIF_RSP_ERROR;
  636. }
  637. /*
  638. * If all of the bio's have completed it is time to unmap
  639. * the grant references associated with 'request' and provide
  640. * the proper response on the ring.
  641. */
  642. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  643. xen_blkbk_unmap(pending_req);
  644. make_response(pending_req->blkif, pending_req->id,
  645. pending_req->operation, pending_req->status);
  646. xen_blkif_put(pending_req->blkif);
  647. if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
  648. if (atomic_read(&pending_req->blkif->drain))
  649. complete(&pending_req->blkif->drain_complete);
  650. }
  651. free_req(pending_req);
  652. }
  653. }
  654. /*
  655. * bio callback.
  656. */
  657. static void end_block_io_op(struct bio *bio, int error)
  658. {
  659. __end_block_io_op(bio->bi_private, error);
  660. bio_put(bio);
  661. }
  662. /*
  663. * Function to copy the from the ring buffer the 'struct blkif_request'
  664. * (which has the sectors we want, number of them, grant references, etc),
  665. * and transmute it to the block API to hand it over to the proper block disk.
  666. */
  667. static int
  668. __do_block_io_op(struct xen_blkif *blkif)
  669. {
  670. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  671. struct blkif_request req;
  672. struct pending_req *pending_req;
  673. RING_IDX rc, rp;
  674. int more_to_do = 0;
  675. rc = blk_rings->common.req_cons;
  676. rp = blk_rings->common.sring->req_prod;
  677. rmb(); /* Ensure we see queued requests up to 'rp'. */
  678. while (rc != rp) {
  679. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  680. break;
  681. if (kthread_should_stop()) {
  682. more_to_do = 1;
  683. break;
  684. }
  685. pending_req = alloc_req();
  686. if (NULL == pending_req) {
  687. blkif->st_oo_req++;
  688. more_to_do = 1;
  689. break;
  690. }
  691. switch (blkif->blk_protocol) {
  692. case BLKIF_PROTOCOL_NATIVE:
  693. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  694. break;
  695. case BLKIF_PROTOCOL_X86_32:
  696. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  697. break;
  698. case BLKIF_PROTOCOL_X86_64:
  699. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  700. break;
  701. default:
  702. BUG();
  703. }
  704. blk_rings->common.req_cons = ++rc; /* before make_response() */
  705. /* Apply all sanity checks to /private copy/ of request. */
  706. barrier();
  707. switch (req.operation) {
  708. case BLKIF_OP_READ:
  709. case BLKIF_OP_WRITE:
  710. case BLKIF_OP_WRITE_BARRIER:
  711. case BLKIF_OP_FLUSH_DISKCACHE:
  712. if (dispatch_rw_block_io(blkif, &req, pending_req))
  713. goto done;
  714. break;
  715. case BLKIF_OP_DISCARD:
  716. free_req(pending_req);
  717. if (dispatch_discard_io(blkif, &req))
  718. goto done;
  719. break;
  720. default:
  721. if (dispatch_other_io(blkif, &req, pending_req))
  722. goto done;
  723. break;
  724. }
  725. /* Yield point for this unbounded loop. */
  726. cond_resched();
  727. }
  728. done:
  729. return more_to_do;
  730. }
  731. static int
  732. do_block_io_op(struct xen_blkif *blkif)
  733. {
  734. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  735. int more_to_do;
  736. do {
  737. more_to_do = __do_block_io_op(blkif);
  738. if (more_to_do)
  739. break;
  740. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  741. } while (more_to_do);
  742. return more_to_do;
  743. }
  744. /*
  745. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  746. * and call the 'submit_bio' to pass it to the underlying storage.
  747. */
  748. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  749. struct blkif_request *req,
  750. struct pending_req *pending_req)
  751. {
  752. struct phys_req preq;
  753. struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  754. unsigned int nseg;
  755. struct bio *bio = NULL;
  756. struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  757. int i, nbio = 0;
  758. int operation;
  759. struct blk_plug plug;
  760. bool drain = false;
  761. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  762. switch (req->operation) {
  763. case BLKIF_OP_READ:
  764. blkif->st_rd_req++;
  765. operation = READ;
  766. break;
  767. case BLKIF_OP_WRITE:
  768. blkif->st_wr_req++;
  769. operation = WRITE_ODIRECT;
  770. break;
  771. case BLKIF_OP_WRITE_BARRIER:
  772. drain = true;
  773. case BLKIF_OP_FLUSH_DISKCACHE:
  774. blkif->st_f_req++;
  775. operation = WRITE_FLUSH;
  776. break;
  777. default:
  778. operation = 0; /* make gcc happy */
  779. goto fail_response;
  780. break;
  781. }
  782. /* Check that the number of segments is sane. */
  783. nseg = req->u.rw.nr_segments;
  784. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  785. unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  786. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  787. nseg);
  788. /* Haven't submitted any bio's yet. */
  789. goto fail_response;
  790. }
  791. preq.sector_number = req->u.rw.sector_number;
  792. preq.nr_sects = 0;
  793. pending_req->blkif = blkif;
  794. pending_req->id = req->u.rw.id;
  795. pending_req->operation = req->operation;
  796. pending_req->status = BLKIF_RSP_OKAY;
  797. pending_req->nr_pages = nseg;
  798. for (i = 0; i < nseg; i++) {
  799. seg[i].nsec = req->u.rw.seg[i].last_sect -
  800. req->u.rw.seg[i].first_sect + 1;
  801. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  802. (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
  803. goto fail_response;
  804. preq.nr_sects += seg[i].nsec;
  805. }
  806. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  807. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  808. operation == READ ? "read" : "write",
  809. preq.sector_number,
  810. preq.sector_number + preq.nr_sects,
  811. blkif->vbd.pdevice);
  812. goto fail_response;
  813. }
  814. /*
  815. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  816. * is set there.
  817. */
  818. for (i = 0; i < nseg; i++) {
  819. if (((int)preq.sector_number|(int)seg[i].nsec) &
  820. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  821. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  822. blkif->domid);
  823. goto fail_response;
  824. }
  825. }
  826. /* Wait on all outstanding I/O's and once that has been completed
  827. * issue the WRITE_FLUSH.
  828. */
  829. if (drain)
  830. xen_blk_drain_io(pending_req->blkif);
  831. /*
  832. * If we have failed at this point, we need to undo the M2P override,
  833. * set gnttab_set_unmap_op on all of the grant references and perform
  834. * the hypercall to unmap the grants - that is all done in
  835. * xen_blkbk_unmap.
  836. */
  837. if (xen_blkbk_map(req, pending_req, seg, pages))
  838. goto fail_flush;
  839. /*
  840. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  841. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  842. */
  843. xen_blkif_get(blkif);
  844. for (i = 0; i < nseg; i++) {
  845. while ((bio == NULL) ||
  846. (bio_add_page(bio,
  847. pages[i],
  848. seg[i].nsec << 9,
  849. seg[i].offset) == 0)) {
  850. bio = bio_alloc(GFP_KERNEL, nseg-i);
  851. if (unlikely(bio == NULL))
  852. goto fail_put_bio;
  853. biolist[nbio++] = bio;
  854. bio->bi_bdev = preq.bdev;
  855. bio->bi_private = pending_req;
  856. bio->bi_end_io = end_block_io_op;
  857. bio->bi_sector = preq.sector_number;
  858. }
  859. preq.sector_number += seg[i].nsec;
  860. }
  861. /* This will be hit if the operation was a flush or discard. */
  862. if (!bio) {
  863. BUG_ON(operation != WRITE_FLUSH);
  864. bio = bio_alloc(GFP_KERNEL, 0);
  865. if (unlikely(bio == NULL))
  866. goto fail_put_bio;
  867. biolist[nbio++] = bio;
  868. bio->bi_bdev = preq.bdev;
  869. bio->bi_private = pending_req;
  870. bio->bi_end_io = end_block_io_op;
  871. }
  872. atomic_set(&pending_req->pendcnt, nbio);
  873. blk_start_plug(&plug);
  874. for (i = 0; i < nbio; i++)
  875. submit_bio(operation, biolist[i]);
  876. /* Let the I/Os go.. */
  877. blk_finish_plug(&plug);
  878. if (operation == READ)
  879. blkif->st_rd_sect += preq.nr_sects;
  880. else if (operation & WRITE)
  881. blkif->st_wr_sect += preq.nr_sects;
  882. return 0;
  883. fail_flush:
  884. xen_blkbk_unmap(pending_req);
  885. fail_response:
  886. /* Haven't submitted any bio's yet. */
  887. make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
  888. free_req(pending_req);
  889. msleep(1); /* back off a bit */
  890. return -EIO;
  891. fail_put_bio:
  892. for (i = 0; i < nbio; i++)
  893. bio_put(biolist[i]);
  894. atomic_set(&pending_req->pendcnt, 1);
  895. __end_block_io_op(pending_req, -EINVAL);
  896. msleep(1); /* back off a bit */
  897. return -EIO;
  898. }
  899. /*
  900. * Put a response on the ring on how the operation fared.
  901. */
  902. static void make_response(struct xen_blkif *blkif, u64 id,
  903. unsigned short op, int st)
  904. {
  905. struct blkif_response resp;
  906. unsigned long flags;
  907. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  908. int notify;
  909. resp.id = id;
  910. resp.operation = op;
  911. resp.status = st;
  912. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  913. /* Place on the response ring for the relevant domain. */
  914. switch (blkif->blk_protocol) {
  915. case BLKIF_PROTOCOL_NATIVE:
  916. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  917. &resp, sizeof(resp));
  918. break;
  919. case BLKIF_PROTOCOL_X86_32:
  920. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  921. &resp, sizeof(resp));
  922. break;
  923. case BLKIF_PROTOCOL_X86_64:
  924. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  925. &resp, sizeof(resp));
  926. break;
  927. default:
  928. BUG();
  929. }
  930. blk_rings->common.rsp_prod_pvt++;
  931. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  932. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  933. if (notify)
  934. notify_remote_via_irq(blkif->irq);
  935. }
  936. static int __init xen_blkif_init(void)
  937. {
  938. int i, mmap_pages;
  939. int rc = 0;
  940. if (!xen_domain())
  941. return -ENODEV;
  942. blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
  943. if (!blkbk) {
  944. pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
  945. return -ENOMEM;
  946. }
  947. mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  948. blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
  949. xen_blkif_reqs, GFP_KERNEL);
  950. blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
  951. mmap_pages, GFP_KERNEL);
  952. blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
  953. mmap_pages, GFP_KERNEL);
  954. if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
  955. !blkbk->pending_pages) {
  956. rc = -ENOMEM;
  957. goto out_of_memory;
  958. }
  959. for (i = 0; i < mmap_pages; i++) {
  960. blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
  961. blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
  962. if (blkbk->pending_pages[i] == NULL) {
  963. rc = -ENOMEM;
  964. goto out_of_memory;
  965. }
  966. }
  967. rc = xen_blkif_interface_init();
  968. if (rc)
  969. goto failed_init;
  970. INIT_LIST_HEAD(&blkbk->pending_free);
  971. spin_lock_init(&blkbk->pending_free_lock);
  972. init_waitqueue_head(&blkbk->pending_free_wq);
  973. for (i = 0; i < xen_blkif_reqs; i++)
  974. list_add_tail(&blkbk->pending_reqs[i].free_list,
  975. &blkbk->pending_free);
  976. rc = xen_blkif_xenbus_init();
  977. if (rc)
  978. goto failed_init;
  979. return 0;
  980. out_of_memory:
  981. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  982. failed_init:
  983. kfree(blkbk->pending_reqs);
  984. kfree(blkbk->pending_grant_handles);
  985. if (blkbk->pending_pages) {
  986. for (i = 0; i < mmap_pages; i++) {
  987. if (blkbk->pending_pages[i])
  988. __free_page(blkbk->pending_pages[i]);
  989. }
  990. kfree(blkbk->pending_pages);
  991. }
  992. kfree(blkbk);
  993. blkbk = NULL;
  994. return rc;
  995. }
  996. module_init(xen_blkif_init);
  997. MODULE_LICENSE("Dual BSD/GPL");
  998. MODULE_ALIAS("xen-backend:vbd");