blkback.c 44 KB

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