xen-blkfront.c 29 KB

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