xen-blkfront.c 31 KB

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