xen-blkfront.c 26 KB

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