xen-blkfront.c 73 KB

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