xen-blkfront.c 38 KB

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