xen-blkfront.c 27 KB

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