xen-blkfront.c 24 KB

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