xen-blkfront.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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/hdreg.h>
  40. #include <linux/cdrom.h>
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include <linux/mutex.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/bitmap.h>
  46. #include <linux/list.h>
  47. #include <xen/xen.h>
  48. #include <xen/xenbus.h>
  49. #include <xen/grant_table.h>
  50. #include <xen/events.h>
  51. #include <xen/page.h>
  52. #include <xen/platform_pci.h>
  53. #include <xen/interface/grant_table.h>
  54. #include <xen/interface/io/blkif.h>
  55. #include <xen/interface/io/protocols.h>
  56. #include <asm/xen/hypervisor.h>
  57. enum blkif_state {
  58. BLKIF_STATE_DISCONNECTED,
  59. BLKIF_STATE_CONNECTED,
  60. BLKIF_STATE_SUSPENDED,
  61. };
  62. struct grant {
  63. grant_ref_t gref;
  64. unsigned long pfn;
  65. struct list_head node;
  66. };
  67. struct blk_shadow {
  68. struct blkif_request req;
  69. struct request *request;
  70. struct grant **grants_used;
  71. struct grant **indirect_grants;
  72. };
  73. struct split_bio {
  74. struct bio *bio;
  75. atomic_t pending;
  76. int err;
  77. };
  78. static DEFINE_MUTEX(blkfront_mutex);
  79. static const struct block_device_operations xlvbd_block_fops;
  80. /*
  81. * Maximum number of segments in indirect requests, the actual value used by
  82. * the frontend driver is the minimum of this value and the value provided
  83. * by the backend driver.
  84. */
  85. static unsigned int xen_blkif_max_segments = 32;
  86. #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
  87. /*
  88. * We have one of these per vbd, whether ide, scsi or 'other'. They
  89. * hang in private_data off the gendisk structure. We may end up
  90. * putting all kinds of interesting stuff here :-)
  91. */
  92. struct blkfront_info
  93. {
  94. spinlock_t io_lock;
  95. struct mutex mutex;
  96. struct xenbus_device *xbdev;
  97. struct gendisk *gd;
  98. int vdevice;
  99. blkif_vdev_t handle;
  100. enum blkif_state connected;
  101. int ring_ref;
  102. struct blkif_front_ring ring;
  103. struct scatterlist *sg;
  104. unsigned int evtchn, irq;
  105. struct request_queue *rq;
  106. struct work_struct work;
  107. struct gnttab_free_callback callback;
  108. struct blk_shadow shadow[BLK_RING_SIZE];
  109. struct list_head persistent_gnts;
  110. unsigned int persistent_gnts_c;
  111. unsigned long shadow_free;
  112. unsigned int feature_flush;
  113. unsigned int flush_op;
  114. unsigned int feature_discard:1;
  115. unsigned int feature_secdiscard:1;
  116. unsigned int discard_granularity;
  117. unsigned int discard_alignment;
  118. unsigned int feature_persistent:1;
  119. unsigned int max_indirect_segments;
  120. int is_ready;
  121. };
  122. static unsigned int nr_minors;
  123. static unsigned long *minors;
  124. static DEFINE_SPINLOCK(minor_lock);
  125. #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
  126. (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
  127. #define GRANT_INVALID_REF 0
  128. #define PARTS_PER_DISK 16
  129. #define PARTS_PER_EXT_DISK 256
  130. #define BLKIF_MAJOR(dev) ((dev)>>8)
  131. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  132. #define EXT_SHIFT 28
  133. #define EXTENDED (1<<EXT_SHIFT)
  134. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  135. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  136. #define EMULATED_HD_DISK_MINOR_OFFSET (0)
  137. #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
  138. #define EMULATED_SD_DISK_MINOR_OFFSET (0)
  139. #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
  140. #define DEV_NAME "xvd" /* name in /dev */
  141. #define SEGS_PER_INDIRECT_FRAME \
  142. (PAGE_SIZE/sizeof(struct blkif_request_segment_aligned))
  143. #define INDIRECT_GREFS(_segs) \
  144. ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
  145. static int blkfront_setup_indirect(struct blkfront_info *info);
  146. static int get_id_from_freelist(struct blkfront_info *info)
  147. {
  148. unsigned long free = info->shadow_free;
  149. BUG_ON(free >= BLK_RING_SIZE);
  150. info->shadow_free = info->shadow[free].req.u.rw.id;
  151. info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
  152. return free;
  153. }
  154. static int add_id_to_freelist(struct blkfront_info *info,
  155. unsigned long id)
  156. {
  157. if (info->shadow[id].req.u.rw.id != id)
  158. return -EINVAL;
  159. if (info->shadow[id].request == NULL)
  160. return -EINVAL;
  161. info->shadow[id].req.u.rw.id = info->shadow_free;
  162. info->shadow[id].request = NULL;
  163. info->shadow_free = id;
  164. return 0;
  165. }
  166. static int fill_grant_buffer(struct blkfront_info *info, int num)
  167. {
  168. struct page *granted_page;
  169. struct grant *gnt_list_entry, *n;
  170. int i = 0;
  171. while(i < num) {
  172. gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
  173. if (!gnt_list_entry)
  174. goto out_of_memory;
  175. granted_page = alloc_page(GFP_NOIO);
  176. if (!granted_page) {
  177. kfree(gnt_list_entry);
  178. goto out_of_memory;
  179. }
  180. gnt_list_entry->pfn = page_to_pfn(granted_page);
  181. gnt_list_entry->gref = GRANT_INVALID_REF;
  182. list_add(&gnt_list_entry->node, &info->persistent_gnts);
  183. i++;
  184. }
  185. return 0;
  186. out_of_memory:
  187. list_for_each_entry_safe(gnt_list_entry, n,
  188. &info->persistent_gnts, node) {
  189. list_del(&gnt_list_entry->node);
  190. __free_page(pfn_to_page(gnt_list_entry->pfn));
  191. kfree(gnt_list_entry);
  192. i--;
  193. }
  194. BUG_ON(i != 0);
  195. return -ENOMEM;
  196. }
  197. static struct grant *get_grant(grant_ref_t *gref_head,
  198. struct blkfront_info *info)
  199. {
  200. struct grant *gnt_list_entry;
  201. unsigned long buffer_mfn;
  202. BUG_ON(list_empty(&info->persistent_gnts));
  203. gnt_list_entry = list_first_entry(&info->persistent_gnts, struct grant,
  204. node);
  205. list_del(&gnt_list_entry->node);
  206. if (gnt_list_entry->gref != GRANT_INVALID_REF) {
  207. info->persistent_gnts_c--;
  208. return gnt_list_entry;
  209. }
  210. /* Assign a gref to this page */
  211. gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
  212. BUG_ON(gnt_list_entry->gref == -ENOSPC);
  213. buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
  214. gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
  215. info->xbdev->otherend_id,
  216. buffer_mfn, 0);
  217. return gnt_list_entry;
  218. }
  219. static const char *op_name(int op)
  220. {
  221. static const char *const names[] = {
  222. [BLKIF_OP_READ] = "read",
  223. [BLKIF_OP_WRITE] = "write",
  224. [BLKIF_OP_WRITE_BARRIER] = "barrier",
  225. [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
  226. [BLKIF_OP_DISCARD] = "discard" };
  227. if (op < 0 || op >= ARRAY_SIZE(names))
  228. return "unknown";
  229. if (!names[op])
  230. return "reserved";
  231. return names[op];
  232. }
  233. static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
  234. {
  235. unsigned int end = minor + nr;
  236. int rc;
  237. if (end > nr_minors) {
  238. unsigned long *bitmap, *old;
  239. bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
  240. GFP_KERNEL);
  241. if (bitmap == NULL)
  242. return -ENOMEM;
  243. spin_lock(&minor_lock);
  244. if (end > nr_minors) {
  245. old = minors;
  246. memcpy(bitmap, minors,
  247. BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
  248. minors = bitmap;
  249. nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
  250. } else
  251. old = bitmap;
  252. spin_unlock(&minor_lock);
  253. kfree(old);
  254. }
  255. spin_lock(&minor_lock);
  256. if (find_next_bit(minors, end, minor) >= end) {
  257. bitmap_set(minors, minor, nr);
  258. rc = 0;
  259. } else
  260. rc = -EBUSY;
  261. spin_unlock(&minor_lock);
  262. return rc;
  263. }
  264. static void xlbd_release_minors(unsigned int minor, unsigned int nr)
  265. {
  266. unsigned int end = minor + nr;
  267. BUG_ON(end > nr_minors);
  268. spin_lock(&minor_lock);
  269. bitmap_clear(minors, minor, nr);
  270. spin_unlock(&minor_lock);
  271. }
  272. static void blkif_restart_queue_callback(void *arg)
  273. {
  274. struct blkfront_info *info = (struct blkfront_info *)arg;
  275. schedule_work(&info->work);
  276. }
  277. static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
  278. {
  279. /* We don't have real geometry info, but let's at least return
  280. values consistent with the size of the device */
  281. sector_t nsect = get_capacity(bd->bd_disk);
  282. sector_t cylinders = nsect;
  283. hg->heads = 0xff;
  284. hg->sectors = 0x3f;
  285. sector_div(cylinders, hg->heads * hg->sectors);
  286. hg->cylinders = cylinders;
  287. if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
  288. hg->cylinders = 0xffff;
  289. return 0;
  290. }
  291. static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
  292. unsigned command, unsigned long argument)
  293. {
  294. struct blkfront_info *info = bdev->bd_disk->private_data;
  295. int i;
  296. dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
  297. command, (long)argument);
  298. switch (command) {
  299. case CDROMMULTISESSION:
  300. dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
  301. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  302. if (put_user(0, (char __user *)(argument + i)))
  303. return -EFAULT;
  304. return 0;
  305. case CDROM_GET_CAPABILITY: {
  306. struct gendisk *gd = info->gd;
  307. if (gd->flags & GENHD_FL_CD)
  308. return 0;
  309. return -EINVAL;
  310. }
  311. default:
  312. /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
  313. command);*/
  314. return -EINVAL; /* same return as native Linux */
  315. }
  316. return 0;
  317. }
  318. /*
  319. * Generate a Xen blkfront IO request from a blk layer request. Reads
  320. * and writes are handled as expected.
  321. *
  322. * @req: a request struct
  323. */
  324. static int blkif_queue_request(struct request *req)
  325. {
  326. struct blkfront_info *info = req->rq_disk->private_data;
  327. struct blkif_request *ring_req;
  328. unsigned long id;
  329. unsigned int fsect, lsect;
  330. int i, ref, n;
  331. struct blkif_request_segment_aligned *segments = NULL;
  332. /*
  333. * Used to store if we are able to queue the request by just using
  334. * existing persistent grants, or if we have to get new grants,
  335. * as there are not sufficiently many free.
  336. */
  337. bool new_persistent_gnts;
  338. grant_ref_t gref_head;
  339. struct grant *gnt_list_entry = NULL;
  340. struct scatterlist *sg;
  341. int nseg, max_grefs;
  342. if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
  343. return 1;
  344. max_grefs = info->max_indirect_segments ?
  345. info->max_indirect_segments +
  346. INDIRECT_GREFS(info->max_indirect_segments) :
  347. BLKIF_MAX_SEGMENTS_PER_REQUEST;
  348. /* Check if we have enough grants to allocate a requests */
  349. if (info->persistent_gnts_c < max_grefs) {
  350. new_persistent_gnts = 1;
  351. if (gnttab_alloc_grant_references(
  352. max_grefs - info->persistent_gnts_c,
  353. &gref_head) < 0) {
  354. gnttab_request_free_callback(
  355. &info->callback,
  356. blkif_restart_queue_callback,
  357. info,
  358. max_grefs);
  359. return 1;
  360. }
  361. } else
  362. new_persistent_gnts = 0;
  363. /* Fill out a communications ring structure. */
  364. ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  365. id = get_id_from_freelist(info);
  366. info->shadow[id].request = req;
  367. if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
  368. ring_req->operation = BLKIF_OP_DISCARD;
  369. ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
  370. ring_req->u.discard.id = id;
  371. ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
  372. if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
  373. ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
  374. else
  375. ring_req->u.discard.flag = 0;
  376. } else {
  377. BUG_ON(info->max_indirect_segments == 0 &&
  378. req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
  379. BUG_ON(info->max_indirect_segments &&
  380. req->nr_phys_segments > info->max_indirect_segments);
  381. nseg = blk_rq_map_sg(req->q, req, info->sg);
  382. ring_req->u.rw.id = id;
  383. if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  384. /*
  385. * The indirect operation can only be a BLKIF_OP_READ or
  386. * BLKIF_OP_WRITE
  387. */
  388. BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
  389. ring_req->operation = BLKIF_OP_INDIRECT;
  390. ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
  391. BLKIF_OP_WRITE : BLKIF_OP_READ;
  392. ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
  393. ring_req->u.indirect.handle = info->handle;
  394. ring_req->u.indirect.nr_segments = nseg;
  395. } else {
  396. ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
  397. ring_req->u.rw.handle = info->handle;
  398. ring_req->operation = rq_data_dir(req) ?
  399. BLKIF_OP_WRITE : BLKIF_OP_READ;
  400. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
  401. /*
  402. * Ideally we can do an unordered flush-to-disk. In case the
  403. * backend onlysupports barriers, use that. A barrier request
  404. * a superset of FUA, so we can implement it the same
  405. * way. (It's also a FLUSH+FUA, since it is
  406. * guaranteed ordered WRT previous writes.)
  407. */
  408. ring_req->operation = info->flush_op;
  409. }
  410. ring_req->u.rw.nr_segments = nseg;
  411. }
  412. for_each_sg(info->sg, sg, nseg, i) {
  413. fsect = sg->offset >> 9;
  414. lsect = fsect + (sg->length >> 9) - 1;
  415. if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
  416. (i % SEGS_PER_INDIRECT_FRAME == 0)) {
  417. if (segments)
  418. kunmap_atomic(segments);
  419. n = i / SEGS_PER_INDIRECT_FRAME;
  420. gnt_list_entry = get_grant(&gref_head, info);
  421. info->shadow[id].indirect_grants[n] = gnt_list_entry;
  422. segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
  423. ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
  424. }
  425. gnt_list_entry = get_grant(&gref_head, info);
  426. ref = gnt_list_entry->gref;
  427. info->shadow[id].grants_used[i] = gnt_list_entry;
  428. if (rq_data_dir(req)) {
  429. char *bvec_data;
  430. void *shared_data;
  431. BUG_ON(sg->offset + sg->length > PAGE_SIZE);
  432. shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
  433. bvec_data = kmap_atomic(sg_page(sg));
  434. /*
  435. * this does not wipe data stored outside the
  436. * range sg->offset..sg->offset+sg->length.
  437. * Therefore, blkback *could* see data from
  438. * previous requests. This is OK as long as
  439. * persistent grants are shared with just one
  440. * domain. It may need refactoring if this
  441. * changes
  442. */
  443. memcpy(shared_data + sg->offset,
  444. bvec_data + sg->offset,
  445. sg->length);
  446. kunmap_atomic(bvec_data);
  447. kunmap_atomic(shared_data);
  448. }
  449. if (ring_req->operation != BLKIF_OP_INDIRECT) {
  450. ring_req->u.rw.seg[i] =
  451. (struct blkif_request_segment) {
  452. .gref = ref,
  453. .first_sect = fsect,
  454. .last_sect = lsect };
  455. } else {
  456. n = i % SEGS_PER_INDIRECT_FRAME;
  457. segments[n] =
  458. (struct blkif_request_segment_aligned) {
  459. .gref = ref,
  460. .first_sect = fsect,
  461. .last_sect = lsect };
  462. }
  463. }
  464. if (segments)
  465. kunmap_atomic(segments);
  466. }
  467. info->ring.req_prod_pvt++;
  468. /* Keep a private copy so we can reissue requests when recovering. */
  469. info->shadow[id].req = *ring_req;
  470. if (new_persistent_gnts)
  471. gnttab_free_grant_references(gref_head);
  472. return 0;
  473. }
  474. static inline void flush_requests(struct blkfront_info *info)
  475. {
  476. int notify;
  477. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
  478. if (notify)
  479. notify_remote_via_irq(info->irq);
  480. }
  481. /*
  482. * do_blkif_request
  483. * read a block; request is in a request queue
  484. */
  485. static void do_blkif_request(struct request_queue *rq)
  486. {
  487. struct blkfront_info *info = NULL;
  488. struct request *req;
  489. int queued;
  490. pr_debug("Entered do_blkif_request\n");
  491. queued = 0;
  492. while ((req = blk_peek_request(rq)) != NULL) {
  493. info = req->rq_disk->private_data;
  494. if (RING_FULL(&info->ring))
  495. goto wait;
  496. blk_start_request(req);
  497. if ((req->cmd_type != REQ_TYPE_FS) ||
  498. ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
  499. !info->flush_op)) {
  500. __blk_end_request_all(req, -EIO);
  501. continue;
  502. }
  503. pr_debug("do_blk_req %p: cmd %p, sec %lx, "
  504. "(%u/%u) buffer:%p [%s]\n",
  505. req, req->cmd, (unsigned long)blk_rq_pos(req),
  506. blk_rq_cur_sectors(req), blk_rq_sectors(req),
  507. req->buffer, rq_data_dir(req) ? "write" : "read");
  508. if (blkif_queue_request(req)) {
  509. blk_requeue_request(rq, req);
  510. wait:
  511. /* Avoid pointless unplugs. */
  512. blk_stop_queue(rq);
  513. break;
  514. }
  515. queued++;
  516. }
  517. if (queued != 0)
  518. flush_requests(info);
  519. }
  520. static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
  521. unsigned int segments)
  522. {
  523. struct request_queue *rq;
  524. struct blkfront_info *info = gd->private_data;
  525. rq = blk_init_queue(do_blkif_request, &info->io_lock);
  526. if (rq == NULL)
  527. return -1;
  528. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
  529. if (info->feature_discard) {
  530. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
  531. blk_queue_max_discard_sectors(rq, get_capacity(gd));
  532. rq->limits.discard_granularity = info->discard_granularity;
  533. rq->limits.discard_alignment = info->discard_alignment;
  534. if (info->feature_secdiscard)
  535. queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
  536. }
  537. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  538. blk_queue_logical_block_size(rq, sector_size);
  539. blk_queue_max_hw_sectors(rq, 512);
  540. /* Each segment in a request is up to an aligned page in size. */
  541. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  542. blk_queue_max_segment_size(rq, PAGE_SIZE);
  543. /* Ensure a merged request will fit in a single I/O ring slot. */
  544. blk_queue_max_segments(rq, segments);
  545. /* Make sure buffer addresses are sector-aligned. */
  546. blk_queue_dma_alignment(rq, 511);
  547. /* Make sure we don't use bounce buffers. */
  548. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  549. gd->queue = rq;
  550. return 0;
  551. }
  552. static void xlvbd_flush(struct blkfront_info *info)
  553. {
  554. blk_queue_flush(info->rq, info->feature_flush);
  555. printk(KERN_INFO "blkfront: %s: %s: %s %s %s %s %s\n",
  556. info->gd->disk_name,
  557. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  558. "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
  559. "flush diskcache" : "barrier or flush"),
  560. info->feature_flush ? "enabled;" : "disabled;",
  561. "persistent grants:",
  562. info->feature_persistent ? "enabled;" : "disabled;",
  563. "indirect descriptors:",
  564. info->max_indirect_segments ? "enabled;" : "disabled;");
  565. }
  566. static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
  567. {
  568. int major;
  569. major = BLKIF_MAJOR(vdevice);
  570. *minor = BLKIF_MINOR(vdevice);
  571. switch (major) {
  572. case XEN_IDE0_MAJOR:
  573. *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
  574. *minor = ((*minor / 64) * PARTS_PER_DISK) +
  575. EMULATED_HD_DISK_MINOR_OFFSET;
  576. break;
  577. case XEN_IDE1_MAJOR:
  578. *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
  579. *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
  580. EMULATED_HD_DISK_MINOR_OFFSET;
  581. break;
  582. case XEN_SCSI_DISK0_MAJOR:
  583. *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
  584. *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
  585. break;
  586. case XEN_SCSI_DISK1_MAJOR:
  587. case XEN_SCSI_DISK2_MAJOR:
  588. case XEN_SCSI_DISK3_MAJOR:
  589. case XEN_SCSI_DISK4_MAJOR:
  590. case XEN_SCSI_DISK5_MAJOR:
  591. case XEN_SCSI_DISK6_MAJOR:
  592. case XEN_SCSI_DISK7_MAJOR:
  593. *offset = (*minor / PARTS_PER_DISK) +
  594. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
  595. EMULATED_SD_DISK_NAME_OFFSET;
  596. *minor = *minor +
  597. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
  598. EMULATED_SD_DISK_MINOR_OFFSET;
  599. break;
  600. case XEN_SCSI_DISK8_MAJOR:
  601. case XEN_SCSI_DISK9_MAJOR:
  602. case XEN_SCSI_DISK10_MAJOR:
  603. case XEN_SCSI_DISK11_MAJOR:
  604. case XEN_SCSI_DISK12_MAJOR:
  605. case XEN_SCSI_DISK13_MAJOR:
  606. case XEN_SCSI_DISK14_MAJOR:
  607. case XEN_SCSI_DISK15_MAJOR:
  608. *offset = (*minor / PARTS_PER_DISK) +
  609. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
  610. EMULATED_SD_DISK_NAME_OFFSET;
  611. *minor = *minor +
  612. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
  613. EMULATED_SD_DISK_MINOR_OFFSET;
  614. break;
  615. case XENVBD_MAJOR:
  616. *offset = *minor / PARTS_PER_DISK;
  617. break;
  618. default:
  619. printk(KERN_WARNING "blkfront: your disk configuration is "
  620. "incorrect, please use an xvd device instead\n");
  621. return -ENODEV;
  622. }
  623. return 0;
  624. }
  625. static char *encode_disk_name(char *ptr, unsigned int n)
  626. {
  627. if (n >= 26)
  628. ptr = encode_disk_name(ptr, n / 26 - 1);
  629. *ptr = 'a' + n % 26;
  630. return ptr + 1;
  631. }
  632. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  633. struct blkfront_info *info,
  634. u16 vdisk_info, u16 sector_size)
  635. {
  636. struct gendisk *gd;
  637. int nr_minors = 1;
  638. int err;
  639. unsigned int offset;
  640. int minor;
  641. int nr_parts;
  642. char *ptr;
  643. BUG_ON(info->gd != NULL);
  644. BUG_ON(info->rq != NULL);
  645. if ((info->vdevice>>EXT_SHIFT) > 1) {
  646. /* this is above the extended range; something is wrong */
  647. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  648. return -ENODEV;
  649. }
  650. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  651. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  652. if (err)
  653. return err;
  654. nr_parts = PARTS_PER_DISK;
  655. } else {
  656. minor = BLKIF_MINOR_EXT(info->vdevice);
  657. nr_parts = PARTS_PER_EXT_DISK;
  658. offset = minor / nr_parts;
  659. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  660. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  661. "emulated IDE disks,\n\t choose an xvd device name"
  662. "from xvde on\n", info->vdevice);
  663. }
  664. if (minor >> MINORBITS) {
  665. pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
  666. info->vdevice, minor);
  667. return -ENODEV;
  668. }
  669. if ((minor % nr_parts) == 0)
  670. nr_minors = nr_parts;
  671. err = xlbd_reserve_minors(minor, nr_minors);
  672. if (err)
  673. goto out;
  674. err = -ENODEV;
  675. gd = alloc_disk(nr_minors);
  676. if (gd == NULL)
  677. goto release;
  678. strcpy(gd->disk_name, DEV_NAME);
  679. ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
  680. BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
  681. if (nr_minors > 1)
  682. *ptr = 0;
  683. else
  684. snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
  685. "%d", minor & (nr_parts - 1));
  686. gd->major = XENVBD_MAJOR;
  687. gd->first_minor = minor;
  688. gd->fops = &xlvbd_block_fops;
  689. gd->private_data = info;
  690. gd->driverfs_dev = &(info->xbdev->dev);
  691. set_capacity(gd, capacity);
  692. if (xlvbd_init_blk_queue(gd, sector_size,
  693. info->max_indirect_segments ? :
  694. BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
  695. del_gendisk(gd);
  696. goto release;
  697. }
  698. info->rq = gd->queue;
  699. info->gd = gd;
  700. xlvbd_flush(info);
  701. if (vdisk_info & VDISK_READONLY)
  702. set_disk_ro(gd, 1);
  703. if (vdisk_info & VDISK_REMOVABLE)
  704. gd->flags |= GENHD_FL_REMOVABLE;
  705. if (vdisk_info & VDISK_CDROM)
  706. gd->flags |= GENHD_FL_CD;
  707. return 0;
  708. release:
  709. xlbd_release_minors(minor, nr_minors);
  710. out:
  711. return err;
  712. }
  713. static void xlvbd_release_gendisk(struct blkfront_info *info)
  714. {
  715. unsigned int minor, nr_minors;
  716. unsigned long flags;
  717. if (info->rq == NULL)
  718. return;
  719. spin_lock_irqsave(&info->io_lock, flags);
  720. /* No more blkif_request(). */
  721. blk_stop_queue(info->rq);
  722. /* No more gnttab callback work. */
  723. gnttab_cancel_free_callback(&info->callback);
  724. spin_unlock_irqrestore(&info->io_lock, flags);
  725. /* Flush gnttab callback work. Must be done with no locks held. */
  726. flush_work(&info->work);
  727. del_gendisk(info->gd);
  728. minor = info->gd->first_minor;
  729. nr_minors = info->gd->minors;
  730. xlbd_release_minors(minor, nr_minors);
  731. blk_cleanup_queue(info->rq);
  732. info->rq = NULL;
  733. put_disk(info->gd);
  734. info->gd = NULL;
  735. }
  736. static void kick_pending_request_queues(struct blkfront_info *info)
  737. {
  738. if (!RING_FULL(&info->ring)) {
  739. /* Re-enable calldowns. */
  740. blk_start_queue(info->rq);
  741. /* Kick things off immediately. */
  742. do_blkif_request(info->rq);
  743. }
  744. }
  745. static void blkif_restart_queue(struct work_struct *work)
  746. {
  747. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  748. spin_lock_irq(&info->io_lock);
  749. if (info->connected == BLKIF_STATE_CONNECTED)
  750. kick_pending_request_queues(info);
  751. spin_unlock_irq(&info->io_lock);
  752. }
  753. static void blkif_free(struct blkfront_info *info, int suspend)
  754. {
  755. struct grant *persistent_gnt;
  756. struct grant *n;
  757. int i, j, segs;
  758. /* Prevent new requests being issued until we fix things up. */
  759. spin_lock_irq(&info->io_lock);
  760. info->connected = suspend ?
  761. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  762. /* No more blkif_request(). */
  763. if (info->rq)
  764. blk_stop_queue(info->rq);
  765. /* Remove all persistent grants */
  766. if (!list_empty(&info->persistent_gnts)) {
  767. list_for_each_entry_safe(persistent_gnt, n,
  768. &info->persistent_gnts, node) {
  769. list_del(&persistent_gnt->node);
  770. if (persistent_gnt->gref != GRANT_INVALID_REF) {
  771. gnttab_end_foreign_access(persistent_gnt->gref,
  772. 0, 0UL);
  773. info->persistent_gnts_c--;
  774. }
  775. __free_page(pfn_to_page(persistent_gnt->pfn));
  776. kfree(persistent_gnt);
  777. }
  778. }
  779. BUG_ON(info->persistent_gnts_c != 0);
  780. kfree(info->sg);
  781. info->sg = NULL;
  782. for (i = 0; i < BLK_RING_SIZE; i++) {
  783. /*
  784. * Clear persistent grants present in requests already
  785. * on the shared ring
  786. */
  787. if (!info->shadow[i].request)
  788. goto free_shadow;
  789. segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
  790. info->shadow[i].req.u.indirect.nr_segments :
  791. info->shadow[i].req.u.rw.nr_segments;
  792. for (j = 0; j < segs; j++) {
  793. persistent_gnt = info->shadow[i].grants_used[j];
  794. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  795. __free_page(pfn_to_page(persistent_gnt->pfn));
  796. kfree(persistent_gnt);
  797. }
  798. if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
  799. /*
  800. * If this is not an indirect operation don't try to
  801. * free indirect segments
  802. */
  803. goto free_shadow;
  804. for (j = 0; j < INDIRECT_GREFS(segs); j++) {
  805. persistent_gnt = info->shadow[i].indirect_grants[j];
  806. gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
  807. __free_page(pfn_to_page(persistent_gnt->pfn));
  808. kfree(persistent_gnt);
  809. }
  810. free_shadow:
  811. kfree(info->shadow[i].grants_used);
  812. info->shadow[i].grants_used = NULL;
  813. kfree(info->shadow[i].indirect_grants);
  814. info->shadow[i].indirect_grants = NULL;
  815. }
  816. /* No more gnttab callback work. */
  817. gnttab_cancel_free_callback(&info->callback);
  818. spin_unlock_irq(&info->io_lock);
  819. /* Flush gnttab callback work. Must be done with no locks held. */
  820. flush_work(&info->work);
  821. /* Free resources associated with old device channel. */
  822. if (info->ring_ref != GRANT_INVALID_REF) {
  823. gnttab_end_foreign_access(info->ring_ref, 0,
  824. (unsigned long)info->ring.sring);
  825. info->ring_ref = GRANT_INVALID_REF;
  826. info->ring.sring = NULL;
  827. }
  828. if (info->irq)
  829. unbind_from_irqhandler(info->irq, info);
  830. info->evtchn = info->irq = 0;
  831. }
  832. static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
  833. struct blkif_response *bret)
  834. {
  835. int i = 0;
  836. struct bio_vec *bvec;
  837. struct req_iterator iter;
  838. unsigned long flags;
  839. char *bvec_data;
  840. void *shared_data;
  841. unsigned int offset = 0;
  842. int nseg;
  843. nseg = s->req.operation == BLKIF_OP_INDIRECT ?
  844. s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
  845. if (bret->operation == BLKIF_OP_READ) {
  846. /*
  847. * Copy the data received from the backend into the bvec.
  848. * Since bv_offset can be different than 0, and bv_len different
  849. * than PAGE_SIZE, we have to keep track of the current offset,
  850. * to be sure we are copying the data from the right shared page.
  851. */
  852. rq_for_each_segment(bvec, s->request, iter) {
  853. BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
  854. if (bvec->bv_offset < offset)
  855. i++;
  856. BUG_ON(i >= nseg);
  857. shared_data = kmap_atomic(
  858. pfn_to_page(s->grants_used[i]->pfn));
  859. bvec_data = bvec_kmap_irq(bvec, &flags);
  860. memcpy(bvec_data, shared_data + bvec->bv_offset,
  861. bvec->bv_len);
  862. bvec_kunmap_irq(bvec_data, &flags);
  863. kunmap_atomic(shared_data);
  864. offset = bvec->bv_offset + bvec->bv_len;
  865. }
  866. }
  867. /* Add the persistent grant into the list of free grants */
  868. for (i = 0; i < nseg; i++) {
  869. list_add(&s->grants_used[i]->node, &info->persistent_gnts);
  870. info->persistent_gnts_c++;
  871. }
  872. if (s->req.operation == BLKIF_OP_INDIRECT) {
  873. for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
  874. list_add(&s->indirect_grants[i]->node, &info->persistent_gnts);
  875. info->persistent_gnts_c++;
  876. }
  877. }
  878. }
  879. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  880. {
  881. struct request *req;
  882. struct blkif_response *bret;
  883. RING_IDX i, rp;
  884. unsigned long flags;
  885. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  886. int error;
  887. spin_lock_irqsave(&info->io_lock, flags);
  888. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  889. spin_unlock_irqrestore(&info->io_lock, flags);
  890. return IRQ_HANDLED;
  891. }
  892. again:
  893. rp = info->ring.sring->rsp_prod;
  894. rmb(); /* Ensure we see queued responses up to 'rp'. */
  895. for (i = info->ring.rsp_cons; i != rp; i++) {
  896. unsigned long id;
  897. bret = RING_GET_RESPONSE(&info->ring, i);
  898. id = bret->id;
  899. /*
  900. * The backend has messed up and given us an id that we would
  901. * never have given to it (we stamp it up to BLK_RING_SIZE -
  902. * look in get_id_from_freelist.
  903. */
  904. if (id >= BLK_RING_SIZE) {
  905. WARN(1, "%s: response to %s has incorrect id (%ld)\n",
  906. info->gd->disk_name, op_name(bret->operation), id);
  907. /* We can't safely get the 'struct request' as
  908. * the id is busted. */
  909. continue;
  910. }
  911. req = info->shadow[id].request;
  912. if (bret->operation != BLKIF_OP_DISCARD)
  913. blkif_completion(&info->shadow[id], info, bret);
  914. if (add_id_to_freelist(info, id)) {
  915. WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
  916. info->gd->disk_name, op_name(bret->operation), id);
  917. continue;
  918. }
  919. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  920. switch (bret->operation) {
  921. case BLKIF_OP_DISCARD:
  922. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  923. struct request_queue *rq = info->rq;
  924. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  925. info->gd->disk_name, op_name(bret->operation));
  926. error = -EOPNOTSUPP;
  927. info->feature_discard = 0;
  928. info->feature_secdiscard = 0;
  929. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  930. queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
  931. }
  932. __blk_end_request_all(req, error);
  933. break;
  934. case BLKIF_OP_FLUSH_DISKCACHE:
  935. case BLKIF_OP_WRITE_BARRIER:
  936. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  937. printk(KERN_WARNING "blkfront: %s: %s op failed\n",
  938. info->gd->disk_name, op_name(bret->operation));
  939. error = -EOPNOTSUPP;
  940. }
  941. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  942. info->shadow[id].req.u.rw.nr_segments == 0)) {
  943. printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
  944. info->gd->disk_name, op_name(bret->operation));
  945. error = -EOPNOTSUPP;
  946. }
  947. if (unlikely(error)) {
  948. if (error == -EOPNOTSUPP)
  949. error = 0;
  950. info->feature_flush = 0;
  951. info->flush_op = 0;
  952. xlvbd_flush(info);
  953. }
  954. /* fall through */
  955. case BLKIF_OP_READ:
  956. case BLKIF_OP_WRITE:
  957. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  958. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  959. "request: %x\n", bret->status);
  960. __blk_end_request_all(req, error);
  961. break;
  962. default:
  963. BUG();
  964. }
  965. }
  966. info->ring.rsp_cons = i;
  967. if (i != info->ring.req_prod_pvt) {
  968. int more_to_do;
  969. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  970. if (more_to_do)
  971. goto again;
  972. } else
  973. info->ring.sring->rsp_event = i + 1;
  974. kick_pending_request_queues(info);
  975. spin_unlock_irqrestore(&info->io_lock, flags);
  976. return IRQ_HANDLED;
  977. }
  978. static int setup_blkring(struct xenbus_device *dev,
  979. struct blkfront_info *info)
  980. {
  981. struct blkif_sring *sring;
  982. int err;
  983. info->ring_ref = GRANT_INVALID_REF;
  984. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  985. if (!sring) {
  986. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  987. return -ENOMEM;
  988. }
  989. SHARED_RING_INIT(sring);
  990. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  991. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  992. if (err < 0) {
  993. free_page((unsigned long)sring);
  994. info->ring.sring = NULL;
  995. goto fail;
  996. }
  997. info->ring_ref = err;
  998. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  999. if (err)
  1000. goto fail;
  1001. err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
  1002. "blkif", info);
  1003. if (err <= 0) {
  1004. xenbus_dev_fatal(dev, err,
  1005. "bind_evtchn_to_irqhandler failed");
  1006. goto fail;
  1007. }
  1008. info->irq = err;
  1009. return 0;
  1010. fail:
  1011. blkif_free(info, 0);
  1012. return err;
  1013. }
  1014. /* Common code used when first setting up, and when resuming. */
  1015. static int talk_to_blkback(struct xenbus_device *dev,
  1016. struct blkfront_info *info)
  1017. {
  1018. const char *message = NULL;
  1019. struct xenbus_transaction xbt;
  1020. int err;
  1021. /* Create shared ring, alloc event channel. */
  1022. err = setup_blkring(dev, info);
  1023. if (err)
  1024. goto out;
  1025. again:
  1026. err = xenbus_transaction_start(&xbt);
  1027. if (err) {
  1028. xenbus_dev_fatal(dev, err, "starting transaction");
  1029. goto destroy_blkring;
  1030. }
  1031. err = xenbus_printf(xbt, dev->nodename,
  1032. "ring-ref", "%u", info->ring_ref);
  1033. if (err) {
  1034. message = "writing ring-ref";
  1035. goto abort_transaction;
  1036. }
  1037. err = xenbus_printf(xbt, dev->nodename,
  1038. "event-channel", "%u", info->evtchn);
  1039. if (err) {
  1040. message = "writing event-channel";
  1041. goto abort_transaction;
  1042. }
  1043. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  1044. XEN_IO_PROTO_ABI_NATIVE);
  1045. if (err) {
  1046. message = "writing protocol";
  1047. goto abort_transaction;
  1048. }
  1049. err = xenbus_printf(xbt, dev->nodename,
  1050. "feature-persistent", "%u", 1);
  1051. if (err)
  1052. dev_warn(&dev->dev,
  1053. "writing persistent grants feature to xenbus");
  1054. err = xenbus_transaction_end(xbt, 0);
  1055. if (err) {
  1056. if (err == -EAGAIN)
  1057. goto again;
  1058. xenbus_dev_fatal(dev, err, "completing transaction");
  1059. goto destroy_blkring;
  1060. }
  1061. xenbus_switch_state(dev, XenbusStateInitialised);
  1062. return 0;
  1063. abort_transaction:
  1064. xenbus_transaction_end(xbt, 1);
  1065. if (message)
  1066. xenbus_dev_fatal(dev, err, "%s", message);
  1067. destroy_blkring:
  1068. blkif_free(info, 0);
  1069. out:
  1070. return err;
  1071. }
  1072. /**
  1073. * Entry point to this code when a new device is created. Allocate the basic
  1074. * structures and the ring buffer for communication with the backend, and
  1075. * inform the backend of the appropriate details for those. Switch to
  1076. * Initialised state.
  1077. */
  1078. static int blkfront_probe(struct xenbus_device *dev,
  1079. const struct xenbus_device_id *id)
  1080. {
  1081. int err, vdevice, i;
  1082. struct blkfront_info *info;
  1083. /* FIXME: Use dynamic device id if this is not set. */
  1084. err = xenbus_scanf(XBT_NIL, dev->nodename,
  1085. "virtual-device", "%i", &vdevice);
  1086. if (err != 1) {
  1087. /* go looking in the extended area instead */
  1088. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  1089. "%i", &vdevice);
  1090. if (err != 1) {
  1091. xenbus_dev_fatal(dev, err, "reading virtual-device");
  1092. return err;
  1093. }
  1094. }
  1095. if (xen_hvm_domain()) {
  1096. char *type;
  1097. int len;
  1098. /* no unplug has been done: do not hook devices != xen vbds */
  1099. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  1100. int major;
  1101. if (!VDEV_IS_EXTENDED(vdevice))
  1102. major = BLKIF_MAJOR(vdevice);
  1103. else
  1104. major = XENVBD_MAJOR;
  1105. if (major != XENVBD_MAJOR) {
  1106. printk(KERN_INFO
  1107. "%s: HVM does not support vbd %d as xen block device\n",
  1108. __FUNCTION__, vdevice);
  1109. return -ENODEV;
  1110. }
  1111. }
  1112. /* do not create a PV cdrom device if we are an HVM guest */
  1113. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  1114. if (IS_ERR(type))
  1115. return -ENODEV;
  1116. if (strncmp(type, "cdrom", 5) == 0) {
  1117. kfree(type);
  1118. return -ENODEV;
  1119. }
  1120. kfree(type);
  1121. }
  1122. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1123. if (!info) {
  1124. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  1125. return -ENOMEM;
  1126. }
  1127. mutex_init(&info->mutex);
  1128. spin_lock_init(&info->io_lock);
  1129. info->xbdev = dev;
  1130. info->vdevice = vdevice;
  1131. INIT_LIST_HEAD(&info->persistent_gnts);
  1132. info->persistent_gnts_c = 0;
  1133. info->connected = BLKIF_STATE_DISCONNECTED;
  1134. INIT_WORK(&info->work, blkif_restart_queue);
  1135. for (i = 0; i < BLK_RING_SIZE; i++)
  1136. info->shadow[i].req.u.rw.id = i+1;
  1137. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1138. /* Front end dir is a number, which is used as the id. */
  1139. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  1140. dev_set_drvdata(&dev->dev, info);
  1141. err = talk_to_blkback(dev, info);
  1142. if (err) {
  1143. kfree(info);
  1144. dev_set_drvdata(&dev->dev, NULL);
  1145. return err;
  1146. }
  1147. return 0;
  1148. }
  1149. /*
  1150. * This is a clone of md_trim_bio, used to split a bio into smaller ones
  1151. */
  1152. static void trim_bio(struct bio *bio, int offset, int size)
  1153. {
  1154. /* 'bio' is a cloned bio which we need to trim to match
  1155. * the given offset and size.
  1156. * This requires adjusting bi_sector, bi_size, and bi_io_vec
  1157. */
  1158. int i;
  1159. struct bio_vec *bvec;
  1160. int sofar = 0;
  1161. size <<= 9;
  1162. if (offset == 0 && size == bio->bi_size)
  1163. return;
  1164. bio->bi_sector += offset;
  1165. bio->bi_size = size;
  1166. offset <<= 9;
  1167. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1168. while (bio->bi_idx < bio->bi_vcnt &&
  1169. bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
  1170. /* remove this whole bio_vec */
  1171. offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
  1172. bio->bi_idx++;
  1173. }
  1174. if (bio->bi_idx < bio->bi_vcnt) {
  1175. bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
  1176. bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
  1177. }
  1178. /* avoid any complications with bi_idx being non-zero*/
  1179. if (bio->bi_idx) {
  1180. memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
  1181. (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
  1182. bio->bi_vcnt -= bio->bi_idx;
  1183. bio->bi_idx = 0;
  1184. }
  1185. /* Make sure vcnt and last bv are not too big */
  1186. bio_for_each_segment(bvec, bio, i) {
  1187. if (sofar + bvec->bv_len > size)
  1188. bvec->bv_len = size - sofar;
  1189. if (bvec->bv_len == 0) {
  1190. bio->bi_vcnt = i;
  1191. break;
  1192. }
  1193. sofar += bvec->bv_len;
  1194. }
  1195. }
  1196. static void split_bio_end(struct bio *bio, int error)
  1197. {
  1198. struct split_bio *split_bio = bio->bi_private;
  1199. if (error)
  1200. split_bio->err = error;
  1201. if (atomic_dec_and_test(&split_bio->pending)) {
  1202. split_bio->bio->bi_phys_segments = 0;
  1203. bio_endio(split_bio->bio, split_bio->err);
  1204. kfree(split_bio);
  1205. }
  1206. bio_put(bio);
  1207. }
  1208. static int blkif_recover(struct blkfront_info *info)
  1209. {
  1210. int i;
  1211. struct request *req, *n;
  1212. struct blk_shadow *copy;
  1213. int rc;
  1214. struct bio *bio, *cloned_bio;
  1215. struct bio_list bio_list, merge_bio;
  1216. unsigned int segs, offset;
  1217. int pending, size;
  1218. struct split_bio *split_bio;
  1219. struct list_head requests;
  1220. /* Stage 1: Make a safe copy of the shadow state. */
  1221. copy = kmemdup(info->shadow, sizeof(info->shadow),
  1222. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  1223. if (!copy)
  1224. return -ENOMEM;
  1225. /* Stage 2: Set up free list. */
  1226. memset(&info->shadow, 0, sizeof(info->shadow));
  1227. for (i = 0; i < BLK_RING_SIZE; i++)
  1228. info->shadow[i].req.u.rw.id = i+1;
  1229. info->shadow_free = info->ring.req_prod_pvt;
  1230. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  1231. rc = blkfront_setup_indirect(info);
  1232. if (rc) {
  1233. kfree(copy);
  1234. return rc;
  1235. }
  1236. segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1237. blk_queue_max_segments(info->rq, segs);
  1238. bio_list_init(&bio_list);
  1239. INIT_LIST_HEAD(&requests);
  1240. for (i = 0; i < BLK_RING_SIZE; i++) {
  1241. /* Not in use? */
  1242. if (!copy[i].request)
  1243. continue;
  1244. /*
  1245. * Get the bios in the request so we can re-queue them.
  1246. */
  1247. if (copy[i].request->cmd_flags &
  1248. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1249. /*
  1250. * Flush operations don't contain bios, so
  1251. * we need to requeue the whole request
  1252. */
  1253. list_add(&copy[i].request->queuelist, &requests);
  1254. continue;
  1255. }
  1256. merge_bio.head = copy[i].request->bio;
  1257. merge_bio.tail = copy[i].request->biotail;
  1258. bio_list_merge(&bio_list, &merge_bio);
  1259. copy[i].request->bio = NULL;
  1260. blk_put_request(copy[i].request);
  1261. }
  1262. kfree(copy);
  1263. /*
  1264. * Empty the queue, this is important because we might have
  1265. * requests in the queue with more segments than what we
  1266. * can handle now.
  1267. */
  1268. spin_lock_irq(&info->io_lock);
  1269. while ((req = blk_fetch_request(info->rq)) != NULL) {
  1270. if (req->cmd_flags &
  1271. (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
  1272. list_add(&req->queuelist, &requests);
  1273. continue;
  1274. }
  1275. merge_bio.head = req->bio;
  1276. merge_bio.tail = req->biotail;
  1277. bio_list_merge(&bio_list, &merge_bio);
  1278. req->bio = NULL;
  1279. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
  1280. pr_alert("diskcache flush request found!\n");
  1281. __blk_put_request(info->rq, req);
  1282. }
  1283. spin_unlock_irq(&info->io_lock);
  1284. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1285. spin_lock_irq(&info->io_lock);
  1286. /* Now safe for us to use the shared ring */
  1287. info->connected = BLKIF_STATE_CONNECTED;
  1288. /* Kick any other new requests queued since we resumed */
  1289. kick_pending_request_queues(info);
  1290. list_for_each_entry_safe(req, n, &requests, queuelist) {
  1291. /* Requeue pending requests (flush or discard) */
  1292. list_del_init(&req->queuelist);
  1293. BUG_ON(req->nr_phys_segments > segs);
  1294. blk_requeue_request(info->rq, req);
  1295. }
  1296. spin_unlock_irq(&info->io_lock);
  1297. while ((bio = bio_list_pop(&bio_list)) != NULL) {
  1298. /* Traverse the list of pending bios and re-queue them */
  1299. if (bio_segments(bio) > segs) {
  1300. /*
  1301. * This bio has more segments than what we can
  1302. * handle, we have to split it.
  1303. */
  1304. pending = (bio_segments(bio) + segs - 1) / segs;
  1305. split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
  1306. BUG_ON(split_bio == NULL);
  1307. atomic_set(&split_bio->pending, pending);
  1308. split_bio->bio = bio;
  1309. for (i = 0; i < pending; i++) {
  1310. offset = (i * segs * PAGE_SIZE) >> 9;
  1311. size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
  1312. (unsigned int)(bio->bi_size >> 9) - offset);
  1313. cloned_bio = bio_clone(bio, GFP_NOIO);
  1314. BUG_ON(cloned_bio == NULL);
  1315. trim_bio(cloned_bio, offset, size);
  1316. cloned_bio->bi_private = split_bio;
  1317. cloned_bio->bi_end_io = split_bio_end;
  1318. submit_bio(cloned_bio->bi_rw, cloned_bio);
  1319. }
  1320. /*
  1321. * Now we have to wait for all those smaller bios to
  1322. * end, so we can also end the "parent" bio.
  1323. */
  1324. continue;
  1325. }
  1326. /* We don't need to split this bio */
  1327. submit_bio(bio->bi_rw, bio);
  1328. }
  1329. return 0;
  1330. }
  1331. /**
  1332. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1333. * driver restart. We tear down our blkif structure and recreate it, but
  1334. * leave the device-layer structures intact so that this is transparent to the
  1335. * rest of the kernel.
  1336. */
  1337. static int blkfront_resume(struct xenbus_device *dev)
  1338. {
  1339. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1340. int err;
  1341. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  1342. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  1343. err = talk_to_blkback(dev, info);
  1344. /*
  1345. * We have to wait for the backend to switch to
  1346. * connected state, since we want to read which
  1347. * features it supports.
  1348. */
  1349. return err;
  1350. }
  1351. static void
  1352. blkfront_closing(struct blkfront_info *info)
  1353. {
  1354. struct xenbus_device *xbdev = info->xbdev;
  1355. struct block_device *bdev = NULL;
  1356. mutex_lock(&info->mutex);
  1357. if (xbdev->state == XenbusStateClosing) {
  1358. mutex_unlock(&info->mutex);
  1359. return;
  1360. }
  1361. if (info->gd)
  1362. bdev = bdget_disk(info->gd, 0);
  1363. mutex_unlock(&info->mutex);
  1364. if (!bdev) {
  1365. xenbus_frontend_closed(xbdev);
  1366. return;
  1367. }
  1368. mutex_lock(&bdev->bd_mutex);
  1369. if (bdev->bd_openers) {
  1370. xenbus_dev_error(xbdev, -EBUSY,
  1371. "Device in use; refusing to close");
  1372. xenbus_switch_state(xbdev, XenbusStateClosing);
  1373. } else {
  1374. xlvbd_release_gendisk(info);
  1375. xenbus_frontend_closed(xbdev);
  1376. }
  1377. mutex_unlock(&bdev->bd_mutex);
  1378. bdput(bdev);
  1379. }
  1380. static void blkfront_setup_discard(struct blkfront_info *info)
  1381. {
  1382. int err;
  1383. char *type;
  1384. unsigned int discard_granularity;
  1385. unsigned int discard_alignment;
  1386. unsigned int discard_secure;
  1387. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  1388. if (IS_ERR(type))
  1389. return;
  1390. info->feature_secdiscard = 0;
  1391. if (strncmp(type, "phy", 3) == 0) {
  1392. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1393. "discard-granularity", "%u", &discard_granularity,
  1394. "discard-alignment", "%u", &discard_alignment,
  1395. NULL);
  1396. if (!err) {
  1397. info->feature_discard = 1;
  1398. info->discard_granularity = discard_granularity;
  1399. info->discard_alignment = discard_alignment;
  1400. }
  1401. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1402. "discard-secure", "%d", &discard_secure,
  1403. NULL);
  1404. if (!err)
  1405. info->feature_secdiscard = discard_secure;
  1406. } else if (strncmp(type, "file", 4) == 0)
  1407. info->feature_discard = 1;
  1408. kfree(type);
  1409. }
  1410. static int blkfront_setup_indirect(struct blkfront_info *info)
  1411. {
  1412. unsigned int indirect_segments, segs;
  1413. int err, i;
  1414. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1415. "feature-max-indirect-segments", "%u", &indirect_segments,
  1416. NULL);
  1417. if (err) {
  1418. info->max_indirect_segments = 0;
  1419. segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  1420. } else {
  1421. info->max_indirect_segments = min(indirect_segments,
  1422. xen_blkif_max_segments);
  1423. segs = info->max_indirect_segments;
  1424. }
  1425. info->sg = kzalloc(sizeof(info->sg[0]) * segs, GFP_KERNEL);
  1426. if (info->sg == NULL)
  1427. goto out_of_memory;
  1428. sg_init_table(info->sg, segs);
  1429. err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
  1430. if (err)
  1431. goto out_of_memory;
  1432. for (i = 0; i < BLK_RING_SIZE; i++) {
  1433. info->shadow[i].grants_used = kzalloc(
  1434. sizeof(info->shadow[i].grants_used[0]) * segs,
  1435. GFP_NOIO);
  1436. if (info->max_indirect_segments)
  1437. info->shadow[i].indirect_grants = kzalloc(
  1438. sizeof(info->shadow[i].indirect_grants[0]) *
  1439. INDIRECT_GREFS(segs),
  1440. GFP_NOIO);
  1441. if ((info->shadow[i].grants_used == NULL) ||
  1442. (info->max_indirect_segments &&
  1443. (info->shadow[i].indirect_grants == NULL)))
  1444. goto out_of_memory;
  1445. }
  1446. return 0;
  1447. out_of_memory:
  1448. kfree(info->sg);
  1449. info->sg = NULL;
  1450. for (i = 0; i < BLK_RING_SIZE; i++) {
  1451. kfree(info->shadow[i].grants_used);
  1452. info->shadow[i].grants_used = NULL;
  1453. kfree(info->shadow[i].indirect_grants);
  1454. info->shadow[i].indirect_grants = NULL;
  1455. }
  1456. return -ENOMEM;
  1457. }
  1458. /*
  1459. * Invoked when the backend is finally 'ready' (and has told produced
  1460. * the details about the physical device - #sectors, size, etc).
  1461. */
  1462. static void blkfront_connect(struct blkfront_info *info)
  1463. {
  1464. unsigned long long sectors;
  1465. unsigned long sector_size;
  1466. unsigned int binfo;
  1467. int err;
  1468. int barrier, flush, discard, persistent;
  1469. switch (info->connected) {
  1470. case BLKIF_STATE_CONNECTED:
  1471. /*
  1472. * Potentially, the back-end may be signalling
  1473. * a capacity change; update the capacity.
  1474. */
  1475. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1476. "sectors", "%Lu", &sectors);
  1477. if (XENBUS_EXIST_ERR(err))
  1478. return;
  1479. printk(KERN_INFO "Setting capacity to %Lu\n",
  1480. sectors);
  1481. set_capacity(info->gd, sectors);
  1482. revalidate_disk(info->gd);
  1483. return;
  1484. case BLKIF_STATE_SUSPENDED:
  1485. /*
  1486. * If we are recovering from suspension, we need to wait
  1487. * for the backend to announce it's features before
  1488. * reconnecting, at least we need to know if the backend
  1489. * supports indirect descriptors, and how many.
  1490. */
  1491. blkif_recover(info);
  1492. return;
  1493. default:
  1494. break;
  1495. }
  1496. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1497. __func__, info->xbdev->otherend);
  1498. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1499. "sectors", "%llu", &sectors,
  1500. "info", "%u", &binfo,
  1501. "sector-size", "%lu", &sector_size,
  1502. NULL);
  1503. if (err) {
  1504. xenbus_dev_fatal(info->xbdev, err,
  1505. "reading backend fields at %s",
  1506. info->xbdev->otherend);
  1507. return;
  1508. }
  1509. info->feature_flush = 0;
  1510. info->flush_op = 0;
  1511. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1512. "feature-barrier", "%d", &barrier,
  1513. NULL);
  1514. /*
  1515. * If there's no "feature-barrier" defined, then it means
  1516. * we're dealing with a very old backend which writes
  1517. * synchronously; nothing to do.
  1518. *
  1519. * If there are barriers, then we use flush.
  1520. */
  1521. if (!err && barrier) {
  1522. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1523. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1524. }
  1525. /*
  1526. * And if there is "feature-flush-cache" use that above
  1527. * barriers.
  1528. */
  1529. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1530. "feature-flush-cache", "%d", &flush,
  1531. NULL);
  1532. if (!err && flush) {
  1533. info->feature_flush = REQ_FLUSH;
  1534. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1535. }
  1536. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1537. "feature-discard", "%d", &discard,
  1538. NULL);
  1539. if (!err && discard)
  1540. blkfront_setup_discard(info);
  1541. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1542. "feature-persistent", "%u", &persistent,
  1543. NULL);
  1544. if (err)
  1545. info->feature_persistent = 0;
  1546. else
  1547. info->feature_persistent = persistent;
  1548. err = blkfront_setup_indirect(info);
  1549. if (err) {
  1550. xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
  1551. info->xbdev->otherend);
  1552. return;
  1553. }
  1554. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  1555. if (err) {
  1556. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1557. info->xbdev->otherend);
  1558. return;
  1559. }
  1560. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1561. /* Kick pending requests. */
  1562. spin_lock_irq(&info->io_lock);
  1563. info->connected = BLKIF_STATE_CONNECTED;
  1564. kick_pending_request_queues(info);
  1565. spin_unlock_irq(&info->io_lock);
  1566. add_disk(info->gd);
  1567. info->is_ready = 1;
  1568. }
  1569. /**
  1570. * Callback received when the backend's state changes.
  1571. */
  1572. static void blkback_changed(struct xenbus_device *dev,
  1573. enum xenbus_state backend_state)
  1574. {
  1575. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1576. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1577. switch (backend_state) {
  1578. case XenbusStateInitialising:
  1579. case XenbusStateInitWait:
  1580. case XenbusStateInitialised:
  1581. case XenbusStateReconfiguring:
  1582. case XenbusStateReconfigured:
  1583. case XenbusStateUnknown:
  1584. case XenbusStateClosed:
  1585. break;
  1586. case XenbusStateConnected:
  1587. blkfront_connect(info);
  1588. break;
  1589. case XenbusStateClosing:
  1590. blkfront_closing(info);
  1591. break;
  1592. }
  1593. }
  1594. static int blkfront_remove(struct xenbus_device *xbdev)
  1595. {
  1596. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1597. struct block_device *bdev = NULL;
  1598. struct gendisk *disk;
  1599. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1600. blkif_free(info, 0);
  1601. mutex_lock(&info->mutex);
  1602. disk = info->gd;
  1603. if (disk)
  1604. bdev = bdget_disk(disk, 0);
  1605. info->xbdev = NULL;
  1606. mutex_unlock(&info->mutex);
  1607. if (!bdev) {
  1608. kfree(info);
  1609. return 0;
  1610. }
  1611. /*
  1612. * The xbdev was removed before we reached the Closed
  1613. * state. See if it's safe to remove the disk. If the bdev
  1614. * isn't closed yet, we let release take care of it.
  1615. */
  1616. mutex_lock(&bdev->bd_mutex);
  1617. info = disk->private_data;
  1618. dev_warn(disk_to_dev(disk),
  1619. "%s was hot-unplugged, %d stale handles\n",
  1620. xbdev->nodename, bdev->bd_openers);
  1621. if (info && !bdev->bd_openers) {
  1622. xlvbd_release_gendisk(info);
  1623. disk->private_data = NULL;
  1624. kfree(info);
  1625. }
  1626. mutex_unlock(&bdev->bd_mutex);
  1627. bdput(bdev);
  1628. return 0;
  1629. }
  1630. static int blkfront_is_ready(struct xenbus_device *dev)
  1631. {
  1632. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1633. return info->is_ready && info->xbdev;
  1634. }
  1635. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1636. {
  1637. struct gendisk *disk = bdev->bd_disk;
  1638. struct blkfront_info *info;
  1639. int err = 0;
  1640. mutex_lock(&blkfront_mutex);
  1641. info = disk->private_data;
  1642. if (!info) {
  1643. /* xbdev gone */
  1644. err = -ERESTARTSYS;
  1645. goto out;
  1646. }
  1647. mutex_lock(&info->mutex);
  1648. if (!info->gd)
  1649. /* xbdev is closed */
  1650. err = -ERESTARTSYS;
  1651. mutex_unlock(&info->mutex);
  1652. out:
  1653. mutex_unlock(&blkfront_mutex);
  1654. return err;
  1655. }
  1656. static int blkif_release(struct gendisk *disk, fmode_t mode)
  1657. {
  1658. struct blkfront_info *info = disk->private_data;
  1659. struct block_device *bdev;
  1660. struct xenbus_device *xbdev;
  1661. mutex_lock(&blkfront_mutex);
  1662. bdev = bdget_disk(disk, 0);
  1663. if (bdev->bd_openers)
  1664. goto out;
  1665. /*
  1666. * Check if we have been instructed to close. We will have
  1667. * deferred this request, because the bdev was still open.
  1668. */
  1669. mutex_lock(&info->mutex);
  1670. xbdev = info->xbdev;
  1671. if (xbdev && xbdev->state == XenbusStateClosing) {
  1672. /* pending switch to state closed */
  1673. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1674. xlvbd_release_gendisk(info);
  1675. xenbus_frontend_closed(info->xbdev);
  1676. }
  1677. mutex_unlock(&info->mutex);
  1678. if (!xbdev) {
  1679. /* sudden device removal */
  1680. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1681. xlvbd_release_gendisk(info);
  1682. disk->private_data = NULL;
  1683. kfree(info);
  1684. }
  1685. out:
  1686. bdput(bdev);
  1687. mutex_unlock(&blkfront_mutex);
  1688. return 0;
  1689. }
  1690. static const struct block_device_operations xlvbd_block_fops =
  1691. {
  1692. .owner = THIS_MODULE,
  1693. .open = blkif_open,
  1694. .release = blkif_release,
  1695. .getgeo = blkif_getgeo,
  1696. .ioctl = blkif_ioctl,
  1697. };
  1698. static const struct xenbus_device_id blkfront_ids[] = {
  1699. { "vbd" },
  1700. { "" }
  1701. };
  1702. static DEFINE_XENBUS_DRIVER(blkfront, ,
  1703. .probe = blkfront_probe,
  1704. .remove = blkfront_remove,
  1705. .resume = blkfront_resume,
  1706. .otherend_changed = blkback_changed,
  1707. .is_ready = blkfront_is_ready,
  1708. );
  1709. static int __init xlblk_init(void)
  1710. {
  1711. int ret;
  1712. if (!xen_domain())
  1713. return -ENODEV;
  1714. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1715. return -ENODEV;
  1716. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1717. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1718. XENVBD_MAJOR, DEV_NAME);
  1719. return -ENODEV;
  1720. }
  1721. ret = xenbus_register_frontend(&blkfront_driver);
  1722. if (ret) {
  1723. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1724. return ret;
  1725. }
  1726. return 0;
  1727. }
  1728. module_init(xlblk_init);
  1729. static void __exit xlblk_exit(void)
  1730. {
  1731. xenbus_unregister_driver(&blkfront_driver);
  1732. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1733. kfree(minors);
  1734. }
  1735. module_exit(xlblk_exit);
  1736. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1737. MODULE_LICENSE("GPL");
  1738. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1739. MODULE_ALIAS("xen:vbd");
  1740. MODULE_ALIAS("xenblk");