xen-blkfront.c 31 KB

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