xen-blkfront.c 25 KB

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