xen-blkfront.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 kick_pending_request_queues(struct blkfront_info *info)
  438. {
  439. if (!RING_FULL(&info->ring)) {
  440. /* Re-enable calldowns. */
  441. blk_start_queue(info->rq);
  442. /* Kick things off immediately. */
  443. do_blkif_request(info->rq);
  444. }
  445. }
  446. static void blkif_restart_queue(struct work_struct *work)
  447. {
  448. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  449. spin_lock_irq(&blkif_io_lock);
  450. if (info->connected == BLKIF_STATE_CONNECTED)
  451. kick_pending_request_queues(info);
  452. spin_unlock_irq(&blkif_io_lock);
  453. }
  454. static void blkif_free(struct blkfront_info *info, int suspend)
  455. {
  456. /* Prevent new requests being issued until we fix things up. */
  457. spin_lock_irq(&blkif_io_lock);
  458. info->connected = suspend ?
  459. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  460. /* No more blkif_request(). */
  461. if (info->rq)
  462. blk_stop_queue(info->rq);
  463. /* No more gnttab callback work. */
  464. gnttab_cancel_free_callback(&info->callback);
  465. spin_unlock_irq(&blkif_io_lock);
  466. /* Flush gnttab callback work. Must be done with no locks held. */
  467. flush_scheduled_work();
  468. /* Free resources associated with old device channel. */
  469. if (info->ring_ref != GRANT_INVALID_REF) {
  470. gnttab_end_foreign_access(info->ring_ref, 0,
  471. (unsigned long)info->ring.sring);
  472. info->ring_ref = GRANT_INVALID_REF;
  473. info->ring.sring = NULL;
  474. }
  475. if (info->irq)
  476. unbind_from_irqhandler(info->irq, info);
  477. info->evtchn = info->irq = 0;
  478. }
  479. static void blkif_completion(struct blk_shadow *s)
  480. {
  481. int i;
  482. for (i = 0; i < s->req.nr_segments; i++)
  483. gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
  484. }
  485. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  486. {
  487. struct request *req;
  488. struct blkif_response *bret;
  489. RING_IDX i, rp;
  490. unsigned long flags;
  491. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  492. int error;
  493. spin_lock_irqsave(&blkif_io_lock, flags);
  494. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  495. spin_unlock_irqrestore(&blkif_io_lock, flags);
  496. return IRQ_HANDLED;
  497. }
  498. again:
  499. rp = info->ring.sring->rsp_prod;
  500. rmb(); /* Ensure we see queued responses up to 'rp'. */
  501. for (i = info->ring.rsp_cons; i != rp; i++) {
  502. unsigned long id;
  503. bret = RING_GET_RESPONSE(&info->ring, i);
  504. id = bret->id;
  505. req = (struct request *)info->shadow[id].request;
  506. blkif_completion(&info->shadow[id]);
  507. add_id_to_freelist(info, id);
  508. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  509. switch (bret->operation) {
  510. case BLKIF_OP_WRITE_BARRIER:
  511. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  512. printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
  513. info->gd->disk_name);
  514. error = -EOPNOTSUPP;
  515. info->feature_barrier = 0;
  516. xlvbd_barrier(info);
  517. }
  518. /* fall through */
  519. case BLKIF_OP_READ:
  520. case BLKIF_OP_WRITE:
  521. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  522. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  523. "request: %x\n", bret->status);
  524. __blk_end_request_all(req, error);
  525. break;
  526. default:
  527. BUG();
  528. }
  529. }
  530. info->ring.rsp_cons = i;
  531. if (i != info->ring.req_prod_pvt) {
  532. int more_to_do;
  533. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  534. if (more_to_do)
  535. goto again;
  536. } else
  537. info->ring.sring->rsp_event = i + 1;
  538. kick_pending_request_queues(info);
  539. spin_unlock_irqrestore(&blkif_io_lock, flags);
  540. return IRQ_HANDLED;
  541. }
  542. static int setup_blkring(struct xenbus_device *dev,
  543. struct blkfront_info *info)
  544. {
  545. struct blkif_sring *sring;
  546. int err;
  547. info->ring_ref = GRANT_INVALID_REF;
  548. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  549. if (!sring) {
  550. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  551. return -ENOMEM;
  552. }
  553. SHARED_RING_INIT(sring);
  554. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  555. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  556. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  557. if (err < 0) {
  558. free_page((unsigned long)sring);
  559. info->ring.sring = NULL;
  560. goto fail;
  561. }
  562. info->ring_ref = err;
  563. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  564. if (err)
  565. goto fail;
  566. err = bind_evtchn_to_irqhandler(info->evtchn,
  567. blkif_interrupt,
  568. IRQF_SAMPLE_RANDOM, "blkif", info);
  569. if (err <= 0) {
  570. xenbus_dev_fatal(dev, err,
  571. "bind_evtchn_to_irqhandler failed");
  572. goto fail;
  573. }
  574. info->irq = err;
  575. return 0;
  576. fail:
  577. blkif_free(info, 0);
  578. return err;
  579. }
  580. /* Common code used when first setting up, and when resuming. */
  581. static int talk_to_blkback(struct xenbus_device *dev,
  582. struct blkfront_info *info)
  583. {
  584. const char *message = NULL;
  585. struct xenbus_transaction xbt;
  586. int err;
  587. /* Create shared ring, alloc event channel. */
  588. err = setup_blkring(dev, info);
  589. if (err)
  590. goto out;
  591. again:
  592. err = xenbus_transaction_start(&xbt);
  593. if (err) {
  594. xenbus_dev_fatal(dev, err, "starting transaction");
  595. goto destroy_blkring;
  596. }
  597. err = xenbus_printf(xbt, dev->nodename,
  598. "ring-ref", "%u", info->ring_ref);
  599. if (err) {
  600. message = "writing ring-ref";
  601. goto abort_transaction;
  602. }
  603. err = xenbus_printf(xbt, dev->nodename,
  604. "event-channel", "%u", info->evtchn);
  605. if (err) {
  606. message = "writing event-channel";
  607. goto abort_transaction;
  608. }
  609. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  610. XEN_IO_PROTO_ABI_NATIVE);
  611. if (err) {
  612. message = "writing protocol";
  613. goto abort_transaction;
  614. }
  615. err = xenbus_transaction_end(xbt, 0);
  616. if (err) {
  617. if (err == -EAGAIN)
  618. goto again;
  619. xenbus_dev_fatal(dev, err, "completing transaction");
  620. goto destroy_blkring;
  621. }
  622. xenbus_switch_state(dev, XenbusStateInitialised);
  623. return 0;
  624. abort_transaction:
  625. xenbus_transaction_end(xbt, 1);
  626. if (message)
  627. xenbus_dev_fatal(dev, err, "%s", message);
  628. destroy_blkring:
  629. blkif_free(info, 0);
  630. out:
  631. return err;
  632. }
  633. /**
  634. * Entry point to this code when a new device is created. Allocate the basic
  635. * structures and the ring buffer for communication with the backend, and
  636. * inform the backend of the appropriate details for those. Switch to
  637. * Initialised state.
  638. */
  639. static int blkfront_probe(struct xenbus_device *dev,
  640. const struct xenbus_device_id *id)
  641. {
  642. int err, vdevice, i;
  643. struct blkfront_info *info;
  644. /* FIXME: Use dynamic device id if this is not set. */
  645. err = xenbus_scanf(XBT_NIL, dev->nodename,
  646. "virtual-device", "%i", &vdevice);
  647. if (err != 1) {
  648. /* go looking in the extended area instead */
  649. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  650. "%i", &vdevice);
  651. if (err != 1) {
  652. xenbus_dev_fatal(dev, err, "reading virtual-device");
  653. return err;
  654. }
  655. }
  656. info = kzalloc(sizeof(*info), GFP_KERNEL);
  657. if (!info) {
  658. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  659. return -ENOMEM;
  660. }
  661. info->xbdev = dev;
  662. info->vdevice = vdevice;
  663. info->connected = BLKIF_STATE_DISCONNECTED;
  664. INIT_WORK(&info->work, blkif_restart_queue);
  665. for (i = 0; i < BLK_RING_SIZE; i++)
  666. info->shadow[i].req.id = i+1;
  667. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  668. /* Front end dir is a number, which is used as the id. */
  669. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  670. dev_set_drvdata(&dev->dev, info);
  671. err = talk_to_blkback(dev, info);
  672. if (err) {
  673. kfree(info);
  674. dev_set_drvdata(&dev->dev, NULL);
  675. return err;
  676. }
  677. return 0;
  678. }
  679. static int blkif_recover(struct blkfront_info *info)
  680. {
  681. int i;
  682. struct blkif_request *req;
  683. struct blk_shadow *copy;
  684. int j;
  685. /* Stage 1: Make a safe copy of the shadow state. */
  686. copy = kmalloc(sizeof(info->shadow),
  687. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  688. if (!copy)
  689. return -ENOMEM;
  690. memcpy(copy, info->shadow, sizeof(info->shadow));
  691. /* Stage 2: Set up free list. */
  692. memset(&info->shadow, 0, sizeof(info->shadow));
  693. for (i = 0; i < BLK_RING_SIZE; i++)
  694. info->shadow[i].req.id = i+1;
  695. info->shadow_free = info->ring.req_prod_pvt;
  696. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  697. /* Stage 3: Find pending requests and requeue them. */
  698. for (i = 0; i < BLK_RING_SIZE; i++) {
  699. /* Not in use? */
  700. if (copy[i].request == 0)
  701. continue;
  702. /* Grab a request slot and copy shadow state into it. */
  703. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  704. *req = copy[i].req;
  705. /* We get a new request id, and must reset the shadow state. */
  706. req->id = get_id_from_freelist(info);
  707. memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
  708. /* Rewrite any grant references invalidated by susp/resume. */
  709. for (j = 0; j < req->nr_segments; j++)
  710. gnttab_grant_foreign_access_ref(
  711. req->seg[j].gref,
  712. info->xbdev->otherend_id,
  713. pfn_to_mfn(info->shadow[req->id].frame[j]),
  714. rq_data_dir(
  715. (struct request *)
  716. info->shadow[req->id].request));
  717. info->shadow[req->id].req = *req;
  718. info->ring.req_prod_pvt++;
  719. }
  720. kfree(copy);
  721. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  722. spin_lock_irq(&blkif_io_lock);
  723. /* Now safe for us to use the shared ring */
  724. info->connected = BLKIF_STATE_CONNECTED;
  725. /* Send off requeued requests */
  726. flush_requests(info);
  727. /* Kick any other new requests queued since we resumed */
  728. kick_pending_request_queues(info);
  729. spin_unlock_irq(&blkif_io_lock);
  730. return 0;
  731. }
  732. /**
  733. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  734. * driver restart. We tear down our blkif structure and recreate it, but
  735. * leave the device-layer structures intact so that this is transparent to the
  736. * rest of the kernel.
  737. */
  738. static int blkfront_resume(struct xenbus_device *dev)
  739. {
  740. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  741. int err;
  742. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  743. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  744. err = talk_to_blkback(dev, info);
  745. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  746. err = blkif_recover(info);
  747. return err;
  748. }
  749. /*
  750. * Invoked when the backend is finally 'ready' (and has told produced
  751. * the details about the physical device - #sectors, size, etc).
  752. */
  753. static void blkfront_connect(struct blkfront_info *info)
  754. {
  755. unsigned long long sectors;
  756. unsigned long sector_size;
  757. unsigned int binfo;
  758. int err;
  759. if ((info->connected == BLKIF_STATE_CONNECTED) ||
  760. (info->connected == BLKIF_STATE_SUSPENDED) )
  761. return;
  762. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  763. __func__, info->xbdev->otherend);
  764. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  765. "sectors", "%llu", &sectors,
  766. "info", "%u", &binfo,
  767. "sector-size", "%lu", &sector_size,
  768. NULL);
  769. if (err) {
  770. xenbus_dev_fatal(info->xbdev, err,
  771. "reading backend fields at %s",
  772. info->xbdev->otherend);
  773. return;
  774. }
  775. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  776. "feature-barrier", "%lu", &info->feature_barrier,
  777. NULL);
  778. if (err)
  779. info->feature_barrier = 0;
  780. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  781. if (err) {
  782. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  783. info->xbdev->otherend);
  784. return;
  785. }
  786. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  787. /* Kick pending requests. */
  788. spin_lock_irq(&blkif_io_lock);
  789. info->connected = BLKIF_STATE_CONNECTED;
  790. kick_pending_request_queues(info);
  791. spin_unlock_irq(&blkif_io_lock);
  792. add_disk(info->gd);
  793. info->is_ready = 1;
  794. }
  795. /**
  796. * Handle the change of state of the backend to Closing. We must delete our
  797. * device-layer structures now, to ensure that writes are flushed through to
  798. * the backend. Once is this done, we can switch to Closed in
  799. * acknowledgement.
  800. */
  801. static void blkfront_closing(struct xenbus_device *dev)
  802. {
  803. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  804. unsigned int minor, nr_minors;
  805. unsigned long flags;
  806. dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
  807. if (info->rq == NULL)
  808. goto out;
  809. spin_lock_irqsave(&blkif_io_lock, flags);
  810. /* No more blkif_request(). */
  811. blk_stop_queue(info->rq);
  812. /* No more gnttab callback work. */
  813. gnttab_cancel_free_callback(&info->callback);
  814. spin_unlock_irqrestore(&blkif_io_lock, flags);
  815. /* Flush gnttab callback work. Must be done with no locks held. */
  816. flush_scheduled_work();
  817. blk_cleanup_queue(info->rq);
  818. info->rq = NULL;
  819. minor = info->gd->first_minor;
  820. nr_minors = info->gd->minors;
  821. del_gendisk(info->gd);
  822. xlbd_release_minors(minor, nr_minors);
  823. out:
  824. xenbus_frontend_closed(dev);
  825. }
  826. /**
  827. * Callback received when the backend's state changes.
  828. */
  829. static void blkback_changed(struct xenbus_device *dev,
  830. enum xenbus_state backend_state)
  831. {
  832. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  833. struct block_device *bd;
  834. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  835. switch (backend_state) {
  836. case XenbusStateInitialising:
  837. case XenbusStateInitWait:
  838. case XenbusStateInitialised:
  839. case XenbusStateUnknown:
  840. case XenbusStateClosed:
  841. break;
  842. case XenbusStateConnected:
  843. blkfront_connect(info);
  844. break;
  845. case XenbusStateClosing:
  846. if (info->gd == NULL) {
  847. xenbus_frontend_closed(dev);
  848. break;
  849. }
  850. bd = bdget_disk(info->gd, 0);
  851. if (bd == NULL)
  852. xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
  853. mutex_lock(&bd->bd_mutex);
  854. if (info->users > 0)
  855. xenbus_dev_error(dev, -EBUSY,
  856. "Device in use; refusing to close");
  857. else
  858. blkfront_closing(dev);
  859. mutex_unlock(&bd->bd_mutex);
  860. bdput(bd);
  861. break;
  862. }
  863. }
  864. static int blkfront_remove(struct xenbus_device *dev)
  865. {
  866. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  867. dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
  868. blkif_free(info, 0);
  869. if(info->users == 0)
  870. kfree(info);
  871. else
  872. info->is_ready = -1;
  873. return 0;
  874. }
  875. static int blkfront_is_ready(struct xenbus_device *dev)
  876. {
  877. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  878. return info->is_ready > 0;
  879. }
  880. static int blkif_open(struct block_device *bdev, fmode_t mode)
  881. {
  882. struct blkfront_info *info = bdev->bd_disk->private_data;
  883. int ret = 0;
  884. lock_kernel();
  885. if (info->is_ready < 0)
  886. ret = -ENODEV;
  887. else
  888. info->users++;
  889. unlock_kernel();
  890. return ret;
  891. }
  892. static int blkif_release(struct gendisk *disk, fmode_t mode)
  893. {
  894. struct blkfront_info *info = disk->private_data;
  895. lock_kernel();
  896. info->users--;
  897. if (info->users == 0) {
  898. /* Check whether we have been instructed to close. We will
  899. have ignored this request initially, as the device was
  900. still mounted. */
  901. struct xenbus_device *dev = info->xbdev;
  902. enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
  903. if(info->is_ready < 0) {
  904. blkfront_closing(dev);
  905. kfree(info);
  906. } else if (state == XenbusStateClosing && info->is_ready)
  907. blkfront_closing(dev);
  908. }
  909. unlock_kernel();
  910. return 0;
  911. }
  912. static const struct block_device_operations xlvbd_block_fops =
  913. {
  914. .owner = THIS_MODULE,
  915. .open = blkif_open,
  916. .release = blkif_release,
  917. .getgeo = blkif_getgeo,
  918. .ioctl = blkif_ioctl,
  919. };
  920. static const struct xenbus_device_id blkfront_ids[] = {
  921. { "vbd" },
  922. { "" }
  923. };
  924. static struct xenbus_driver blkfront = {
  925. .name = "vbd",
  926. .owner = THIS_MODULE,
  927. .ids = blkfront_ids,
  928. .probe = blkfront_probe,
  929. .remove = blkfront_remove,
  930. .resume = blkfront_resume,
  931. .otherend_changed = blkback_changed,
  932. .is_ready = blkfront_is_ready,
  933. };
  934. static int __init xlblk_init(void)
  935. {
  936. if (!xen_domain())
  937. return -ENODEV;
  938. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  939. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  940. XENVBD_MAJOR, DEV_NAME);
  941. return -ENODEV;
  942. }
  943. return xenbus_register_frontend(&blkfront);
  944. }
  945. module_init(xlblk_init);
  946. static void __exit xlblk_exit(void)
  947. {
  948. return xenbus_unregister_driver(&blkfront);
  949. }
  950. module_exit(xlblk_exit);
  951. MODULE_DESCRIPTION("Xen virtual block device frontend");
  952. MODULE_LICENSE("GPL");
  953. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  954. MODULE_ALIAS("xen:vbd");
  955. MODULE_ALIAS("xenblk");