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