xen-blkfront.c 31 KB

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