xen-blkfront.c 74 KB

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