blkback.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. * Maximum number of unused free pages to keep in the internal buffer.
  51. * Setting this to a value too low will reduce memory used in each backend,
  52. * but can have a performance penalty.
  53. *
  54. * A sane value is xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST, but can
  55. * be set to a lower value that might degrade performance on some intensive
  56. * IO workloads.
  57. */
  58. static int xen_blkif_max_buffer_pages = 1024;
  59. module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
  60. MODULE_PARM_DESC(max_buffer_pages,
  61. "Maximum number of free pages to keep in each block backend buffer");
  62. /*
  63. * Maximum number of grants to map persistently in blkback. For maximum
  64. * performance this should be the total numbers of grants that can be used
  65. * to fill the ring, but since this might become too high, specially with
  66. * the use of indirect descriptors, we set it to a value that provides good
  67. * performance without using too much memory.
  68. *
  69. * When the list of persistent grants is full we clean it up using a LRU
  70. * algorithm.
  71. */
  72. static int xen_blkif_max_pgrants = 1056;
  73. module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
  74. MODULE_PARM_DESC(max_persistent_grants,
  75. "Maximum number of grants to map persistently");
  76. /*
  77. * The LRU mechanism to clean the lists of persistent grants needs to
  78. * be executed periodically. The time interval between consecutive executions
  79. * of the purge mechanism is set in ms.
  80. */
  81. #define LRU_INTERVAL 100
  82. /*
  83. * When the persistent grants list is full we will remove unused grants
  84. * from the list. The percent number of grants to be removed at each LRU
  85. * execution.
  86. */
  87. #define LRU_PERCENT_CLEAN 5
  88. /* Run-time switchable: /sys/module/blkback/parameters/ */
  89. static unsigned int log_stats;
  90. module_param(log_stats, int, 0644);
  91. #define BLKBACK_INVALID_HANDLE (~0)
  92. /* Number of free pages to remove on each call to free_xenballooned_pages */
  93. #define NUM_BATCH_FREE_PAGES 10
  94. static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
  95. {
  96. unsigned long flags;
  97. spin_lock_irqsave(&blkif->free_pages_lock, flags);
  98. if (list_empty(&blkif->free_pages)) {
  99. BUG_ON(blkif->free_pages_num != 0);
  100. spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
  101. return alloc_xenballooned_pages(1, page, false);
  102. }
  103. BUG_ON(blkif->free_pages_num == 0);
  104. page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
  105. list_del(&page[0]->lru);
  106. blkif->free_pages_num--;
  107. spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
  108. return 0;
  109. }
  110. static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
  111. int num)
  112. {
  113. unsigned long flags;
  114. int i;
  115. spin_lock_irqsave(&blkif->free_pages_lock, flags);
  116. for (i = 0; i < num; i++)
  117. list_add(&page[i]->lru, &blkif->free_pages);
  118. blkif->free_pages_num += num;
  119. spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
  120. }
  121. static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
  122. {
  123. /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
  124. struct page *page[NUM_BATCH_FREE_PAGES];
  125. unsigned int num_pages = 0;
  126. unsigned long flags;
  127. spin_lock_irqsave(&blkif->free_pages_lock, flags);
  128. while (blkif->free_pages_num > num) {
  129. BUG_ON(list_empty(&blkif->free_pages));
  130. page[num_pages] = list_first_entry(&blkif->free_pages,
  131. struct page, lru);
  132. list_del(&page[num_pages]->lru);
  133. blkif->free_pages_num--;
  134. if (++num_pages == NUM_BATCH_FREE_PAGES) {
  135. spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
  136. free_xenballooned_pages(num_pages, page);
  137. spin_lock_irqsave(&blkif->free_pages_lock, flags);
  138. num_pages = 0;
  139. }
  140. }
  141. spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
  142. if (num_pages != 0)
  143. free_xenballooned_pages(num_pages, page);
  144. }
  145. #define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
  146. static int do_block_io_op(struct xen_blkif *blkif);
  147. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  148. struct blkif_request *req,
  149. struct pending_req *pending_req);
  150. static void make_response(struct xen_blkif *blkif, u64 id,
  151. unsigned short op, int st);
  152. #define foreach_grant_safe(pos, n, rbtree, node) \
  153. for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
  154. (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
  155. &(pos)->node != NULL; \
  156. (pos) = container_of(n, typeof(*(pos)), node), \
  157. (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
  158. /*
  159. * We don't need locking around the persistent grant helpers
  160. * because blkback uses a single-thread for each backed, so we
  161. * can be sure that this functions will never be called recursively.
  162. *
  163. * The only exception to that is put_persistent_grant, that can be called
  164. * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
  165. * bit operations to modify the flags of a persistent grant and to count
  166. * the number of used grants.
  167. */
  168. static int add_persistent_gnt(struct xen_blkif *blkif,
  169. struct persistent_gnt *persistent_gnt)
  170. {
  171. struct rb_node **new = NULL, *parent = NULL;
  172. struct persistent_gnt *this;
  173. if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
  174. if (!blkif->vbd.overflow_max_grants)
  175. blkif->vbd.overflow_max_grants = 1;
  176. return -EBUSY;
  177. }
  178. /* Figure out where to put new node */
  179. new = &blkif->persistent_gnts.rb_node;
  180. while (*new) {
  181. this = container_of(*new, struct persistent_gnt, node);
  182. parent = *new;
  183. if (persistent_gnt->gnt < this->gnt)
  184. new = &((*new)->rb_left);
  185. else if (persistent_gnt->gnt > this->gnt)
  186. new = &((*new)->rb_right);
  187. else {
  188. pr_alert_ratelimited(DRV_PFX " trying to add a gref that's already in the tree\n");
  189. return -EINVAL;
  190. }
  191. }
  192. bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
  193. set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
  194. /* Add new node and rebalance tree. */
  195. rb_link_node(&(persistent_gnt->node), parent, new);
  196. rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
  197. blkif->persistent_gnt_c++;
  198. atomic_inc(&blkif->persistent_gnt_in_use);
  199. return 0;
  200. }
  201. static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
  202. grant_ref_t gref)
  203. {
  204. struct persistent_gnt *data;
  205. struct rb_node *node = NULL;
  206. node = blkif->persistent_gnts.rb_node;
  207. while (node) {
  208. data = container_of(node, struct persistent_gnt, node);
  209. if (gref < data->gnt)
  210. node = node->rb_left;
  211. else if (gref > data->gnt)
  212. node = node->rb_right;
  213. else {
  214. if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
  215. pr_alert_ratelimited(DRV_PFX " requesting a grant already in use\n");
  216. return NULL;
  217. }
  218. set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
  219. atomic_inc(&blkif->persistent_gnt_in_use);
  220. return data;
  221. }
  222. }
  223. return NULL;
  224. }
  225. static void put_persistent_gnt(struct xen_blkif *blkif,
  226. struct persistent_gnt *persistent_gnt)
  227. {
  228. if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
  229. pr_alert_ratelimited(DRV_PFX " freeing a grant already unused");
  230. set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
  231. clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
  232. atomic_dec(&blkif->persistent_gnt_in_use);
  233. }
  234. static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
  235. unsigned int num)
  236. {
  237. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  238. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  239. struct persistent_gnt *persistent_gnt;
  240. struct rb_node *n;
  241. int ret = 0;
  242. int segs_to_unmap = 0;
  243. foreach_grant_safe(persistent_gnt, n, root, node) {
  244. BUG_ON(persistent_gnt->handle ==
  245. BLKBACK_INVALID_HANDLE);
  246. gnttab_set_unmap_op(&unmap[segs_to_unmap],
  247. (unsigned long) pfn_to_kaddr(page_to_pfn(
  248. persistent_gnt->page)),
  249. GNTMAP_host_map,
  250. persistent_gnt->handle);
  251. pages[segs_to_unmap] = persistent_gnt->page;
  252. if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
  253. !rb_next(&persistent_gnt->node)) {
  254. ret = gnttab_unmap_refs(unmap, NULL, pages,
  255. segs_to_unmap);
  256. BUG_ON(ret);
  257. put_free_pages(blkif, pages, segs_to_unmap);
  258. segs_to_unmap = 0;
  259. }
  260. rb_erase(&persistent_gnt->node, root);
  261. kfree(persistent_gnt);
  262. num--;
  263. }
  264. BUG_ON(num != 0);
  265. }
  266. void xen_blkbk_unmap_purged_grants(struct work_struct *work)
  267. {
  268. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  269. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  270. struct persistent_gnt *persistent_gnt;
  271. int ret, segs_to_unmap = 0;
  272. struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
  273. while(!list_empty(&blkif->persistent_purge_list)) {
  274. persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
  275. struct persistent_gnt,
  276. remove_node);
  277. list_del(&persistent_gnt->remove_node);
  278. gnttab_set_unmap_op(&unmap[segs_to_unmap],
  279. vaddr(persistent_gnt->page),
  280. GNTMAP_host_map,
  281. persistent_gnt->handle);
  282. pages[segs_to_unmap] = persistent_gnt->page;
  283. if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  284. ret = gnttab_unmap_refs(unmap, NULL, pages,
  285. segs_to_unmap);
  286. BUG_ON(ret);
  287. put_free_pages(blkif, pages, segs_to_unmap);
  288. segs_to_unmap = 0;
  289. }
  290. kfree(persistent_gnt);
  291. }
  292. if (segs_to_unmap > 0) {
  293. ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
  294. BUG_ON(ret);
  295. put_free_pages(blkif, pages, segs_to_unmap);
  296. }
  297. }
  298. static void purge_persistent_gnt(struct xen_blkif *blkif)
  299. {
  300. struct persistent_gnt *persistent_gnt;
  301. struct rb_node *n;
  302. unsigned int num_clean, total;
  303. bool scan_used = false, clean_used = false;
  304. struct rb_root *root;
  305. if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
  306. (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
  307. !blkif->vbd.overflow_max_grants)) {
  308. return;
  309. }
  310. if (work_pending(&blkif->persistent_purge_work)) {
  311. pr_alert_ratelimited(DRV_PFX "Scheduled work from previous purge is still pending, cannot purge list\n");
  312. return;
  313. }
  314. num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
  315. num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
  316. num_clean = min(blkif->persistent_gnt_c, num_clean);
  317. if ((num_clean == 0) ||
  318. (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
  319. return;
  320. /*
  321. * At this point, we can assure that there will be no calls
  322. * to get_persistent_grant (because we are executing this code from
  323. * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
  324. * which means that the number of currently used grants will go down,
  325. * but never up, so we will always be able to remove the requested
  326. * number of grants.
  327. */
  328. total = num_clean;
  329. pr_debug(DRV_PFX "Going to purge %u persistent grants\n", num_clean);
  330. BUG_ON(!list_empty(&blkif->persistent_purge_list));
  331. root = &blkif->persistent_gnts;
  332. purge_list:
  333. foreach_grant_safe(persistent_gnt, n, root, node) {
  334. BUG_ON(persistent_gnt->handle ==
  335. BLKBACK_INVALID_HANDLE);
  336. if (clean_used) {
  337. clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
  338. continue;
  339. }
  340. if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
  341. continue;
  342. if (!scan_used &&
  343. (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
  344. continue;
  345. rb_erase(&persistent_gnt->node, root);
  346. list_add(&persistent_gnt->remove_node,
  347. &blkif->persistent_purge_list);
  348. if (--num_clean == 0)
  349. goto finished;
  350. }
  351. /*
  352. * If we get here it means we also need to start cleaning
  353. * grants that were used since last purge in order to cope
  354. * with the requested num
  355. */
  356. if (!scan_used && !clean_used) {
  357. pr_debug(DRV_PFX "Still missing %u purged frames\n", num_clean);
  358. scan_used = true;
  359. goto purge_list;
  360. }
  361. finished:
  362. if (!clean_used) {
  363. pr_debug(DRV_PFX "Finished scanning for grants to clean, removing used flag\n");
  364. clean_used = true;
  365. goto purge_list;
  366. }
  367. blkif->persistent_gnt_c -= (total - num_clean);
  368. blkif->vbd.overflow_max_grants = 0;
  369. /* We can defer this work */
  370. schedule_work(&blkif->persistent_purge_work);
  371. pr_debug(DRV_PFX "Purged %u/%u\n", (total - num_clean), total);
  372. return;
  373. }
  374. /*
  375. * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  376. */
  377. static struct pending_req *alloc_req(struct xen_blkif *blkif)
  378. {
  379. struct pending_req *req = NULL;
  380. unsigned long flags;
  381. spin_lock_irqsave(&blkif->pending_free_lock, flags);
  382. if (!list_empty(&blkif->pending_free)) {
  383. req = list_entry(blkif->pending_free.next, struct pending_req,
  384. free_list);
  385. list_del(&req->free_list);
  386. }
  387. spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
  388. return req;
  389. }
  390. /*
  391. * Return the 'pending_req' structure back to the freepool. We also
  392. * wake up the thread if it was waiting for a free page.
  393. */
  394. static void free_req(struct xen_blkif *blkif, struct pending_req *req)
  395. {
  396. unsigned long flags;
  397. int was_empty;
  398. spin_lock_irqsave(&blkif->pending_free_lock, flags);
  399. was_empty = list_empty(&blkif->pending_free);
  400. list_add(&req->free_list, &blkif->pending_free);
  401. spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
  402. if (was_empty)
  403. wake_up(&blkif->pending_free_wq);
  404. }
  405. /*
  406. * Routines for managing virtual block devices (vbds).
  407. */
  408. static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
  409. int operation)
  410. {
  411. struct xen_vbd *vbd = &blkif->vbd;
  412. int rc = -EACCES;
  413. if ((operation != READ) && vbd->readonly)
  414. goto out;
  415. if (likely(req->nr_sects)) {
  416. blkif_sector_t end = req->sector_number + req->nr_sects;
  417. if (unlikely(end < req->sector_number))
  418. goto out;
  419. if (unlikely(end > vbd_sz(vbd)))
  420. goto out;
  421. }
  422. req->dev = vbd->pdevice;
  423. req->bdev = vbd->bdev;
  424. rc = 0;
  425. out:
  426. return rc;
  427. }
  428. static void xen_vbd_resize(struct xen_blkif *blkif)
  429. {
  430. struct xen_vbd *vbd = &blkif->vbd;
  431. struct xenbus_transaction xbt;
  432. int err;
  433. struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
  434. unsigned long long new_size = vbd_sz(vbd);
  435. pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
  436. blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
  437. pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
  438. vbd->size = new_size;
  439. again:
  440. err = xenbus_transaction_start(&xbt);
  441. if (err) {
  442. pr_warn(DRV_PFX "Error starting transaction");
  443. return;
  444. }
  445. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  446. (unsigned long long)vbd_sz(vbd));
  447. if (err) {
  448. pr_warn(DRV_PFX "Error writing new size");
  449. goto abort;
  450. }
  451. /*
  452. * Write the current state; we will use this to synchronize
  453. * the front-end. If the current state is "connected" the
  454. * front-end will get the new size information online.
  455. */
  456. err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
  457. if (err) {
  458. pr_warn(DRV_PFX "Error writing the state");
  459. goto abort;
  460. }
  461. err = xenbus_transaction_end(xbt, 0);
  462. if (err == -EAGAIN)
  463. goto again;
  464. if (err)
  465. pr_warn(DRV_PFX "Error ending transaction");
  466. return;
  467. abort:
  468. xenbus_transaction_end(xbt, 1);
  469. }
  470. /*
  471. * Notification from the guest OS.
  472. */
  473. static void blkif_notify_work(struct xen_blkif *blkif)
  474. {
  475. blkif->waiting_reqs = 1;
  476. wake_up(&blkif->wq);
  477. }
  478. irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
  479. {
  480. blkif_notify_work(dev_id);
  481. return IRQ_HANDLED;
  482. }
  483. /*
  484. * SCHEDULER FUNCTIONS
  485. */
  486. static void print_stats(struct xen_blkif *blkif)
  487. {
  488. pr_info("xen-blkback (%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
  489. " | ds %4llu | pg: %4u/%4d\n",
  490. current->comm, blkif->st_oo_req,
  491. blkif->st_rd_req, blkif->st_wr_req,
  492. blkif->st_f_req, blkif->st_ds_req,
  493. blkif->persistent_gnt_c,
  494. xen_blkif_max_pgrants);
  495. blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
  496. blkif->st_rd_req = 0;
  497. blkif->st_wr_req = 0;
  498. blkif->st_oo_req = 0;
  499. blkif->st_ds_req = 0;
  500. }
  501. int xen_blkif_schedule(void *arg)
  502. {
  503. struct xen_blkif *blkif = arg;
  504. struct xen_vbd *vbd = &blkif->vbd;
  505. unsigned long timeout;
  506. int ret;
  507. xen_blkif_get(blkif);
  508. while (!kthread_should_stop()) {
  509. if (try_to_freeze())
  510. continue;
  511. if (unlikely(vbd->size != vbd_sz(vbd)))
  512. xen_vbd_resize(blkif);
  513. timeout = msecs_to_jiffies(LRU_INTERVAL);
  514. timeout = wait_event_interruptible_timeout(
  515. blkif->wq,
  516. blkif->waiting_reqs || kthread_should_stop(),
  517. timeout);
  518. if (timeout == 0)
  519. goto purge_gnt_list;
  520. timeout = wait_event_interruptible_timeout(
  521. blkif->pending_free_wq,
  522. !list_empty(&blkif->pending_free) ||
  523. kthread_should_stop(),
  524. timeout);
  525. if (timeout == 0)
  526. goto purge_gnt_list;
  527. blkif->waiting_reqs = 0;
  528. smp_mb(); /* clear flag *before* checking for work */
  529. ret = do_block_io_op(blkif);
  530. if (ret > 0)
  531. blkif->waiting_reqs = 1;
  532. if (ret == -EACCES)
  533. wait_event_interruptible(blkif->shutdown_wq,
  534. kthread_should_stop());
  535. purge_gnt_list:
  536. if (blkif->vbd.feature_gnt_persistent &&
  537. time_after(jiffies, blkif->next_lru)) {
  538. purge_persistent_gnt(blkif);
  539. blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
  540. }
  541. /* Shrink if we have more than xen_blkif_max_buffer_pages */
  542. shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
  543. if (log_stats && time_after(jiffies, blkif->st_print))
  544. print_stats(blkif);
  545. }
  546. /* Drain pending purge work */
  547. flush_work(&blkif->persistent_purge_work);
  548. if (log_stats)
  549. print_stats(blkif);
  550. blkif->xenblkd = NULL;
  551. xen_blkif_put(blkif);
  552. return 0;
  553. }
  554. /*
  555. * Remove persistent grants and empty the pool of free pages
  556. */
  557. void xen_blkbk_free_caches(struct xen_blkif *blkif)
  558. {
  559. /* Free all persistent grant pages */
  560. if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
  561. free_persistent_gnts(blkif, &blkif->persistent_gnts,
  562. blkif->persistent_gnt_c);
  563. BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
  564. blkif->persistent_gnt_c = 0;
  565. /* Since we are shutting down remove all pages from the buffer */
  566. shrink_free_pagepool(blkif, 0 /* All */);
  567. }
  568. /*
  569. * Unmap the grant references, and also remove the M2P over-rides
  570. * used in the 'pending_req'.
  571. */
  572. static void xen_blkbk_unmap(struct xen_blkif *blkif,
  573. struct grant_page *pages[],
  574. int num)
  575. {
  576. struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  577. struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  578. unsigned int i, invcount = 0;
  579. int ret;
  580. for (i = 0; i < num; i++) {
  581. if (pages[i]->persistent_gnt != NULL) {
  582. put_persistent_gnt(blkif, pages[i]->persistent_gnt);
  583. continue;
  584. }
  585. if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
  586. continue;
  587. unmap_pages[invcount] = pages[i]->page;
  588. gnttab_set_unmap_op(&unmap[invcount], vaddr(pages[i]->page),
  589. GNTMAP_host_map, pages[i]->handle);
  590. pages[i]->handle = BLKBACK_INVALID_HANDLE;
  591. if (++invcount == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  592. ret = gnttab_unmap_refs(unmap, NULL, unmap_pages,
  593. invcount);
  594. BUG_ON(ret);
  595. put_free_pages(blkif, unmap_pages, invcount);
  596. invcount = 0;
  597. }
  598. }
  599. if (invcount) {
  600. ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
  601. BUG_ON(ret);
  602. put_free_pages(blkif, unmap_pages, invcount);
  603. }
  604. }
  605. static int xen_blkbk_map(struct xen_blkif *blkif,
  606. struct grant_page *pages[],
  607. int num, bool ro)
  608. {
  609. struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  610. struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  611. struct persistent_gnt *persistent_gnt = NULL;
  612. phys_addr_t addr = 0;
  613. int i, seg_idx, new_map_idx;
  614. int segs_to_map = 0;
  615. int ret = 0;
  616. int last_map = 0, map_until = 0;
  617. int use_persistent_gnts;
  618. use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
  619. /*
  620. * Fill out preq.nr_sects with proper amount of sectors, and setup
  621. * assign map[..] with the PFN of the page in our domain with the
  622. * corresponding grant reference for each page.
  623. */
  624. again:
  625. for (i = map_until; i < num; i++) {
  626. uint32_t flags;
  627. if (use_persistent_gnts)
  628. persistent_gnt = get_persistent_gnt(
  629. blkif,
  630. pages[i]->gref);
  631. if (persistent_gnt) {
  632. /*
  633. * We are using persistent grants and
  634. * the grant is already mapped
  635. */
  636. pages[i]->page = persistent_gnt->page;
  637. pages[i]->persistent_gnt = persistent_gnt;
  638. } else {
  639. if (get_free_page(blkif, &pages[i]->page))
  640. goto out_of_memory;
  641. addr = vaddr(pages[i]->page);
  642. pages_to_gnt[segs_to_map] = pages[i]->page;
  643. pages[i]->persistent_gnt = NULL;
  644. flags = GNTMAP_host_map;
  645. if (!use_persistent_gnts && ro)
  646. flags |= GNTMAP_readonly;
  647. gnttab_set_map_op(&map[segs_to_map++], addr,
  648. flags, pages[i]->gref,
  649. blkif->domid);
  650. }
  651. map_until = i + 1;
  652. if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
  653. break;
  654. }
  655. if (segs_to_map) {
  656. ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
  657. BUG_ON(ret);
  658. }
  659. /*
  660. * Now swizzle the MFN in our domain with the MFN from the other domain
  661. * so that when we access vaddr(pending_req,i) it has the contents of
  662. * the page from the other domain.
  663. */
  664. for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
  665. if (!pages[seg_idx]->persistent_gnt) {
  666. /* This is a newly mapped grant */
  667. BUG_ON(new_map_idx >= segs_to_map);
  668. if (unlikely(map[new_map_idx].status != 0)) {
  669. pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
  670. put_free_pages(blkif, &pages[seg_idx]->page, 1);
  671. pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
  672. ret |= 1;
  673. goto next;
  674. }
  675. pages[seg_idx]->handle = map[new_map_idx].handle;
  676. } else {
  677. continue;
  678. }
  679. if (use_persistent_gnts &&
  680. blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
  681. /*
  682. * We are using persistent grants, the grant is
  683. * not mapped but we might have room for it.
  684. */
  685. persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
  686. GFP_KERNEL);
  687. if (!persistent_gnt) {
  688. /*
  689. * If we don't have enough memory to
  690. * allocate the persistent_gnt struct
  691. * map this grant non-persistenly
  692. */
  693. goto next;
  694. }
  695. persistent_gnt->gnt = map[new_map_idx].ref;
  696. persistent_gnt->handle = map[new_map_idx].handle;
  697. persistent_gnt->page = pages[seg_idx]->page;
  698. if (add_persistent_gnt(blkif,
  699. persistent_gnt)) {
  700. kfree(persistent_gnt);
  701. persistent_gnt = NULL;
  702. goto next;
  703. }
  704. pages[seg_idx]->persistent_gnt = persistent_gnt;
  705. pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
  706. persistent_gnt->gnt, blkif->persistent_gnt_c,
  707. xen_blkif_max_pgrants);
  708. goto next;
  709. }
  710. if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
  711. blkif->vbd.overflow_max_grants = 1;
  712. pr_debug(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
  713. blkif->domid, blkif->vbd.handle);
  714. }
  715. /*
  716. * We could not map this grant persistently, so use it as
  717. * a non-persistent grant.
  718. */
  719. next:
  720. new_map_idx++;
  721. }
  722. segs_to_map = 0;
  723. last_map = map_until;
  724. if (map_until != num)
  725. goto again;
  726. return ret;
  727. out_of_memory:
  728. pr_alert(DRV_PFX "%s: out of memory\n", __func__);
  729. put_free_pages(blkif, pages_to_gnt, segs_to_map);
  730. return -ENOMEM;
  731. }
  732. static int xen_blkbk_map_seg(struct pending_req *pending_req)
  733. {
  734. int rc;
  735. rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
  736. pending_req->nr_pages,
  737. (pending_req->operation != BLKIF_OP_READ));
  738. return rc;
  739. }
  740. static int xen_blkbk_parse_indirect(struct blkif_request *req,
  741. struct pending_req *pending_req,
  742. struct seg_buf seg[],
  743. struct phys_req *preq)
  744. {
  745. struct grant_page **pages = pending_req->indirect_pages;
  746. struct xen_blkif *blkif = pending_req->blkif;
  747. int indirect_grefs, rc, n, nseg, i;
  748. struct blkif_request_segment *segments = NULL;
  749. nseg = pending_req->nr_pages;
  750. indirect_grefs = INDIRECT_PAGES(nseg);
  751. BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
  752. for (i = 0; i < indirect_grefs; i++)
  753. pages[i]->gref = req->u.indirect.indirect_grefs[i];
  754. rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
  755. if (rc)
  756. goto unmap;
  757. for (n = 0, i = 0; n < nseg; n++) {
  758. if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
  759. /* Map indirect segments */
  760. if (segments)
  761. kunmap_atomic(segments);
  762. segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
  763. }
  764. i = n % SEGS_PER_INDIRECT_FRAME;
  765. pending_req->segments[n]->gref = segments[i].gref;
  766. seg[n].nsec = segments[i].last_sect -
  767. segments[i].first_sect + 1;
  768. seg[n].offset = (segments[i].first_sect << 9);
  769. if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
  770. (segments[i].last_sect < segments[i].first_sect)) {
  771. rc = -EINVAL;
  772. goto unmap;
  773. }
  774. preq->nr_sects += seg[n].nsec;
  775. }
  776. unmap:
  777. if (segments)
  778. kunmap_atomic(segments);
  779. xen_blkbk_unmap(blkif, pages, indirect_grefs);
  780. return rc;
  781. }
  782. static int dispatch_discard_io(struct xen_blkif *blkif,
  783. struct blkif_request *req)
  784. {
  785. int err = 0;
  786. int status = BLKIF_RSP_OKAY;
  787. struct block_device *bdev = blkif->vbd.bdev;
  788. unsigned long secure;
  789. struct phys_req preq;
  790. xen_blkif_get(blkif);
  791. preq.sector_number = req->u.discard.sector_number;
  792. preq.nr_sects = req->u.discard.nr_sectors;
  793. err = xen_vbd_translate(&preq, blkif, WRITE);
  794. if (err) {
  795. pr_warn(DRV_PFX "access denied: DISCARD [%llu->%llu] on dev=%04x\n",
  796. preq.sector_number,
  797. preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
  798. goto fail_response;
  799. }
  800. blkif->st_ds_req++;
  801. secure = (blkif->vbd.discard_secure &&
  802. (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
  803. BLKDEV_DISCARD_SECURE : 0;
  804. err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
  805. req->u.discard.nr_sectors,
  806. GFP_KERNEL, secure);
  807. fail_response:
  808. if (err == -EOPNOTSUPP) {
  809. pr_debug(DRV_PFX "discard op failed, not supported\n");
  810. status = BLKIF_RSP_EOPNOTSUPP;
  811. } else if (err)
  812. status = BLKIF_RSP_ERROR;
  813. make_response(blkif, req->u.discard.id, req->operation, status);
  814. xen_blkif_put(blkif);
  815. return err;
  816. }
  817. static int dispatch_other_io(struct xen_blkif *blkif,
  818. struct blkif_request *req,
  819. struct pending_req *pending_req)
  820. {
  821. free_req(blkif, pending_req);
  822. make_response(blkif, req->u.other.id, req->operation,
  823. BLKIF_RSP_EOPNOTSUPP);
  824. return -EIO;
  825. }
  826. static void xen_blk_drain_io(struct xen_blkif *blkif)
  827. {
  828. atomic_set(&blkif->drain, 1);
  829. do {
  830. if (atomic_read(&blkif->inflight) == 0)
  831. break;
  832. wait_for_completion_interruptible_timeout(
  833. &blkif->drain_complete, HZ);
  834. if (!atomic_read(&blkif->drain))
  835. break;
  836. } while (!kthread_should_stop());
  837. atomic_set(&blkif->drain, 0);
  838. }
  839. /*
  840. * Completion callback on the bio's. Called as bh->b_end_io()
  841. */
  842. static void __end_block_io_op(struct pending_req *pending_req, int error)
  843. {
  844. /* An error fails the entire request. */
  845. if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
  846. (error == -EOPNOTSUPP)) {
  847. pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
  848. xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
  849. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  850. } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
  851. (error == -EOPNOTSUPP)) {
  852. pr_debug(DRV_PFX "write barrier op failed, not supported\n");
  853. xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
  854. pending_req->status = BLKIF_RSP_EOPNOTSUPP;
  855. } else if (error) {
  856. pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
  857. " error=%d\n", error);
  858. pending_req->status = BLKIF_RSP_ERROR;
  859. }
  860. /*
  861. * If all of the bio's have completed it is time to unmap
  862. * the grant references associated with 'request' and provide
  863. * the proper response on the ring.
  864. */
  865. if (atomic_dec_and_test(&pending_req->pendcnt)) {
  866. struct xen_blkif *blkif = pending_req->blkif;
  867. xen_blkbk_unmap(blkif,
  868. pending_req->segments,
  869. pending_req->nr_pages);
  870. make_response(blkif, pending_req->id,
  871. pending_req->operation, pending_req->status);
  872. free_req(blkif, pending_req);
  873. /*
  874. * Make sure the request is freed before releasing blkif,
  875. * or there could be a race between free_req and the
  876. * cleanup done in xen_blkif_free during shutdown.
  877. *
  878. * NB: The fact that we might try to wake up pending_free_wq
  879. * before drain_complete (in case there's a drain going on)
  880. * it's not a problem with our current implementation
  881. * because we can assure there's no thread waiting on
  882. * pending_free_wq if there's a drain going on, but it has
  883. * to be taken into account if the current model is changed.
  884. */
  885. if (atomic_dec_and_test(&blkif->inflight) && atomic_read(&blkif->drain)) {
  886. complete(&blkif->drain_complete);
  887. }
  888. xen_blkif_put(blkif);
  889. }
  890. }
  891. /*
  892. * bio callback.
  893. */
  894. static void end_block_io_op(struct bio *bio, int error)
  895. {
  896. __end_block_io_op(bio->bi_private, error);
  897. bio_put(bio);
  898. }
  899. /*
  900. * Function to copy the from the ring buffer the 'struct blkif_request'
  901. * (which has the sectors we want, number of them, grant references, etc),
  902. * and transmute it to the block API to hand it over to the proper block disk.
  903. */
  904. static int
  905. __do_block_io_op(struct xen_blkif *blkif)
  906. {
  907. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  908. struct blkif_request req;
  909. struct pending_req *pending_req;
  910. RING_IDX rc, rp;
  911. int more_to_do = 0;
  912. rc = blk_rings->common.req_cons;
  913. rp = blk_rings->common.sring->req_prod;
  914. rmb(); /* Ensure we see queued requests up to 'rp'. */
  915. if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
  916. rc = blk_rings->common.rsp_prod_pvt;
  917. pr_warn(DRV_PFX "Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
  918. rp, rc, rp - rc, blkif->vbd.pdevice);
  919. return -EACCES;
  920. }
  921. while (rc != rp) {
  922. if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
  923. break;
  924. if (kthread_should_stop()) {
  925. more_to_do = 1;
  926. break;
  927. }
  928. pending_req = alloc_req(blkif);
  929. if (NULL == pending_req) {
  930. blkif->st_oo_req++;
  931. more_to_do = 1;
  932. break;
  933. }
  934. switch (blkif->blk_protocol) {
  935. case BLKIF_PROTOCOL_NATIVE:
  936. memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
  937. break;
  938. case BLKIF_PROTOCOL_X86_32:
  939. blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
  940. break;
  941. case BLKIF_PROTOCOL_X86_64:
  942. blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
  943. break;
  944. default:
  945. BUG();
  946. }
  947. blk_rings->common.req_cons = ++rc; /* before make_response() */
  948. /* Apply all sanity checks to /private copy/ of request. */
  949. barrier();
  950. switch (req.operation) {
  951. case BLKIF_OP_READ:
  952. case BLKIF_OP_WRITE:
  953. case BLKIF_OP_WRITE_BARRIER:
  954. case BLKIF_OP_FLUSH_DISKCACHE:
  955. case BLKIF_OP_INDIRECT:
  956. if (dispatch_rw_block_io(blkif, &req, pending_req))
  957. goto done;
  958. break;
  959. case BLKIF_OP_DISCARD:
  960. free_req(blkif, pending_req);
  961. if (dispatch_discard_io(blkif, &req))
  962. goto done;
  963. break;
  964. default:
  965. if (dispatch_other_io(blkif, &req, pending_req))
  966. goto done;
  967. break;
  968. }
  969. /* Yield point for this unbounded loop. */
  970. cond_resched();
  971. }
  972. done:
  973. return more_to_do;
  974. }
  975. static int
  976. do_block_io_op(struct xen_blkif *blkif)
  977. {
  978. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  979. int more_to_do;
  980. do {
  981. more_to_do = __do_block_io_op(blkif);
  982. if (more_to_do)
  983. break;
  984. RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
  985. } while (more_to_do);
  986. return more_to_do;
  987. }
  988. /*
  989. * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
  990. * and call the 'submit_bio' to pass it to the underlying storage.
  991. */
  992. static int dispatch_rw_block_io(struct xen_blkif *blkif,
  993. struct blkif_request *req,
  994. struct pending_req *pending_req)
  995. {
  996. struct phys_req preq;
  997. struct seg_buf *seg = pending_req->seg;
  998. unsigned int nseg;
  999. struct bio *bio = NULL;
  1000. struct bio **biolist = pending_req->biolist;
  1001. int i, nbio = 0;
  1002. int operation;
  1003. struct blk_plug plug;
  1004. bool drain = false;
  1005. struct grant_page **pages = pending_req->segments;
  1006. unsigned short req_operation;
  1007. req_operation = req->operation == BLKIF_OP_INDIRECT ?
  1008. req->u.indirect.indirect_op : req->operation;
  1009. if ((req->operation == BLKIF_OP_INDIRECT) &&
  1010. (req_operation != BLKIF_OP_READ) &&
  1011. (req_operation != BLKIF_OP_WRITE)) {
  1012. pr_debug(DRV_PFX "Invalid indirect operation (%u)\n",
  1013. req_operation);
  1014. goto fail_response;
  1015. }
  1016. switch (req_operation) {
  1017. case BLKIF_OP_READ:
  1018. blkif->st_rd_req++;
  1019. operation = READ;
  1020. break;
  1021. case BLKIF_OP_WRITE:
  1022. blkif->st_wr_req++;
  1023. operation = WRITE_ODIRECT;
  1024. break;
  1025. case BLKIF_OP_WRITE_BARRIER:
  1026. drain = true;
  1027. case BLKIF_OP_FLUSH_DISKCACHE:
  1028. blkif->st_f_req++;
  1029. operation = WRITE_FLUSH;
  1030. break;
  1031. default:
  1032. operation = 0; /* make gcc happy */
  1033. goto fail_response;
  1034. break;
  1035. }
  1036. /* Check that the number of segments is sane. */
  1037. nseg = req->operation == BLKIF_OP_INDIRECT ?
  1038. req->u.indirect.nr_segments : req->u.rw.nr_segments;
  1039. if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
  1040. unlikely((req->operation != BLKIF_OP_INDIRECT) &&
  1041. (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
  1042. unlikely((req->operation == BLKIF_OP_INDIRECT) &&
  1043. (nseg > MAX_INDIRECT_SEGMENTS))) {
  1044. pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
  1045. nseg);
  1046. /* Haven't submitted any bio's yet. */
  1047. goto fail_response;
  1048. }
  1049. preq.nr_sects = 0;
  1050. pending_req->blkif = blkif;
  1051. pending_req->id = req->u.rw.id;
  1052. pending_req->operation = req_operation;
  1053. pending_req->status = BLKIF_RSP_OKAY;
  1054. pending_req->nr_pages = nseg;
  1055. if (req->operation != BLKIF_OP_INDIRECT) {
  1056. preq.dev = req->u.rw.handle;
  1057. preq.sector_number = req->u.rw.sector_number;
  1058. for (i = 0; i < nseg; i++) {
  1059. pages[i]->gref = req->u.rw.seg[i].gref;
  1060. seg[i].nsec = req->u.rw.seg[i].last_sect -
  1061. req->u.rw.seg[i].first_sect + 1;
  1062. seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
  1063. if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
  1064. (req->u.rw.seg[i].last_sect <
  1065. req->u.rw.seg[i].first_sect))
  1066. goto fail_response;
  1067. preq.nr_sects += seg[i].nsec;
  1068. }
  1069. } else {
  1070. preq.dev = req->u.indirect.handle;
  1071. preq.sector_number = req->u.indirect.sector_number;
  1072. if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
  1073. goto fail_response;
  1074. }
  1075. if (xen_vbd_translate(&preq, blkif, operation) != 0) {
  1076. pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
  1077. operation == READ ? "read" : "write",
  1078. preq.sector_number,
  1079. preq.sector_number + preq.nr_sects,
  1080. blkif->vbd.pdevice);
  1081. goto fail_response;
  1082. }
  1083. /*
  1084. * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
  1085. * is set there.
  1086. */
  1087. for (i = 0; i < nseg; i++) {
  1088. if (((int)preq.sector_number|(int)seg[i].nsec) &
  1089. ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
  1090. pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
  1091. blkif->domid);
  1092. goto fail_response;
  1093. }
  1094. }
  1095. /* Wait on all outstanding I/O's and once that has been completed
  1096. * issue the WRITE_FLUSH.
  1097. */
  1098. if (drain)
  1099. xen_blk_drain_io(pending_req->blkif);
  1100. /*
  1101. * If we have failed at this point, we need to undo the M2P override,
  1102. * set gnttab_set_unmap_op on all of the grant references and perform
  1103. * the hypercall to unmap the grants - that is all done in
  1104. * xen_blkbk_unmap.
  1105. */
  1106. if (xen_blkbk_map_seg(pending_req))
  1107. goto fail_flush;
  1108. /*
  1109. * This corresponding xen_blkif_put is done in __end_block_io_op, or
  1110. * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
  1111. */
  1112. xen_blkif_get(blkif);
  1113. atomic_inc(&blkif->inflight);
  1114. for (i = 0; i < nseg; i++) {
  1115. while ((bio == NULL) ||
  1116. (bio_add_page(bio,
  1117. pages[i]->page,
  1118. seg[i].nsec << 9,
  1119. seg[i].offset) == 0)) {
  1120. int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
  1121. bio = bio_alloc(GFP_KERNEL, nr_iovecs);
  1122. if (unlikely(bio == NULL))
  1123. goto fail_put_bio;
  1124. biolist[nbio++] = bio;
  1125. bio->bi_bdev = preq.bdev;
  1126. bio->bi_private = pending_req;
  1127. bio->bi_end_io = end_block_io_op;
  1128. bio->bi_iter.bi_sector = preq.sector_number;
  1129. }
  1130. preq.sector_number += seg[i].nsec;
  1131. }
  1132. /* This will be hit if the operation was a flush or discard. */
  1133. if (!bio) {
  1134. BUG_ON(operation != WRITE_FLUSH);
  1135. bio = bio_alloc(GFP_KERNEL, 0);
  1136. if (unlikely(bio == NULL))
  1137. goto fail_put_bio;
  1138. biolist[nbio++] = bio;
  1139. bio->bi_bdev = preq.bdev;
  1140. bio->bi_private = pending_req;
  1141. bio->bi_end_io = end_block_io_op;
  1142. }
  1143. atomic_set(&pending_req->pendcnt, nbio);
  1144. blk_start_plug(&plug);
  1145. for (i = 0; i < nbio; i++)
  1146. submit_bio(operation, biolist[i]);
  1147. /* Let the I/Os go.. */
  1148. blk_finish_plug(&plug);
  1149. if (operation == READ)
  1150. blkif->st_rd_sect += preq.nr_sects;
  1151. else if (operation & WRITE)
  1152. blkif->st_wr_sect += preq.nr_sects;
  1153. return 0;
  1154. fail_flush:
  1155. xen_blkbk_unmap(blkif, pending_req->segments,
  1156. pending_req->nr_pages);
  1157. fail_response:
  1158. /* Haven't submitted any bio's yet. */
  1159. make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
  1160. free_req(blkif, pending_req);
  1161. msleep(1); /* back off a bit */
  1162. return -EIO;
  1163. fail_put_bio:
  1164. for (i = 0; i < nbio; i++)
  1165. bio_put(biolist[i]);
  1166. atomic_set(&pending_req->pendcnt, 1);
  1167. __end_block_io_op(pending_req, -EINVAL);
  1168. msleep(1); /* back off a bit */
  1169. return -EIO;
  1170. }
  1171. /*
  1172. * Put a response on the ring on how the operation fared.
  1173. */
  1174. static void make_response(struct xen_blkif *blkif, u64 id,
  1175. unsigned short op, int st)
  1176. {
  1177. struct blkif_response resp;
  1178. unsigned long flags;
  1179. union blkif_back_rings *blk_rings = &blkif->blk_rings;
  1180. int notify;
  1181. resp.id = id;
  1182. resp.operation = op;
  1183. resp.status = st;
  1184. spin_lock_irqsave(&blkif->blk_ring_lock, flags);
  1185. /* Place on the response ring for the relevant domain. */
  1186. switch (blkif->blk_protocol) {
  1187. case BLKIF_PROTOCOL_NATIVE:
  1188. memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
  1189. &resp, sizeof(resp));
  1190. break;
  1191. case BLKIF_PROTOCOL_X86_32:
  1192. memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
  1193. &resp, sizeof(resp));
  1194. break;
  1195. case BLKIF_PROTOCOL_X86_64:
  1196. memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
  1197. &resp, sizeof(resp));
  1198. break;
  1199. default:
  1200. BUG();
  1201. }
  1202. blk_rings->common.rsp_prod_pvt++;
  1203. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
  1204. spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
  1205. if (notify)
  1206. notify_remote_via_irq(blkif->irq);
  1207. }
  1208. static int __init xen_blkif_init(void)
  1209. {
  1210. int rc = 0;
  1211. if (!xen_domain())
  1212. return -ENODEV;
  1213. rc = xen_blkif_interface_init();
  1214. if (rc)
  1215. goto failed_init;
  1216. rc = xen_blkif_xenbus_init();
  1217. if (rc)
  1218. goto failed_init;
  1219. failed_init:
  1220. return rc;
  1221. }
  1222. module_init(xen_blkif_init);
  1223. MODULE_LICENSE("Dual BSD/GPL");
  1224. MODULE_ALIAS("xen-backend:vbd");