xen-blkfront.c 37 KB

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