xen-blkfront.c 73 KB

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