xen-blkfront.c 31 KB

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