xen-blkfront.c 36 KB

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