xen-blkfront.c 36 KB

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