xen-blkfront.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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 <xen/xenbus.h>
  43. #include <xen/grant_table.h>
  44. #include <xen/events.h>
  45. #include <xen/page.h>
  46. #include <xen/interface/grant_table.h>
  47. #include <xen/interface/io/blkif.h>
  48. #include <xen/interface/io/protocols.h>
  49. #include <asm/xen/hypervisor.h>
  50. enum blkif_state {
  51. BLKIF_STATE_DISCONNECTED,
  52. BLKIF_STATE_CONNECTED,
  53. BLKIF_STATE_SUSPENDED,
  54. };
  55. struct blk_shadow {
  56. struct blkif_request req;
  57. unsigned long request;
  58. unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  59. };
  60. static struct block_device_operations xlvbd_block_fops;
  61. #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
  62. /*
  63. * We have one of these per vbd, whether ide, scsi or 'other'. They
  64. * hang in private_data off the gendisk structure. We may end up
  65. * putting all kinds of interesting stuff here :-)
  66. */
  67. struct blkfront_info
  68. {
  69. struct xenbus_device *xbdev;
  70. struct gendisk *gd;
  71. int vdevice;
  72. blkif_vdev_t handle;
  73. enum blkif_state connected;
  74. int ring_ref;
  75. struct blkif_front_ring ring;
  76. unsigned int evtchn, irq;
  77. struct request_queue *rq;
  78. struct work_struct work;
  79. struct gnttab_free_callback callback;
  80. struct blk_shadow shadow[BLK_RING_SIZE];
  81. unsigned long shadow_free;
  82. int feature_barrier;
  83. int is_ready;
  84. /**
  85. * The number of people holding this device open. We won't allow a
  86. * hot-unplug unless this is 0.
  87. */
  88. int users;
  89. };
  90. static DEFINE_SPINLOCK(blkif_io_lock);
  91. #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
  92. (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
  93. #define GRANT_INVALID_REF 0
  94. #define PARTS_PER_DISK 16
  95. #define PARTS_PER_EXT_DISK 256
  96. #define BLKIF_MAJOR(dev) ((dev)>>8)
  97. #define BLKIF_MINOR(dev) ((dev) & 0xff)
  98. #define EXT_SHIFT 28
  99. #define EXTENDED (1<<EXT_SHIFT)
  100. #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
  101. #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
  102. #define DEV_NAME "xvd" /* name in /dev */
  103. static int get_id_from_freelist(struct blkfront_info *info)
  104. {
  105. unsigned long free = info->shadow_free;
  106. BUG_ON(free > BLK_RING_SIZE);
  107. info->shadow_free = info->shadow[free].req.id;
  108. info->shadow[free].req.id = 0x0fffffee; /* debug */
  109. return free;
  110. }
  111. static void add_id_to_freelist(struct blkfront_info *info,
  112. unsigned long id)
  113. {
  114. info->shadow[id].req.id = info->shadow_free;
  115. info->shadow[id].request = 0;
  116. info->shadow_free = id;
  117. }
  118. static void blkif_restart_queue_callback(void *arg)
  119. {
  120. struct blkfront_info *info = (struct blkfront_info *)arg;
  121. schedule_work(&info->work);
  122. }
  123. static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
  124. {
  125. /* We don't have real geometry info, but let's at least return
  126. values consistent with the size of the device */
  127. sector_t nsect = get_capacity(bd->bd_disk);
  128. sector_t cylinders = nsect;
  129. hg->heads = 0xff;
  130. hg->sectors = 0x3f;
  131. sector_div(cylinders, hg->heads * hg->sectors);
  132. hg->cylinders = cylinders;
  133. if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
  134. hg->cylinders = 0xffff;
  135. return 0;
  136. }
  137. static int blkif_ioctl(struct inode *inode, struct file *filep,
  138. unsigned command, unsigned long argument)
  139. {
  140. struct blkfront_info *info =
  141. inode->i_bdev->bd_disk->private_data;
  142. int i;
  143. dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
  144. command, (long)argument);
  145. switch (command) {
  146. case CDROMMULTISESSION:
  147. dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
  148. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  149. if (put_user(0, (char __user *)(argument + i)))
  150. return -EFAULT;
  151. return 0;
  152. case CDROM_GET_CAPABILITY: {
  153. struct gendisk *gd = info->gd;
  154. if (gd->flags & GENHD_FL_CD)
  155. return 0;
  156. return -EINVAL;
  157. }
  158. default:
  159. /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
  160. command);*/
  161. return -EINVAL; /* same return as native Linux */
  162. }
  163. return 0;
  164. }
  165. /*
  166. * blkif_queue_request
  167. *
  168. * request block io
  169. *
  170. * id: for guest use only.
  171. * operation: BLKIF_OP_{READ,WRITE,PROBE}
  172. * buffer: buffer to read/write into. this should be a
  173. * virtual address in the guest os.
  174. */
  175. static int blkif_queue_request(struct request *req)
  176. {
  177. struct blkfront_info *info = req->rq_disk->private_data;
  178. unsigned long buffer_mfn;
  179. struct blkif_request *ring_req;
  180. struct req_iterator iter;
  181. struct bio_vec *bvec;
  182. unsigned long id;
  183. unsigned int fsect, lsect;
  184. int ref;
  185. grant_ref_t gref_head;
  186. if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
  187. return 1;
  188. if (gnttab_alloc_grant_references(
  189. BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
  190. gnttab_request_free_callback(
  191. &info->callback,
  192. blkif_restart_queue_callback,
  193. info,
  194. BLKIF_MAX_SEGMENTS_PER_REQUEST);
  195. return 1;
  196. }
  197. /* Fill out a communications ring structure. */
  198. ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  199. id = get_id_from_freelist(info);
  200. info->shadow[id].request = (unsigned long)req;
  201. ring_req->id = id;
  202. ring_req->sector_number = (blkif_sector_t)req->sector;
  203. ring_req->handle = info->handle;
  204. ring_req->operation = rq_data_dir(req) ?
  205. BLKIF_OP_WRITE : BLKIF_OP_READ;
  206. if (blk_barrier_rq(req))
  207. ring_req->operation = BLKIF_OP_WRITE_BARRIER;
  208. ring_req->nr_segments = 0;
  209. rq_for_each_segment(bvec, req, iter) {
  210. BUG_ON(ring_req->nr_segments == BLKIF_MAX_SEGMENTS_PER_REQUEST);
  211. buffer_mfn = pfn_to_mfn(page_to_pfn(bvec->bv_page));
  212. fsect = bvec->bv_offset >> 9;
  213. lsect = fsect + (bvec->bv_len >> 9) - 1;
  214. /* install a grant reference. */
  215. ref = gnttab_claim_grant_reference(&gref_head);
  216. BUG_ON(ref == -ENOSPC);
  217. gnttab_grant_foreign_access_ref(
  218. ref,
  219. info->xbdev->otherend_id,
  220. buffer_mfn,
  221. rq_data_dir(req) );
  222. info->shadow[id].frame[ring_req->nr_segments] =
  223. mfn_to_pfn(buffer_mfn);
  224. ring_req->seg[ring_req->nr_segments] =
  225. (struct blkif_request_segment) {
  226. .gref = ref,
  227. .first_sect = fsect,
  228. .last_sect = lsect };
  229. ring_req->nr_segments++;
  230. }
  231. info->ring.req_prod_pvt++;
  232. /* Keep a private copy so we can reissue requests when recovering. */
  233. info->shadow[id].req = *ring_req;
  234. gnttab_free_grant_references(gref_head);
  235. return 0;
  236. }
  237. static inline void flush_requests(struct blkfront_info *info)
  238. {
  239. int notify;
  240. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
  241. if (notify)
  242. notify_remote_via_irq(info->irq);
  243. }
  244. /*
  245. * do_blkif_request
  246. * read a block; request is in a request queue
  247. */
  248. static void do_blkif_request(struct request_queue *rq)
  249. {
  250. struct blkfront_info *info = NULL;
  251. struct request *req;
  252. int queued;
  253. pr_debug("Entered do_blkif_request\n");
  254. queued = 0;
  255. while ((req = elv_next_request(rq)) != NULL) {
  256. info = req->rq_disk->private_data;
  257. if (!blk_fs_request(req)) {
  258. end_request(req, 0);
  259. continue;
  260. }
  261. if (RING_FULL(&info->ring))
  262. goto wait;
  263. pr_debug("do_blk_req %p: cmd %p, sec %lx, "
  264. "(%u/%li) buffer:%p [%s]\n",
  265. req, req->cmd, (unsigned long)req->sector,
  266. req->current_nr_sectors,
  267. req->nr_sectors, req->buffer,
  268. rq_data_dir(req) ? "write" : "read");
  269. blkdev_dequeue_request(req);
  270. if (blkif_queue_request(req)) {
  271. blk_requeue_request(rq, req);
  272. wait:
  273. /* Avoid pointless unplugs. */
  274. blk_stop_queue(rq);
  275. break;
  276. }
  277. queued++;
  278. }
  279. if (queued != 0)
  280. flush_requests(info);
  281. }
  282. static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
  283. {
  284. struct request_queue *rq;
  285. rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
  286. if (rq == NULL)
  287. return -1;
  288. elevator_init(rq, "noop");
  289. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  290. blk_queue_hardsect_size(rq, sector_size);
  291. blk_queue_max_sectors(rq, 512);
  292. /* Each segment in a request is up to an aligned page in size. */
  293. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  294. blk_queue_max_segment_size(rq, PAGE_SIZE);
  295. /* Ensure a merged request will fit in a single I/O ring slot. */
  296. blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  297. blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  298. /* Make sure buffer addresses are sector-aligned. */
  299. blk_queue_dma_alignment(rq, 511);
  300. /* Make sure we don't use bounce buffers. */
  301. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  302. gd->queue = rq;
  303. return 0;
  304. }
  305. static int xlvbd_barrier(struct blkfront_info *info)
  306. {
  307. int err;
  308. err = blk_queue_ordered(info->rq,
  309. info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE,
  310. NULL);
  311. if (err)
  312. return err;
  313. printk(KERN_INFO "blkfront: %s: barriers %s\n",
  314. info->gd->disk_name,
  315. info->feature_barrier ? "enabled" : "disabled");
  316. return 0;
  317. }
  318. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  319. struct blkfront_info *info,
  320. u16 vdisk_info, u16 sector_size)
  321. {
  322. struct gendisk *gd;
  323. int nr_minors = 1;
  324. int err = -ENODEV;
  325. unsigned int offset;
  326. int minor;
  327. int nr_parts;
  328. BUG_ON(info->gd != NULL);
  329. BUG_ON(info->rq != NULL);
  330. if ((info->vdevice>>EXT_SHIFT) > 1) {
  331. /* this is above the extended range; something is wrong */
  332. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  333. return -ENODEV;
  334. }
  335. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  336. minor = BLKIF_MINOR(info->vdevice);
  337. nr_parts = PARTS_PER_DISK;
  338. } else {
  339. minor = BLKIF_MINOR_EXT(info->vdevice);
  340. nr_parts = PARTS_PER_EXT_DISK;
  341. }
  342. if ((minor % nr_parts) == 0)
  343. nr_minors = nr_parts;
  344. gd = alloc_disk(nr_minors);
  345. if (gd == NULL)
  346. goto out;
  347. offset = minor / nr_parts;
  348. if (nr_minors > 1) {
  349. if (offset < 26)
  350. sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
  351. else
  352. sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
  353. 'a' + ((offset / 26)-1), 'a' + (offset % 26));
  354. } else {
  355. if (offset < 26)
  356. sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
  357. 'a' + offset,
  358. minor & (nr_parts - 1));
  359. else
  360. sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
  361. 'a' + ((offset / 26) - 1),
  362. 'a' + (offset % 26),
  363. minor & (nr_parts - 1));
  364. }
  365. gd->major = XENVBD_MAJOR;
  366. gd->first_minor = minor;
  367. gd->fops = &xlvbd_block_fops;
  368. gd->private_data = info;
  369. gd->driverfs_dev = &(info->xbdev->dev);
  370. set_capacity(gd, capacity);
  371. if (xlvbd_init_blk_queue(gd, sector_size)) {
  372. del_gendisk(gd);
  373. goto out;
  374. }
  375. info->rq = gd->queue;
  376. info->gd = gd;
  377. if (info->feature_barrier)
  378. xlvbd_barrier(info);
  379. if (vdisk_info & VDISK_READONLY)
  380. set_disk_ro(gd, 1);
  381. if (vdisk_info & VDISK_REMOVABLE)
  382. gd->flags |= GENHD_FL_REMOVABLE;
  383. if (vdisk_info & VDISK_CDROM)
  384. gd->flags |= GENHD_FL_CD;
  385. return 0;
  386. out:
  387. return err;
  388. }
  389. static void kick_pending_request_queues(struct blkfront_info *info)
  390. {
  391. if (!RING_FULL(&info->ring)) {
  392. /* Re-enable calldowns. */
  393. blk_start_queue(info->rq);
  394. /* Kick things off immediately. */
  395. do_blkif_request(info->rq);
  396. }
  397. }
  398. static void blkif_restart_queue(struct work_struct *work)
  399. {
  400. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  401. spin_lock_irq(&blkif_io_lock);
  402. if (info->connected == BLKIF_STATE_CONNECTED)
  403. kick_pending_request_queues(info);
  404. spin_unlock_irq(&blkif_io_lock);
  405. }
  406. static void blkif_free(struct blkfront_info *info, int suspend)
  407. {
  408. /* Prevent new requests being issued until we fix things up. */
  409. spin_lock_irq(&blkif_io_lock);
  410. info->connected = suspend ?
  411. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  412. /* No more blkif_request(). */
  413. if (info->rq)
  414. blk_stop_queue(info->rq);
  415. /* No more gnttab callback work. */
  416. gnttab_cancel_free_callback(&info->callback);
  417. spin_unlock_irq(&blkif_io_lock);
  418. /* Flush gnttab callback work. Must be done with no locks held. */
  419. flush_scheduled_work();
  420. /* Free resources associated with old device channel. */
  421. if (info->ring_ref != GRANT_INVALID_REF) {
  422. gnttab_end_foreign_access(info->ring_ref, 0,
  423. (unsigned long)info->ring.sring);
  424. info->ring_ref = GRANT_INVALID_REF;
  425. info->ring.sring = NULL;
  426. }
  427. if (info->irq)
  428. unbind_from_irqhandler(info->irq, info);
  429. info->evtchn = info->irq = 0;
  430. }
  431. static void blkif_completion(struct blk_shadow *s)
  432. {
  433. int i;
  434. for (i = 0; i < s->req.nr_segments; i++)
  435. gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
  436. }
  437. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  438. {
  439. struct request *req;
  440. struct blkif_response *bret;
  441. RING_IDX i, rp;
  442. unsigned long flags;
  443. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  444. int error;
  445. spin_lock_irqsave(&blkif_io_lock, flags);
  446. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  447. spin_unlock_irqrestore(&blkif_io_lock, flags);
  448. return IRQ_HANDLED;
  449. }
  450. again:
  451. rp = info->ring.sring->rsp_prod;
  452. rmb(); /* Ensure we see queued responses up to 'rp'. */
  453. for (i = info->ring.rsp_cons; i != rp; i++) {
  454. unsigned long id;
  455. int ret;
  456. bret = RING_GET_RESPONSE(&info->ring, i);
  457. id = bret->id;
  458. req = (struct request *)info->shadow[id].request;
  459. blkif_completion(&info->shadow[id]);
  460. add_id_to_freelist(info, id);
  461. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  462. switch (bret->operation) {
  463. case BLKIF_OP_WRITE_BARRIER:
  464. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  465. printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
  466. info->gd->disk_name);
  467. error = -EOPNOTSUPP;
  468. info->feature_barrier = 0;
  469. xlvbd_barrier(info);
  470. }
  471. /* fall through */
  472. case BLKIF_OP_READ:
  473. case BLKIF_OP_WRITE:
  474. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  475. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  476. "request: %x\n", bret->status);
  477. ret = __blk_end_request(req, error, blk_rq_bytes(req));
  478. BUG_ON(ret);
  479. break;
  480. default:
  481. BUG();
  482. }
  483. }
  484. info->ring.rsp_cons = i;
  485. if (i != info->ring.req_prod_pvt) {
  486. int more_to_do;
  487. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  488. if (more_to_do)
  489. goto again;
  490. } else
  491. info->ring.sring->rsp_event = i + 1;
  492. kick_pending_request_queues(info);
  493. spin_unlock_irqrestore(&blkif_io_lock, flags);
  494. return IRQ_HANDLED;
  495. }
  496. static int setup_blkring(struct xenbus_device *dev,
  497. struct blkfront_info *info)
  498. {
  499. struct blkif_sring *sring;
  500. int err;
  501. info->ring_ref = GRANT_INVALID_REF;
  502. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  503. if (!sring) {
  504. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  505. return -ENOMEM;
  506. }
  507. SHARED_RING_INIT(sring);
  508. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  509. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  510. if (err < 0) {
  511. free_page((unsigned long)sring);
  512. info->ring.sring = NULL;
  513. goto fail;
  514. }
  515. info->ring_ref = err;
  516. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  517. if (err)
  518. goto fail;
  519. err = bind_evtchn_to_irqhandler(info->evtchn,
  520. blkif_interrupt,
  521. IRQF_SAMPLE_RANDOM, "blkif", info);
  522. if (err <= 0) {
  523. xenbus_dev_fatal(dev, err,
  524. "bind_evtchn_to_irqhandler failed");
  525. goto fail;
  526. }
  527. info->irq = err;
  528. return 0;
  529. fail:
  530. blkif_free(info, 0);
  531. return err;
  532. }
  533. /* Common code used when first setting up, and when resuming. */
  534. static int talk_to_backend(struct xenbus_device *dev,
  535. struct blkfront_info *info)
  536. {
  537. const char *message = NULL;
  538. struct xenbus_transaction xbt;
  539. int err;
  540. /* Create shared ring, alloc event channel. */
  541. err = setup_blkring(dev, info);
  542. if (err)
  543. goto out;
  544. again:
  545. err = xenbus_transaction_start(&xbt);
  546. if (err) {
  547. xenbus_dev_fatal(dev, err, "starting transaction");
  548. goto destroy_blkring;
  549. }
  550. err = xenbus_printf(xbt, dev->nodename,
  551. "ring-ref", "%u", info->ring_ref);
  552. if (err) {
  553. message = "writing ring-ref";
  554. goto abort_transaction;
  555. }
  556. err = xenbus_printf(xbt, dev->nodename,
  557. "event-channel", "%u", info->evtchn);
  558. if (err) {
  559. message = "writing event-channel";
  560. goto abort_transaction;
  561. }
  562. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  563. XEN_IO_PROTO_ABI_NATIVE);
  564. if (err) {
  565. message = "writing protocol";
  566. goto abort_transaction;
  567. }
  568. err = xenbus_transaction_end(xbt, 0);
  569. if (err) {
  570. if (err == -EAGAIN)
  571. goto again;
  572. xenbus_dev_fatal(dev, err, "completing transaction");
  573. goto destroy_blkring;
  574. }
  575. xenbus_switch_state(dev, XenbusStateInitialised);
  576. return 0;
  577. abort_transaction:
  578. xenbus_transaction_end(xbt, 1);
  579. if (message)
  580. xenbus_dev_fatal(dev, err, "%s", message);
  581. destroy_blkring:
  582. blkif_free(info, 0);
  583. out:
  584. return err;
  585. }
  586. /**
  587. * Entry point to this code when a new device is created. Allocate the basic
  588. * structures and the ring buffer for communication with the backend, and
  589. * inform the backend of the appropriate details for those. Switch to
  590. * Initialised state.
  591. */
  592. static int blkfront_probe(struct xenbus_device *dev,
  593. const struct xenbus_device_id *id)
  594. {
  595. int err, vdevice, i;
  596. struct blkfront_info *info;
  597. /* FIXME: Use dynamic device id if this is not set. */
  598. err = xenbus_scanf(XBT_NIL, dev->nodename,
  599. "virtual-device", "%i", &vdevice);
  600. if (err != 1) {
  601. /* go looking in the extended area instead */
  602. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  603. "%i", &vdevice);
  604. if (err != 1) {
  605. xenbus_dev_fatal(dev, err, "reading virtual-device");
  606. return err;
  607. }
  608. }
  609. info = kzalloc(sizeof(*info), GFP_KERNEL);
  610. if (!info) {
  611. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  612. return -ENOMEM;
  613. }
  614. info->xbdev = dev;
  615. info->vdevice = vdevice;
  616. info->connected = BLKIF_STATE_DISCONNECTED;
  617. INIT_WORK(&info->work, blkif_restart_queue);
  618. for (i = 0; i < BLK_RING_SIZE; i++)
  619. info->shadow[i].req.id = i+1;
  620. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  621. /* Front end dir is a number, which is used as the id. */
  622. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  623. dev->dev.driver_data = info;
  624. err = talk_to_backend(dev, info);
  625. if (err) {
  626. kfree(info);
  627. dev->dev.driver_data = NULL;
  628. return err;
  629. }
  630. return 0;
  631. }
  632. static int blkif_recover(struct blkfront_info *info)
  633. {
  634. int i;
  635. struct blkif_request *req;
  636. struct blk_shadow *copy;
  637. int j;
  638. /* Stage 1: Make a safe copy of the shadow state. */
  639. copy = kmalloc(sizeof(info->shadow),
  640. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  641. if (!copy)
  642. return -ENOMEM;
  643. memcpy(copy, info->shadow, sizeof(info->shadow));
  644. /* Stage 2: Set up free list. */
  645. memset(&info->shadow, 0, sizeof(info->shadow));
  646. for (i = 0; i < BLK_RING_SIZE; i++)
  647. info->shadow[i].req.id = i+1;
  648. info->shadow_free = info->ring.req_prod_pvt;
  649. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  650. /* Stage 3: Find pending requests and requeue them. */
  651. for (i = 0; i < BLK_RING_SIZE; i++) {
  652. /* Not in use? */
  653. if (copy[i].request == 0)
  654. continue;
  655. /* Grab a request slot and copy shadow state into it. */
  656. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  657. *req = copy[i].req;
  658. /* We get a new request id, and must reset the shadow state. */
  659. req->id = get_id_from_freelist(info);
  660. memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
  661. /* Rewrite any grant references invalidated by susp/resume. */
  662. for (j = 0; j < req->nr_segments; j++)
  663. gnttab_grant_foreign_access_ref(
  664. req->seg[j].gref,
  665. info->xbdev->otherend_id,
  666. pfn_to_mfn(info->shadow[req->id].frame[j]),
  667. rq_data_dir(
  668. (struct request *)
  669. info->shadow[req->id].request));
  670. info->shadow[req->id].req = *req;
  671. info->ring.req_prod_pvt++;
  672. }
  673. kfree(copy);
  674. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  675. spin_lock_irq(&blkif_io_lock);
  676. /* Now safe for us to use the shared ring */
  677. info->connected = BLKIF_STATE_CONNECTED;
  678. /* Send off requeued requests */
  679. flush_requests(info);
  680. /* Kick any other new requests queued since we resumed */
  681. kick_pending_request_queues(info);
  682. spin_unlock_irq(&blkif_io_lock);
  683. return 0;
  684. }
  685. /**
  686. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  687. * driver restart. We tear down our blkif structure and recreate it, but
  688. * leave the device-layer structures intact so that this is transparent to the
  689. * rest of the kernel.
  690. */
  691. static int blkfront_resume(struct xenbus_device *dev)
  692. {
  693. struct blkfront_info *info = dev->dev.driver_data;
  694. int err;
  695. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  696. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  697. err = talk_to_backend(dev, info);
  698. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  699. err = blkif_recover(info);
  700. return err;
  701. }
  702. /*
  703. * Invoked when the backend is finally 'ready' (and has told produced
  704. * the details about the physical device - #sectors, size, etc).
  705. */
  706. static void blkfront_connect(struct blkfront_info *info)
  707. {
  708. unsigned long long sectors;
  709. unsigned long sector_size;
  710. unsigned int binfo;
  711. int err;
  712. if ((info->connected == BLKIF_STATE_CONNECTED) ||
  713. (info->connected == BLKIF_STATE_SUSPENDED) )
  714. return;
  715. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  716. __func__, info->xbdev->otherend);
  717. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  718. "sectors", "%llu", &sectors,
  719. "info", "%u", &binfo,
  720. "sector-size", "%lu", &sector_size,
  721. NULL);
  722. if (err) {
  723. xenbus_dev_fatal(info->xbdev, err,
  724. "reading backend fields at %s",
  725. info->xbdev->otherend);
  726. return;
  727. }
  728. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  729. "feature-barrier", "%lu", &info->feature_barrier,
  730. NULL);
  731. if (err)
  732. info->feature_barrier = 0;
  733. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  734. if (err) {
  735. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  736. info->xbdev->otherend);
  737. return;
  738. }
  739. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  740. /* Kick pending requests. */
  741. spin_lock_irq(&blkif_io_lock);
  742. info->connected = BLKIF_STATE_CONNECTED;
  743. kick_pending_request_queues(info);
  744. spin_unlock_irq(&blkif_io_lock);
  745. add_disk(info->gd);
  746. info->is_ready = 1;
  747. }
  748. /**
  749. * Handle the change of state of the backend to Closing. We must delete our
  750. * device-layer structures now, to ensure that writes are flushed through to
  751. * the backend. Once is this done, we can switch to Closed in
  752. * acknowledgement.
  753. */
  754. static void blkfront_closing(struct xenbus_device *dev)
  755. {
  756. struct blkfront_info *info = dev->dev.driver_data;
  757. unsigned long flags;
  758. dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
  759. if (info->rq == NULL)
  760. goto out;
  761. spin_lock_irqsave(&blkif_io_lock, flags);
  762. del_gendisk(info->gd);
  763. /* No more blkif_request(). */
  764. blk_stop_queue(info->rq);
  765. /* No more gnttab callback work. */
  766. gnttab_cancel_free_callback(&info->callback);
  767. spin_unlock_irqrestore(&blkif_io_lock, flags);
  768. /* Flush gnttab callback work. Must be done with no locks held. */
  769. flush_scheduled_work();
  770. blk_cleanup_queue(info->rq);
  771. info->rq = NULL;
  772. out:
  773. xenbus_frontend_closed(dev);
  774. }
  775. /**
  776. * Callback received when the backend's state changes.
  777. */
  778. static void backend_changed(struct xenbus_device *dev,
  779. enum xenbus_state backend_state)
  780. {
  781. struct blkfront_info *info = dev->dev.driver_data;
  782. struct block_device *bd;
  783. dev_dbg(&dev->dev, "blkfront:backend_changed.\n");
  784. switch (backend_state) {
  785. case XenbusStateInitialising:
  786. case XenbusStateInitWait:
  787. case XenbusStateInitialised:
  788. case XenbusStateUnknown:
  789. case XenbusStateClosed:
  790. break;
  791. case XenbusStateConnected:
  792. blkfront_connect(info);
  793. break;
  794. case XenbusStateClosing:
  795. bd = bdget_disk(info->gd, 0);
  796. if (bd == NULL)
  797. xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
  798. mutex_lock(&bd->bd_mutex);
  799. if (info->users > 0)
  800. xenbus_dev_error(dev, -EBUSY,
  801. "Device in use; refusing to close");
  802. else
  803. blkfront_closing(dev);
  804. mutex_unlock(&bd->bd_mutex);
  805. bdput(bd);
  806. break;
  807. }
  808. }
  809. static int blkfront_remove(struct xenbus_device *dev)
  810. {
  811. struct blkfront_info *info = dev->dev.driver_data;
  812. dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
  813. blkif_free(info, 0);
  814. kfree(info);
  815. return 0;
  816. }
  817. static int blkfront_is_ready(struct xenbus_device *dev)
  818. {
  819. struct blkfront_info *info = dev->dev.driver_data;
  820. return info->is_ready;
  821. }
  822. static int blkif_open(struct inode *inode, struct file *filep)
  823. {
  824. struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
  825. info->users++;
  826. return 0;
  827. }
  828. static int blkif_release(struct inode *inode, struct file *filep)
  829. {
  830. struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
  831. info->users--;
  832. if (info->users == 0) {
  833. /* Check whether we have been instructed to close. We will
  834. have ignored this request initially, as the device was
  835. still mounted. */
  836. struct xenbus_device *dev = info->xbdev;
  837. enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
  838. if (state == XenbusStateClosing && info->is_ready)
  839. blkfront_closing(dev);
  840. }
  841. return 0;
  842. }
  843. static struct block_device_operations xlvbd_block_fops =
  844. {
  845. .owner = THIS_MODULE,
  846. .open = blkif_open,
  847. .release = blkif_release,
  848. .getgeo = blkif_getgeo,
  849. .ioctl = blkif_ioctl,
  850. };
  851. static struct xenbus_device_id blkfront_ids[] = {
  852. { "vbd" },
  853. { "" }
  854. };
  855. static struct xenbus_driver blkfront = {
  856. .name = "vbd",
  857. .owner = THIS_MODULE,
  858. .ids = blkfront_ids,
  859. .probe = blkfront_probe,
  860. .remove = blkfront_remove,
  861. .resume = blkfront_resume,
  862. .otherend_changed = backend_changed,
  863. .is_ready = blkfront_is_ready,
  864. };
  865. static int __init xlblk_init(void)
  866. {
  867. if (!is_running_on_xen())
  868. return -ENODEV;
  869. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  870. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  871. XENVBD_MAJOR, DEV_NAME);
  872. return -ENODEV;
  873. }
  874. return xenbus_register_frontend(&blkfront);
  875. }
  876. module_init(xlblk_init);
  877. static void __exit xlblk_exit(void)
  878. {
  879. return xenbus_unregister_driver(&blkfront);
  880. }
  881. module_exit(xlblk_exit);
  882. MODULE_DESCRIPTION("Xen virtual block device frontend");
  883. MODULE_LICENSE("GPL");
  884. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  885. MODULE_ALIAS("xen:vbd");
  886. MODULE_ALIAS("xenblk");