xen-blkfront.c 27 KB

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