xen-blkfront.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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 <xen/xen.h>
  46. #include <xen/xenbus.h>
  47. #include <xen/grant_table.h>
  48. #include <xen/events.h>
  49. #include <xen/page.h>
  50. #include <xen/platform_pci.h>
  51. #include <xen/interface/grant_table.h>
  52. #include <xen/interface/io/blkif.h>
  53. #include <xen/interface/io/protocols.h>
  54. #include <asm/xen/hypervisor.h>
  55. enum blkif_state {
  56. BLKIF_STATE_DISCONNECTED,
  57. BLKIF_STATE_CONNECTED,
  58. BLKIF_STATE_SUSPENDED,
  59. };
  60. struct blk_shadow {
  61. struct blkif_request req;
  62. struct request *request;
  63. unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  64. };
  65. static DEFINE_MUTEX(blkfront_mutex);
  66. static const struct block_device_operations xlvbd_block_fops;
  67. #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
  68. /*
  69. * We have one of these per vbd, whether ide, scsi or 'other'. They
  70. * hang in private_data off the gendisk structure. We may end up
  71. * putting all kinds of interesting stuff here :-)
  72. */
  73. struct blkfront_info
  74. {
  75. struct mutex mutex;
  76. struct xenbus_device *xbdev;
  77. struct gendisk *gd;
  78. int vdevice;
  79. blkif_vdev_t handle;
  80. enum blkif_state connected;
  81. int ring_ref;
  82. struct blkif_front_ring ring;
  83. struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  84. unsigned int evtchn, irq;
  85. struct request_queue *rq;
  86. struct work_struct work;
  87. struct gnttab_free_callback callback;
  88. struct blk_shadow shadow[BLK_RING_SIZE];
  89. unsigned long shadow_free;
  90. unsigned int feature_flush;
  91. unsigned int flush_op;
  92. unsigned int feature_discard;
  93. unsigned int discard_granularity;
  94. unsigned int discard_alignment;
  95. int is_ready;
  96. };
  97. static DEFINE_SPINLOCK(blkif_io_lock);
  98. static unsigned int nr_minors;
  99. static unsigned long *minors;
  100. static DEFINE_SPINLOCK(minor_lock);
  101. #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
  102. (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
  103. #define GRANT_INVALID_REF 0
  104. #define PARTS_PER_DISK 16
  105. #define PARTS_PER_EXT_DISK 256
  106. #define BLKIF_MAJOR(dev) ((dev)>>8)
  107. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  108. #define EXT_SHIFT 28
  109. #define EXTENDED (1<<EXT_SHIFT)
  110. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  111. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  112. #define EMULATED_HD_DISK_MINOR_OFFSET (0)
  113. #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
  114. #define EMULATED_SD_DISK_MINOR_OFFSET (0)
  115. #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
  116. #define DEV_NAME "xvd" /* name in /dev */
  117. static int get_id_from_freelist(struct blkfront_info *info)
  118. {
  119. unsigned long free = info->shadow_free;
  120. BUG_ON(free >= BLK_RING_SIZE);
  121. info->shadow_free = info->shadow[free].req.u.rw.id;
  122. info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
  123. return free;
  124. }
  125. static void add_id_to_freelist(struct blkfront_info *info,
  126. unsigned long id)
  127. {
  128. info->shadow[id].req.u.rw.id = info->shadow_free;
  129. info->shadow[id].request = NULL;
  130. info->shadow_free = id;
  131. }
  132. static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
  133. {
  134. unsigned int end = minor + nr;
  135. int rc;
  136. if (end > nr_minors) {
  137. unsigned long *bitmap, *old;
  138. bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
  139. GFP_KERNEL);
  140. if (bitmap == NULL)
  141. return -ENOMEM;
  142. spin_lock(&minor_lock);
  143. if (end > nr_minors) {
  144. old = minors;
  145. memcpy(bitmap, minors,
  146. BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
  147. minors = bitmap;
  148. nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
  149. } else
  150. old = bitmap;
  151. spin_unlock(&minor_lock);
  152. kfree(old);
  153. }
  154. spin_lock(&minor_lock);
  155. if (find_next_bit(minors, end, minor) >= end) {
  156. for (; minor < end; ++minor)
  157. __set_bit(minor, minors);
  158. rc = 0;
  159. } else
  160. rc = -EBUSY;
  161. spin_unlock(&minor_lock);
  162. return rc;
  163. }
  164. static void xlbd_release_minors(unsigned int minor, unsigned int nr)
  165. {
  166. unsigned int end = minor + nr;
  167. BUG_ON(end > nr_minors);
  168. spin_lock(&minor_lock);
  169. for (; minor < end; ++minor)
  170. __clear_bit(minor, minors);
  171. spin_unlock(&minor_lock);
  172. }
  173. static void blkif_restart_queue_callback(void *arg)
  174. {
  175. struct blkfront_info *info = (struct blkfront_info *)arg;
  176. schedule_work(&info->work);
  177. }
  178. static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
  179. {
  180. /* We don't have real geometry info, but let's at least return
  181. values consistent with the size of the device */
  182. sector_t nsect = get_capacity(bd->bd_disk);
  183. sector_t cylinders = nsect;
  184. hg->heads = 0xff;
  185. hg->sectors = 0x3f;
  186. sector_div(cylinders, hg->heads * hg->sectors);
  187. hg->cylinders = cylinders;
  188. if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
  189. hg->cylinders = 0xffff;
  190. return 0;
  191. }
  192. static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
  193. unsigned command, unsigned long argument)
  194. {
  195. struct blkfront_info *info = bdev->bd_disk->private_data;
  196. int i;
  197. dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
  198. command, (long)argument);
  199. switch (command) {
  200. case CDROMMULTISESSION:
  201. dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
  202. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  203. if (put_user(0, (char __user *)(argument + i)))
  204. return -EFAULT;
  205. return 0;
  206. case CDROM_GET_CAPABILITY: {
  207. struct gendisk *gd = info->gd;
  208. if (gd->flags & GENHD_FL_CD)
  209. return 0;
  210. return -EINVAL;
  211. }
  212. default:
  213. /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
  214. command);*/
  215. return -EINVAL; /* same return as native Linux */
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Generate a Xen blkfront IO request from a blk layer request. Reads
  221. * and writes are handled as expected.
  222. *
  223. * @req: a request struct
  224. */
  225. static int blkif_queue_request(struct request *req)
  226. {
  227. struct blkfront_info *info = req->rq_disk->private_data;
  228. unsigned long buffer_mfn;
  229. struct blkif_request *ring_req;
  230. unsigned long id;
  231. unsigned int fsect, lsect;
  232. int i, ref;
  233. grant_ref_t gref_head;
  234. struct scatterlist *sg;
  235. if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
  236. return 1;
  237. if (gnttab_alloc_grant_references(
  238. BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
  239. gnttab_request_free_callback(
  240. &info->callback,
  241. blkif_restart_queue_callback,
  242. info,
  243. BLKIF_MAX_SEGMENTS_PER_REQUEST);
  244. return 1;
  245. }
  246. /* Fill out a communications ring structure. */
  247. ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  248. id = get_id_from_freelist(info);
  249. info->shadow[id].request = req;
  250. ring_req->u.rw.id = id;
  251. ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
  252. ring_req->u.rw.handle = info->handle;
  253. ring_req->operation = rq_data_dir(req) ?
  254. BLKIF_OP_WRITE : BLKIF_OP_READ;
  255. if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
  256. /*
  257. * Ideally we can do an unordered flush-to-disk. In case the
  258. * backend onlysupports barriers, use that. A barrier request
  259. * a superset of FUA, so we can implement it the same
  260. * way. (It's also a FLUSH+FUA, since it is
  261. * guaranteed ordered WRT previous writes.)
  262. */
  263. ring_req->operation = info->flush_op;
  264. }
  265. if (unlikely(req->cmd_flags & REQ_DISCARD)) {
  266. /* id, sector_number and handle are set above. */
  267. ring_req->operation = BLKIF_OP_DISCARD;
  268. ring_req->u.discard.nr_segments = 0;
  269. ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
  270. } else {
  271. ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
  272. info->sg);
  273. BUG_ON(ring_req->u.rw.nr_segments >
  274. BLKIF_MAX_SEGMENTS_PER_REQUEST);
  275. for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
  276. buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
  277. fsect = sg->offset >> 9;
  278. lsect = fsect + (sg->length >> 9) - 1;
  279. /* install a grant reference. */
  280. ref = gnttab_claim_grant_reference(&gref_head);
  281. BUG_ON(ref == -ENOSPC);
  282. gnttab_grant_foreign_access_ref(
  283. ref,
  284. info->xbdev->otherend_id,
  285. buffer_mfn,
  286. rq_data_dir(req));
  287. info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
  288. ring_req->u.rw.seg[i] =
  289. (struct blkif_request_segment) {
  290. .gref = ref,
  291. .first_sect = fsect,
  292. .last_sect = lsect };
  293. }
  294. }
  295. info->ring.req_prod_pvt++;
  296. /* Keep a private copy so we can reissue requests when recovering. */
  297. info->shadow[id].req = *ring_req;
  298. gnttab_free_grant_references(gref_head);
  299. return 0;
  300. }
  301. static inline void flush_requests(struct blkfront_info *info)
  302. {
  303. int notify;
  304. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
  305. if (notify)
  306. notify_remote_via_irq(info->irq);
  307. }
  308. /*
  309. * do_blkif_request
  310. * read a block; request is in a request queue
  311. */
  312. static void do_blkif_request(struct request_queue *rq)
  313. {
  314. struct blkfront_info *info = NULL;
  315. struct request *req;
  316. int queued;
  317. pr_debug("Entered do_blkif_request\n");
  318. queued = 0;
  319. while ((req = blk_peek_request(rq)) != NULL) {
  320. info = req->rq_disk->private_data;
  321. if (RING_FULL(&info->ring))
  322. goto wait;
  323. blk_start_request(req);
  324. if ((req->cmd_type != REQ_TYPE_FS) ||
  325. ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
  326. !info->flush_op)) {
  327. __blk_end_request_all(req, -EIO);
  328. continue;
  329. }
  330. pr_debug("do_blk_req %p: cmd %p, sec %lx, "
  331. "(%u/%u) buffer:%p [%s]\n",
  332. req, req->cmd, (unsigned long)blk_rq_pos(req),
  333. blk_rq_cur_sectors(req), blk_rq_sectors(req),
  334. req->buffer, rq_data_dir(req) ? "write" : "read");
  335. if (blkif_queue_request(req)) {
  336. blk_requeue_request(rq, req);
  337. wait:
  338. /* Avoid pointless unplugs. */
  339. blk_stop_queue(rq);
  340. break;
  341. }
  342. queued++;
  343. }
  344. if (queued != 0)
  345. flush_requests(info);
  346. }
  347. static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
  348. {
  349. struct request_queue *rq;
  350. struct blkfront_info *info = gd->private_data;
  351. rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
  352. if (rq == NULL)
  353. return -1;
  354. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
  355. if (info->feature_discard) {
  356. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
  357. blk_queue_max_discard_sectors(rq, get_capacity(gd));
  358. rq->limits.discard_granularity = info->discard_granularity;
  359. rq->limits.discard_alignment = info->discard_alignment;
  360. }
  361. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  362. blk_queue_logical_block_size(rq, sector_size);
  363. blk_queue_max_hw_sectors(rq, 512);
  364. /* Each segment in a request is up to an aligned page in size. */
  365. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  366. blk_queue_max_segment_size(rq, PAGE_SIZE);
  367. /* Ensure a merged request will fit in a single I/O ring slot. */
  368. blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  369. /* Make sure buffer addresses are sector-aligned. */
  370. blk_queue_dma_alignment(rq, 511);
  371. /* Make sure we don't use bounce buffers. */
  372. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  373. gd->queue = rq;
  374. return 0;
  375. }
  376. static void xlvbd_flush(struct blkfront_info *info)
  377. {
  378. blk_queue_flush(info->rq, info->feature_flush);
  379. printk(KERN_INFO "blkfront: %s: %s: %s\n",
  380. info->gd->disk_name,
  381. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  382. "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
  383. "flush diskcache" : "barrier or flush"),
  384. info->feature_flush ? "enabled" : "disabled");
  385. }
  386. static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
  387. {
  388. int major;
  389. major = BLKIF_MAJOR(vdevice);
  390. *minor = BLKIF_MINOR(vdevice);
  391. switch (major) {
  392. case XEN_IDE0_MAJOR:
  393. *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
  394. *minor = ((*minor / 64) * PARTS_PER_DISK) +
  395. EMULATED_HD_DISK_MINOR_OFFSET;
  396. break;
  397. case XEN_IDE1_MAJOR:
  398. *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
  399. *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
  400. EMULATED_HD_DISK_MINOR_OFFSET;
  401. break;
  402. case XEN_SCSI_DISK0_MAJOR:
  403. *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
  404. *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
  405. break;
  406. case XEN_SCSI_DISK1_MAJOR:
  407. case XEN_SCSI_DISK2_MAJOR:
  408. case XEN_SCSI_DISK3_MAJOR:
  409. case XEN_SCSI_DISK4_MAJOR:
  410. case XEN_SCSI_DISK5_MAJOR:
  411. case XEN_SCSI_DISK6_MAJOR:
  412. case XEN_SCSI_DISK7_MAJOR:
  413. *offset = (*minor / PARTS_PER_DISK) +
  414. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
  415. EMULATED_SD_DISK_NAME_OFFSET;
  416. *minor = *minor +
  417. ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
  418. EMULATED_SD_DISK_MINOR_OFFSET;
  419. break;
  420. case XEN_SCSI_DISK8_MAJOR:
  421. case XEN_SCSI_DISK9_MAJOR:
  422. case XEN_SCSI_DISK10_MAJOR:
  423. case XEN_SCSI_DISK11_MAJOR:
  424. case XEN_SCSI_DISK12_MAJOR:
  425. case XEN_SCSI_DISK13_MAJOR:
  426. case XEN_SCSI_DISK14_MAJOR:
  427. case XEN_SCSI_DISK15_MAJOR:
  428. *offset = (*minor / PARTS_PER_DISK) +
  429. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
  430. EMULATED_SD_DISK_NAME_OFFSET;
  431. *minor = *minor +
  432. ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
  433. EMULATED_SD_DISK_MINOR_OFFSET;
  434. break;
  435. case XENVBD_MAJOR:
  436. *offset = *minor / PARTS_PER_DISK;
  437. break;
  438. default:
  439. printk(KERN_WARNING "blkfront: your disk configuration is "
  440. "incorrect, please use an xvd device instead\n");
  441. return -ENODEV;
  442. }
  443. return 0;
  444. }
  445. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  446. struct blkfront_info *info,
  447. u16 vdisk_info, u16 sector_size)
  448. {
  449. struct gendisk *gd;
  450. int nr_minors = 1;
  451. int err;
  452. unsigned int offset;
  453. int minor;
  454. int nr_parts;
  455. BUG_ON(info->gd != NULL);
  456. BUG_ON(info->rq != NULL);
  457. if ((info->vdevice>>EXT_SHIFT) > 1) {
  458. /* this is above the extended range; something is wrong */
  459. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  460. return -ENODEV;
  461. }
  462. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  463. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  464. if (err)
  465. return err;
  466. nr_parts = PARTS_PER_DISK;
  467. } else {
  468. minor = BLKIF_MINOR_EXT(info->vdevice);
  469. nr_parts = PARTS_PER_EXT_DISK;
  470. offset = minor / nr_parts;
  471. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  472. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  473. "emulated IDE disks,\n\t choose an xvd device name"
  474. "from xvde on\n", info->vdevice);
  475. }
  476. err = -ENODEV;
  477. if ((minor % nr_parts) == 0)
  478. nr_minors = nr_parts;
  479. err = xlbd_reserve_minors(minor, nr_minors);
  480. if (err)
  481. goto out;
  482. err = -ENODEV;
  483. gd = alloc_disk(nr_minors);
  484. if (gd == NULL)
  485. goto release;
  486. if (nr_minors > 1) {
  487. if (offset < 26)
  488. sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
  489. else
  490. sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
  491. 'a' + ((offset / 26)-1), 'a' + (offset % 26));
  492. } else {
  493. if (offset < 26)
  494. sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
  495. 'a' + offset,
  496. minor & (nr_parts - 1));
  497. else
  498. sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
  499. 'a' + ((offset / 26) - 1),
  500. 'a' + (offset % 26),
  501. minor & (nr_parts - 1));
  502. }
  503. gd->major = XENVBD_MAJOR;
  504. gd->first_minor = minor;
  505. gd->fops = &xlvbd_block_fops;
  506. gd->private_data = info;
  507. gd->driverfs_dev = &(info->xbdev->dev);
  508. set_capacity(gd, capacity);
  509. if (xlvbd_init_blk_queue(gd, sector_size)) {
  510. del_gendisk(gd);
  511. goto release;
  512. }
  513. info->rq = gd->queue;
  514. info->gd = gd;
  515. xlvbd_flush(info);
  516. if (vdisk_info & VDISK_READONLY)
  517. set_disk_ro(gd, 1);
  518. if (vdisk_info & VDISK_REMOVABLE)
  519. gd->flags |= GENHD_FL_REMOVABLE;
  520. if (vdisk_info & VDISK_CDROM)
  521. gd->flags |= GENHD_FL_CD;
  522. return 0;
  523. release:
  524. xlbd_release_minors(minor, nr_minors);
  525. out:
  526. return err;
  527. }
  528. static void xlvbd_release_gendisk(struct blkfront_info *info)
  529. {
  530. unsigned int minor, nr_minors;
  531. unsigned long flags;
  532. if (info->rq == NULL)
  533. return;
  534. spin_lock_irqsave(&blkif_io_lock, flags);
  535. /* No more blkif_request(). */
  536. blk_stop_queue(info->rq);
  537. /* No more gnttab callback work. */
  538. gnttab_cancel_free_callback(&info->callback);
  539. spin_unlock_irqrestore(&blkif_io_lock, flags);
  540. /* Flush gnttab callback work. Must be done with no locks held. */
  541. flush_work_sync(&info->work);
  542. del_gendisk(info->gd);
  543. minor = info->gd->first_minor;
  544. nr_minors = info->gd->minors;
  545. xlbd_release_minors(minor, nr_minors);
  546. blk_cleanup_queue(info->rq);
  547. info->rq = NULL;
  548. put_disk(info->gd);
  549. info->gd = NULL;
  550. }
  551. static void kick_pending_request_queues(struct blkfront_info *info)
  552. {
  553. if (!RING_FULL(&info->ring)) {
  554. /* Re-enable calldowns. */
  555. blk_start_queue(info->rq);
  556. /* Kick things off immediately. */
  557. do_blkif_request(info->rq);
  558. }
  559. }
  560. static void blkif_restart_queue(struct work_struct *work)
  561. {
  562. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  563. spin_lock_irq(&blkif_io_lock);
  564. if (info->connected == BLKIF_STATE_CONNECTED)
  565. kick_pending_request_queues(info);
  566. spin_unlock_irq(&blkif_io_lock);
  567. }
  568. static void blkif_free(struct blkfront_info *info, int suspend)
  569. {
  570. /* Prevent new requests being issued until we fix things up. */
  571. spin_lock_irq(&blkif_io_lock);
  572. info->connected = suspend ?
  573. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  574. /* No more blkif_request(). */
  575. if (info->rq)
  576. blk_stop_queue(info->rq);
  577. /* No more gnttab callback work. */
  578. gnttab_cancel_free_callback(&info->callback);
  579. spin_unlock_irq(&blkif_io_lock);
  580. /* Flush gnttab callback work. Must be done with no locks held. */
  581. flush_work_sync(&info->work);
  582. /* Free resources associated with old device channel. */
  583. if (info->ring_ref != GRANT_INVALID_REF) {
  584. gnttab_end_foreign_access(info->ring_ref, 0,
  585. (unsigned long)info->ring.sring);
  586. info->ring_ref = GRANT_INVALID_REF;
  587. info->ring.sring = NULL;
  588. }
  589. if (info->irq)
  590. unbind_from_irqhandler(info->irq, info);
  591. info->evtchn = info->irq = 0;
  592. }
  593. static void blkif_completion(struct blk_shadow *s)
  594. {
  595. int i;
  596. for (i = 0; i < s->req.u.rw.nr_segments; i++)
  597. gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL);
  598. }
  599. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  600. {
  601. struct request *req;
  602. struct blkif_response *bret;
  603. RING_IDX i, rp;
  604. unsigned long flags;
  605. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  606. int error;
  607. spin_lock_irqsave(&blkif_io_lock, flags);
  608. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  609. spin_unlock_irqrestore(&blkif_io_lock, flags);
  610. return IRQ_HANDLED;
  611. }
  612. again:
  613. rp = info->ring.sring->rsp_prod;
  614. rmb(); /* Ensure we see queued responses up to 'rp'. */
  615. for (i = info->ring.rsp_cons; i != rp; i++) {
  616. unsigned long id;
  617. bret = RING_GET_RESPONSE(&info->ring, i);
  618. id = bret->id;
  619. req = info->shadow[id].request;
  620. blkif_completion(&info->shadow[id]);
  621. add_id_to_freelist(info, id);
  622. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  623. switch (bret->operation) {
  624. case BLKIF_OP_DISCARD:
  625. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  626. struct request_queue *rq = info->rq;
  627. printk(KERN_WARNING "blkfront: %s: discard op failed\n",
  628. info->gd->disk_name);
  629. error = -EOPNOTSUPP;
  630. info->feature_discard = 0;
  631. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  632. }
  633. __blk_end_request_all(req, error);
  634. break;
  635. case BLKIF_OP_FLUSH_DISKCACHE:
  636. case BLKIF_OP_WRITE_BARRIER:
  637. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  638. printk(KERN_WARNING "blkfront: %s: write %s op failed\n",
  639. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  640. "barrier" : "flush disk cache",
  641. info->gd->disk_name);
  642. error = -EOPNOTSUPP;
  643. }
  644. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  645. info->shadow[id].req.u.rw.nr_segments == 0)) {
  646. printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n",
  647. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  648. "barrier" : "flush disk cache",
  649. info->gd->disk_name);
  650. error = -EOPNOTSUPP;
  651. }
  652. if (unlikely(error)) {
  653. if (error == -EOPNOTSUPP)
  654. error = 0;
  655. info->feature_flush = 0;
  656. info->flush_op = 0;
  657. xlvbd_flush(info);
  658. }
  659. /* fall through */
  660. case BLKIF_OP_READ:
  661. case BLKIF_OP_WRITE:
  662. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  663. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  664. "request: %x\n", bret->status);
  665. __blk_end_request_all(req, error);
  666. break;
  667. default:
  668. BUG();
  669. }
  670. }
  671. info->ring.rsp_cons = i;
  672. if (i != info->ring.req_prod_pvt) {
  673. int more_to_do;
  674. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  675. if (more_to_do)
  676. goto again;
  677. } else
  678. info->ring.sring->rsp_event = i + 1;
  679. kick_pending_request_queues(info);
  680. spin_unlock_irqrestore(&blkif_io_lock, flags);
  681. return IRQ_HANDLED;
  682. }
  683. static int setup_blkring(struct xenbus_device *dev,
  684. struct blkfront_info *info)
  685. {
  686. struct blkif_sring *sring;
  687. int err;
  688. info->ring_ref = GRANT_INVALID_REF;
  689. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  690. if (!sring) {
  691. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  692. return -ENOMEM;
  693. }
  694. SHARED_RING_INIT(sring);
  695. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  696. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  697. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  698. if (err < 0) {
  699. free_page((unsigned long)sring);
  700. info->ring.sring = NULL;
  701. goto fail;
  702. }
  703. info->ring_ref = err;
  704. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  705. if (err)
  706. goto fail;
  707. err = bind_evtchn_to_irqhandler(info->evtchn,
  708. blkif_interrupt,
  709. IRQF_SAMPLE_RANDOM, "blkif", info);
  710. if (err <= 0) {
  711. xenbus_dev_fatal(dev, err,
  712. "bind_evtchn_to_irqhandler failed");
  713. goto fail;
  714. }
  715. info->irq = err;
  716. return 0;
  717. fail:
  718. blkif_free(info, 0);
  719. return err;
  720. }
  721. /* Common code used when first setting up, and when resuming. */
  722. static int talk_to_blkback(struct xenbus_device *dev,
  723. struct blkfront_info *info)
  724. {
  725. const char *message = NULL;
  726. struct xenbus_transaction xbt;
  727. int err;
  728. /* Create shared ring, alloc event channel. */
  729. err = setup_blkring(dev, info);
  730. if (err)
  731. goto out;
  732. again:
  733. err = xenbus_transaction_start(&xbt);
  734. if (err) {
  735. xenbus_dev_fatal(dev, err, "starting transaction");
  736. goto destroy_blkring;
  737. }
  738. err = xenbus_printf(xbt, dev->nodename,
  739. "ring-ref", "%u", info->ring_ref);
  740. if (err) {
  741. message = "writing ring-ref";
  742. goto abort_transaction;
  743. }
  744. err = xenbus_printf(xbt, dev->nodename,
  745. "event-channel", "%u", info->evtchn);
  746. if (err) {
  747. message = "writing event-channel";
  748. goto abort_transaction;
  749. }
  750. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  751. XEN_IO_PROTO_ABI_NATIVE);
  752. if (err) {
  753. message = "writing protocol";
  754. goto abort_transaction;
  755. }
  756. err = xenbus_transaction_end(xbt, 0);
  757. if (err) {
  758. if (err == -EAGAIN)
  759. goto again;
  760. xenbus_dev_fatal(dev, err, "completing transaction");
  761. goto destroy_blkring;
  762. }
  763. xenbus_switch_state(dev, XenbusStateInitialised);
  764. return 0;
  765. abort_transaction:
  766. xenbus_transaction_end(xbt, 1);
  767. if (message)
  768. xenbus_dev_fatal(dev, err, "%s", message);
  769. destroy_blkring:
  770. blkif_free(info, 0);
  771. out:
  772. return err;
  773. }
  774. /**
  775. * Entry point to this code when a new device is created. Allocate the basic
  776. * structures and the ring buffer for communication with the backend, and
  777. * inform the backend of the appropriate details for those. Switch to
  778. * Initialised state.
  779. */
  780. static int blkfront_probe(struct xenbus_device *dev,
  781. const struct xenbus_device_id *id)
  782. {
  783. int err, vdevice, i;
  784. struct blkfront_info *info;
  785. /* FIXME: Use dynamic device id if this is not set. */
  786. err = xenbus_scanf(XBT_NIL, dev->nodename,
  787. "virtual-device", "%i", &vdevice);
  788. if (err != 1) {
  789. /* go looking in the extended area instead */
  790. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  791. "%i", &vdevice);
  792. if (err != 1) {
  793. xenbus_dev_fatal(dev, err, "reading virtual-device");
  794. return err;
  795. }
  796. }
  797. if (xen_hvm_domain()) {
  798. char *type;
  799. int len;
  800. /* no unplug has been done: do not hook devices != xen vbds */
  801. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  802. int major;
  803. if (!VDEV_IS_EXTENDED(vdevice))
  804. major = BLKIF_MAJOR(vdevice);
  805. else
  806. major = XENVBD_MAJOR;
  807. if (major != XENVBD_MAJOR) {
  808. printk(KERN_INFO
  809. "%s: HVM does not support vbd %d as xen block device\n",
  810. __FUNCTION__, vdevice);
  811. return -ENODEV;
  812. }
  813. }
  814. /* do not create a PV cdrom device if we are an HVM guest */
  815. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  816. if (IS_ERR(type))
  817. return -ENODEV;
  818. if (strncmp(type, "cdrom", 5) == 0) {
  819. kfree(type);
  820. return -ENODEV;
  821. }
  822. kfree(type);
  823. }
  824. info = kzalloc(sizeof(*info), GFP_KERNEL);
  825. if (!info) {
  826. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  827. return -ENOMEM;
  828. }
  829. mutex_init(&info->mutex);
  830. info->xbdev = dev;
  831. info->vdevice = vdevice;
  832. info->connected = BLKIF_STATE_DISCONNECTED;
  833. INIT_WORK(&info->work, blkif_restart_queue);
  834. for (i = 0; i < BLK_RING_SIZE; i++)
  835. info->shadow[i].req.u.rw.id = i+1;
  836. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  837. /* Front end dir is a number, which is used as the id. */
  838. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  839. dev_set_drvdata(&dev->dev, info);
  840. err = talk_to_blkback(dev, info);
  841. if (err) {
  842. kfree(info);
  843. dev_set_drvdata(&dev->dev, NULL);
  844. return err;
  845. }
  846. return 0;
  847. }
  848. static int blkif_recover(struct blkfront_info *info)
  849. {
  850. int i;
  851. struct blkif_request *req;
  852. struct blk_shadow *copy;
  853. int j;
  854. /* Stage 1: Make a safe copy of the shadow state. */
  855. copy = kmalloc(sizeof(info->shadow),
  856. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  857. if (!copy)
  858. return -ENOMEM;
  859. memcpy(copy, info->shadow, sizeof(info->shadow));
  860. /* Stage 2: Set up free list. */
  861. memset(&info->shadow, 0, sizeof(info->shadow));
  862. for (i = 0; i < BLK_RING_SIZE; i++)
  863. info->shadow[i].req.u.rw.id = i+1;
  864. info->shadow_free = info->ring.req_prod_pvt;
  865. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  866. /* Stage 3: Find pending requests and requeue them. */
  867. for (i = 0; i < BLK_RING_SIZE; i++) {
  868. /* Not in use? */
  869. if (!copy[i].request)
  870. continue;
  871. /* Grab a request slot and copy shadow state into it. */
  872. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  873. *req = copy[i].req;
  874. /* We get a new request id, and must reset the shadow state. */
  875. req->u.rw.id = get_id_from_freelist(info);
  876. memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
  877. /* Rewrite any grant references invalidated by susp/resume. */
  878. for (j = 0; j < req->u.rw.nr_segments; j++)
  879. gnttab_grant_foreign_access_ref(
  880. req->u.rw.seg[j].gref,
  881. info->xbdev->otherend_id,
  882. pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
  883. rq_data_dir(info->shadow[req->u.rw.id].request));
  884. info->shadow[req->u.rw.id].req = *req;
  885. info->ring.req_prod_pvt++;
  886. }
  887. kfree(copy);
  888. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  889. spin_lock_irq(&blkif_io_lock);
  890. /* Now safe for us to use the shared ring */
  891. info->connected = BLKIF_STATE_CONNECTED;
  892. /* Send off requeued requests */
  893. flush_requests(info);
  894. /* Kick any other new requests queued since we resumed */
  895. kick_pending_request_queues(info);
  896. spin_unlock_irq(&blkif_io_lock);
  897. return 0;
  898. }
  899. /**
  900. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  901. * driver restart. We tear down our blkif structure and recreate it, but
  902. * leave the device-layer structures intact so that this is transparent to the
  903. * rest of the kernel.
  904. */
  905. static int blkfront_resume(struct xenbus_device *dev)
  906. {
  907. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  908. int err;
  909. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  910. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  911. err = talk_to_blkback(dev, info);
  912. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  913. err = blkif_recover(info);
  914. return err;
  915. }
  916. static void
  917. blkfront_closing(struct blkfront_info *info)
  918. {
  919. struct xenbus_device *xbdev = info->xbdev;
  920. struct block_device *bdev = NULL;
  921. mutex_lock(&info->mutex);
  922. if (xbdev->state == XenbusStateClosing) {
  923. mutex_unlock(&info->mutex);
  924. return;
  925. }
  926. if (info->gd)
  927. bdev = bdget_disk(info->gd, 0);
  928. mutex_unlock(&info->mutex);
  929. if (!bdev) {
  930. xenbus_frontend_closed(xbdev);
  931. return;
  932. }
  933. mutex_lock(&bdev->bd_mutex);
  934. if (bdev->bd_openers) {
  935. xenbus_dev_error(xbdev, -EBUSY,
  936. "Device in use; refusing to close");
  937. xenbus_switch_state(xbdev, XenbusStateClosing);
  938. } else {
  939. xlvbd_release_gendisk(info);
  940. xenbus_frontend_closed(xbdev);
  941. }
  942. mutex_unlock(&bdev->bd_mutex);
  943. bdput(bdev);
  944. }
  945. static void blkfront_setup_discard(struct blkfront_info *info)
  946. {
  947. int err;
  948. char *type;
  949. unsigned int discard_granularity;
  950. unsigned int discard_alignment;
  951. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  952. if (IS_ERR(type))
  953. return;
  954. if (strncmp(type, "phy", 3) == 0) {
  955. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  956. "discard-granularity", "%u", &discard_granularity,
  957. "discard-alignment", "%u", &discard_alignment,
  958. NULL);
  959. if (!err) {
  960. info->feature_discard = 1;
  961. info->discard_granularity = discard_granularity;
  962. info->discard_alignment = discard_alignment;
  963. }
  964. } else if (strncmp(type, "file", 4) == 0)
  965. info->feature_discard = 1;
  966. kfree(type);
  967. }
  968. /*
  969. * Invoked when the backend is finally 'ready' (and has told produced
  970. * the details about the physical device - #sectors, size, etc).
  971. */
  972. static void blkfront_connect(struct blkfront_info *info)
  973. {
  974. unsigned long long sectors;
  975. unsigned long sector_size;
  976. unsigned int binfo;
  977. int err;
  978. int barrier, flush, discard;
  979. switch (info->connected) {
  980. case BLKIF_STATE_CONNECTED:
  981. /*
  982. * Potentially, the back-end may be signalling
  983. * a capacity change; update the capacity.
  984. */
  985. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  986. "sectors", "%Lu", &sectors);
  987. if (XENBUS_EXIST_ERR(err))
  988. return;
  989. printk(KERN_INFO "Setting capacity to %Lu\n",
  990. sectors);
  991. set_capacity(info->gd, sectors);
  992. revalidate_disk(info->gd);
  993. /* fall through */
  994. case BLKIF_STATE_SUSPENDED:
  995. return;
  996. default:
  997. break;
  998. }
  999. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1000. __func__, info->xbdev->otherend);
  1001. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1002. "sectors", "%llu", &sectors,
  1003. "info", "%u", &binfo,
  1004. "sector-size", "%lu", &sector_size,
  1005. NULL);
  1006. if (err) {
  1007. xenbus_dev_fatal(info->xbdev, err,
  1008. "reading backend fields at %s",
  1009. info->xbdev->otherend);
  1010. return;
  1011. }
  1012. info->feature_flush = 0;
  1013. info->flush_op = 0;
  1014. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1015. "feature-barrier", "%d", &barrier,
  1016. NULL);
  1017. /*
  1018. * If there's no "feature-barrier" defined, then it means
  1019. * we're dealing with a very old backend which writes
  1020. * synchronously; nothing to do.
  1021. *
  1022. * If there are barriers, then we use flush.
  1023. */
  1024. if (!err && barrier) {
  1025. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1026. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1027. }
  1028. /*
  1029. * And if there is "feature-flush-cache" use that above
  1030. * barriers.
  1031. */
  1032. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1033. "feature-flush-cache", "%d", &flush,
  1034. NULL);
  1035. if (!err && flush) {
  1036. info->feature_flush = REQ_FLUSH;
  1037. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1038. }
  1039. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1040. "feature-discard", "%d", &discard,
  1041. NULL);
  1042. if (!err && discard)
  1043. blkfront_setup_discard(info);
  1044. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  1045. if (err) {
  1046. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1047. info->xbdev->otherend);
  1048. return;
  1049. }
  1050. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1051. /* Kick pending requests. */
  1052. spin_lock_irq(&blkif_io_lock);
  1053. info->connected = BLKIF_STATE_CONNECTED;
  1054. kick_pending_request_queues(info);
  1055. spin_unlock_irq(&blkif_io_lock);
  1056. add_disk(info->gd);
  1057. info->is_ready = 1;
  1058. }
  1059. /**
  1060. * Callback received when the backend's state changes.
  1061. */
  1062. static void blkback_changed(struct xenbus_device *dev,
  1063. enum xenbus_state backend_state)
  1064. {
  1065. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1066. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1067. switch (backend_state) {
  1068. case XenbusStateInitialising:
  1069. case XenbusStateInitWait:
  1070. case XenbusStateInitialised:
  1071. case XenbusStateReconfiguring:
  1072. case XenbusStateReconfigured:
  1073. case XenbusStateUnknown:
  1074. case XenbusStateClosed:
  1075. break;
  1076. case XenbusStateConnected:
  1077. blkfront_connect(info);
  1078. break;
  1079. case XenbusStateClosing:
  1080. blkfront_closing(info);
  1081. break;
  1082. }
  1083. }
  1084. static int blkfront_remove(struct xenbus_device *xbdev)
  1085. {
  1086. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1087. struct block_device *bdev = NULL;
  1088. struct gendisk *disk;
  1089. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1090. blkif_free(info, 0);
  1091. mutex_lock(&info->mutex);
  1092. disk = info->gd;
  1093. if (disk)
  1094. bdev = bdget_disk(disk, 0);
  1095. info->xbdev = NULL;
  1096. mutex_unlock(&info->mutex);
  1097. if (!bdev) {
  1098. kfree(info);
  1099. return 0;
  1100. }
  1101. /*
  1102. * The xbdev was removed before we reached the Closed
  1103. * state. See if it's safe to remove the disk. If the bdev
  1104. * isn't closed yet, we let release take care of it.
  1105. */
  1106. mutex_lock(&bdev->bd_mutex);
  1107. info = disk->private_data;
  1108. dev_warn(disk_to_dev(disk),
  1109. "%s was hot-unplugged, %d stale handles\n",
  1110. xbdev->nodename, bdev->bd_openers);
  1111. if (info && !bdev->bd_openers) {
  1112. xlvbd_release_gendisk(info);
  1113. disk->private_data = NULL;
  1114. kfree(info);
  1115. }
  1116. mutex_unlock(&bdev->bd_mutex);
  1117. bdput(bdev);
  1118. return 0;
  1119. }
  1120. static int blkfront_is_ready(struct xenbus_device *dev)
  1121. {
  1122. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1123. return info->is_ready && info->xbdev;
  1124. }
  1125. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1126. {
  1127. struct gendisk *disk = bdev->bd_disk;
  1128. struct blkfront_info *info;
  1129. int err = 0;
  1130. mutex_lock(&blkfront_mutex);
  1131. info = disk->private_data;
  1132. if (!info) {
  1133. /* xbdev gone */
  1134. err = -ERESTARTSYS;
  1135. goto out;
  1136. }
  1137. mutex_lock(&info->mutex);
  1138. if (!info->gd)
  1139. /* xbdev is closed */
  1140. err = -ERESTARTSYS;
  1141. mutex_unlock(&info->mutex);
  1142. out:
  1143. mutex_unlock(&blkfront_mutex);
  1144. return err;
  1145. }
  1146. static int blkif_release(struct gendisk *disk, fmode_t mode)
  1147. {
  1148. struct blkfront_info *info = disk->private_data;
  1149. struct block_device *bdev;
  1150. struct xenbus_device *xbdev;
  1151. mutex_lock(&blkfront_mutex);
  1152. bdev = bdget_disk(disk, 0);
  1153. bdput(bdev);
  1154. if (bdev->bd_openers)
  1155. goto out;
  1156. /*
  1157. * Check if we have been instructed to close. We will have
  1158. * deferred this request, because the bdev was still open.
  1159. */
  1160. mutex_lock(&info->mutex);
  1161. xbdev = info->xbdev;
  1162. if (xbdev && xbdev->state == XenbusStateClosing) {
  1163. /* pending switch to state closed */
  1164. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1165. xlvbd_release_gendisk(info);
  1166. xenbus_frontend_closed(info->xbdev);
  1167. }
  1168. mutex_unlock(&info->mutex);
  1169. if (!xbdev) {
  1170. /* sudden device removal */
  1171. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1172. xlvbd_release_gendisk(info);
  1173. disk->private_data = NULL;
  1174. kfree(info);
  1175. }
  1176. out:
  1177. mutex_unlock(&blkfront_mutex);
  1178. return 0;
  1179. }
  1180. static const struct block_device_operations xlvbd_block_fops =
  1181. {
  1182. .owner = THIS_MODULE,
  1183. .open = blkif_open,
  1184. .release = blkif_release,
  1185. .getgeo = blkif_getgeo,
  1186. .ioctl = blkif_ioctl,
  1187. };
  1188. static const struct xenbus_device_id blkfront_ids[] = {
  1189. { "vbd" },
  1190. { "" }
  1191. };
  1192. static struct xenbus_driver blkfront = {
  1193. .name = "vbd",
  1194. .owner = THIS_MODULE,
  1195. .ids = blkfront_ids,
  1196. .probe = blkfront_probe,
  1197. .remove = blkfront_remove,
  1198. .resume = blkfront_resume,
  1199. .otherend_changed = blkback_changed,
  1200. .is_ready = blkfront_is_ready,
  1201. };
  1202. static int __init xlblk_init(void)
  1203. {
  1204. int ret;
  1205. if (!xen_domain())
  1206. return -ENODEV;
  1207. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1208. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1209. XENVBD_MAJOR, DEV_NAME);
  1210. return -ENODEV;
  1211. }
  1212. ret = xenbus_register_frontend(&blkfront);
  1213. if (ret) {
  1214. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1215. return ret;
  1216. }
  1217. return 0;
  1218. }
  1219. module_init(xlblk_init);
  1220. static void __exit xlblk_exit(void)
  1221. {
  1222. return xenbus_unregister_driver(&blkfront);
  1223. }
  1224. module_exit(xlblk_exit);
  1225. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1226. MODULE_LICENSE("GPL");
  1227. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1228. MODULE_ALIAS("xen:vbd");
  1229. MODULE_ALIAS("xenblk");