xen-blkfront.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * blkfront.c
  3. *
  4. * XenLinux virtual block device driver.
  5. *
  6. * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
  7. * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
  8. * Copyright (c) 2004, Christian Limpach
  9. * Copyright (c) 2004, Andrew Warfield
  10. * Copyright (c) 2005, Christopher Clark
  11. * Copyright (c) 2005, XenSource Ltd
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation; or, when distributed
  16. * separately from the Linux kernel or incorporated into other
  17. * software packages, subject to the following license:
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this source file (the "Software"), to deal in the Software without
  21. * restriction, including without limitation the rights to use, copy, modify,
  22. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  23. * and to permit persons to whom the Software is furnished to do so, subject to
  24. * the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  35. * IN THE SOFTWARE.
  36. */
  37. #include <linux/interrupt.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/cdrom.h>
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include <linux/mutex.h>
  44. #include <linux/scatterlist.h>
  45. #include <linux/bitmap.h>
  46. #include <xen/xen.h>
  47. #include <xen/xenbus.h>
  48. #include <xen/grant_table.h>
  49. #include <xen/events.h>
  50. #include <xen/page.h>
  51. #include <xen/platform_pci.h>
  52. #include <xen/interface/grant_table.h>
  53. #include <xen/interface/io/blkif.h>
  54. #include <xen/interface/io/protocols.h>
  55. #include <asm/xen/hypervisor.h>
  56. enum blkif_state {
  57. BLKIF_STATE_DISCONNECTED,
  58. BLKIF_STATE_CONNECTED,
  59. BLKIF_STATE_SUSPENDED,
  60. };
  61. struct blk_shadow {
  62. struct blkif_request req;
  63. struct request *request;
  64. unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  65. };
  66. static DEFINE_MUTEX(blkfront_mutex);
  67. static const struct block_device_operations xlvbd_block_fops;
  68. #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
  69. /*
  70. * We have one of these per vbd, whether ide, scsi or 'other'. They
  71. * hang in private_data off the gendisk structure. We may end up
  72. * putting all kinds of interesting stuff here :-)
  73. */
  74. struct blkfront_info
  75. {
  76. spinlock_t io_lock;
  77. struct mutex mutex;
  78. struct xenbus_device *xbdev;
  79. struct gendisk *gd;
  80. int vdevice;
  81. blkif_vdev_t handle;
  82. enum blkif_state connected;
  83. int ring_ref;
  84. struct blkif_front_ring ring;
  85. struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  86. unsigned int evtchn, irq;
  87. struct request_queue *rq;
  88. struct work_struct work;
  89. struct gnttab_free_callback callback;
  90. struct blk_shadow shadow[BLK_RING_SIZE];
  91. unsigned long shadow_free;
  92. unsigned int feature_flush;
  93. unsigned int flush_op;
  94. unsigned int feature_discard:1;
  95. unsigned int feature_secdiscard:1;
  96. unsigned int discard_granularity;
  97. unsigned int discard_alignment;
  98. int is_ready;
  99. };
  100. static unsigned int nr_minors;
  101. static unsigned long *minors;
  102. static DEFINE_SPINLOCK(minor_lock);
  103. #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
  104. (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
  105. #define GRANT_INVALID_REF 0
  106. #define PARTS_PER_DISK 16
  107. #define PARTS_PER_EXT_DISK 256
  108. #define BLKIF_MAJOR(dev) ((dev)>>8)
  109. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  110. #define EXT_SHIFT 28
  111. #define EXTENDED (1<<EXT_SHIFT)
  112. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  113. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  114. #define EMULATED_HD_DISK_MINOR_OFFSET (0)
  115. #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
  116. #define EMULATED_SD_DISK_MINOR_OFFSET (0)
  117. #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
  118. #define DEV_NAME "xvd" /* name in /dev */
  119. static int get_id_from_freelist(struct blkfront_info *info)
  120. {
  121. unsigned long free = info->shadow_free;
  122. BUG_ON(free >= BLK_RING_SIZE);
  123. info->shadow_free = info->shadow[free].req.u.rw.id;
  124. info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
  125. return free;
  126. }
  127. static 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, &info->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 char *encode_disk_name(char *ptr, unsigned int n)
  451. {
  452. if (n >= 26)
  453. ptr = encode_disk_name(ptr, n / 26 - 1);
  454. *ptr = 'a' + n % 26;
  455. return ptr + 1;
  456. }
  457. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  458. struct blkfront_info *info,
  459. u16 vdisk_info, u16 sector_size)
  460. {
  461. struct gendisk *gd;
  462. int nr_minors = 1;
  463. int err;
  464. unsigned int offset;
  465. int minor;
  466. int nr_parts;
  467. char *ptr;
  468. BUG_ON(info->gd != NULL);
  469. BUG_ON(info->rq != NULL);
  470. if ((info->vdevice>>EXT_SHIFT) > 1) {
  471. /* this is above the extended range; something is wrong */
  472. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  473. return -ENODEV;
  474. }
  475. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  476. err = xen_translate_vdev(info->vdevice, &minor, &offset);
  477. if (err)
  478. return err;
  479. nr_parts = PARTS_PER_DISK;
  480. } else {
  481. minor = BLKIF_MINOR_EXT(info->vdevice);
  482. nr_parts = PARTS_PER_EXT_DISK;
  483. offset = minor / nr_parts;
  484. if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
  485. printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
  486. "emulated IDE disks,\n\t choose an xvd device name"
  487. "from xvde on\n", info->vdevice);
  488. }
  489. if (minor >> MINORBITS) {
  490. pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
  491. info->vdevice, minor);
  492. return -ENODEV;
  493. }
  494. if ((minor % nr_parts) == 0)
  495. nr_minors = nr_parts;
  496. err = xlbd_reserve_minors(minor, nr_minors);
  497. if (err)
  498. goto out;
  499. err = -ENODEV;
  500. gd = alloc_disk(nr_minors);
  501. if (gd == NULL)
  502. goto release;
  503. strcpy(gd->disk_name, DEV_NAME);
  504. ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
  505. BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
  506. if (nr_minors > 1)
  507. *ptr = 0;
  508. else
  509. snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
  510. "%d", minor & (nr_parts - 1));
  511. gd->major = XENVBD_MAJOR;
  512. gd->first_minor = minor;
  513. gd->fops = &xlvbd_block_fops;
  514. gd->private_data = info;
  515. gd->driverfs_dev = &(info->xbdev->dev);
  516. set_capacity(gd, capacity);
  517. if (xlvbd_init_blk_queue(gd, sector_size)) {
  518. del_gendisk(gd);
  519. goto release;
  520. }
  521. info->rq = gd->queue;
  522. info->gd = gd;
  523. xlvbd_flush(info);
  524. if (vdisk_info & VDISK_READONLY)
  525. set_disk_ro(gd, 1);
  526. if (vdisk_info & VDISK_REMOVABLE)
  527. gd->flags |= GENHD_FL_REMOVABLE;
  528. if (vdisk_info & VDISK_CDROM)
  529. gd->flags |= GENHD_FL_CD;
  530. return 0;
  531. release:
  532. xlbd_release_minors(minor, nr_minors);
  533. out:
  534. return err;
  535. }
  536. static void xlvbd_release_gendisk(struct blkfront_info *info)
  537. {
  538. unsigned int minor, nr_minors;
  539. unsigned long flags;
  540. if (info->rq == NULL)
  541. return;
  542. spin_lock_irqsave(&info->io_lock, flags);
  543. /* No more blkif_request(). */
  544. blk_stop_queue(info->rq);
  545. /* No more gnttab callback work. */
  546. gnttab_cancel_free_callback(&info->callback);
  547. spin_unlock_irqrestore(&info->io_lock, flags);
  548. /* Flush gnttab callback work. Must be done with no locks held. */
  549. flush_work_sync(&info->work);
  550. del_gendisk(info->gd);
  551. minor = info->gd->first_minor;
  552. nr_minors = info->gd->minors;
  553. xlbd_release_minors(minor, nr_minors);
  554. blk_cleanup_queue(info->rq);
  555. info->rq = NULL;
  556. put_disk(info->gd);
  557. info->gd = NULL;
  558. }
  559. static void kick_pending_request_queues(struct blkfront_info *info)
  560. {
  561. if (!RING_FULL(&info->ring)) {
  562. /* Re-enable calldowns. */
  563. blk_start_queue(info->rq);
  564. /* Kick things off immediately. */
  565. do_blkif_request(info->rq);
  566. }
  567. }
  568. static void blkif_restart_queue(struct work_struct *work)
  569. {
  570. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  571. spin_lock_irq(&info->io_lock);
  572. if (info->connected == BLKIF_STATE_CONNECTED)
  573. kick_pending_request_queues(info);
  574. spin_unlock_irq(&info->io_lock);
  575. }
  576. static void blkif_free(struct blkfront_info *info, int suspend)
  577. {
  578. /* Prevent new requests being issued until we fix things up. */
  579. spin_lock_irq(&info->io_lock);
  580. info->connected = suspend ?
  581. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  582. /* No more blkif_request(). */
  583. if (info->rq)
  584. blk_stop_queue(info->rq);
  585. /* No more gnttab callback work. */
  586. gnttab_cancel_free_callback(&info->callback);
  587. spin_unlock_irq(&info->io_lock);
  588. /* Flush gnttab callback work. Must be done with no locks held. */
  589. flush_work_sync(&info->work);
  590. /* Free resources associated with old device channel. */
  591. if (info->ring_ref != GRANT_INVALID_REF) {
  592. gnttab_end_foreign_access(info->ring_ref, 0,
  593. (unsigned long)info->ring.sring);
  594. info->ring_ref = GRANT_INVALID_REF;
  595. info->ring.sring = NULL;
  596. }
  597. if (info->irq)
  598. unbind_from_irqhandler(info->irq, info);
  599. info->evtchn = info->irq = 0;
  600. }
  601. static void blkif_completion(struct blk_shadow *s)
  602. {
  603. int i;
  604. /* Do not let BLKIF_OP_DISCARD as nr_segment is in the same place
  605. * flag. */
  606. for (i = 0; i < s->req.u.rw.nr_segments; i++)
  607. gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL);
  608. }
  609. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  610. {
  611. struct request *req;
  612. struct blkif_response *bret;
  613. RING_IDX i, rp;
  614. unsigned long flags;
  615. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  616. int error;
  617. spin_lock_irqsave(&info->io_lock, flags);
  618. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  619. spin_unlock_irqrestore(&info->io_lock, flags);
  620. return IRQ_HANDLED;
  621. }
  622. again:
  623. rp = info->ring.sring->rsp_prod;
  624. rmb(); /* Ensure we see queued responses up to 'rp'. */
  625. for (i = info->ring.rsp_cons; i != rp; i++) {
  626. unsigned long id;
  627. bret = RING_GET_RESPONSE(&info->ring, i);
  628. id = bret->id;
  629. req = info->shadow[id].request;
  630. if (bret->operation != BLKIF_OP_DISCARD)
  631. blkif_completion(&info->shadow[id]);
  632. add_id_to_freelist(info, id);
  633. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  634. switch (bret->operation) {
  635. case BLKIF_OP_DISCARD:
  636. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  637. struct request_queue *rq = info->rq;
  638. printk(KERN_WARNING "blkfront: %s: discard op failed\n",
  639. info->gd->disk_name);
  640. error = -EOPNOTSUPP;
  641. info->feature_discard = 0;
  642. info->feature_secdiscard = 0;
  643. queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
  644. queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
  645. }
  646. __blk_end_request_all(req, error);
  647. break;
  648. case BLKIF_OP_FLUSH_DISKCACHE:
  649. case BLKIF_OP_WRITE_BARRIER:
  650. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  651. printk(KERN_WARNING "blkfront: %s: write %s op failed\n",
  652. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  653. "barrier" : "flush disk cache",
  654. info->gd->disk_name);
  655. error = -EOPNOTSUPP;
  656. }
  657. if (unlikely(bret->status == BLKIF_RSP_ERROR &&
  658. info->shadow[id].req.u.rw.nr_segments == 0)) {
  659. printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n",
  660. info->flush_op == BLKIF_OP_WRITE_BARRIER ?
  661. "barrier" : "flush disk cache",
  662. info->gd->disk_name);
  663. error = -EOPNOTSUPP;
  664. }
  665. if (unlikely(error)) {
  666. if (error == -EOPNOTSUPP)
  667. error = 0;
  668. info->feature_flush = 0;
  669. info->flush_op = 0;
  670. xlvbd_flush(info);
  671. }
  672. /* fall through */
  673. case BLKIF_OP_READ:
  674. case BLKIF_OP_WRITE:
  675. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  676. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  677. "request: %x\n", bret->status);
  678. __blk_end_request_all(req, error);
  679. break;
  680. default:
  681. BUG();
  682. }
  683. }
  684. info->ring.rsp_cons = i;
  685. if (i != info->ring.req_prod_pvt) {
  686. int more_to_do;
  687. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  688. if (more_to_do)
  689. goto again;
  690. } else
  691. info->ring.sring->rsp_event = i + 1;
  692. kick_pending_request_queues(info);
  693. spin_unlock_irqrestore(&info->io_lock, flags);
  694. return IRQ_HANDLED;
  695. }
  696. static int setup_blkring(struct xenbus_device *dev,
  697. struct blkfront_info *info)
  698. {
  699. struct blkif_sring *sring;
  700. int err;
  701. info->ring_ref = GRANT_INVALID_REF;
  702. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  703. if (!sring) {
  704. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  705. return -ENOMEM;
  706. }
  707. SHARED_RING_INIT(sring);
  708. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  709. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  710. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  711. if (err < 0) {
  712. free_page((unsigned long)sring);
  713. info->ring.sring = NULL;
  714. goto fail;
  715. }
  716. info->ring_ref = err;
  717. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  718. if (err)
  719. goto fail;
  720. err = bind_evtchn_to_irqhandler(info->evtchn,
  721. blkif_interrupt,
  722. IRQF_SAMPLE_RANDOM, "blkif", info);
  723. if (err <= 0) {
  724. xenbus_dev_fatal(dev, err,
  725. "bind_evtchn_to_irqhandler failed");
  726. goto fail;
  727. }
  728. info->irq = err;
  729. return 0;
  730. fail:
  731. blkif_free(info, 0);
  732. return err;
  733. }
  734. /* Common code used when first setting up, and when resuming. */
  735. static int talk_to_blkback(struct xenbus_device *dev,
  736. struct blkfront_info *info)
  737. {
  738. const char *message = NULL;
  739. struct xenbus_transaction xbt;
  740. int err;
  741. /* Create shared ring, alloc event channel. */
  742. err = setup_blkring(dev, info);
  743. if (err)
  744. goto out;
  745. again:
  746. err = xenbus_transaction_start(&xbt);
  747. if (err) {
  748. xenbus_dev_fatal(dev, err, "starting transaction");
  749. goto destroy_blkring;
  750. }
  751. err = xenbus_printf(xbt, dev->nodename,
  752. "ring-ref", "%u", info->ring_ref);
  753. if (err) {
  754. message = "writing ring-ref";
  755. goto abort_transaction;
  756. }
  757. err = xenbus_printf(xbt, dev->nodename,
  758. "event-channel", "%u", info->evtchn);
  759. if (err) {
  760. message = "writing event-channel";
  761. goto abort_transaction;
  762. }
  763. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  764. XEN_IO_PROTO_ABI_NATIVE);
  765. if (err) {
  766. message = "writing protocol";
  767. goto abort_transaction;
  768. }
  769. err = xenbus_transaction_end(xbt, 0);
  770. if (err) {
  771. if (err == -EAGAIN)
  772. goto again;
  773. xenbus_dev_fatal(dev, err, "completing transaction");
  774. goto destroy_blkring;
  775. }
  776. xenbus_switch_state(dev, XenbusStateInitialised);
  777. return 0;
  778. abort_transaction:
  779. xenbus_transaction_end(xbt, 1);
  780. if (message)
  781. xenbus_dev_fatal(dev, err, "%s", message);
  782. destroy_blkring:
  783. blkif_free(info, 0);
  784. out:
  785. return err;
  786. }
  787. /**
  788. * Entry point to this code when a new device is created. Allocate the basic
  789. * structures and the ring buffer for communication with the backend, and
  790. * inform the backend of the appropriate details for those. Switch to
  791. * Initialised state.
  792. */
  793. static int blkfront_probe(struct xenbus_device *dev,
  794. const struct xenbus_device_id *id)
  795. {
  796. int err, vdevice, i;
  797. struct blkfront_info *info;
  798. /* FIXME: Use dynamic device id if this is not set. */
  799. err = xenbus_scanf(XBT_NIL, dev->nodename,
  800. "virtual-device", "%i", &vdevice);
  801. if (err != 1) {
  802. /* go looking in the extended area instead */
  803. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  804. "%i", &vdevice);
  805. if (err != 1) {
  806. xenbus_dev_fatal(dev, err, "reading virtual-device");
  807. return err;
  808. }
  809. }
  810. if (xen_hvm_domain()) {
  811. char *type;
  812. int len;
  813. /* no unplug has been done: do not hook devices != xen vbds */
  814. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  815. int major;
  816. if (!VDEV_IS_EXTENDED(vdevice))
  817. major = BLKIF_MAJOR(vdevice);
  818. else
  819. major = XENVBD_MAJOR;
  820. if (major != XENVBD_MAJOR) {
  821. printk(KERN_INFO
  822. "%s: HVM does not support vbd %d as xen block device\n",
  823. __FUNCTION__, vdevice);
  824. return -ENODEV;
  825. }
  826. }
  827. /* do not create a PV cdrom device if we are an HVM guest */
  828. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  829. if (IS_ERR(type))
  830. return -ENODEV;
  831. if (strncmp(type, "cdrom", 5) == 0) {
  832. kfree(type);
  833. return -ENODEV;
  834. }
  835. kfree(type);
  836. }
  837. info = kzalloc(sizeof(*info), GFP_KERNEL);
  838. if (!info) {
  839. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  840. return -ENOMEM;
  841. }
  842. mutex_init(&info->mutex);
  843. spin_lock_init(&info->io_lock);
  844. info->xbdev = dev;
  845. info->vdevice = vdevice;
  846. info->connected = BLKIF_STATE_DISCONNECTED;
  847. INIT_WORK(&info->work, blkif_restart_queue);
  848. for (i = 0; i < BLK_RING_SIZE; i++)
  849. info->shadow[i].req.u.rw.id = i+1;
  850. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  851. /* Front end dir is a number, which is used as the id. */
  852. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  853. dev_set_drvdata(&dev->dev, info);
  854. err = talk_to_blkback(dev, info);
  855. if (err) {
  856. kfree(info);
  857. dev_set_drvdata(&dev->dev, NULL);
  858. return err;
  859. }
  860. return 0;
  861. }
  862. static int blkif_recover(struct blkfront_info *info)
  863. {
  864. int i;
  865. struct blkif_request *req;
  866. struct blk_shadow *copy;
  867. int j;
  868. /* Stage 1: Make a safe copy of the shadow state. */
  869. copy = kmalloc(sizeof(info->shadow),
  870. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  871. if (!copy)
  872. return -ENOMEM;
  873. memcpy(copy, info->shadow, sizeof(info->shadow));
  874. /* Stage 2: Set up free list. */
  875. memset(&info->shadow, 0, sizeof(info->shadow));
  876. for (i = 0; i < BLK_RING_SIZE; i++)
  877. info->shadow[i].req.u.rw.id = i+1;
  878. info->shadow_free = info->ring.req_prod_pvt;
  879. info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
  880. /* Stage 3: Find pending requests and requeue them. */
  881. for (i = 0; i < BLK_RING_SIZE; i++) {
  882. /* Not in use? */
  883. if (!copy[i].request)
  884. continue;
  885. /* Grab a request slot and copy shadow state into it. */
  886. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  887. *req = copy[i].req;
  888. /* We get a new request id, and must reset the shadow state. */
  889. req->u.rw.id = get_id_from_freelist(info);
  890. memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
  891. if (req->operation != BLKIF_OP_DISCARD) {
  892. /* Rewrite any grant references invalidated by susp/resume. */
  893. for (j = 0; j < req->u.rw.nr_segments; j++)
  894. gnttab_grant_foreign_access_ref(
  895. req->u.rw.seg[j].gref,
  896. info->xbdev->otherend_id,
  897. pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
  898. rq_data_dir(info->shadow[req->u.rw.id].request));
  899. }
  900. info->shadow[req->u.rw.id].req = *req;
  901. info->ring.req_prod_pvt++;
  902. }
  903. kfree(copy);
  904. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  905. spin_lock_irq(&info->io_lock);
  906. /* Now safe for us to use the shared ring */
  907. info->connected = BLKIF_STATE_CONNECTED;
  908. /* Send off requeued requests */
  909. flush_requests(info);
  910. /* Kick any other new requests queued since we resumed */
  911. kick_pending_request_queues(info);
  912. spin_unlock_irq(&info->io_lock);
  913. return 0;
  914. }
  915. /**
  916. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  917. * driver restart. We tear down our blkif structure and recreate it, but
  918. * leave the device-layer structures intact so that this is transparent to the
  919. * rest of the kernel.
  920. */
  921. static int blkfront_resume(struct xenbus_device *dev)
  922. {
  923. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  924. int err;
  925. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  926. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  927. err = talk_to_blkback(dev, info);
  928. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  929. err = blkif_recover(info);
  930. return err;
  931. }
  932. static void
  933. blkfront_closing(struct blkfront_info *info)
  934. {
  935. struct xenbus_device *xbdev = info->xbdev;
  936. struct block_device *bdev = NULL;
  937. mutex_lock(&info->mutex);
  938. if (xbdev->state == XenbusStateClosing) {
  939. mutex_unlock(&info->mutex);
  940. return;
  941. }
  942. if (info->gd)
  943. bdev = bdget_disk(info->gd, 0);
  944. mutex_unlock(&info->mutex);
  945. if (!bdev) {
  946. xenbus_frontend_closed(xbdev);
  947. return;
  948. }
  949. mutex_lock(&bdev->bd_mutex);
  950. if (bdev->bd_openers) {
  951. xenbus_dev_error(xbdev, -EBUSY,
  952. "Device in use; refusing to close");
  953. xenbus_switch_state(xbdev, XenbusStateClosing);
  954. } else {
  955. xlvbd_release_gendisk(info);
  956. xenbus_frontend_closed(xbdev);
  957. }
  958. mutex_unlock(&bdev->bd_mutex);
  959. bdput(bdev);
  960. }
  961. static void blkfront_setup_discard(struct blkfront_info *info)
  962. {
  963. int err;
  964. char *type;
  965. unsigned int discard_granularity;
  966. unsigned int discard_alignment;
  967. unsigned int discard_secure;
  968. type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
  969. if (IS_ERR(type))
  970. return;
  971. info->feature_secdiscard = 0;
  972. if (strncmp(type, "phy", 3) == 0) {
  973. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  974. "discard-granularity", "%u", &discard_granularity,
  975. "discard-alignment", "%u", &discard_alignment,
  976. NULL);
  977. if (!err) {
  978. info->feature_discard = 1;
  979. info->discard_granularity = discard_granularity;
  980. info->discard_alignment = discard_alignment;
  981. }
  982. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  983. "discard-secure", "%d", &discard_secure,
  984. NULL);
  985. if (!err)
  986. info->feature_secdiscard = discard_secure;
  987. } else if (strncmp(type, "file", 4) == 0)
  988. info->feature_discard = 1;
  989. kfree(type);
  990. }
  991. /*
  992. * Invoked when the backend is finally 'ready' (and has told produced
  993. * the details about the physical device - #sectors, size, etc).
  994. */
  995. static void blkfront_connect(struct blkfront_info *info)
  996. {
  997. unsigned long long sectors;
  998. unsigned long sector_size;
  999. unsigned int binfo;
  1000. int err;
  1001. int barrier, flush, discard;
  1002. switch (info->connected) {
  1003. case BLKIF_STATE_CONNECTED:
  1004. /*
  1005. * Potentially, the back-end may be signalling
  1006. * a capacity change; update the capacity.
  1007. */
  1008. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1009. "sectors", "%Lu", &sectors);
  1010. if (XENBUS_EXIST_ERR(err))
  1011. return;
  1012. printk(KERN_INFO "Setting capacity to %Lu\n",
  1013. sectors);
  1014. set_capacity(info->gd, sectors);
  1015. revalidate_disk(info->gd);
  1016. /* fall through */
  1017. case BLKIF_STATE_SUSPENDED:
  1018. return;
  1019. default:
  1020. break;
  1021. }
  1022. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  1023. __func__, info->xbdev->otherend);
  1024. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1025. "sectors", "%llu", &sectors,
  1026. "info", "%u", &binfo,
  1027. "sector-size", "%lu", &sector_size,
  1028. NULL);
  1029. if (err) {
  1030. xenbus_dev_fatal(info->xbdev, err,
  1031. "reading backend fields at %s",
  1032. info->xbdev->otherend);
  1033. return;
  1034. }
  1035. info->feature_flush = 0;
  1036. info->flush_op = 0;
  1037. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1038. "feature-barrier", "%d", &barrier,
  1039. NULL);
  1040. /*
  1041. * If there's no "feature-barrier" defined, then it means
  1042. * we're dealing with a very old backend which writes
  1043. * synchronously; nothing to do.
  1044. *
  1045. * If there are barriers, then we use flush.
  1046. */
  1047. if (!err && barrier) {
  1048. info->feature_flush = REQ_FLUSH | REQ_FUA;
  1049. info->flush_op = BLKIF_OP_WRITE_BARRIER;
  1050. }
  1051. /*
  1052. * And if there is "feature-flush-cache" use that above
  1053. * barriers.
  1054. */
  1055. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1056. "feature-flush-cache", "%d", &flush,
  1057. NULL);
  1058. if (!err && flush) {
  1059. info->feature_flush = REQ_FLUSH;
  1060. info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
  1061. }
  1062. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  1063. "feature-discard", "%d", &discard,
  1064. NULL);
  1065. if (!err && discard)
  1066. blkfront_setup_discard(info);
  1067. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  1068. if (err) {
  1069. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  1070. info->xbdev->otherend);
  1071. return;
  1072. }
  1073. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  1074. /* Kick pending requests. */
  1075. spin_lock_irq(&info->io_lock);
  1076. info->connected = BLKIF_STATE_CONNECTED;
  1077. kick_pending_request_queues(info);
  1078. spin_unlock_irq(&info->io_lock);
  1079. add_disk(info->gd);
  1080. info->is_ready = 1;
  1081. }
  1082. /**
  1083. * Callback received when the backend's state changes.
  1084. */
  1085. static void blkback_changed(struct xenbus_device *dev,
  1086. enum xenbus_state backend_state)
  1087. {
  1088. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1089. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  1090. switch (backend_state) {
  1091. case XenbusStateInitialising:
  1092. case XenbusStateInitWait:
  1093. case XenbusStateInitialised:
  1094. case XenbusStateReconfiguring:
  1095. case XenbusStateReconfigured:
  1096. case XenbusStateUnknown:
  1097. case XenbusStateClosed:
  1098. break;
  1099. case XenbusStateConnected:
  1100. blkfront_connect(info);
  1101. break;
  1102. case XenbusStateClosing:
  1103. blkfront_closing(info);
  1104. break;
  1105. }
  1106. }
  1107. static int blkfront_remove(struct xenbus_device *xbdev)
  1108. {
  1109. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  1110. struct block_device *bdev = NULL;
  1111. struct gendisk *disk;
  1112. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  1113. blkif_free(info, 0);
  1114. mutex_lock(&info->mutex);
  1115. disk = info->gd;
  1116. if (disk)
  1117. bdev = bdget_disk(disk, 0);
  1118. info->xbdev = NULL;
  1119. mutex_unlock(&info->mutex);
  1120. if (!bdev) {
  1121. kfree(info);
  1122. return 0;
  1123. }
  1124. /*
  1125. * The xbdev was removed before we reached the Closed
  1126. * state. See if it's safe to remove the disk. If the bdev
  1127. * isn't closed yet, we let release take care of it.
  1128. */
  1129. mutex_lock(&bdev->bd_mutex);
  1130. info = disk->private_data;
  1131. dev_warn(disk_to_dev(disk),
  1132. "%s was hot-unplugged, %d stale handles\n",
  1133. xbdev->nodename, bdev->bd_openers);
  1134. if (info && !bdev->bd_openers) {
  1135. xlvbd_release_gendisk(info);
  1136. disk->private_data = NULL;
  1137. kfree(info);
  1138. }
  1139. mutex_unlock(&bdev->bd_mutex);
  1140. bdput(bdev);
  1141. return 0;
  1142. }
  1143. static int blkfront_is_ready(struct xenbus_device *dev)
  1144. {
  1145. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  1146. return info->is_ready && info->xbdev;
  1147. }
  1148. static int blkif_open(struct block_device *bdev, fmode_t mode)
  1149. {
  1150. struct gendisk *disk = bdev->bd_disk;
  1151. struct blkfront_info *info;
  1152. int err = 0;
  1153. mutex_lock(&blkfront_mutex);
  1154. info = disk->private_data;
  1155. if (!info) {
  1156. /* xbdev gone */
  1157. err = -ERESTARTSYS;
  1158. goto out;
  1159. }
  1160. mutex_lock(&info->mutex);
  1161. if (!info->gd)
  1162. /* xbdev is closed */
  1163. err = -ERESTARTSYS;
  1164. mutex_unlock(&info->mutex);
  1165. out:
  1166. mutex_unlock(&blkfront_mutex);
  1167. return err;
  1168. }
  1169. static int blkif_release(struct gendisk *disk, fmode_t mode)
  1170. {
  1171. struct blkfront_info *info = disk->private_data;
  1172. struct block_device *bdev;
  1173. struct xenbus_device *xbdev;
  1174. mutex_lock(&blkfront_mutex);
  1175. bdev = bdget_disk(disk, 0);
  1176. if (bdev->bd_openers)
  1177. goto out;
  1178. /*
  1179. * Check if we have been instructed to close. We will have
  1180. * deferred this request, because the bdev was still open.
  1181. */
  1182. mutex_lock(&info->mutex);
  1183. xbdev = info->xbdev;
  1184. if (xbdev && xbdev->state == XenbusStateClosing) {
  1185. /* pending switch to state closed */
  1186. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1187. xlvbd_release_gendisk(info);
  1188. xenbus_frontend_closed(info->xbdev);
  1189. }
  1190. mutex_unlock(&info->mutex);
  1191. if (!xbdev) {
  1192. /* sudden device removal */
  1193. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1194. xlvbd_release_gendisk(info);
  1195. disk->private_data = NULL;
  1196. kfree(info);
  1197. }
  1198. out:
  1199. bdput(bdev);
  1200. mutex_unlock(&blkfront_mutex);
  1201. return 0;
  1202. }
  1203. static const struct block_device_operations xlvbd_block_fops =
  1204. {
  1205. .owner = THIS_MODULE,
  1206. .open = blkif_open,
  1207. .release = blkif_release,
  1208. .getgeo = blkif_getgeo,
  1209. .ioctl = blkif_ioctl,
  1210. };
  1211. static const struct xenbus_device_id blkfront_ids[] = {
  1212. { "vbd" },
  1213. { "" }
  1214. };
  1215. static DEFINE_XENBUS_DRIVER(blkfront, ,
  1216. .probe = blkfront_probe,
  1217. .remove = blkfront_remove,
  1218. .resume = blkfront_resume,
  1219. .otherend_changed = blkback_changed,
  1220. .is_ready = blkfront_is_ready,
  1221. );
  1222. static int __init xlblk_init(void)
  1223. {
  1224. int ret;
  1225. if (!xen_domain())
  1226. return -ENODEV;
  1227. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1228. return -ENODEV;
  1229. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1230. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1231. XENVBD_MAJOR, DEV_NAME);
  1232. return -ENODEV;
  1233. }
  1234. ret = xenbus_register_frontend(&blkfront_driver);
  1235. if (ret) {
  1236. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1237. return ret;
  1238. }
  1239. return 0;
  1240. }
  1241. module_init(xlblk_init);
  1242. static void __exit xlblk_exit(void)
  1243. {
  1244. xenbus_unregister_driver(&blkfront_driver);
  1245. unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
  1246. kfree(minors);
  1247. }
  1248. module_exit(xlblk_exit);
  1249. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1250. MODULE_LICENSE("GPL");
  1251. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1252. MODULE_ALIAS("xen:vbd");
  1253. MODULE_ALIAS("xenblk");