xen-blkfront.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. int feature_barrier;
  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 int xlvbd_barrier(struct blkfront_info *info)
  346. {
  347. int err;
  348. const char *barrier;
  349. switch (info->feature_barrier) {
  350. case QUEUE_ORDERED_DRAIN: barrier = "enabled (drain)"; break;
  351. case QUEUE_ORDERED_TAG: barrier = "enabled (tag)"; break;
  352. case QUEUE_ORDERED_NONE: barrier = "disabled"; break;
  353. default: return -EINVAL;
  354. }
  355. err = blk_queue_ordered(info->rq, info->feature_barrier);
  356. if (err)
  357. return err;
  358. printk(KERN_INFO "blkfront: %s: barriers %s\n",
  359. info->gd->disk_name, barrier);
  360. return 0;
  361. }
  362. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  363. struct blkfront_info *info,
  364. u16 vdisk_info, u16 sector_size)
  365. {
  366. struct gendisk *gd;
  367. int nr_minors = 1;
  368. int err = -ENODEV;
  369. unsigned int offset;
  370. int minor;
  371. int nr_parts;
  372. BUG_ON(info->gd != NULL);
  373. BUG_ON(info->rq != NULL);
  374. if ((info->vdevice>>EXT_SHIFT) > 1) {
  375. /* this is above the extended range; something is wrong */
  376. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  377. return -ENODEV;
  378. }
  379. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  380. minor = BLKIF_MINOR(info->vdevice);
  381. nr_parts = PARTS_PER_DISK;
  382. } else {
  383. minor = BLKIF_MINOR_EXT(info->vdevice);
  384. nr_parts = PARTS_PER_EXT_DISK;
  385. }
  386. if ((minor % nr_parts) == 0)
  387. nr_minors = nr_parts;
  388. err = xlbd_reserve_minors(minor, nr_minors);
  389. if (err)
  390. goto out;
  391. err = -ENODEV;
  392. gd = alloc_disk(nr_minors);
  393. if (gd == NULL)
  394. goto release;
  395. offset = minor / nr_parts;
  396. if (nr_minors > 1) {
  397. if (offset < 26)
  398. sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
  399. else
  400. sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
  401. 'a' + ((offset / 26)-1), 'a' + (offset % 26));
  402. } else {
  403. if (offset < 26)
  404. sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
  405. 'a' + offset,
  406. minor & (nr_parts - 1));
  407. else
  408. sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
  409. 'a' + ((offset / 26) - 1),
  410. 'a' + (offset % 26),
  411. minor & (nr_parts - 1));
  412. }
  413. gd->major = XENVBD_MAJOR;
  414. gd->first_minor = minor;
  415. gd->fops = &xlvbd_block_fops;
  416. gd->private_data = info;
  417. gd->driverfs_dev = &(info->xbdev->dev);
  418. set_capacity(gd, capacity);
  419. if (xlvbd_init_blk_queue(gd, sector_size)) {
  420. del_gendisk(gd);
  421. goto release;
  422. }
  423. info->rq = gd->queue;
  424. info->gd = gd;
  425. xlvbd_barrier(info);
  426. if (vdisk_info & VDISK_READONLY)
  427. set_disk_ro(gd, 1);
  428. if (vdisk_info & VDISK_REMOVABLE)
  429. gd->flags |= GENHD_FL_REMOVABLE;
  430. if (vdisk_info & VDISK_CDROM)
  431. gd->flags |= GENHD_FL_CD;
  432. return 0;
  433. release:
  434. xlbd_release_minors(minor, nr_minors);
  435. out:
  436. return err;
  437. }
  438. static void xlvbd_release_gendisk(struct blkfront_info *info)
  439. {
  440. unsigned int minor, nr_minors;
  441. unsigned long flags;
  442. if (info->rq == NULL)
  443. return;
  444. spin_lock_irqsave(&blkif_io_lock, flags);
  445. /* No more blkif_request(). */
  446. blk_stop_queue(info->rq);
  447. /* No more gnttab callback work. */
  448. gnttab_cancel_free_callback(&info->callback);
  449. spin_unlock_irqrestore(&blkif_io_lock, flags);
  450. /* Flush gnttab callback work. Must be done with no locks held. */
  451. flush_scheduled_work();
  452. del_gendisk(info->gd);
  453. minor = info->gd->first_minor;
  454. nr_minors = info->gd->minors;
  455. xlbd_release_minors(minor, nr_minors);
  456. blk_cleanup_queue(info->rq);
  457. info->rq = NULL;
  458. put_disk(info->gd);
  459. info->gd = NULL;
  460. }
  461. static void kick_pending_request_queues(struct blkfront_info *info)
  462. {
  463. if (!RING_FULL(&info->ring)) {
  464. /* Re-enable calldowns. */
  465. blk_start_queue(info->rq);
  466. /* Kick things off immediately. */
  467. do_blkif_request(info->rq);
  468. }
  469. }
  470. static void blkif_restart_queue(struct work_struct *work)
  471. {
  472. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  473. spin_lock_irq(&blkif_io_lock);
  474. if (info->connected == BLKIF_STATE_CONNECTED)
  475. kick_pending_request_queues(info);
  476. spin_unlock_irq(&blkif_io_lock);
  477. }
  478. static void blkif_free(struct blkfront_info *info, int suspend)
  479. {
  480. /* Prevent new requests being issued until we fix things up. */
  481. spin_lock_irq(&blkif_io_lock);
  482. info->connected = suspend ?
  483. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  484. /* No more blkif_request(). */
  485. if (info->rq)
  486. blk_stop_queue(info->rq);
  487. /* No more gnttab callback work. */
  488. gnttab_cancel_free_callback(&info->callback);
  489. spin_unlock_irq(&blkif_io_lock);
  490. /* Flush gnttab callback work. Must be done with no locks held. */
  491. flush_scheduled_work();
  492. /* Free resources associated with old device channel. */
  493. if (info->ring_ref != GRANT_INVALID_REF) {
  494. gnttab_end_foreign_access(info->ring_ref, 0,
  495. (unsigned long)info->ring.sring);
  496. info->ring_ref = GRANT_INVALID_REF;
  497. info->ring.sring = NULL;
  498. }
  499. if (info->irq)
  500. unbind_from_irqhandler(info->irq, info);
  501. info->evtchn = info->irq = 0;
  502. }
  503. static void blkif_completion(struct blk_shadow *s)
  504. {
  505. int i;
  506. for (i = 0; i < s->req.nr_segments; i++)
  507. gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
  508. }
  509. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  510. {
  511. struct request *req;
  512. struct blkif_response *bret;
  513. RING_IDX i, rp;
  514. unsigned long flags;
  515. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  516. int error;
  517. spin_lock_irqsave(&blkif_io_lock, flags);
  518. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  519. spin_unlock_irqrestore(&blkif_io_lock, flags);
  520. return IRQ_HANDLED;
  521. }
  522. again:
  523. rp = info->ring.sring->rsp_prod;
  524. rmb(); /* Ensure we see queued responses up to 'rp'. */
  525. for (i = info->ring.rsp_cons; i != rp; i++) {
  526. unsigned long id;
  527. bret = RING_GET_RESPONSE(&info->ring, i);
  528. id = bret->id;
  529. req = (struct request *)info->shadow[id].request;
  530. blkif_completion(&info->shadow[id]);
  531. add_id_to_freelist(info, id);
  532. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  533. switch (bret->operation) {
  534. case BLKIF_OP_WRITE_BARRIER:
  535. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  536. printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
  537. info->gd->disk_name);
  538. error = -EOPNOTSUPP;
  539. info->feature_barrier = QUEUE_ORDERED_NONE;
  540. xlvbd_barrier(info);
  541. }
  542. /* fall through */
  543. case BLKIF_OP_READ:
  544. case BLKIF_OP_WRITE:
  545. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  546. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  547. "request: %x\n", bret->status);
  548. __blk_end_request_all(req, error);
  549. break;
  550. default:
  551. BUG();
  552. }
  553. }
  554. info->ring.rsp_cons = i;
  555. if (i != info->ring.req_prod_pvt) {
  556. int more_to_do;
  557. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  558. if (more_to_do)
  559. goto again;
  560. } else
  561. info->ring.sring->rsp_event = i + 1;
  562. kick_pending_request_queues(info);
  563. spin_unlock_irqrestore(&blkif_io_lock, flags);
  564. return IRQ_HANDLED;
  565. }
  566. static int setup_blkring(struct xenbus_device *dev,
  567. struct blkfront_info *info)
  568. {
  569. struct blkif_sring *sring;
  570. int err;
  571. info->ring_ref = GRANT_INVALID_REF;
  572. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  573. if (!sring) {
  574. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  575. return -ENOMEM;
  576. }
  577. SHARED_RING_INIT(sring);
  578. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  579. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  580. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  581. if (err < 0) {
  582. free_page((unsigned long)sring);
  583. info->ring.sring = NULL;
  584. goto fail;
  585. }
  586. info->ring_ref = err;
  587. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  588. if (err)
  589. goto fail;
  590. err = bind_evtchn_to_irqhandler(info->evtchn,
  591. blkif_interrupt,
  592. IRQF_SAMPLE_RANDOM, "blkif", info);
  593. if (err <= 0) {
  594. xenbus_dev_fatal(dev, err,
  595. "bind_evtchn_to_irqhandler failed");
  596. goto fail;
  597. }
  598. info->irq = err;
  599. return 0;
  600. fail:
  601. blkif_free(info, 0);
  602. return err;
  603. }
  604. /* Common code used when first setting up, and when resuming. */
  605. static int talk_to_blkback(struct xenbus_device *dev,
  606. struct blkfront_info *info)
  607. {
  608. const char *message = NULL;
  609. struct xenbus_transaction xbt;
  610. int err;
  611. /* Create shared ring, alloc event channel. */
  612. err = setup_blkring(dev, info);
  613. if (err)
  614. goto out;
  615. again:
  616. err = xenbus_transaction_start(&xbt);
  617. if (err) {
  618. xenbus_dev_fatal(dev, err, "starting transaction");
  619. goto destroy_blkring;
  620. }
  621. err = xenbus_printf(xbt, dev->nodename,
  622. "ring-ref", "%u", info->ring_ref);
  623. if (err) {
  624. message = "writing ring-ref";
  625. goto abort_transaction;
  626. }
  627. err = xenbus_printf(xbt, dev->nodename,
  628. "event-channel", "%u", info->evtchn);
  629. if (err) {
  630. message = "writing event-channel";
  631. goto abort_transaction;
  632. }
  633. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  634. XEN_IO_PROTO_ABI_NATIVE);
  635. if (err) {
  636. message = "writing protocol";
  637. goto abort_transaction;
  638. }
  639. err = xenbus_transaction_end(xbt, 0);
  640. if (err) {
  641. if (err == -EAGAIN)
  642. goto again;
  643. xenbus_dev_fatal(dev, err, "completing transaction");
  644. goto destroy_blkring;
  645. }
  646. xenbus_switch_state(dev, XenbusStateInitialised);
  647. return 0;
  648. abort_transaction:
  649. xenbus_transaction_end(xbt, 1);
  650. if (message)
  651. xenbus_dev_fatal(dev, err, "%s", message);
  652. destroy_blkring:
  653. blkif_free(info, 0);
  654. out:
  655. return err;
  656. }
  657. /**
  658. * Entry point to this code when a new device is created. Allocate the basic
  659. * structures and the ring buffer for communication with the backend, and
  660. * inform the backend of the appropriate details for those. Switch to
  661. * Initialised state.
  662. */
  663. static int blkfront_probe(struct xenbus_device *dev,
  664. const struct xenbus_device_id *id)
  665. {
  666. int err, vdevice, i;
  667. struct blkfront_info *info;
  668. /* FIXME: Use dynamic device id if this is not set. */
  669. err = xenbus_scanf(XBT_NIL, dev->nodename,
  670. "virtual-device", "%i", &vdevice);
  671. if (err != 1) {
  672. /* go looking in the extended area instead */
  673. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  674. "%i", &vdevice);
  675. if (err != 1) {
  676. xenbus_dev_fatal(dev, err, "reading virtual-device");
  677. return err;
  678. }
  679. }
  680. if (xen_hvm_domain()) {
  681. char *type;
  682. int len;
  683. /* no unplug has been done: do not hook devices != xen vbds */
  684. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
  685. int major;
  686. if (!VDEV_IS_EXTENDED(vdevice))
  687. major = BLKIF_MAJOR(vdevice);
  688. else
  689. major = XENVBD_MAJOR;
  690. if (major != XENVBD_MAJOR) {
  691. printk(KERN_INFO
  692. "%s: HVM does not support vbd %d as xen block device\n",
  693. __FUNCTION__, vdevice);
  694. return -ENODEV;
  695. }
  696. }
  697. /* do not create a PV cdrom device if we are an HVM guest */
  698. type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
  699. if (IS_ERR(type))
  700. return -ENODEV;
  701. if (strncmp(type, "cdrom", 5) == 0) {
  702. kfree(type);
  703. return -ENODEV;
  704. }
  705. kfree(type);
  706. }
  707. info = kzalloc(sizeof(*info), GFP_KERNEL);
  708. if (!info) {
  709. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  710. return -ENOMEM;
  711. }
  712. mutex_init(&info->mutex);
  713. info->xbdev = dev;
  714. info->vdevice = vdevice;
  715. info->connected = BLKIF_STATE_DISCONNECTED;
  716. INIT_WORK(&info->work, blkif_restart_queue);
  717. for (i = 0; i < BLK_RING_SIZE; i++)
  718. info->shadow[i].req.id = i+1;
  719. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  720. /* Front end dir is a number, which is used as the id. */
  721. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  722. dev_set_drvdata(&dev->dev, info);
  723. err = talk_to_blkback(dev, info);
  724. if (err) {
  725. kfree(info);
  726. dev_set_drvdata(&dev->dev, NULL);
  727. return err;
  728. }
  729. return 0;
  730. }
  731. static int blkif_recover(struct blkfront_info *info)
  732. {
  733. int i;
  734. struct blkif_request *req;
  735. struct blk_shadow *copy;
  736. int j;
  737. /* Stage 1: Make a safe copy of the shadow state. */
  738. copy = kmalloc(sizeof(info->shadow),
  739. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  740. if (!copy)
  741. return -ENOMEM;
  742. memcpy(copy, info->shadow, sizeof(info->shadow));
  743. /* Stage 2: Set up free list. */
  744. memset(&info->shadow, 0, sizeof(info->shadow));
  745. for (i = 0; i < BLK_RING_SIZE; i++)
  746. info->shadow[i].req.id = i+1;
  747. info->shadow_free = info->ring.req_prod_pvt;
  748. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  749. /* Stage 3: Find pending requests and requeue them. */
  750. for (i = 0; i < BLK_RING_SIZE; i++) {
  751. /* Not in use? */
  752. if (copy[i].request == 0)
  753. continue;
  754. /* Grab a request slot and copy shadow state into it. */
  755. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  756. *req = copy[i].req;
  757. /* We get a new request id, and must reset the shadow state. */
  758. req->id = get_id_from_freelist(info);
  759. memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
  760. /* Rewrite any grant references invalidated by susp/resume. */
  761. for (j = 0; j < req->nr_segments; j++)
  762. gnttab_grant_foreign_access_ref(
  763. req->seg[j].gref,
  764. info->xbdev->otherend_id,
  765. pfn_to_mfn(info->shadow[req->id].frame[j]),
  766. rq_data_dir(
  767. (struct request *)
  768. info->shadow[req->id].request));
  769. info->shadow[req->id].req = *req;
  770. info->ring.req_prod_pvt++;
  771. }
  772. kfree(copy);
  773. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  774. spin_lock_irq(&blkif_io_lock);
  775. /* Now safe for us to use the shared ring */
  776. info->connected = BLKIF_STATE_CONNECTED;
  777. /* Send off requeued requests */
  778. flush_requests(info);
  779. /* Kick any other new requests queued since we resumed */
  780. kick_pending_request_queues(info);
  781. spin_unlock_irq(&blkif_io_lock);
  782. return 0;
  783. }
  784. /**
  785. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  786. * driver restart. We tear down our blkif structure and recreate it, but
  787. * leave the device-layer structures intact so that this is transparent to the
  788. * rest of the kernel.
  789. */
  790. static int blkfront_resume(struct xenbus_device *dev)
  791. {
  792. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  793. int err;
  794. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  795. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  796. err = talk_to_blkback(dev, info);
  797. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  798. err = blkif_recover(info);
  799. return err;
  800. }
  801. static void
  802. blkfront_closing(struct blkfront_info *info)
  803. {
  804. struct xenbus_device *xbdev = info->xbdev;
  805. struct block_device *bdev = NULL;
  806. mutex_lock(&info->mutex);
  807. if (xbdev->state == XenbusStateClosing) {
  808. mutex_unlock(&info->mutex);
  809. return;
  810. }
  811. if (info->gd)
  812. bdev = bdget_disk(info->gd, 0);
  813. mutex_unlock(&info->mutex);
  814. if (!bdev) {
  815. xenbus_frontend_closed(xbdev);
  816. return;
  817. }
  818. mutex_lock(&bdev->bd_mutex);
  819. if (bdev->bd_openers) {
  820. xenbus_dev_error(xbdev, -EBUSY,
  821. "Device in use; refusing to close");
  822. xenbus_switch_state(xbdev, XenbusStateClosing);
  823. } else {
  824. xlvbd_release_gendisk(info);
  825. xenbus_frontend_closed(xbdev);
  826. }
  827. mutex_unlock(&bdev->bd_mutex);
  828. bdput(bdev);
  829. }
  830. /*
  831. * Invoked when the backend is finally 'ready' (and has told produced
  832. * the details about the physical device - #sectors, size, etc).
  833. */
  834. static void blkfront_connect(struct blkfront_info *info)
  835. {
  836. unsigned long long sectors;
  837. unsigned long sector_size;
  838. unsigned int binfo;
  839. int err;
  840. int barrier;
  841. switch (info->connected) {
  842. case BLKIF_STATE_CONNECTED:
  843. /*
  844. * Potentially, the back-end may be signalling
  845. * a capacity change; update the capacity.
  846. */
  847. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  848. "sectors", "%Lu", &sectors);
  849. if (XENBUS_EXIST_ERR(err))
  850. return;
  851. printk(KERN_INFO "Setting capacity to %Lu\n",
  852. sectors);
  853. set_capacity(info->gd, sectors);
  854. revalidate_disk(info->gd);
  855. /* fall through */
  856. case BLKIF_STATE_SUSPENDED:
  857. return;
  858. default:
  859. break;
  860. }
  861. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  862. __func__, info->xbdev->otherend);
  863. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  864. "sectors", "%llu", &sectors,
  865. "info", "%u", &binfo,
  866. "sector-size", "%lu", &sector_size,
  867. NULL);
  868. if (err) {
  869. xenbus_dev_fatal(info->xbdev, err,
  870. "reading backend fields at %s",
  871. info->xbdev->otherend);
  872. return;
  873. }
  874. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  875. "feature-barrier", "%lu", &barrier,
  876. NULL);
  877. /*
  878. * If there's no "feature-barrier" defined, then it means
  879. * we're dealing with a very old backend which writes
  880. * synchronously; draining will do what needs to get done.
  881. *
  882. * If there are barriers, then we can do full queued writes
  883. * with tagged barriers.
  884. *
  885. * If barriers are not supported, then there's no much we can
  886. * do, so just set ordering to NONE.
  887. */
  888. if (err)
  889. info->feature_barrier = QUEUE_ORDERED_DRAIN;
  890. else if (barrier)
  891. info->feature_barrier = QUEUE_ORDERED_TAG;
  892. else
  893. info->feature_barrier = QUEUE_ORDERED_NONE;
  894. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  895. if (err) {
  896. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  897. info->xbdev->otherend);
  898. return;
  899. }
  900. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  901. /* Kick pending requests. */
  902. spin_lock_irq(&blkif_io_lock);
  903. info->connected = BLKIF_STATE_CONNECTED;
  904. kick_pending_request_queues(info);
  905. spin_unlock_irq(&blkif_io_lock);
  906. add_disk(info->gd);
  907. info->is_ready = 1;
  908. }
  909. /**
  910. * Callback received when the backend's state changes.
  911. */
  912. static void blkback_changed(struct xenbus_device *dev,
  913. enum xenbus_state backend_state)
  914. {
  915. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  916. dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
  917. switch (backend_state) {
  918. case XenbusStateInitialising:
  919. case XenbusStateInitWait:
  920. case XenbusStateInitialised:
  921. case XenbusStateUnknown:
  922. case XenbusStateClosed:
  923. break;
  924. case XenbusStateConnected:
  925. blkfront_connect(info);
  926. break;
  927. case XenbusStateClosing:
  928. blkfront_closing(info);
  929. break;
  930. }
  931. }
  932. static int blkfront_remove(struct xenbus_device *xbdev)
  933. {
  934. struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
  935. struct block_device *bdev = NULL;
  936. struct gendisk *disk;
  937. dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
  938. blkif_free(info, 0);
  939. mutex_lock(&info->mutex);
  940. disk = info->gd;
  941. if (disk)
  942. bdev = bdget_disk(disk, 0);
  943. info->xbdev = NULL;
  944. mutex_unlock(&info->mutex);
  945. if (!bdev) {
  946. kfree(info);
  947. return 0;
  948. }
  949. /*
  950. * The xbdev was removed before we reached the Closed
  951. * state. See if it's safe to remove the disk. If the bdev
  952. * isn't closed yet, we let release take care of it.
  953. */
  954. mutex_lock(&bdev->bd_mutex);
  955. info = disk->private_data;
  956. dev_warn(disk_to_dev(disk),
  957. "%s was hot-unplugged, %d stale handles\n",
  958. xbdev->nodename, bdev->bd_openers);
  959. if (info && !bdev->bd_openers) {
  960. xlvbd_release_gendisk(info);
  961. disk->private_data = NULL;
  962. kfree(info);
  963. }
  964. mutex_unlock(&bdev->bd_mutex);
  965. bdput(bdev);
  966. return 0;
  967. }
  968. static int blkfront_is_ready(struct xenbus_device *dev)
  969. {
  970. struct blkfront_info *info = dev_get_drvdata(&dev->dev);
  971. return info->is_ready && info->xbdev;
  972. }
  973. static int blkif_open(struct block_device *bdev, fmode_t mode)
  974. {
  975. struct gendisk *disk = bdev->bd_disk;
  976. struct blkfront_info *info;
  977. int err = 0;
  978. lock_kernel();
  979. info = disk->private_data;
  980. if (!info) {
  981. /* xbdev gone */
  982. err = -ERESTARTSYS;
  983. goto out;
  984. }
  985. mutex_lock(&info->mutex);
  986. if (!info->gd)
  987. /* xbdev is closed */
  988. err = -ERESTARTSYS;
  989. mutex_unlock(&info->mutex);
  990. out:
  991. unlock_kernel();
  992. return err;
  993. }
  994. static int blkif_release(struct gendisk *disk, fmode_t mode)
  995. {
  996. struct blkfront_info *info = disk->private_data;
  997. struct block_device *bdev;
  998. struct xenbus_device *xbdev;
  999. lock_kernel();
  1000. bdev = bdget_disk(disk, 0);
  1001. bdput(bdev);
  1002. if (bdev->bd_openers)
  1003. goto out;
  1004. /*
  1005. * Check if we have been instructed to close. We will have
  1006. * deferred this request, because the bdev was still open.
  1007. */
  1008. mutex_lock(&info->mutex);
  1009. xbdev = info->xbdev;
  1010. if (xbdev && xbdev->state == XenbusStateClosing) {
  1011. /* pending switch to state closed */
  1012. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1013. xlvbd_release_gendisk(info);
  1014. xenbus_frontend_closed(info->xbdev);
  1015. }
  1016. mutex_unlock(&info->mutex);
  1017. if (!xbdev) {
  1018. /* sudden device removal */
  1019. dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
  1020. xlvbd_release_gendisk(info);
  1021. disk->private_data = NULL;
  1022. kfree(info);
  1023. }
  1024. out:
  1025. unlock_kernel();
  1026. return 0;
  1027. }
  1028. static const struct block_device_operations xlvbd_block_fops =
  1029. {
  1030. .owner = THIS_MODULE,
  1031. .open = blkif_open,
  1032. .release = blkif_release,
  1033. .getgeo = blkif_getgeo,
  1034. .ioctl = blkif_ioctl,
  1035. };
  1036. static const struct xenbus_device_id blkfront_ids[] = {
  1037. { "vbd" },
  1038. { "" }
  1039. };
  1040. static struct xenbus_driver blkfront = {
  1041. .name = "vbd",
  1042. .owner = THIS_MODULE,
  1043. .ids = blkfront_ids,
  1044. .probe = blkfront_probe,
  1045. .remove = blkfront_remove,
  1046. .resume = blkfront_resume,
  1047. .otherend_changed = blkback_changed,
  1048. .is_ready = blkfront_is_ready,
  1049. };
  1050. static int __init xlblk_init(void)
  1051. {
  1052. if (!xen_domain())
  1053. return -ENODEV;
  1054. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  1055. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  1056. XENVBD_MAJOR, DEV_NAME);
  1057. return -ENODEV;
  1058. }
  1059. return xenbus_register_frontend(&blkfront);
  1060. }
  1061. module_init(xlblk_init);
  1062. static void __exit xlblk_exit(void)
  1063. {
  1064. return xenbus_unregister_driver(&blkfront);
  1065. }
  1066. module_exit(xlblk_exit);
  1067. MODULE_DESCRIPTION("Xen virtual block device frontend");
  1068. MODULE_LICENSE("GPL");
  1069. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  1070. MODULE_ALIAS("xen:vbd");
  1071. MODULE_ALIAS("xenblk");