xen-blkfront.c 38 KB

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