xen-blkfront.c 53 KB

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