xen-blkfront.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647
  1. /*
  2. * blkfront.c
  3. *
  4. * XenLinux virtual block device driver.
  5. *
  6. * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
  7. * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
  8. * Copyright (c) 2004, Christian Limpach
  9. * Copyright (c) 2004, Andrew Warfield
  10. * Copyright (c) 2005, Christopher Clark
  11. * Copyright (c) 2005, XenSource Ltd
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation; or, when distributed
  16. * separately from the Linux kernel or incorporated into other
  17. * software packages, subject to the following license:
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this source file (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use, copy, modify,
  22. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so, subject to
  24. * the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  35. * IN THE SOFTWARE.
  36. */
  37. #include <linux/interrupt.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/blk-mq.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/cdrom.h>
  42. #include <linux/module.h>
  43. #include <linux/slab.h>
  44. #include <linux/major.h>
  45. #include <linux/mutex.h>
  46. #include <linux/scatterlist.h>
  47. #include <linux/bitmap.h>
  48. #include <linux/list.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/sched/mm.h>
  51. #include <xen/xen.h>
  52. #include <xen/xenbus.h>
  53. #include <xen/grant_table.h>
  54. #include <xen/events.h>
  55. #include <xen/page.h>
  56. #include <xen/platform_pci.h>
  57. #include <xen/interface/grant_table.h>
  58. #include <xen/interface/io/blkif.h>
  59. #include <xen/interface/io/protocols.h>
  60. #include <asm/xen/hypervisor.h>
  61. /*
  62. * The minimal size of segment supported by the block framework is PAGE_SIZE.
  63. * When Linux is using a different page size than Xen, it may not be possible
  64. * to put all the data in a single segment.
  65. * This can happen when the backend doesn't support indirect descriptor and
  66. * therefore the maximum amount of data that a request can carry is
  67. * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
  68. *
  69. * Note that we only support one extra request. So the Linux page size
  70. * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
  71. * 88KB.
  72. */
  73. #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
  74. enum blkif_state {
  75. BLKIF_STATE_DISCONNECTED,
  76. BLKIF_STATE_CONNECTED,
  77. BLKIF_STATE_SUSPENDED,
  78. BLKIF_STATE_ERROR,
  79. };
  80. struct grant {
  81. grant_ref_t gref;
  82. struct page *page;
  83. struct list_head node;
  84. };
  85. enum blk_req_status {
  86. REQ_PROCESSING,
  87. REQ_WAITING,
  88. REQ_DONE,
  89. REQ_ERROR,
  90. REQ_EOPNOTSUPP,
  91. };
  92. struct blk_shadow {
  93. struct blkif_request req;
  94. struct request *request;
  95. struct grant **grants_used;
  96. struct grant **indirect_grants;
  97. struct scatterlist *sg;
  98. unsigned int num_sg;
  99. enum blk_req_status status;
  100. #define NO_ASSOCIATED_ID ~0UL
  101. /*
  102. * Id of the sibling if we ever need 2 requests when handling a
  103. * block I/O request
  104. */
  105. unsigned long associated_id;
  106. };
  107. struct blkif_req {
  108. blk_status_t error;
  109. };
  110. static inline struct blkif_req *blkif_req(struct request *rq)
  111. {
  112. return blk_mq_rq_to_pdu(rq);
  113. }
  114. static DEFINE_MUTEX(blkfront_mutex);
  115. static const struct block_device_operations xlvbd_block_fops;
  116. static struct delayed_work blkfront_work;
  117. static LIST_HEAD(info_list);
  118. /*
  119. * Maximum number of segments in indirect requests, the actual value used by
  120. * the frontend driver is the minimum of this value and the value provided
  121. * by the backend driver.
  122. */
  123. static unsigned int xen_blkif_max_segments = 32;
  124. module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
  125. MODULE_PARM_DESC(max_indirect_segments,
  126. "Maximum amount of segments in indirect requests (default is 32)");
  127. static unsigned int xen_blkif_max_queues = 4;
  128. module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
  129. MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
  130. /*
  131. * Maximum order of pages to be used for the shared ring between front and
  132. * backend, 4KB page granularity is used.
  133. */
  134. static unsigned int xen_blkif_max_ring_order;
  135. module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
  136. MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
  137. static bool __read_mostly xen_blkif_trusted = true;
  138. module_param_named(trusted, xen_blkif_trusted, bool, 0644);
  139. MODULE_PARM_DESC(trusted, "Is the backend trusted");
  140. #define BLK_RING_SIZE(info) \
  141. __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
  142. /*
  143. * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
  144. * characters are enough. Define to 20 to keep consistent with backend.
  145. */
  146. #define RINGREF_NAME_LEN (20)
  147. /*
  148. * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
  149. */
  150. #define QUEUE_NAME_LEN (17)
  151. /*
  152. * Per-ring info.
  153. * Every blkfront device can associate with one or more blkfront_ring_info,
  154. * depending on how many hardware queues/rings to be used.
  155. */
  156. struct blkfront_ring_info {
  157. /* Lock to protect data in every ring buffer. */
  158. spinlock_t ring_lock;
  159. struct blkif_front_ring ring;
  160. unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
  161. unsigned int evtchn, irq;
  162. struct work_struct work;
  163. struct gnttab_free_callback callback;
  164. struct list_head indirect_pages;
  165. struct list_head grants;
  166. unsigned int persistent_gnts_c;
  167. unsigned long shadow_free;
  168. struct blkfront_info *dev_info;
  169. struct blk_shadow shadow[];
  170. };
  171. /*
  172. * We have one of these per vbd, whether ide, scsi or 'other'. They
  173. * hang in private_data off the gendisk structure. We may end up
  174. * putting all kinds of interesting stuff here :-)
  175. */
  176. struct blkfront_info
  177. {
  178. struct mutex mutex;
  179. struct xenbus_device *xbdev;
  180. struct gendisk *gd;
  181. u16 sector_size;
  182. unsigned int physical_sector_size;
  183. unsigned long vdisk_info;
  184. int vdevice;
  185. blkif_vdev_t handle;
  186. enum blkif_state connected;
  187. /* Number of pages per ring buffer. */
  188. unsigned int nr_ring_pages;
  189. struct request_queue *rq;
  190. unsigned int feature_flush:1;
  191. unsigned int feature_fua:1;
  192. unsigned int feature_discard:1;
  193. unsigned int feature_secdiscard:1;
  194. /* Connect-time cached feature_persistent parameter */
  195. unsigned int feature_persistent_parm:1;
  196. /* Persistent grants feature negotiation result */
  197. unsigned int feature_persistent:1;
  198. unsigned int bounce:1;
  199. unsigned int discard_granularity;
  200. unsigned int discard_alignment;
  201. /* Number of 4KB segments handled */
  202. unsigned int max_indirect_segments;
  203. int is_ready;
  204. struct blk_mq_tag_set tag_set;
  205. struct blkfront_ring_info *rinfo;
  206. unsigned int nr_rings;
  207. unsigned int rinfo_size;
  208. /* Save uncomplete reqs and bios for migration. */
  209. struct list_head requests;
  210. struct bio_list bio_list;
  211. struct list_head info_list;
  212. };
  213. static unsigned int nr_minors;
  214. static unsigned long *minors;
  215. static DEFINE_SPINLOCK(minor_lock);
  216. #define PARTS_PER_DISK 16
  217. #define PARTS_PER_EXT_DISK 256
  218. #define BLKIF_MAJOR(dev) ((dev)>>8)
  219. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  220. #define EXT_SHIFT 28
  221. #define EXTENDED (1<<EXT_SHIFT)
  222. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  223. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  224. #define EMULATED_HD_DISK_MINOR_OFFSET (0)
  225. #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
  226. #define EMULATED_SD_DISK_MINOR_OFFSET (0)
  227. #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
  228. #define DEV_NAME "xvd" /* name in /dev */
  229. /*
  230. * Grants are always the same size as a Xen page (i.e 4KB).
  231. * A physical segment is always the same size as a Linux page.
  232. * Number of grants per physical segment
  233. */
  234. #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
  235. #define GRANTS_PER_INDIRECT_FRAME \
  236. (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
  237. #define INDIRECT_GREFS(_grants) \
  238. DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
  239. static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
  240. static void blkfront_gather_backend_features(struct blkfront_info *info);
  241. static int negotiate_mq(struct blkfront_info *info);
  242. #define for_each_rinfo(info, ptr, idx) \
  243. for ((ptr) = (info)->rinfo, (idx) = 0; \
  244. (idx) < (info)->nr_rings; \
  245. (idx)++, (ptr) = (void *)(ptr) + (info)->rinfo_size)
  246. static inline struct blkfront_ring_info *
  247. get_rinfo(const struct blkfront_info *info, unsigned int i)
  248. {
  249. BUG_ON(i >= info->nr_rings);
  250. return (void *)info->rinfo + i * info->rinfo_size;
  251. }
  252. static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
  253. {
  254. unsigned long free = rinfo->shadow_free;
  255. BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
  256. rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
  257. rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
  258. return free;
  259. }
  260. static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
  261. unsigned long id)
  262. {
  263. if (rinfo->shadow[id].req.u.rw.id != id)
  264. return -EINVAL;
  265. if (rinfo->shadow[id].request == NULL)
  266. return -EINVAL;
  267. rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
  268. rinfo->shadow[id].request = NULL;
  269. rinfo->shadow_free = id;
  270. return 0;
  271. }
  272. static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
  273. {
  274. struct blkfront_info *info = rinfo->dev_info;
  275. struct page *granted_page;
  276. struct grant *gnt_list_entry, *n;
  277. int i = 0;
  278. while (i < num) {
  279. gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
  280. if (!gnt_list_entry)
  281. goto out_of_memory;
  282. if (info->bounce) {
  283. granted_page = alloc_page(GFP_NOIO | __GFP_ZERO);
  284. if (!granted_page) {
  285. kfree(gnt_list_entry);
  286. goto out_of_memory;
  287. }
  288. gnt_list_entry->page = granted_page;
  289. }
  290. gnt_list_entry->gref = INVALID_GRANT_REF;
  291. list_add(&gnt_list_entry->node, &rinfo->grants);
  292. i++;
  293. }
  294. return 0;
  295. out_of_memory:
  296. list_for_each_entry_safe(gnt_list_entry, n,
  297. &rinfo->grants, node) {
  298. list_del(&gnt_list_entry->node);
  299. if (info->bounce)
  300. __free_page(gnt_list_entry->page);
  301. kfree(gnt_list_entry);
  302. i--;
  303. }
  304. BUG_ON(i != 0);
  305. return -ENOMEM;
  306. }
  307. static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
  308. {
  309. struct grant *gnt_list_entry;
  310. BUG_ON(list_empty(&rinfo->grants));
  311. gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
  312. node);
  313. list_del(&gnt_list_entry->node);
  314. if (gnt_list_entry->gref != INVALID_GRANT_REF)
  315. rinfo->persistent_gnts_c--;
  316. return gnt_list_entry;
  317. }
  318. static inline void grant_foreign_access(const struct grant *gnt_list_entry,
  319. const struct blkfront_info *info)
  320. {
  321. gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
  322. info->xbdev->otherend_id,
  323. gnt_list_entry->page,
  324. 0);
  325. }
  326. static struct grant *get_grant(grant_ref_t *gref_head,
  327. unsigned long gfn,
  328. struct blkfront_ring_info *rinfo)
  329. {
  330. struct grant *gnt_list_entry = get_free_grant(rinfo);
  331. struct blkfront_info *info = rinfo->dev_info;
  332. if (gnt_list_entry->gref != INVALID_GRANT_REF)
  333. return gnt_list_entry;
  334. /* Assign a gref to this page */
  335. gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
  336. BUG_ON(gnt_list_entry->gref == -ENOSPC);
  337. if (info->bounce)
  338. grant_foreign_access(gnt_list_entry, info);
  339. else {
  340. /* Grant access to the GFN passed by the caller */
  341. gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
  342. info->xbdev->otherend_id,
  343. gfn, 0);
  344. }
  345. return gnt_list_entry;
  346. }
  347. static struct grant *get_indirect_grant(grant_ref_t *gref_head,
  348. struct blkfront_ring_info *rinfo)
  349. {
  350. struct grant *gnt_list_entry = get_free_grant(rinfo);
  351. struct blkfront_info *info = rinfo->dev_info;
  352. if (gnt_list_entry->gref != INVALID_GRANT_REF)
  353. return gnt_list_entry;
  354. /* Assign a gref to this page */
  355. gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
  356. BUG_ON(gnt_list_entry->gref == -ENOSPC);
  357. if (!info->bounce) {
  358. struct page *indirect_page;
  359. /* Fetch a pre-allocated page to use for indirect grefs */
  360. BUG_ON(list_empty(&rinfo->indirect_pages));
  361. indirect_page = list_first_entry(&rinfo->indirect_pages,
  362. struct page, lru);
  363. list_del(&indirect_page->lru);
  364. gnt_list_entry->page = indirect_page;
  365. }
  366. grant_foreign_access(gnt_list_entry, info);
  367. return gnt_list_entry;
  368. }
  369. static const char *op_name(int op)
  370. {
  371. static const char *const names[] = {
  372. [BLKIF_OP_READ] = "read",
  373. [BLKIF_OP_WRITE] = "write",
  374. [BLKIF_OP_WRITE_BARRIER] = "barrier",
  375. [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
  376. [BLKIF_OP_DISCARD] = "discard" };
  377. if (op < 0 || op >= ARRAY_SIZE(names))
  378. return "unknown";
  379. if (!names[op])
  380. return "reserved";
  381. return names[op];
  382. }
  383. static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
  384. {
  385. unsigned int end = minor + nr;
  386. int rc;
  387. if (end > nr_minors) {
  388. unsigned long *bitmap, *old;
  389. bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
  390. GFP_KERNEL);
  391. if (bitmap == NULL)
  392. return -ENOMEM;
  393. spin_lock(&minor_lock);
  394. if (end > nr_minors) {
  395. old = minors;
  396. memcpy(bitmap, minors,
  397. BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
  398. minors = bitmap;
  399. nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
  400. } else
  401. old = bitmap;
  402. spin_unlock(&minor_lock);
  403. kfree(old);
  404. }
  405. spin_lock(&minor_lock);
  406. if (find_next_bit(minors, end, minor) >= end) {
  407. bitmap_set(minors, minor, nr);
  408. rc = 0;
  409. } else
  410. rc = -EBUSY;
  411. spin_unlock(&minor_lock);
  412. return rc;
  413. }
  414. static void xlbd_release_minors(unsigned int minor, unsigned int nr)
  415. {
  416. unsigned int end = minor + nr;
  417. BUG_ON(end > nr_minors);
  418. spin_lock(&minor_lock);
  419. bitmap_clear(minors, minor, nr);
  420. spin_unlock(&minor_lock);
  421. }
  422. static void blkif_restart_queue_callback(void *arg)
  423. {
  424. struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
  425. schedule_work(&rinfo->work);
  426. }
  427. static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
  428. {
  429. /* We don't have real geometry info, but let's at least return
  430. values consistent with the size of the device */
  431. sector_t nsect = get_capacity(bd->bd_disk);
  432. sector_t cylinders = nsect;
  433. hg->heads = 0xff;
  434. hg->sectors = 0x3f;
  435. sector_div(cylinders, hg->heads * hg->sectors);
  436. hg->cylinders = cylinders;
  437. if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
  438. hg->cylinders = 0xffff;
  439. return 0;
  440. }
  441. static int blkif_ioctl(struct block_device *bdev, blk_mode_t mode,
  442. unsigned command, unsigned long argument)
  443. {
  444. struct blkfront_info *info = bdev->bd_disk->private_data;
  445. int i;
  446. switch (command) {
  447. case CDROMMULTISESSION:
  448. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  449. if (put_user(0, (char __user *)(argument + i)))
  450. return -EFAULT;
  451. return 0;
  452. case CDROM_GET_CAPABILITY:
  453. if (!(info->vdisk_info & VDISK_CDROM))
  454. return -EINVAL;
  455. return 0;
  456. default:
  457. return -EINVAL;
  458. }
  459. }
  460. static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
  461. struct request *req,
  462. struct blkif_request **ring_req)
  463. {
  464. unsigned long id;
  465. *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
  466. rinfo->ring.req_prod_pvt++;
  467. id = get_id_from_freelist(rinfo);
  468. rinfo->shadow[id].request = req;
  469. rinfo->shadow[id].status = REQ_PROCESSING;
  470. rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
  471. rinfo->shadow[id].req.u.rw.id = id;
  472. return id;
  473. }
  474. static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
  475. {
  476. struct blkfront_info *info = rinfo->dev_info;
  477. struct blkif_request *ring_req, *final_ring_req;
  478. unsigned long id;
  479. /* Fill out a communications ring structure. */
  480. id = blkif_ring_get_request(rinfo, req, &final_ring_req);
  481. ring_req = &rinfo->shadow[id].req;
  482. ring_req->operation = BLKIF_OP_DISCARD;
  483. ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
  484. ring_req->u.discard.id = id;
  485. ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
  486. if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
  487. ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
  488. else
  489. ring_req->u.discard.flag = 0;
  490. /* Copy the request to the ring page. */
  491. *final_ring_req = *ring_req;
  492. rinfo->shadow[id].status = REQ_WAITING;
  493. return 0;
  494. }
  495. struct setup_rw_req {
  496. unsigned int grant_idx;
  497. struct blkif_request_segment *segments;
  498. struct blkfront_ring_info *rinfo;
  499. struct blkif_request *ring_req;
  500. grant_ref_t gref_head;
  501. unsigned int id;
  502. /* Only used when persistent grant is used and it's a write request */
  503. bool need_copy;
  504. unsigned int bvec_off;
  505. char *bvec_data;
  506. bool require_extra_req;
  507. struct blkif_request *extra_ring_req;
  508. };
  509. static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
  510. unsigned int len, void *data)
  511. {
  512. struct setup_rw_req *setup = data;
  513. int n, ref;
  514. struct grant *gnt_list_entry;
  515. unsigned int fsect, lsect;
  516. /* Convenient aliases */
  517. unsigned int grant_idx = setup->grant_idx;
  518. struct blkif_request *ring_req = setup->ring_req;
  519. struct blkfront_ring_info *rinfo = setup->rinfo;
  520. /*
  521. * We always use the shadow of the first request to store the list
  522. * of grant associated to the block I/O request. This made the
  523. * completion more easy to handle even if the block I/O request is
  524. * split.
  525. */
  526. struct blk_shadow *shadow = &rinfo->shadow[setup->id];
  527. if (unlikely(setup->require_extra_req &&
  528. grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  529. /*
  530. * We are using the second request, setup grant_idx
  531. * to be the index of the segment array.
  532. */
  533. grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
  534. ring_req = setup->extra_ring_req;
  535. }
  536. if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
  537. (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
  538. if (setup->segments)
  539. kunmap_atomic(setup->segments);
  540. n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
  541. gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
  542. shadow->indirect_grants[n] = gnt_list_entry;
  543. setup->segments = kmap_atomic(gnt_list_entry->page);
  544. ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
  545. }
  546. gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
  547. ref = gnt_list_entry->gref;
  548. /*
  549. * All the grants are stored in the shadow of the first
  550. * request. Therefore we have to use the global index.
  551. */
  552. shadow->grants_used[setup->grant_idx] = gnt_list_entry;
  553. if (setup->need_copy) {
  554. void *shared_data;
  555. shared_data = kmap_atomic(gnt_list_entry->page);
  556. /*
  557. * this does not wipe data stored outside the
  558. * range sg->offset..sg->offset+sg->length.
  559. * Therefore, blkback *could* see data from
  560. * previous requests. This is OK as long as
  561. * persistent grants are shared with just one
  562. * domain. It may need refactoring if this
  563. * changes
  564. */
  565. memcpy(shared_data + offset,
  566. setup->bvec_data + setup->bvec_off,
  567. len);
  568. kunmap_atomic(shared_data);
  569. setup->bvec_off += len;
  570. }
  571. fsect = offset >> 9;
  572. lsect = fsect + (len >> 9) - 1;
  573. if (ring_req->operation != BLKIF_OP_INDIRECT) {
  574. ring_req->u.rw.seg[grant_idx] =
  575. (struct blkif_request_segment) {
  576. .gref = ref,
  577. .first_sect = fsect,
  578. .last_sect = lsect };
  579. } else {
  580. setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
  581. (struct blkif_request_segment) {
  582. .gref = ref,
  583. .first_sect = fsect,
  584. .last_sect = lsect };
  585. }
  586. (setup->grant_idx)++;
  587. }
  588. static void blkif_setup_extra_req(struct blkif_request *first,
  589. struct blkif_request *second)
  590. {
  591. uint16_t nr_segments = first->u.rw.nr_segments;
  592. /*
  593. * The second request is only present when the first request uses
  594. * all its segments. It's always the continuity of the first one.
  595. */
  596. first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  597. second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
  598. second->u.rw.sector_number = first->u.rw.sector_number +
  599. (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
  600. second->u.rw.handle = first->u.rw.handle;
  601. second->operation = first->operation;
  602. }
  603. static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
  604. {
  605. struct blkfront_info *info = rinfo->dev_info;
  606. struct blkif_request *ring_req, *extra_ring_req = NULL;
  607. struct blkif_request *final_ring_req, *final_extra_ring_req = NULL;
  608. unsigned long id, extra_id = NO_ASSOCIATED_ID;
  609. bool require_extra_req = false;
  610. int i;
  611. struct setup_rw_req setup = {
  612. .grant_idx = 0,
  613. .segments = NULL,
  614. .rinfo = rinfo,
  615. .need_copy = rq_data_dir(req) && info->bounce,
  616. };
  617. /*
  618. * Used to store if we are able to queue the request by just using
  619. * existing persistent grants, or if we have to get new grants,
  620. * as there are not sufficiently many free.
  621. */
  622. bool new_persistent_gnts = false;
  623. struct scatterlist *sg;
  624. int num_sg, max_grefs, num_grant;
  625. max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
  626. if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
  627. /*
  628. * If we are using indirect segments we need to account
  629. * for the indirect grefs used in the request.
  630. */
  631. max_grefs += INDIRECT_GREFS(max_grefs);
  632. /* Check if we have enough persistent grants to allocate a requests */
  633. if (rinfo->persistent_gnts_c < max_grefs) {
  634. new_persistent_gnts = true;
  635. if (gnttab_alloc_grant_references(
  636. max_grefs - rinfo->persistent_gnts_c,
  637. &setup.gref_head) < 0) {
  638. gnttab_request_free_callback(
  639. &rinfo->callback,
  640. blkif_restart_queue_callback,
  641. rinfo,
  642. max_grefs - rinfo->persistent_gnts_c);
  643. return 1;
  644. }
  645. }
  646. /* Fill out a communications ring structure. */
  647. id = blkif_ring_get_request(rinfo, req, &final_ring_req);
  648. ring_req = &rinfo->shadow[id].req;
  649. num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
  650. num_grant = 0;
  651. /* Calculate the number of grant used */
  652. for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
  653. num_grant += gnttab_count_grant(sg->offset, sg->length);
  654. require_extra_req = info->max_indirect_segments == 0 &&
  655. num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
  656. BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
  657. rinfo->shadow[id].num_sg = num_sg;
  658. if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
  659. likely(!require_extra_req)) {
  660. /*
  661. * The indirect operation can only be a BLKIF_OP_READ or
  662. * BLKIF_OP_WRITE
  663. */
  664. BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
  665. ring_req->operation = BLKIF_OP_INDIRECT;
  666. ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
  667. BLKIF_OP_WRITE : BLKIF_OP_READ;
  668. ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
  669. ring_req->u.indirect.handle = info->handle;
  670. ring_req->u.indirect.nr_segments = num_grant;
  671. } else {
  672. ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
  673. ring_req->u.rw.handle = info->handle;
  674. ring_req->operation = rq_data_dir(req) ?
  675. BLKIF_OP_WRITE : BLKIF_OP_READ;
  676. if (req_op(req) == REQ_OP_FLUSH ||
  677. (req_op(req) == REQ_OP_WRITE && (req->cmd_flags & REQ_FUA))) {
  678. /*
  679. * Ideally we can do an unordered flush-to-disk.
  680. * In case the backend onlysupports barriers, use that.
  681. * A barrier request a superset of FUA, so we can
  682. * implement it the same way. (It's also a FLUSH+FUA,
  683. * since it is guaranteed ordered WRT previous writes.)
  684. *
  685. * Note that can end up here with a FUA write and the
  686. * flags cleared. This happens when the flag was
  687. * run-time disabled after a failing I/O, and we'll
  688. * simplify submit it as a normal write.
  689. */
  690. if (info->feature_flush && info->feature_fua)
  691. ring_req->operation =
  692. BLKIF_OP_WRITE_BARRIER;
  693. else if (info->feature_flush)
  694. ring_req->operation =
  695. BLKIF_OP_FLUSH_DISKCACHE;
  696. }
  697. ring_req->u.rw.nr_segments = num_grant;
  698. if (unlikely(require_extra_req)) {
  699. extra_id = blkif_ring_get_request(rinfo, req,
  700. &final_extra_ring_req);
  701. extra_ring_req = &rinfo->shadow[extra_id].req;
  702. /*
  703. * Only the first request contains the scatter-gather
  704. * list.
  705. */
  706. rinfo->shadow[extra_id].num_sg = 0;
  707. blkif_setup_extra_req(ring_req, extra_ring_req);
  708. /* Link the 2 requests together */
  709. rinfo->shadow[extra_id].associated_id = id;
  710. rinfo->shadow[id].associated_id = extra_id;
  711. }
  712. }
  713. setup.ring_req = ring_req;
  714. setup.id = id;
  715. setup.require_extra_req = require_extra_req;
  716. if (unlikely(require_extra_req))
  717. setup.extra_ring_req = extra_ring_req;
  718. for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
  719. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  720. if (setup.need_copy) {
  721. setup.bvec_off = sg->offset;
  722. setup.bvec_data = kmap_atomic(sg_page(sg));
  723. }
  724. gnttab_foreach_grant_in_range(sg_page(sg),
  725. sg->offset,
  726. sg->length,
  727. blkif_setup_rw_req_grant,
  728. &setup);
  729. if (setup.need_copy)
  730. kunmap_atomic(setup.bvec_data);
  731. }
  732. if (setup.segments)
  733. kunmap_atomic(setup.segments);
  734. /* Copy request(s) to the ring page. */
  735. *final_ring_req = *ring_req;
  736. rinfo->shadow[id].status = REQ_WAITING;
  737. if (unlikely(require_extra_req)) {
  738. *final_extra_ring_req = *extra_ring_req;
  739. rinfo->shadow[extra_id].status = REQ_WAITING;
  740. }
  741. if (new_persistent_gnts)
  742. gnttab_free_grant_references(setup.gref_head);
  743. return 0;
  744. }
  745. /*
  746. * Generate a Xen blkfront IO request from a blk layer request. Reads
  747. * and writes are handled as expected.
  748. *
  749. * @req: a request struct
  750. */
  751. static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
  752. {
  753. if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
  754. return 1;
  755. if (unlikely(req_op(req) == REQ_OP_DISCARD ||
  756. req_op(req) == REQ_OP_SECURE_ERASE))
  757. return blkif_queue_discard_req(req, rinfo);
  758. else
  759. return blkif_queue_rw_req(req, rinfo);
  760. }
  761. static inline void flush_requests(struct blkfront_ring_info *rinfo)
  762. {
  763. int notify;
  764. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
  765. if (notify)
  766. notify_remote_via_irq(rinfo->irq);
  767. }
  768. static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
  769. const struct blk_mq_queue_data *qd)
  770. {
  771. unsigned long flags;
  772. int qid = hctx->queue_num;
  773. struct blkfront_info *info = hctx->queue->queuedata;
  774. struct blkfront_ring_info *rinfo = NULL;
  775. rinfo = get_rinfo(info, qid);
  776. blk_mq_start_request(qd->rq);
  777. spin_lock_irqsave(&rinfo->ring_lock, flags);
  778. /*
  779. * Check if the backend actually supports flushes.
  780. *
  781. * While the block layer won't send us flushes if we don't claim to
  782. * support them, the Xen protocol allows the backend to revoke support
  783. * at any time. That is of course a really bad idea and dangerous, but
  784. * has been allowed for 10+ years. In that case we simply clear the
  785. * flags, and directly return here for an empty flush and ignore the
  786. * FUA flag later on.
  787. */
  788. if (unlikely(req_op(qd->rq) == REQ_OP_FLUSH && !info->feature_flush))
  789. goto complete;
  790. if (RING_FULL(&rinfo->ring))
  791. goto out_busy;
  792. if (blkif_queue_request(qd->rq, rinfo))
  793. goto out_busy;
  794. flush_requests(rinfo);
  795. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  796. return BLK_STS_OK;
  797. out_busy:
  798. blk_mq_stop_hw_queue(hctx);
  799. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  800. return BLK_STS_DEV_RESOURCE;
  801. complete:
  802. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  803. blk_mq_end_request(qd->rq, BLK_STS_OK);
  804. return BLK_STS_OK;
  805. }
  806. static void blkif_complete_rq(struct request *rq)
  807. {
  808. blk_mq_end_request(rq, blkif_req(rq)->error);
  809. }
  810. static const struct blk_mq_ops blkfront_mq_ops = {
  811. .queue_rq = blkif_queue_rq,
  812. .complete = blkif_complete_rq,
  813. };
  814. static void blkif_set_queue_limits(const struct blkfront_info *info,
  815. struct queue_limits *lim)
  816. {
  817. unsigned int segments = info->max_indirect_segments ? :
  818. BLKIF_MAX_SEGMENTS_PER_REQUEST;
  819. if (info->feature_discard) {
  820. lim->max_hw_discard_sectors = UINT_MAX;
  821. if (info->discard_granularity)
  822. lim->discard_granularity = info->discard_granularity;
  823. lim->discard_alignment = info->discard_alignment;
  824. if (info->feature_secdiscard)
  825. lim->max_secure_erase_sectors = UINT_MAX;
  826. }
  827. if (info->feature_flush) {
  828. lim->features |= BLK_FEAT_WRITE_CACHE;
  829. if (info->feature_fua)
  830. lim->features |= BLK_FEAT_FUA;
  831. }
  832. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  833. lim->logical_block_size = info->sector_size;
  834. lim->physical_block_size = info->physical_sector_size;
  835. lim->max_hw_sectors = (segments * XEN_PAGE_SIZE) / 512;
  836. /* Each segment in a request is up to an aligned page in size. */
  837. lim->seg_boundary_mask = PAGE_SIZE - 1;
  838. lim->max_segment_size = PAGE_SIZE;
  839. /* Ensure a merged request will fit in a single I/O ring slot. */
  840. lim->max_segments = segments / GRANTS_PER_PSEG;
  841. /* Make sure buffer addresses are sector-aligned. */
  842. lim->dma_alignment = 511;
  843. }
  844. static const char *flush_info(struct blkfront_info *info)
  845. {
  846. if (info->feature_flush && info->feature_fua)
  847. return "barrier: enabled;";
  848. else if (info->feature_flush)
  849. return "flush diskcache: enabled;";
  850. else
  851. return "barrier or flush: disabled;";
  852. }
  853. static void xlvbd_flush(struct blkfront_info *info)
  854. {
  855. pr_info("blkfront: %s: %s %s %s %s %s %s %s\n",
  856. info->gd->disk_name, flush_info(info),
  857. "persistent grants:", info->feature_persistent ?
  858. "enabled;" : "disabled;", "indirect descriptors:",
  859. info->max_indirect_segments ? "enabled;" : "disabled;",
  860. "bounce buffer:", info->bounce ? "enabled" : "disabled;");
  861. }
  862. static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
  863. {
  864. int major;
  865. major = BLKIF_MAJOR(vdevice);
  866. *minor = BLKIF_MINOR(vdevice);
  867. switch (major) {
  868. case XEN_IDE0_MAJOR:
  869. *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
  870. *minor = ((*minor / 64) * PARTS_PER_DISK) +
  871. EMULATED_HD_DISK_MINOR_OFFSET;
  872. break;
  873. case XEN_IDE1_MAJOR:
  874. *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
  875. *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
  876. EMULATED_HD_DISK_MINOR_OFFSET;
  877. break;
  878. case XEN_SCSI_DISK0_MAJOR:
  879. *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
  880. *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
  881. break;
  882. case XEN_SCSI_DISK1_MAJOR:
  883. case XEN_SCSI_DISK2_MAJOR:
  884. case XEN_SCSI_DISK3_MAJOR:
  885. case XEN_SCSI_DISK4_MAJOR:
  886. case XEN_SCSI_DISK5_MAJOR:
  887. case XEN_SCSI_DISK6_MAJOR:
  888. case XEN_SCSI_DISK7_MAJOR:
  889. *offset = (*minor / PARTS_PER_DISK) +
  890. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
  891. EMULATED_SD_DISK_NAME_OFFSET;
  892. *minor = *minor +
  893. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
  894. EMULATED_SD_DISK_MINOR_OFFSET;
  895. break;
  896. case XEN_SCSI_DISK8_MAJOR:
  897. case XEN_SCSI_DISK9_MAJOR:
  898. case XEN_SCSI_DISK10_MAJOR:
  899. case XEN_SCSI_DISK11_MAJOR:
  900. case XEN_SCSI_DISK12_MAJOR:
  901. case XEN_SCSI_DISK13_MAJOR:
  902. case XEN_SCSI_DISK14_MAJOR:
  903. case XEN_SCSI_DISK15_MAJOR:
  904. *offset = (*minor / PARTS_PER_DISK) +
  905. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
  906. EMULATED_SD_DISK_NAME_OFFSET;
  907. *minor = *minor +
  908. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
  909. EMULATED_SD_DISK_MINOR_OFFSET;
  910. break;
  911. case XENVBD_MAJOR:
  912. *offset = *minor / PARTS_PER_DISK;
  913. break;
  914. default:
  915. printk(KERN_WARNING "blkfront: your disk configuration is "
  916. "incorrect, please use an xvd device instead\n");
  917. return -ENODEV;
  918. }
  919. return 0;
  920. }
  921. static char *encode_disk_name(char *ptr, unsigned int n)
  922. {
  923. if (n >= 26)
  924. ptr = encode_disk_name(ptr, n / 26 - 1);
  925. *ptr = 'a' + n % 26;
  926. return ptr + 1;
  927. }
  928. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  929. struct blkfront_info *info)
  930. {
  931. struct queue_limits lim = {};
  932. struct gendisk *gd;
  933. int nr_minors = 1;
  934. int err;
  935. unsigned int offset;
  936. int minor;
  937. int nr_parts;
  938. char *ptr;
  939. BUG_ON(info->gd != NULL);
  940. BUG_ON(info->rq != NULL);
  941. if ((info->vdevice>>EXT_SHIFT) > 1) {
  942. /* this is above the extended range; something is wrong */
  943. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  944. return -ENODEV;
  945. }
  946. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  947. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  948. if (err)
  949. return err;
  950. nr_parts = PARTS_PER_DISK;
  951. } else {
  952. minor = BLKIF_MINOR_EXT(info->vdevice);
  953. nr_parts = PARTS_PER_EXT_DISK;
  954. offset = minor / nr_parts;
  955. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  956. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  957. "emulated IDE disks,\n\t choose an xvd device name"
  958. "from xvde on\n", info->vdevice);
  959. }
  960. if (minor >> MINORBITS) {
  961. pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
  962. info->vdevice, minor);
  963. return -ENODEV;
  964. }
  965. if ((minor % nr_parts) == 0)
  966. nr_minors = nr_parts;
  967. err = xlbd_reserve_minors(minor, nr_minors);
  968. if (err)
  969. return err;
  970. memset(&info->tag_set, 0, sizeof(info->tag_set));
  971. info->tag_set.ops = &blkfront_mq_ops;
  972. info->tag_set.nr_hw_queues = info->nr_rings;
  973. if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
  974. /*
  975. * When indirect descriptior is not supported, the I/O request
  976. * will be split between multiple request in the ring.
  977. * To avoid problems when sending the request, divide by
  978. * 2 the depth of the queue.
  979. */
  980. info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
  981. } else
  982. info->tag_set.queue_depth = BLK_RING_SIZE(info);
  983. info->tag_set.numa_node = NUMA_NO_NODE;
  984. info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  985. info->tag_set.cmd_size = sizeof(struct blkif_req);
  986. info->tag_set.driver_data = info;
  987. err = blk_mq_alloc_tag_set(&info->tag_set);
  988. if (err)
  989. goto out_release_minors;
  990. blkif_set_queue_limits(info, &lim);
  991. gd = blk_mq_alloc_disk(&info->tag_set, &lim, info);
  992. if (IS_ERR(gd)) {
  993. err = PTR_ERR(gd);
  994. goto out_free_tag_set;
  995. }
  996. strcpy(gd->disk_name, DEV_NAME);
  997. ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
  998. BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
  999. if (nr_minors > 1)
  1000. *ptr = 0;
  1001. else
  1002. snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
  1003. "%d", minor & (nr_parts - 1));
  1004. gd->major = XENVBD_MAJOR;
  1005. gd->first_minor = minor;
  1006. gd->minors = nr_minors;
  1007. gd->fops = &xlvbd_block_fops;
  1008. gd->private_data = info;
  1009. set_capacity(gd, capacity);
  1010. info->rq = gd->queue;
  1011. info->gd = gd;
  1012. xlvbd_flush(info);
  1013. if (info->vdisk_info & VDISK_READONLY)
  1014. set_disk_ro(gd, 1);
  1015. if (info->vdisk_info & VDISK_REMOVABLE)
  1016. gd->flags |= GENHD_FL_REMOVABLE;
  1017. return 0;
  1018. out_free_tag_set:
  1019. blk_mq_free_tag_set(&info->tag_set);
  1020. out_release_minors:
  1021. xlbd_release_minors(minor, nr_minors);
  1022. return err;
  1023. }
  1024. /* Already hold rinfo->ring_lock. */
  1025. static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
  1026. {
  1027. if (!RING_FULL(&rinfo->ring))
  1028. blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
  1029. }
  1030. static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
  1031. {
  1032. unsigned long flags;
  1033. spin_lock_irqsave(&rinfo->ring_lock, flags);
  1034. kick_pending_request_queues_locked(rinfo);
  1035. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  1036. }
  1037. static void blkif_restart_queue(struct work_struct *work)
  1038. {
  1039. struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
  1040. if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
  1041. kick_pending_request_queues(rinfo);
  1042. }
  1043. static void blkif_free_ring(struct blkfront_ring_info *rinfo)
  1044. {
  1045. struct grant *persistent_gnt, *n;
  1046. struct blkfront_info *info = rinfo->dev_info;
  1047. int i, j, segs;
  1048. /*
  1049. * Remove indirect pages, this only happens when using indirect
  1050. * descriptors but not persistent grants
  1051. */
  1052. if (!list_empty(&rinfo->indirect_pages)) {
  1053. struct page *indirect_page, *n;
  1054. BUG_ON(info->bounce);
  1055. list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
  1056. list_del(&indirect_page->lru);
  1057. __free_page(indirect_page);
  1058. }
  1059. }
  1060. /* Remove all persistent grants. */
  1061. if (!list_empty(&rinfo->grants)) {
  1062. list_for_each_entry_safe(persistent_gnt, n,
  1063. &rinfo->grants, node) {
  1064. list_del(&persistent_gnt->node);
  1065. if (persistent_gnt->gref != INVALID_GRANT_REF) {
  1066. gnttab_end_foreign_access(persistent_gnt->gref,
  1067. NULL);
  1068. rinfo->persistent_gnts_c--;
  1069. }
  1070. if (info->bounce)
  1071. __free_page(persistent_gnt->page);
  1072. kfree(persistent_gnt);
  1073. }
  1074. }
  1075. BUG_ON(rinfo->persistent_gnts_c != 0);
  1076. for (i = 0; i < BLK_RING_SIZE(info); i++) {
  1077. /*
  1078. * Clear persistent grants present in requests already
  1079. * on the shared ring
  1080. */
  1081. if (!rinfo->shadow[i].request)
  1082. goto free_shadow;
  1083. segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
  1084. rinfo->shadow[i].req.u.indirect.nr_segments :
  1085. rinfo->shadow[i].req.u.rw.nr_segments;
  1086. for (j = 0; j < segs; j++) {
  1087. persistent_gnt = rinfo->shadow[i].grants_used[j];
  1088. gnttab_end_foreign_access(persistent_gnt->gref, NULL);
  1089. if (info->bounce)
  1090. __free_page(persistent_gnt->page);
  1091. kfree(persistent_gnt);
  1092. }
  1093. if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
  1094. /*
  1095. * If this is not an indirect operation don't try to
  1096. * free indirect segments
  1097. */
  1098. goto free_shadow;
  1099. for (j = 0; j < INDIRECT_GREFS(segs); j++) {
  1100. persistent_gnt = rinfo->shadow[i].indirect_grants[j];
  1101. gnttab_end_foreign_access(persistent_gnt->gref, NULL);
  1102. __free_page(persistent_gnt->page);
  1103. kfree(persistent_gnt);
  1104. }
  1105. free_shadow:
  1106. kvfree(rinfo->shadow[i].grants_used);
  1107. rinfo->shadow[i].grants_used = NULL;
  1108. kvfree(rinfo->shadow[i].indirect_grants);
  1109. rinfo->shadow[i].indirect_grants = NULL;
  1110. kvfree(rinfo->shadow[i].sg);
  1111. rinfo->shadow[i].sg = NULL;
  1112. }
  1113. /* No more gnttab callback work. */
  1114. gnttab_cancel_free_callback(&rinfo->callback);
  1115. /* Flush gnttab callback work. Must be done with no locks held. */
  1116. flush_work(&rinfo->work);
  1117. /* Free resources associated with old device channel. */
  1118. xenbus_teardown_ring((void **)&rinfo->ring.sring, info->nr_ring_pages,
  1119. rinfo->ring_ref);
  1120. if (rinfo->irq)
  1121. unbind_from_irqhandler(rinfo->irq, rinfo);
  1122. rinfo->evtchn = rinfo->irq = 0;
  1123. }
  1124. static void blkif_free(struct blkfront_info *info, int suspend)
  1125. {
  1126. unsigned int i;
  1127. struct blkfront_ring_info *rinfo;
  1128. /* Prevent new requests being issued until we fix things up. */
  1129. info->connected = suspend ?
  1130. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  1131. /* No more blkif_request(). */
  1132. if (info->rq)
  1133. blk_mq_stop_hw_queues(info->rq);
  1134. for_each_rinfo(info, rinfo, i)
  1135. blkif_free_ring(rinfo);
  1136. kvfree(info->rinfo);
  1137. info->rinfo = NULL;
  1138. info->nr_rings = 0;
  1139. }
  1140. struct copy_from_grant {
  1141. const struct blk_shadow *s;
  1142. unsigned int grant_idx;
  1143. unsigned int bvec_offset;
  1144. char *bvec_data;
  1145. };
  1146. static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
  1147. unsigned int len, void *data)
  1148. {
  1149. struct copy_from_grant *info = data;
  1150. char *shared_data;
  1151. /* Convenient aliases */
  1152. const struct blk_shadow *s = info->s;
  1153. shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
  1154. memcpy(info->bvec_data + info->bvec_offset,
  1155. shared_data + offset, len);
  1156. info->bvec_offset += len;
  1157. info->grant_idx++;
  1158. kunmap_atomic(shared_data);
  1159. }
  1160. static enum blk_req_status blkif_rsp_to_req_status(int rsp)
  1161. {
  1162. switch (rsp)
  1163. {
  1164. case BLKIF_RSP_OKAY:
  1165. return REQ_DONE;
  1166. case BLKIF_RSP_EOPNOTSUPP:
  1167. return REQ_EOPNOTSUPP;
  1168. case BLKIF_RSP_ERROR:
  1169. default:
  1170. return REQ_ERROR;
  1171. }
  1172. }
  1173. /*
  1174. * Get the final status of the block request based on two ring response
  1175. */
  1176. static int blkif_get_final_status(enum blk_req_status s1,
  1177. enum blk_req_status s2)
  1178. {
  1179. BUG_ON(s1 < REQ_DONE);
  1180. BUG_ON(s2 < REQ_DONE);
  1181. if (s1 == REQ_ERROR || s2 == REQ_ERROR)
  1182. return BLKIF_RSP_ERROR;
  1183. else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
  1184. return BLKIF_RSP_EOPNOTSUPP;
  1185. return BLKIF_RSP_OKAY;
  1186. }
  1187. /*
  1188. * Return values:
  1189. * 1 response processed.
  1190. * 0 missing further responses.
  1191. * -1 error while processing.
  1192. */
  1193. static int blkif_completion(unsigned long *id,
  1194. struct blkfront_ring_info *rinfo,
  1195. struct blkif_response *bret)
  1196. {
  1197. int i = 0;
  1198. struct scatterlist *sg;
  1199. int num_sg, num_grant;
  1200. struct blkfront_info *info = rinfo->dev_info;
  1201. struct blk_shadow *s = &rinfo->shadow[*id];
  1202. struct copy_from_grant data = {
  1203. .grant_idx = 0,
  1204. };
  1205. num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
  1206. s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
  1207. /* The I/O request may be split in two. */
  1208. if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
  1209. struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
  1210. /* Keep the status of the current response in shadow. */
  1211. s->status = blkif_rsp_to_req_status(bret->status);
  1212. /* Wait the second response if not yet here. */
  1213. if (s2->status < REQ_DONE)
  1214. return 0;
  1215. bret->status = blkif_get_final_status(s->status,
  1216. s2->status);
  1217. /*
  1218. * All the grants is stored in the first shadow in order
  1219. * to make the completion code simpler.
  1220. */
  1221. num_grant += s2->req.u.rw.nr_segments;
  1222. /*
  1223. * The two responses may not come in order. Only the
  1224. * first request will store the scatter-gather list.
  1225. */
  1226. if (s2->num_sg != 0) {
  1227. /* Update "id" with the ID of the first response. */
  1228. *id = s->associated_id;
  1229. s = s2;
  1230. }
  1231. /*
  1232. * We don't need anymore the second request, so recycling
  1233. * it now.
  1234. */
  1235. if (add_id_to_freelist(rinfo, s->associated_id))
  1236. WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
  1237. info->gd->disk_name, s->associated_id);
  1238. }
  1239. data.s = s;
  1240. num_sg = s->num_sg;
  1241. if (bret->operation == BLKIF_OP_READ && info->bounce) {
  1242. for_each_sg(s->sg, sg, num_sg, i) {
  1243. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  1244. data.bvec_offset = sg->offset;
  1245. data.bvec_data = kmap_atomic(sg_page(sg));
  1246. gnttab_foreach_grant_in_range(sg_page(sg),
  1247. sg->offset,
  1248. sg->length,
  1249. blkif_copy_from_grant,
  1250. &data);
  1251. kunmap_atomic(data.bvec_data);
  1252. }
  1253. }
  1254. /* Add the persistent grant into the list of free grants */
  1255. for (i = 0; i < num_grant; i++) {
  1256. if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) {
  1257. /*
  1258. * If the grant is still mapped by the backend (the
  1259. * backend has chosen to make this grant persistent)
  1260. * we add it at the head of the list, so it will be
  1261. * reused first.
  1262. */
  1263. if (!info->feature_persistent) {
  1264. pr_alert("backed has not unmapped grant: %u\n",
  1265. s->grants_used[i]->gref);
  1266. return -1;
  1267. }
  1268. list_add(&s->grants_used[i]->node, &rinfo->grants);
  1269. rinfo->persistent_gnts_c++;
  1270. } else {
  1271. /*
  1272. * If the grant is not mapped by the backend we add it
  1273. * to the tail of the list, so it will not be picked
  1274. * again unless we run out of persistent grants.
  1275. */
  1276. s->grants_used[i]->gref = INVALID_GRANT_REF;
  1277. list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
  1278. }
  1279. }
  1280. if (s->req.operation == BLKIF_OP_INDIRECT) {
  1281. for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
  1282. if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) {
  1283. if (!info->feature_persistent) {
  1284. pr_alert("backed has not unmapped grant: %u\n",
  1285. s->indirect_grants[i]->gref);
  1286. return -1;
  1287. }
  1288. list_add(&s->indirect_grants[i]->node, &rinfo->grants);
  1289. rinfo->persistent_gnts_c++;
  1290. } else {
  1291. struct page *indirect_page;
  1292. /*
  1293. * Add the used indirect page back to the list of
  1294. * available pages for indirect grefs.
  1295. */
  1296. if (!info->bounce) {
  1297. indirect_page = s->indirect_grants[i]->page;
  1298. list_add(&indirect_page->lru, &rinfo->indirect_pages);
  1299. }
  1300. s->indirect_grants[i]->gref = INVALID_GRANT_REF;
  1301. list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
  1302. }
  1303. }
  1304. }
  1305. return 1;
  1306. }
  1307. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  1308. {
  1309. struct request *req;
  1310. struct blkif_response bret;
  1311. RING_IDX i, rp;
  1312. unsigned long flags;
  1313. struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
  1314. struct blkfront_info *info = rinfo->dev_info;
  1315. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  1316. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  1317. xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
  1318. return IRQ_HANDLED;
  1319. }
  1320. spin_lock_irqsave(&rinfo->ring_lock, flags);
  1321. again:
  1322. rp = READ_ONCE(rinfo->ring.sring->rsp_prod);
  1323. virt_rmb(); /* Ensure we see queued responses up to 'rp'. */
  1324. if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) {
  1325. pr_alert("%s: illegal number of responses %u\n",
  1326. info->gd->disk_name, rp - rinfo->ring.rsp_cons);
  1327. goto err;
  1328. }
  1329. for (i = rinfo->ring.rsp_cons; i != rp; i++) {
  1330. unsigned long id;
  1331. unsigned int op;
  1332. eoiflag = 0;
  1333. RING_COPY_RESPONSE(&rinfo->ring, i, &bret);
  1334. id = bret.id;
  1335. /*
  1336. * The backend has messed up and given us an id that we would
  1337. * never have given to it (we stamp it up to BLK_RING_SIZE -
  1338. * look in get_id_from_freelist.
  1339. */
  1340. if (id >= BLK_RING_SIZE(info)) {
  1341. pr_alert("%s: response has incorrect id (%ld)\n",
  1342. info->gd->disk_name, id);
  1343. goto err;
  1344. }
  1345. if (rinfo->shadow[id].status != REQ_WAITING) {
  1346. pr_alert("%s: response references no pending request\n",
  1347. info->gd->disk_name);
  1348. goto err;
  1349. }
  1350. rinfo->shadow[id].status = REQ_PROCESSING;
  1351. req = rinfo->shadow[id].request;
  1352. op = rinfo->shadow[id].req.operation;
  1353. if (op == BLKIF_OP_INDIRECT)
  1354. op = rinfo->shadow[id].req.u.indirect.indirect_op;
  1355. if (bret.operation != op) {
  1356. pr_alert("%s: response has wrong operation (%u instead of %u)\n",
  1357. info->gd->disk_name, bret.operation, op);
  1358. goto err;
  1359. }
  1360. if (bret.operation != BLKIF_OP_DISCARD) {
  1361. int ret;
  1362. /*
  1363. * We may need to wait for an extra response if the
  1364. * I/O request is split in 2
  1365. */
  1366. ret = blkif_completion(&id, rinfo, &bret);
  1367. if (!ret)
  1368. continue;
  1369. if (unlikely(ret < 0))
  1370. goto err;
  1371. }
  1372. if (add_id_to_freelist(rinfo, id)) {
  1373. WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
  1374. info->gd->disk_name, op_name(bret.operation), id);
  1375. continue;
  1376. }
  1377. if (bret.status == BLKIF_RSP_OKAY)
  1378. blkif_req(req)->error = BLK_STS_OK;
  1379. else
  1380. blkif_req(req)->error = BLK_STS_IOERR;
  1381. switch (bret.operation) {
  1382. case BLKIF_OP_DISCARD:
  1383. if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
  1384. struct request_queue *rq = info->rq;
  1385. pr_warn_ratelimited("blkfront: %s: %s op failed\n",
  1386. info->gd->disk_name, op_name(bret.operation));
  1387. blkif_req(req)->error = BLK_STS_NOTSUPP;
  1388. info->feature_discard = 0;
  1389. info->feature_secdiscard = 0;
  1390. blk_queue_disable_discard(rq);
  1391. blk_queue_disable_secure_erase(rq);
  1392. }
  1393. break;
  1394. case BLKIF_OP_FLUSH_DISKCACHE:
  1395. case BLKIF_OP_WRITE_BARRIER:
  1396. if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
  1397. pr_warn_ratelimited("blkfront: %s: %s op failed\n",
  1398. info->gd->disk_name, op_name(bret.operation));
  1399. blkif_req(req)->error = BLK_STS_NOTSUPP;
  1400. }
  1401. if (unlikely(bret.status == BLKIF_RSP_ERROR &&
  1402. rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
  1403. pr_warn_ratelimited("blkfront: %s: empty %s op failed\n",
  1404. info->gd->disk_name, op_name(bret.operation));
  1405. blkif_req(req)->error = BLK_STS_NOTSUPP;
  1406. }
  1407. if (unlikely(blkif_req(req)->error)) {
  1408. if (blkif_req(req)->error == BLK_STS_NOTSUPP)
  1409. blkif_req(req)->error = BLK_STS_OK;
  1410. info->feature_fua = 0;
  1411. info->feature_flush = 0;
  1412. }
  1413. fallthrough;
  1414. case BLKIF_OP_READ:
  1415. case BLKIF_OP_WRITE:
  1416. if (unlikely(bret.status != BLKIF_RSP_OKAY))
  1417. dev_dbg_ratelimited(&info->xbdev->dev,
  1418. "Bad return from blkdev data request: %#x\n",
  1419. bret.status);
  1420. break;
  1421. default:
  1422. BUG();
  1423. }
  1424. if (likely(!blk_should_fake_timeout(req->q)))
  1425. blk_mq_complete_request(req);
  1426. }
  1427. rinfo->ring.rsp_cons = i;
  1428. if (i != rinfo->ring.req_prod_pvt) {
  1429. int more_to_do;
  1430. RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
  1431. if (more_to_do)
  1432. goto again;
  1433. } else
  1434. rinfo->ring.sring->rsp_event = i + 1;
  1435. kick_pending_request_queues_locked(rinfo);
  1436. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  1437. xen_irq_lateeoi(irq, eoiflag);
  1438. return IRQ_HANDLED;
  1439. err:
  1440. info->connected = BLKIF_STATE_ERROR;
  1441. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  1442. /* No EOI in order to avoid further interrupts. */
  1443. pr_alert("%s disabled for further use\n", info->gd->disk_name);
  1444. return IRQ_HANDLED;
  1445. }
  1446. static int setup_blkring(struct xenbus_device *dev,
  1447. struct blkfront_ring_info *rinfo)
  1448. {
  1449. struct blkif_sring *sring;
  1450. int err;
  1451. struct blkfront_info *info = rinfo->dev_info;
  1452. unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
  1453. err = xenbus_setup_ring(dev, GFP_NOIO, (void **)&sring,
  1454. info->nr_ring_pages, rinfo->ring_ref);
  1455. if (err)
  1456. goto fail;
  1457. XEN_FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
  1458. err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
  1459. if (err)
  1460. goto fail;
  1461. err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt,
  1462. 0, "blkif", rinfo);
  1463. if (err <= 0) {
  1464. xenbus_dev_fatal(dev, err,
  1465. "bind_evtchn_to_irqhandler failed");
  1466. goto fail;
  1467. }
  1468. rinfo->irq = err;
  1469. return 0;
  1470. fail:
  1471. blkif_free(info, 0);
  1472. return err;
  1473. }
  1474. /*
  1475. * Write out per-ring/queue nodes including ring-ref and event-channel, and each
  1476. * ring buffer may have multi pages depending on ->nr_ring_pages.
  1477. */
  1478. static int write_per_ring_nodes(struct xenbus_transaction xbt,
  1479. struct blkfront_ring_info *rinfo, const char *dir)
  1480. {
  1481. int err;
  1482. unsigned int i;
  1483. const char *message = NULL;
  1484. struct blkfront_info *info = rinfo->dev_info;
  1485. if (info->nr_ring_pages == 1) {
  1486. err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
  1487. if (err) {
  1488. message = "writing ring-ref";
  1489. goto abort_transaction;
  1490. }
  1491. } else {
  1492. for (i = 0; i < info->nr_ring_pages; i++) {
  1493. char ring_ref_name[RINGREF_NAME_LEN];
  1494. snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
  1495. err = xenbus_printf(xbt, dir, ring_ref_name,
  1496. "%u", rinfo->ring_ref[i]);
  1497. if (err) {
  1498. message = "writing ring-ref";
  1499. goto abort_transaction;
  1500. }
  1501. }
  1502. }
  1503. err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
  1504. if (err) {
  1505. message = "writing event-channel";
  1506. goto abort_transaction;
  1507. }
  1508. return 0;
  1509. abort_transaction:
  1510. xenbus_transaction_end(xbt, 1);
  1511. if (message)
  1512. xenbus_dev_fatal(info->xbdev, err, "%s", message);
  1513. return err;
  1514. }
  1515. /* Enable the persistent grants feature. */
  1516. static bool feature_persistent = true;
  1517. module_param(feature_persistent, bool, 0644);
  1518. MODULE_PARM_DESC(feature_persistent,
  1519. "Enables the persistent grants feature");
  1520. /* Common code used when first setting up, and when resuming. */
  1521. static int talk_to_blkback(struct xenbus_device *dev,
  1522. struct blkfront_info *info)
  1523. {
  1524. const char *message = NULL;
  1525. struct xenbus_transaction xbt;
  1526. int err;
  1527. unsigned int i, max_page_order;
  1528. unsigned int ring_page_order;
  1529. struct blkfront_ring_info *rinfo;
  1530. if (!info)
  1531. return -ENODEV;
  1532. /* Check if backend is trusted. */
  1533. info->bounce = !xen_blkif_trusted ||
  1534. !xenbus_read_unsigned(dev->nodename, "trusted", 1);
  1535. max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
  1536. "max-ring-page-order", 0);
  1537. ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
  1538. info->nr_ring_pages = 1 << ring_page_order;
  1539. err = negotiate_mq(info);
  1540. if (err)
  1541. goto destroy_blkring;
  1542. for_each_rinfo(info, rinfo, i) {
  1543. /* Create shared ring, alloc event channel. */
  1544. err = setup_blkring(dev, rinfo);
  1545. if (err)
  1546. goto destroy_blkring;
  1547. }
  1548. again:
  1549. err = xenbus_transaction_start(&xbt);
  1550. if (err) {
  1551. xenbus_dev_fatal(dev, err, "starting transaction");
  1552. goto destroy_blkring;
  1553. }
  1554. if (info->nr_ring_pages > 1) {
  1555. err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
  1556. ring_page_order);
  1557. if (err) {
  1558. message = "writing ring-page-order";
  1559. goto abort_transaction;
  1560. }
  1561. }
  1562. /* We already got the number of queues/rings in _probe */
  1563. if (info->nr_rings == 1) {
  1564. err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename);
  1565. if (err)
  1566. goto destroy_blkring;
  1567. } else {
  1568. char *path;
  1569. size_t pathsize;
  1570. err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
  1571. info->nr_rings);
  1572. if (err) {
  1573. message = "writing multi-queue-num-queues";
  1574. goto abort_transaction;
  1575. }
  1576. pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
  1577. path = kmalloc(pathsize, GFP_KERNEL);
  1578. if (!path) {
  1579. err = -ENOMEM;
  1580. message = "ENOMEM while writing ring references";
  1581. goto abort_transaction;
  1582. }
  1583. for_each_rinfo(info, rinfo, i) {
  1584. memset(path, 0, pathsize);
  1585. snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
  1586. err = write_per_ring_nodes(xbt, rinfo, path);
  1587. if (err) {
  1588. kfree(path);
  1589. goto destroy_blkring;
  1590. }
  1591. }
  1592. kfree(path);
  1593. }
  1594. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  1595. XEN_IO_PROTO_ABI_NATIVE);
  1596. if (err) {
  1597. message = "writing protocol";
  1598. goto abort_transaction;
  1599. }
  1600. info->feature_persistent_parm = feature_persistent;
  1601. err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u",
  1602. info->feature_persistent_parm);
  1603. if (err)
  1604. dev_warn(&dev->dev,
  1605. "writing persistent grants feature to xenbus");
  1606. err = xenbus_transaction_end(xbt, 0);
  1607. if (err) {
  1608. if (err == -EAGAIN)
  1609. goto again;
  1610. xenbus_dev_fatal(dev, err, "completing transaction");
  1611. goto destroy_blkring;
  1612. }
  1613. for_each_rinfo(info, rinfo, i) {
  1614. unsigned int j;
  1615. for (j = 0; j < BLK_RING_SIZE(info); j++)
  1616. rinfo->shadow[j].req.u.rw.id = j + 1;
  1617. rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
  1618. }
  1619. xenbus_switch_state(dev, XenbusStateInitialised);
  1620. return 0;
  1621. abort_transaction:
  1622. xenbus_transaction_end(xbt, 1);
  1623. if (message)
  1624. xenbus_dev_fatal(dev, err, "%s", message);
  1625. destroy_blkring:
  1626. blkif_free(info, 0);
  1627. return err;
  1628. }
  1629. static int negotiate_mq(struct blkfront_info *info)
  1630. {
  1631. unsigned int backend_max_queues;
  1632. unsigned int i;
  1633. struct blkfront_ring_info *rinfo;
  1634. BUG_ON(info->nr_rings);
  1635. /* Check if backend supports multiple queues. */
  1636. backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
  1637. "multi-queue-max-queues", 1);
  1638. info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
  1639. /* We need at least one ring. */
  1640. if (!info->nr_rings)
  1641. info->nr_rings = 1;
  1642. info->rinfo_size = struct_size(info->rinfo, shadow,
  1643. BLK_RING_SIZE(info));
  1644. info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL);
  1645. if (!info->rinfo) {
  1646. xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
  1647. info->nr_rings = 0;
  1648. return -ENOMEM;
  1649. }
  1650. for_each_rinfo(info, rinfo, i) {
  1651. INIT_LIST_HEAD(&rinfo->indirect_pages);
  1652. INIT_LIST_HEAD(&rinfo->grants);
  1653. rinfo->dev_info = info;
  1654. INIT_WORK(&rinfo->work, blkif_restart_queue);
  1655. spin_lock_init(&rinfo->ring_lock);
  1656. }
  1657. return 0;
  1658. }
  1659. /*
  1660. * Entry point to this code when a new device is created. Allocate the basic
  1661. * structures and the ring buffer for communication with the backend, and
  1662. * inform the backend of the appropriate details for those. Switch to
  1663. * Initialised state.
  1664. */
  1665. static int blkfront_probe(struct xenbus_device *dev,
  1666. const struct xenbus_device_id *id)
  1667. {
  1668. int err, vdevice;
  1669. struct blkfront_info *info;
  1670. /* FIXME: Use dynamic device id if this is not set. */
  1671. err = xenbus_scanf(XBT_NIL, dev->nodename,
  1672. "virtual-device", "%i", &vdevice);
  1673. if (err != 1) {
  1674. /* go looking in the extended area instead */
  1675. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  1676. "%i", &vdevice);
  1677. if (err != 1) {
  1678. xenbus_dev_fatal(dev, err, "reading virtual-device");
  1679. return err;
  1680. }
  1681. }
  1682. if (xen_hvm_domain()) {
  1683. char *type;
  1684. int len;
  1685. /* no unplug has been done: do not hook devices != xen vbds */
  1686. if (xen_has_pv_and_legacy_disk_devices()) {
  1687. int major;
  1688. if (!VDEV_IS_EXTENDED(vdevice))
  1689. major = BLKIF_MAJOR(vdevice);
  1690. else
  1691. major = XENVBD_MAJOR;
  1692. if (major != XENVBD_MAJOR) {
  1693. printk(KERN_INFO
  1694. "%s: HVM does not support vbd %d as xen block device\n",
  1695. __func__, vdevice);
  1696. return -ENODEV;
  1697. }
  1698. }
  1699. /* do not create a PV cdrom device if we are an HVM guest */
  1700. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  1701. if (IS_ERR(type))
  1702. return -ENODEV;
  1703. if (strncmp(type, "cdrom", 5) == 0) {
  1704. kfree(type);
  1705. return -ENODEV;
  1706. }
  1707. kfree(type);
  1708. }
  1709. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1710. if (!info) {
  1711. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  1712. return -ENOMEM;
  1713. }
  1714. info->xbdev = dev;
  1715. mutex_init(&info->mutex);
  1716. info->vdevice = vdevice;
  1717. info->connected = BLKIF_STATE_DISCONNECTED;
  1718. /* Front end dir is a number, which is used as the id. */
  1719. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  1720. dev_set_drvdata(&dev->dev, info);
  1721. mutex_lock(&blkfront_mutex);
  1722. list_add(&info->info_list, &info_list);
  1723. mutex_unlock(&blkfront_mutex);
  1724. return 0;
  1725. }
  1726. static int blkif_recover(struct blkfront_info *info)
  1727. {
  1728. struct queue_limits lim;
  1729. unsigned int r_index;
  1730. struct request *req, *n;
  1731. int rc;
  1732. struct bio *bio;
  1733. struct blkfront_ring_info *rinfo;
  1734. lim = queue_limits_start_update(info->rq);
  1735. blkfront_gather_backend_features(info);
  1736. blkif_set_queue_limits(info, &lim);
  1737. rc = queue_limits_commit_update(info->rq, &lim);
  1738. if (rc)
  1739. return rc;
  1740. for_each_rinfo(info, rinfo, r_index) {
  1741. rc = blkfront_setup_indirect(rinfo);
  1742. if (rc)
  1743. return rc;
  1744. }
  1745. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1746. /* Now safe for us to use the shared ring */
  1747. info->connected = BLKIF_STATE_CONNECTED;
  1748. for_each_rinfo(info, rinfo, r_index) {
  1749. /* Kick any other new requests queued since we resumed */
  1750. kick_pending_request_queues(rinfo);
  1751. }
  1752. list_for_each_entry_safe(req, n, &info->requests, queuelist) {
  1753. /* Requeue pending requests (flush or discard) */
  1754. list_del_init(&req->queuelist);
  1755. BUG_ON(req->nr_phys_segments >
  1756. (info->max_indirect_segments ? :
  1757. BLKIF_MAX_SEGMENTS_PER_REQUEST));
  1758. blk_mq_requeue_request(req, false);
  1759. }
  1760. blk_mq_start_stopped_hw_queues(info->rq, true);
  1761. blk_mq_kick_requeue_list(info->rq);
  1762. while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
  1763. /* Traverse the list of pending bios and re-queue them */
  1764. submit_bio(bio);
  1765. }
  1766. return 0;
  1767. }
  1768. /*
  1769. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1770. * driver restart. We tear down our blkif structure and recreate it, but
  1771. * leave the device-layer structures intact so that this is transparent to the
  1772. * rest of the kernel.
  1773. */
  1774. static int blkfront_resume(struct xenbus_device *dev)
  1775. {
  1776. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1777. int err = 0;
  1778. unsigned int i, j;
  1779. struct blkfront_ring_info *rinfo;
  1780. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  1781. bio_list_init(&info->bio_list);
  1782. INIT_LIST_HEAD(&info->requests);
  1783. for_each_rinfo(info, rinfo, i) {
  1784. struct bio_list merge_bio;
  1785. struct blk_shadow *shadow = rinfo->shadow;
  1786. for (j = 0; j < BLK_RING_SIZE(info); j++) {
  1787. /* Not in use? */
  1788. if (!shadow[j].request)
  1789. continue;
  1790. /*
  1791. * Get the bios in the request so we can re-queue them.
  1792. */
  1793. if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
  1794. req_op(shadow[j].request) == REQ_OP_DISCARD ||
  1795. req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
  1796. shadow[j].request->cmd_flags & REQ_FUA) {
  1797. /*
  1798. * Flush operations don't contain bios, so
  1799. * we need to requeue the whole request
  1800. *
  1801. * XXX: but this doesn't make any sense for a
  1802. * write with the FUA flag set..
  1803. */
  1804. list_add(&shadow[j].request->queuelist, &info->requests);
  1805. continue;
  1806. }
  1807. merge_bio.head = shadow[j].request->bio;
  1808. merge_bio.tail = shadow[j].request->biotail;
  1809. bio_list_merge(&info->bio_list, &merge_bio);
  1810. shadow[j].request->bio = NULL;
  1811. blk_mq_end_request(shadow[j].request, BLK_STS_OK);
  1812. }
  1813. }
  1814. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  1815. err = talk_to_blkback(dev, info);
  1816. if (!err)
  1817. blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
  1818. /*
  1819. * We have to wait for the backend to switch to
  1820. * connected state, since we want to read which
  1821. * features it supports.
  1822. */
  1823. return err;
  1824. }
  1825. static void blkfront_closing(struct blkfront_info *info)
  1826. {
  1827. struct xenbus_device *xbdev = info->xbdev;
  1828. struct blkfront_ring_info *rinfo;
  1829. unsigned int i;
  1830. if (xbdev->state == XenbusStateClosing)
  1831. return;
  1832. /* No more blkif_request(). */
  1833. if (info->rq && info->gd) {
  1834. blk_mq_stop_hw_queues(info->rq);
  1835. blk_mark_disk_dead(info->gd);
  1836. }
  1837. for_each_rinfo(info, rinfo, i) {
  1838. /* No more gnttab callback work. */
  1839. gnttab_cancel_free_callback(&rinfo->callback);
  1840. /* Flush gnttab callback work. Must be done with no locks held. */
  1841. flush_work(&rinfo->work);
  1842. }
  1843. xenbus_frontend_closed(xbdev);
  1844. }
  1845. static void blkfront_setup_discard(struct blkfront_info *info)
  1846. {
  1847. info->feature_discard = 1;
  1848. info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend,
  1849. "discard-granularity",
  1850. 0);
  1851. info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend,
  1852. "discard-alignment", 0);
  1853. info->feature_secdiscard =
  1854. !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
  1855. 0);
  1856. }
  1857. static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
  1858. {
  1859. unsigned int psegs, grants, memflags;
  1860. int err, i;
  1861. struct blkfront_info *info = rinfo->dev_info;
  1862. memflags = memalloc_noio_save();
  1863. if (info->max_indirect_segments == 0) {
  1864. if (!HAS_EXTRA_REQ)
  1865. grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1866. else {
  1867. /*
  1868. * When an extra req is required, the maximum
  1869. * grants supported is related to the size of the
  1870. * Linux block segment.
  1871. */
  1872. grants = GRANTS_PER_PSEG;
  1873. }
  1874. }
  1875. else
  1876. grants = info->max_indirect_segments;
  1877. psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
  1878. err = fill_grant_buffer(rinfo,
  1879. (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
  1880. if (err)
  1881. goto out_of_memory;
  1882. if (!info->bounce && info->max_indirect_segments) {
  1883. /*
  1884. * We are using indirect descriptors but don't have a bounce
  1885. * buffer, we need to allocate a set of pages that can be
  1886. * used for mapping indirect grefs
  1887. */
  1888. int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
  1889. BUG_ON(!list_empty(&rinfo->indirect_pages));
  1890. for (i = 0; i < num; i++) {
  1891. struct page *indirect_page = alloc_page(GFP_KERNEL |
  1892. __GFP_ZERO);
  1893. if (!indirect_page)
  1894. goto out_of_memory;
  1895. list_add(&indirect_page->lru, &rinfo->indirect_pages);
  1896. }
  1897. }
  1898. for (i = 0; i < BLK_RING_SIZE(info); i++) {
  1899. rinfo->shadow[i].grants_used =
  1900. kvcalloc(grants,
  1901. sizeof(rinfo->shadow[i].grants_used[0]),
  1902. GFP_KERNEL);
  1903. rinfo->shadow[i].sg = kvcalloc(psegs,
  1904. sizeof(rinfo->shadow[i].sg[0]),
  1905. GFP_KERNEL);
  1906. if (info->max_indirect_segments)
  1907. rinfo->shadow[i].indirect_grants =
  1908. kvcalloc(INDIRECT_GREFS(grants),
  1909. sizeof(rinfo->shadow[i].indirect_grants[0]),
  1910. GFP_KERNEL);
  1911. if ((rinfo->shadow[i].grants_used == NULL) ||
  1912. (rinfo->shadow[i].sg == NULL) ||
  1913. (info->max_indirect_segments &&
  1914. (rinfo->shadow[i].indirect_grants == NULL)))
  1915. goto out_of_memory;
  1916. sg_init_table(rinfo->shadow[i].sg, psegs);
  1917. }
  1918. memalloc_noio_restore(memflags);
  1919. return 0;
  1920. out_of_memory:
  1921. for (i = 0; i < BLK_RING_SIZE(info); i++) {
  1922. kvfree(rinfo->shadow[i].grants_used);
  1923. rinfo->shadow[i].grants_used = NULL;
  1924. kvfree(rinfo->shadow[i].sg);
  1925. rinfo->shadow[i].sg = NULL;
  1926. kvfree(rinfo->shadow[i].indirect_grants);
  1927. rinfo->shadow[i].indirect_grants = NULL;
  1928. }
  1929. if (!list_empty(&rinfo->indirect_pages)) {
  1930. struct page *indirect_page, *n;
  1931. list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
  1932. list_del(&indirect_page->lru);
  1933. __free_page(indirect_page);
  1934. }
  1935. }
  1936. memalloc_noio_restore(memflags);
  1937. return -ENOMEM;
  1938. }
  1939. /*
  1940. * Gather all backend feature-*
  1941. */
  1942. static void blkfront_gather_backend_features(struct blkfront_info *info)
  1943. {
  1944. unsigned int indirect_segments;
  1945. info->feature_flush = 0;
  1946. info->feature_fua = 0;
  1947. /*
  1948. * If there's no "feature-barrier" defined, then it means
  1949. * we're dealing with a very old backend which writes
  1950. * synchronously; nothing to do.
  1951. *
  1952. * If there are barriers, then we use flush.
  1953. */
  1954. if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
  1955. info->feature_flush = 1;
  1956. info->feature_fua = 1;
  1957. }
  1958. /*
  1959. * And if there is "feature-flush-cache" use that above
  1960. * barriers.
  1961. */
  1962. if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
  1963. 0)) {
  1964. info->feature_flush = 1;
  1965. info->feature_fua = 0;
  1966. }
  1967. if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
  1968. blkfront_setup_discard(info);
  1969. if (info->feature_persistent_parm)
  1970. info->feature_persistent =
  1971. !!xenbus_read_unsigned(info->xbdev->otherend,
  1972. "feature-persistent", 0);
  1973. if (info->feature_persistent)
  1974. info->bounce = true;
  1975. indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
  1976. "feature-max-indirect-segments", 0);
  1977. if (indirect_segments > xen_blkif_max_segments)
  1978. indirect_segments = xen_blkif_max_segments;
  1979. if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
  1980. indirect_segments = 0;
  1981. info->max_indirect_segments = indirect_segments;
  1982. if (info->feature_persistent) {
  1983. mutex_lock(&blkfront_mutex);
  1984. schedule_delayed_work(&blkfront_work, HZ * 10);
  1985. mutex_unlock(&blkfront_mutex);
  1986. }
  1987. }
  1988. /*
  1989. * Invoked when the backend is finally 'ready' (and has told produced
  1990. * the details about the physical device - #sectors, size, etc).
  1991. */
  1992. static void blkfront_connect(struct blkfront_info *info)
  1993. {
  1994. unsigned long long sectors;
  1995. int err, i;
  1996. struct blkfront_ring_info *rinfo;
  1997. switch (info->connected) {
  1998. case BLKIF_STATE_CONNECTED:
  1999. /*
  2000. * Potentially, the back-end may be signalling
  2001. * a capacity change; update the capacity.
  2002. */
  2003. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  2004. "sectors", "%Lu", &sectors);
  2005. if (XENBUS_EXIST_ERR(err))
  2006. return;
  2007. printk(KERN_INFO "Setting capacity to %Lu\n",
  2008. sectors);
  2009. set_capacity_and_notify(info->gd, sectors);
  2010. return;
  2011. case BLKIF_STATE_SUSPENDED:
  2012. /*
  2013. * If we are recovering from suspension, we need to wait
  2014. * for the backend to announce it's features before
  2015. * reconnecting, at least we need to know if the backend
  2016. * supports indirect descriptors, and how many.
  2017. */
  2018. blkif_recover(info);
  2019. return;
  2020. default:
  2021. break;
  2022. }
  2023. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  2024. __func__, info->xbdev->otherend);
  2025. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  2026. "sectors", "%llu", &sectors,
  2027. "info", "%u", &info->vdisk_info,
  2028. "sector-size", "%lu", &info->sector_size,
  2029. NULL);
  2030. if (err) {
  2031. xenbus_dev_fatal(info->xbdev, err,
  2032. "reading backend fields at %s",
  2033. info->xbdev->otherend);
  2034. return;
  2035. }
  2036. /*
  2037. * physical-sector-size is a newer field, so old backends may not
  2038. * provide this. Assume physical sector size to be the same as
  2039. * sector_size in that case.
  2040. */
  2041. info->physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
  2042. "physical-sector-size",
  2043. info->sector_size);
  2044. blkfront_gather_backend_features(info);
  2045. for_each_rinfo(info, rinfo, i) {
  2046. err = blkfront_setup_indirect(rinfo);
  2047. if (err) {
  2048. xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
  2049. info->xbdev->otherend);
  2050. blkif_free(info, 0);
  2051. break;
  2052. }
  2053. }
  2054. err = xlvbd_alloc_gendisk(sectors, info);
  2055. if (err) {
  2056. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  2057. info->xbdev->otherend);
  2058. goto fail;
  2059. }
  2060. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  2061. /* Kick pending requests. */
  2062. info->connected = BLKIF_STATE_CONNECTED;
  2063. for_each_rinfo(info, rinfo, i)
  2064. kick_pending_request_queues(rinfo);
  2065. err = device_add_disk(&info->xbdev->dev, info->gd, NULL);
  2066. if (err) {
  2067. put_disk(info->gd);
  2068. blk_mq_free_tag_set(&info->tag_set);
  2069. info->rq = NULL;
  2070. goto fail;
  2071. }
  2072. info->is_ready = 1;
  2073. return;
  2074. fail:
  2075. blkif_free(info, 0);
  2076. return;
  2077. }
  2078. /*
  2079. * Callback received when the backend's state changes.
  2080. */
  2081. static void blkback_changed(struct xenbus_device *dev,
  2082. enum xenbus_state backend_state)
  2083. {
  2084. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  2085. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  2086. switch (backend_state) {
  2087. case XenbusStateInitWait:
  2088. if (dev->state != XenbusStateInitialising)
  2089. break;
  2090. if (talk_to_blkback(dev, info))
  2091. break;
  2092. break;
  2093. case XenbusStateInitialising:
  2094. case XenbusStateInitialised:
  2095. case XenbusStateReconfiguring:
  2096. case XenbusStateReconfigured:
  2097. case XenbusStateUnknown:
  2098. break;
  2099. case XenbusStateConnected:
  2100. /*
  2101. * talk_to_blkback sets state to XenbusStateInitialised
  2102. * and blkfront_connect sets it to XenbusStateConnected
  2103. * (if connection went OK).
  2104. *
  2105. * If the backend (or toolstack) decides to poke at backend
  2106. * state (and re-trigger the watch by setting the state repeatedly
  2107. * to XenbusStateConnected (4)) we need to deal with this.
  2108. * This is allowed as this is used to communicate to the guest
  2109. * that the size of disk has changed!
  2110. */
  2111. if ((dev->state != XenbusStateInitialised) &&
  2112. (dev->state != XenbusStateConnected)) {
  2113. if (talk_to_blkback(dev, info))
  2114. break;
  2115. }
  2116. blkfront_connect(info);
  2117. break;
  2118. case XenbusStateClosed:
  2119. if (dev->state == XenbusStateClosed)
  2120. break;
  2121. fallthrough;
  2122. case XenbusStateClosing:
  2123. blkfront_closing(info);
  2124. break;
  2125. }
  2126. }
  2127. static void blkfront_remove(struct xenbus_device *xbdev)
  2128. {
  2129. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  2130. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  2131. if (info->gd)
  2132. del_gendisk(info->gd);
  2133. mutex_lock(&blkfront_mutex);
  2134. list_del(&info->info_list);
  2135. mutex_unlock(&blkfront_mutex);
  2136. blkif_free(info, 0);
  2137. if (info->gd) {
  2138. xlbd_release_minors(info->gd->first_minor, info->gd->minors);
  2139. put_disk(info->gd);
  2140. blk_mq_free_tag_set(&info->tag_set);
  2141. }
  2142. kfree(info);
  2143. }
  2144. static int blkfront_is_ready(struct xenbus_device *dev)
  2145. {
  2146. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  2147. return info->is_ready && info->xbdev;
  2148. }
  2149. static const struct block_device_operations xlvbd_block_fops =
  2150. {
  2151. .owner = THIS_MODULE,
  2152. .getgeo = blkif_getgeo,
  2153. .ioctl = blkif_ioctl,
  2154. .compat_ioctl = blkdev_compat_ptr_ioctl,
  2155. };
  2156. static const struct xenbus_device_id blkfront_ids[] = {
  2157. { "vbd" },
  2158. { "" }
  2159. };
  2160. static struct xenbus_driver blkfront_driver = {
  2161. .ids = blkfront_ids,
  2162. .probe = blkfront_probe,
  2163. .remove = blkfront_remove,
  2164. .resume = blkfront_resume,
  2165. .otherend_changed = blkback_changed,
  2166. .is_ready = blkfront_is_ready,
  2167. };
  2168. static void purge_persistent_grants(struct blkfront_info *info)
  2169. {
  2170. unsigned int i;
  2171. unsigned long flags;
  2172. struct blkfront_ring_info *rinfo;
  2173. for_each_rinfo(info, rinfo, i) {
  2174. struct grant *gnt_list_entry, *tmp;
  2175. LIST_HEAD(grants);
  2176. spin_lock_irqsave(&rinfo->ring_lock, flags);
  2177. if (rinfo->persistent_gnts_c == 0) {
  2178. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  2179. continue;
  2180. }
  2181. list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
  2182. node) {
  2183. if (gnt_list_entry->gref == INVALID_GRANT_REF ||
  2184. !gnttab_try_end_foreign_access(gnt_list_entry->gref))
  2185. continue;
  2186. list_del(&gnt_list_entry->node);
  2187. rinfo->persistent_gnts_c--;
  2188. gnt_list_entry->gref = INVALID_GRANT_REF;
  2189. list_add_tail(&gnt_list_entry->node, &grants);
  2190. }
  2191. list_splice_tail(&grants, &rinfo->grants);
  2192. spin_unlock_irqrestore(&rinfo->ring_lock, flags);
  2193. }
  2194. }
  2195. static void blkfront_delay_work(struct work_struct *work)
  2196. {
  2197. struct blkfront_info *info;
  2198. bool need_schedule_work = false;
  2199. /*
  2200. * Note that when using bounce buffers but not persistent grants
  2201. * there's no need to run blkfront_delay_work because grants are
  2202. * revoked in blkif_completion or else an error is reported and the
  2203. * connection is closed.
  2204. */
  2205. mutex_lock(&blkfront_mutex);
  2206. list_for_each_entry(info, &info_list, info_list) {
  2207. if (info->feature_persistent) {
  2208. need_schedule_work = true;
  2209. mutex_lock(&info->mutex);
  2210. purge_persistent_grants(info);
  2211. mutex_unlock(&info->mutex);
  2212. }
  2213. }
  2214. if (need_schedule_work)
  2215. schedule_delayed_work(&blkfront_work, HZ * 10);
  2216. mutex_unlock(&blkfront_mutex);
  2217. }
  2218. static int __init xlblk_init(void)
  2219. {
  2220. int ret;
  2221. int nr_cpus = num_online_cpus();
  2222. if (!xen_domain())
  2223. return -ENODEV;
  2224. if (!xen_has_pv_disk_devices())
  2225. return -ENODEV;
  2226. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  2227. pr_warn("xen_blk: can't get major %d with name %s\n",
  2228. XENVBD_MAJOR, DEV_NAME);
  2229. return -ENODEV;
  2230. }
  2231. if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
  2232. xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  2233. if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
  2234. pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
  2235. xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
  2236. xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
  2237. }
  2238. if (xen_blkif_max_queues > nr_cpus) {
  2239. pr_info("Invalid max_queues (%d), will use default max: %d.\n",
  2240. xen_blkif_max_queues, nr_cpus);
  2241. xen_blkif_max_queues = nr_cpus;
  2242. }
  2243. INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
  2244. ret = xenbus_register_frontend(&blkfront_driver);
  2245. if (ret) {
  2246. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  2247. return ret;
  2248. }
  2249. return 0;
  2250. }
  2251. module_init(xlblk_init);
  2252. static void __exit xlblk_exit(void)
  2253. {
  2254. cancel_delayed_work_sync(&blkfront_work);
  2255. xenbus_unregister_driver(&blkfront_driver);
  2256. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  2257. kfree(minors);
  2258. }
  2259. module_exit(xlblk_exit);
  2260. MODULE_DESCRIPTION("Xen virtual block device frontend");
  2261. MODULE_LICENSE("GPL");
  2262. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  2263. MODULE_ALIAS("xen:vbd");
  2264. MODULE_ALIAS("xenblk");