xen-blkfront.c 55 KB

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