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 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)req->sector;
  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 = elv_next_request(rq)) != NULL) {
  254. info = req->rq_disk->private_data;
  255. if (!blk_fs_request(req)) {
  256. end_request(req, 0);
  257. continue;
  258. }
  259. if (RING_FULL(&info->ring))
  260. goto wait;
  261. pr_debug("do_blk_req %p: cmd %p, sec %lx, "
  262. "(%u/%li) buffer:%p [%s]\n",
  263. req, req->cmd, (unsigned long)req->sector,
  264. req->current_nr_sectors,
  265. req->nr_sectors, req->buffer,
  266. rq_data_dir(req) ? "write" : "read");
  267. blkdev_dequeue_request(req);
  268. if (blkif_queue_request(req)) {
  269. blk_requeue_request(rq, req);
  270. wait:
  271. /* Avoid pointless unplugs. */
  272. blk_stop_queue(rq);
  273. break;
  274. }
  275. queued++;
  276. }
  277. if (queued != 0)
  278. flush_requests(info);
  279. }
  280. static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
  281. {
  282. struct request_queue *rq;
  283. rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
  284. if (rq == NULL)
  285. return -1;
  286. queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
  287. /* Hard sector size and max sectors impersonate the equiv. hardware. */
  288. blk_queue_hardsect_size(rq, sector_size);
  289. blk_queue_max_sectors(rq, 512);
  290. /* Each segment in a request is up to an aligned page in size. */
  291. blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
  292. blk_queue_max_segment_size(rq, PAGE_SIZE);
  293. /* Ensure a merged request will fit in a single I/O ring slot. */
  294. blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  295. blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  296. /* Make sure buffer addresses are sector-aligned. */
  297. blk_queue_dma_alignment(rq, 511);
  298. /* Make sure we don't use bounce buffers. */
  299. blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
  300. gd->queue = rq;
  301. return 0;
  302. }
  303. static int xlvbd_barrier(struct blkfront_info *info)
  304. {
  305. int err;
  306. err = blk_queue_ordered(info->rq,
  307. info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE,
  308. NULL);
  309. if (err)
  310. return err;
  311. printk(KERN_INFO "blkfront: %s: barriers %s\n",
  312. info->gd->disk_name,
  313. info->feature_barrier ? "enabled" : "disabled");
  314. return 0;
  315. }
  316. static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
  317. struct blkfront_info *info,
  318. u16 vdisk_info, u16 sector_size)
  319. {
  320. struct gendisk *gd;
  321. int nr_minors = 1;
  322. int err = -ENODEV;
  323. unsigned int offset;
  324. int minor;
  325. int nr_parts;
  326. BUG_ON(info->gd != NULL);
  327. BUG_ON(info->rq != NULL);
  328. if ((info->vdevice>>EXT_SHIFT) > 1) {
  329. /* this is above the extended range; something is wrong */
  330. printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
  331. return -ENODEV;
  332. }
  333. if (!VDEV_IS_EXTENDED(info->vdevice)) {
  334. minor = BLKIF_MINOR(info->vdevice);
  335. nr_parts = PARTS_PER_DISK;
  336. } else {
  337. minor = BLKIF_MINOR_EXT(info->vdevice);
  338. nr_parts = PARTS_PER_EXT_DISK;
  339. }
  340. if ((minor % nr_parts) == 0)
  341. nr_minors = nr_parts;
  342. gd = alloc_disk(nr_minors);
  343. if (gd == NULL)
  344. goto out;
  345. offset = minor / nr_parts;
  346. if (nr_minors > 1) {
  347. if (offset < 26)
  348. sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
  349. else
  350. sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
  351. 'a' + ((offset / 26)-1), 'a' + (offset % 26));
  352. } else {
  353. if (offset < 26)
  354. sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
  355. 'a' + offset,
  356. minor & (nr_parts - 1));
  357. else
  358. sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
  359. 'a' + ((offset / 26) - 1),
  360. 'a' + (offset % 26),
  361. minor & (nr_parts - 1));
  362. }
  363. gd->major = XENVBD_MAJOR;
  364. gd->first_minor = minor;
  365. gd->fops = &xlvbd_block_fops;
  366. gd->private_data = info;
  367. gd->driverfs_dev = &(info->xbdev->dev);
  368. set_capacity(gd, capacity);
  369. if (xlvbd_init_blk_queue(gd, sector_size)) {
  370. del_gendisk(gd);
  371. goto out;
  372. }
  373. info->rq = gd->queue;
  374. info->gd = gd;
  375. if (info->feature_barrier)
  376. xlvbd_barrier(info);
  377. if (vdisk_info & VDISK_READONLY)
  378. set_disk_ro(gd, 1);
  379. if (vdisk_info & VDISK_REMOVABLE)
  380. gd->flags |= GENHD_FL_REMOVABLE;
  381. if (vdisk_info & VDISK_CDROM)
  382. gd->flags |= GENHD_FL_CD;
  383. return 0;
  384. out:
  385. return err;
  386. }
  387. static void kick_pending_request_queues(struct blkfront_info *info)
  388. {
  389. if (!RING_FULL(&info->ring)) {
  390. /* Re-enable calldowns. */
  391. blk_start_queue(info->rq);
  392. /* Kick things off immediately. */
  393. do_blkif_request(info->rq);
  394. }
  395. }
  396. static void blkif_restart_queue(struct work_struct *work)
  397. {
  398. struct blkfront_info *info = container_of(work, struct blkfront_info, work);
  399. spin_lock_irq(&blkif_io_lock);
  400. if (info->connected == BLKIF_STATE_CONNECTED)
  401. kick_pending_request_queues(info);
  402. spin_unlock_irq(&blkif_io_lock);
  403. }
  404. static void blkif_free(struct blkfront_info *info, int suspend)
  405. {
  406. /* Prevent new requests being issued until we fix things up. */
  407. spin_lock_irq(&blkif_io_lock);
  408. info->connected = suspend ?
  409. BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
  410. /* No more blkif_request(). */
  411. if (info->rq)
  412. blk_stop_queue(info->rq);
  413. /* No more gnttab callback work. */
  414. gnttab_cancel_free_callback(&info->callback);
  415. spin_unlock_irq(&blkif_io_lock);
  416. /* Flush gnttab callback work. Must be done with no locks held. */
  417. flush_scheduled_work();
  418. /* Free resources associated with old device channel. */
  419. if (info->ring_ref != GRANT_INVALID_REF) {
  420. gnttab_end_foreign_access(info->ring_ref, 0,
  421. (unsigned long)info->ring.sring);
  422. info->ring_ref = GRANT_INVALID_REF;
  423. info->ring.sring = NULL;
  424. }
  425. if (info->irq)
  426. unbind_from_irqhandler(info->irq, info);
  427. info->evtchn = info->irq = 0;
  428. }
  429. static void blkif_completion(struct blk_shadow *s)
  430. {
  431. int i;
  432. for (i = 0; i < s->req.nr_segments; i++)
  433. gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
  434. }
  435. static irqreturn_t blkif_interrupt(int irq, void *dev_id)
  436. {
  437. struct request *req;
  438. struct blkif_response *bret;
  439. RING_IDX i, rp;
  440. unsigned long flags;
  441. struct blkfront_info *info = (struct blkfront_info *)dev_id;
  442. int error;
  443. spin_lock_irqsave(&blkif_io_lock, flags);
  444. if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
  445. spin_unlock_irqrestore(&blkif_io_lock, flags);
  446. return IRQ_HANDLED;
  447. }
  448. again:
  449. rp = info->ring.sring->rsp_prod;
  450. rmb(); /* Ensure we see queued responses up to 'rp'. */
  451. for (i = info->ring.rsp_cons; i != rp; i++) {
  452. unsigned long id;
  453. int ret;
  454. bret = RING_GET_RESPONSE(&info->ring, i);
  455. id = bret->id;
  456. req = (struct request *)info->shadow[id].request;
  457. blkif_completion(&info->shadow[id]);
  458. add_id_to_freelist(info, id);
  459. error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
  460. switch (bret->operation) {
  461. case BLKIF_OP_WRITE_BARRIER:
  462. if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
  463. printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
  464. info->gd->disk_name);
  465. error = -EOPNOTSUPP;
  466. info->feature_barrier = 0;
  467. xlvbd_barrier(info);
  468. }
  469. /* fall through */
  470. case BLKIF_OP_READ:
  471. case BLKIF_OP_WRITE:
  472. if (unlikely(bret->status != BLKIF_RSP_OKAY))
  473. dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
  474. "request: %x\n", bret->status);
  475. ret = __blk_end_request(req, error, blk_rq_bytes(req));
  476. BUG_ON(ret);
  477. break;
  478. default:
  479. BUG();
  480. }
  481. }
  482. info->ring.rsp_cons = i;
  483. if (i != info->ring.req_prod_pvt) {
  484. int more_to_do;
  485. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  486. if (more_to_do)
  487. goto again;
  488. } else
  489. info->ring.sring->rsp_event = i + 1;
  490. kick_pending_request_queues(info);
  491. spin_unlock_irqrestore(&blkif_io_lock, flags);
  492. return IRQ_HANDLED;
  493. }
  494. static int setup_blkring(struct xenbus_device *dev,
  495. struct blkfront_info *info)
  496. {
  497. struct blkif_sring *sring;
  498. int err;
  499. info->ring_ref = GRANT_INVALID_REF;
  500. sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  501. if (!sring) {
  502. xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
  503. return -ENOMEM;
  504. }
  505. SHARED_RING_INIT(sring);
  506. FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  507. sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  508. err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
  509. if (err < 0) {
  510. free_page((unsigned long)sring);
  511. info->ring.sring = NULL;
  512. goto fail;
  513. }
  514. info->ring_ref = err;
  515. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  516. if (err)
  517. goto fail;
  518. err = bind_evtchn_to_irqhandler(info->evtchn,
  519. blkif_interrupt,
  520. IRQF_SAMPLE_RANDOM, "blkif", info);
  521. if (err <= 0) {
  522. xenbus_dev_fatal(dev, err,
  523. "bind_evtchn_to_irqhandler failed");
  524. goto fail;
  525. }
  526. info->irq = err;
  527. return 0;
  528. fail:
  529. blkif_free(info, 0);
  530. return err;
  531. }
  532. /* Common code used when first setting up, and when resuming. */
  533. static int talk_to_backend(struct xenbus_device *dev,
  534. struct blkfront_info *info)
  535. {
  536. const char *message = NULL;
  537. struct xenbus_transaction xbt;
  538. int err;
  539. /* Create shared ring, alloc event channel. */
  540. err = setup_blkring(dev, info);
  541. if (err)
  542. goto out;
  543. again:
  544. err = xenbus_transaction_start(&xbt);
  545. if (err) {
  546. xenbus_dev_fatal(dev, err, "starting transaction");
  547. goto destroy_blkring;
  548. }
  549. err = xenbus_printf(xbt, dev->nodename,
  550. "ring-ref", "%u", info->ring_ref);
  551. if (err) {
  552. message = "writing ring-ref";
  553. goto abort_transaction;
  554. }
  555. err = xenbus_printf(xbt, dev->nodename,
  556. "event-channel", "%u", info->evtchn);
  557. if (err) {
  558. message = "writing event-channel";
  559. goto abort_transaction;
  560. }
  561. err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
  562. XEN_IO_PROTO_ABI_NATIVE);
  563. if (err) {
  564. message = "writing protocol";
  565. goto abort_transaction;
  566. }
  567. err = xenbus_transaction_end(xbt, 0);
  568. if (err) {
  569. if (err == -EAGAIN)
  570. goto again;
  571. xenbus_dev_fatal(dev, err, "completing transaction");
  572. goto destroy_blkring;
  573. }
  574. xenbus_switch_state(dev, XenbusStateInitialised);
  575. return 0;
  576. abort_transaction:
  577. xenbus_transaction_end(xbt, 1);
  578. if (message)
  579. xenbus_dev_fatal(dev, err, "%s", message);
  580. destroy_blkring:
  581. blkif_free(info, 0);
  582. out:
  583. return err;
  584. }
  585. /**
  586. * Entry point to this code when a new device is created. Allocate the basic
  587. * structures and the ring buffer for communication with the backend, and
  588. * inform the backend of the appropriate details for those. Switch to
  589. * Initialised state.
  590. */
  591. static int blkfront_probe(struct xenbus_device *dev,
  592. const struct xenbus_device_id *id)
  593. {
  594. int err, vdevice, i;
  595. struct blkfront_info *info;
  596. /* FIXME: Use dynamic device id if this is not set. */
  597. err = xenbus_scanf(XBT_NIL, dev->nodename,
  598. "virtual-device", "%i", &vdevice);
  599. if (err != 1) {
  600. /* go looking in the extended area instead */
  601. err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
  602. "%i", &vdevice);
  603. if (err != 1) {
  604. xenbus_dev_fatal(dev, err, "reading virtual-device");
  605. return err;
  606. }
  607. }
  608. info = kzalloc(sizeof(*info), GFP_KERNEL);
  609. if (!info) {
  610. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  611. return -ENOMEM;
  612. }
  613. info->xbdev = dev;
  614. info->vdevice = vdevice;
  615. info->connected = BLKIF_STATE_DISCONNECTED;
  616. INIT_WORK(&info->work, blkif_restart_queue);
  617. for (i = 0; i < BLK_RING_SIZE; i++)
  618. info->shadow[i].req.id = i+1;
  619. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  620. /* Front end dir is a number, which is used as the id. */
  621. info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
  622. dev->dev.driver_data = info;
  623. err = talk_to_backend(dev, info);
  624. if (err) {
  625. kfree(info);
  626. dev->dev.driver_data = NULL;
  627. return err;
  628. }
  629. return 0;
  630. }
  631. static int blkif_recover(struct blkfront_info *info)
  632. {
  633. int i;
  634. struct blkif_request *req;
  635. struct blk_shadow *copy;
  636. int j;
  637. /* Stage 1: Make a safe copy of the shadow state. */
  638. copy = kmalloc(sizeof(info->shadow),
  639. GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
  640. if (!copy)
  641. return -ENOMEM;
  642. memcpy(copy, info->shadow, sizeof(info->shadow));
  643. /* Stage 2: Set up free list. */
  644. memset(&info->shadow, 0, sizeof(info->shadow));
  645. for (i = 0; i < BLK_RING_SIZE; i++)
  646. info->shadow[i].req.id = i+1;
  647. info->shadow_free = info->ring.req_prod_pvt;
  648. info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
  649. /* Stage 3: Find pending requests and requeue them. */
  650. for (i = 0; i < BLK_RING_SIZE; i++) {
  651. /* Not in use? */
  652. if (copy[i].request == 0)
  653. continue;
  654. /* Grab a request slot and copy shadow state into it. */
  655. req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
  656. *req = copy[i].req;
  657. /* We get a new request id, and must reset the shadow state. */
  658. req->id = get_id_from_freelist(info);
  659. memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
  660. /* Rewrite any grant references invalidated by susp/resume. */
  661. for (j = 0; j < req->nr_segments; j++)
  662. gnttab_grant_foreign_access_ref(
  663. req->seg[j].gref,
  664. info->xbdev->otherend_id,
  665. pfn_to_mfn(info->shadow[req->id].frame[j]),
  666. rq_data_dir(
  667. (struct request *)
  668. info->shadow[req->id].request));
  669. info->shadow[req->id].req = *req;
  670. info->ring.req_prod_pvt++;
  671. }
  672. kfree(copy);
  673. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  674. spin_lock_irq(&blkif_io_lock);
  675. /* Now safe for us to use the shared ring */
  676. info->connected = BLKIF_STATE_CONNECTED;
  677. /* Send off requeued requests */
  678. flush_requests(info);
  679. /* Kick any other new requests queued since we resumed */
  680. kick_pending_request_queues(info);
  681. spin_unlock_irq(&blkif_io_lock);
  682. return 0;
  683. }
  684. /**
  685. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  686. * driver restart. We tear down our blkif structure and recreate it, but
  687. * leave the device-layer structures intact so that this is transparent to the
  688. * rest of the kernel.
  689. */
  690. static int blkfront_resume(struct xenbus_device *dev)
  691. {
  692. struct blkfront_info *info = dev->dev.driver_data;
  693. int err;
  694. dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
  695. blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
  696. err = talk_to_backend(dev, info);
  697. if (info->connected == BLKIF_STATE_SUSPENDED && !err)
  698. err = blkif_recover(info);
  699. return err;
  700. }
  701. /*
  702. * Invoked when the backend is finally 'ready' (and has told produced
  703. * the details about the physical device - #sectors, size, etc).
  704. */
  705. static void blkfront_connect(struct blkfront_info *info)
  706. {
  707. unsigned long long sectors;
  708. unsigned long sector_size;
  709. unsigned int binfo;
  710. int err;
  711. if ((info->connected == BLKIF_STATE_CONNECTED) ||
  712. (info->connected == BLKIF_STATE_SUSPENDED) )
  713. return;
  714. dev_dbg(&info->xbdev->dev, "%s:%s.\n",
  715. __func__, info->xbdev->otherend);
  716. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  717. "sectors", "%llu", &sectors,
  718. "info", "%u", &binfo,
  719. "sector-size", "%lu", &sector_size,
  720. NULL);
  721. if (err) {
  722. xenbus_dev_fatal(info->xbdev, err,
  723. "reading backend fields at %s",
  724. info->xbdev->otherend);
  725. return;
  726. }
  727. err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
  728. "feature-barrier", "%lu", &info->feature_barrier,
  729. NULL);
  730. if (err)
  731. info->feature_barrier = 0;
  732. err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
  733. if (err) {
  734. xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
  735. info->xbdev->otherend);
  736. return;
  737. }
  738. xenbus_switch_state(info->xbdev, XenbusStateConnected);
  739. /* Kick pending requests. */
  740. spin_lock_irq(&blkif_io_lock);
  741. info->connected = BLKIF_STATE_CONNECTED;
  742. kick_pending_request_queues(info);
  743. spin_unlock_irq(&blkif_io_lock);
  744. add_disk(info->gd);
  745. info->is_ready = 1;
  746. }
  747. /**
  748. * Handle the change of state of the backend to Closing. We must delete our
  749. * device-layer structures now, to ensure that writes are flushed through to
  750. * the backend. Once is this done, we can switch to Closed in
  751. * acknowledgement.
  752. */
  753. static void blkfront_closing(struct xenbus_device *dev)
  754. {
  755. struct blkfront_info *info = dev->dev.driver_data;
  756. unsigned long flags;
  757. dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
  758. if (info->rq == NULL)
  759. goto out;
  760. spin_lock_irqsave(&blkif_io_lock, flags);
  761. del_gendisk(info->gd);
  762. /* No more blkif_request(). */
  763. blk_stop_queue(info->rq);
  764. /* No more gnttab callback work. */
  765. gnttab_cancel_free_callback(&info->callback);
  766. spin_unlock_irqrestore(&blkif_io_lock, flags);
  767. /* Flush gnttab callback work. Must be done with no locks held. */
  768. flush_scheduled_work();
  769. blk_cleanup_queue(info->rq);
  770. info->rq = NULL;
  771. out:
  772. xenbus_frontend_closed(dev);
  773. }
  774. /**
  775. * Callback received when the backend's state changes.
  776. */
  777. static void backend_changed(struct xenbus_device *dev,
  778. enum xenbus_state backend_state)
  779. {
  780. struct blkfront_info *info = dev->dev.driver_data;
  781. struct block_device *bd;
  782. dev_dbg(&dev->dev, "blkfront:backend_changed.\n");
  783. switch (backend_state) {
  784. case XenbusStateInitialising:
  785. case XenbusStateInitWait:
  786. case XenbusStateInitialised:
  787. case XenbusStateUnknown:
  788. case XenbusStateClosed:
  789. break;
  790. case XenbusStateConnected:
  791. blkfront_connect(info);
  792. break;
  793. case XenbusStateClosing:
  794. if (info->gd == NULL)
  795. xenbus_dev_fatal(dev, -ENODEV, "gd is NULL");
  796. bd = bdget_disk(info->gd, 0);
  797. if (bd == NULL)
  798. xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
  799. mutex_lock(&bd->bd_mutex);
  800. if (info->users > 0)
  801. xenbus_dev_error(dev, -EBUSY,
  802. "Device in use; refusing to close");
  803. else
  804. blkfront_closing(dev);
  805. mutex_unlock(&bd->bd_mutex);
  806. bdput(bd);
  807. break;
  808. }
  809. }
  810. static int blkfront_remove(struct xenbus_device *dev)
  811. {
  812. struct blkfront_info *info = dev->dev.driver_data;
  813. dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
  814. blkif_free(info, 0);
  815. kfree(info);
  816. return 0;
  817. }
  818. static int blkfront_is_ready(struct xenbus_device *dev)
  819. {
  820. struct blkfront_info *info = dev->dev.driver_data;
  821. return info->is_ready;
  822. }
  823. static int blkif_open(struct block_device *bdev, fmode_t mode)
  824. {
  825. struct blkfront_info *info = bdev->bd_disk->private_data;
  826. info->users++;
  827. return 0;
  828. }
  829. static int blkif_release(struct gendisk *disk, fmode_t mode)
  830. {
  831. struct blkfront_info *info = disk->private_data;
  832. info->users--;
  833. if (info->users == 0) {
  834. /* Check whether we have been instructed to close. We will
  835. have ignored this request initially, as the device was
  836. still mounted. */
  837. struct xenbus_device *dev = info->xbdev;
  838. enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
  839. if (state == XenbusStateClosing && info->is_ready)
  840. blkfront_closing(dev);
  841. }
  842. return 0;
  843. }
  844. static struct block_device_operations xlvbd_block_fops =
  845. {
  846. .owner = THIS_MODULE,
  847. .open = blkif_open,
  848. .release = blkif_release,
  849. .getgeo = blkif_getgeo,
  850. .locked_ioctl = blkif_ioctl,
  851. };
  852. static struct xenbus_device_id blkfront_ids[] = {
  853. { "vbd" },
  854. { "" }
  855. };
  856. static struct xenbus_driver blkfront = {
  857. .name = "vbd",
  858. .owner = THIS_MODULE,
  859. .ids = blkfront_ids,
  860. .probe = blkfront_probe,
  861. .remove = blkfront_remove,
  862. .resume = blkfront_resume,
  863. .otherend_changed = backend_changed,
  864. .is_ready = blkfront_is_ready,
  865. };
  866. static int __init xlblk_init(void)
  867. {
  868. if (!xen_domain())
  869. return -ENODEV;
  870. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  871. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  872. XENVBD_MAJOR, DEV_NAME);
  873. return -ENODEV;
  874. }
  875. return xenbus_register_frontend(&blkfront);
  876. }
  877. module_init(xlblk_init);
  878. static void __exit xlblk_exit(void)
  879. {
  880. return xenbus_unregister_driver(&blkfront);
  881. }
  882. module_exit(xlblk_exit);
  883. MODULE_DESCRIPTION("Xen virtual block device frontend");
  884. MODULE_LICENSE("GPL");
  885. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  886. MODULE_ALIAS("xen:vbd");
  887. MODULE_ALIAS("xenblk");