xen-blkfront.c 37 KB

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