xen-blkfront.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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)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 = elv_next_request(rq)) != NULL) {
  254. info = req->rq_disk->private_data;
  255. if (RING_FULL(&info->ring))
  256. goto wait;
  257. blkdev_dequeue_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_hardsect_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->dev.driver_data = info;
  620. err = talk_to_backend(dev, info);
  621. if (err) {
  622. kfree(info);
  623. dev->dev.driver_data = 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->dev.driver_data;
  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->dev.driver_data;
  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. del_gendisk(info->gd);
  759. /* No more blkif_request(). */
  760. blk_stop_queue(info->rq);
  761. /* No more gnttab callback work. */
  762. gnttab_cancel_free_callback(&info->callback);
  763. spin_unlock_irqrestore(&blkif_io_lock, flags);
  764. /* Flush gnttab callback work. Must be done with no locks held. */
  765. flush_scheduled_work();
  766. blk_cleanup_queue(info->rq);
  767. info->rq = NULL;
  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->dev.driver_data;
  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_dev_fatal(dev, -ENODEV, "gd is NULL");
  793. bd = bdget_disk(info->gd, 0);
  794. if (bd == NULL)
  795. xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
  796. mutex_lock(&bd->bd_mutex);
  797. if (info->users > 0)
  798. xenbus_dev_error(dev, -EBUSY,
  799. "Device in use; refusing to close");
  800. else
  801. blkfront_closing(dev);
  802. mutex_unlock(&bd->bd_mutex);
  803. bdput(bd);
  804. break;
  805. }
  806. }
  807. static int blkfront_remove(struct xenbus_device *dev)
  808. {
  809. struct blkfront_info *info = dev->dev.driver_data;
  810. dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
  811. blkif_free(info, 0);
  812. kfree(info);
  813. return 0;
  814. }
  815. static int blkfront_is_ready(struct xenbus_device *dev)
  816. {
  817. struct blkfront_info *info = dev->dev.driver_data;
  818. return info->is_ready;
  819. }
  820. static int blkif_open(struct block_device *bdev, fmode_t mode)
  821. {
  822. struct blkfront_info *info = bdev->bd_disk->private_data;
  823. info->users++;
  824. return 0;
  825. }
  826. static int blkif_release(struct gendisk *disk, fmode_t mode)
  827. {
  828. struct blkfront_info *info = disk->private_data;
  829. info->users--;
  830. if (info->users == 0) {
  831. /* Check whether we have been instructed to close. We will
  832. have ignored this request initially, as the device was
  833. still mounted. */
  834. struct xenbus_device *dev = info->xbdev;
  835. enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
  836. if (state == XenbusStateClosing && info->is_ready)
  837. blkfront_closing(dev);
  838. }
  839. return 0;
  840. }
  841. static struct block_device_operations xlvbd_block_fops =
  842. {
  843. .owner = THIS_MODULE,
  844. .open = blkif_open,
  845. .release = blkif_release,
  846. .getgeo = blkif_getgeo,
  847. .locked_ioctl = blkif_ioctl,
  848. };
  849. static struct xenbus_device_id blkfront_ids[] = {
  850. { "vbd" },
  851. { "" }
  852. };
  853. static struct xenbus_driver blkfront = {
  854. .name = "vbd",
  855. .owner = THIS_MODULE,
  856. .ids = blkfront_ids,
  857. .probe = blkfront_probe,
  858. .remove = blkfront_remove,
  859. .resume = blkfront_resume,
  860. .otherend_changed = backend_changed,
  861. .is_ready = blkfront_is_ready,
  862. };
  863. static int __init xlblk_init(void)
  864. {
  865. if (!xen_domain())
  866. return -ENODEV;
  867. if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
  868. printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
  869. XENVBD_MAJOR, DEV_NAME);
  870. return -ENODEV;
  871. }
  872. return xenbus_register_frontend(&blkfront);
  873. }
  874. module_init(xlblk_init);
  875. static void __exit xlblk_exit(void)
  876. {
  877. return xenbus_unregister_driver(&blkfront);
  878. }
  879. module_exit(xlblk_exit);
  880. MODULE_DESCRIPTION("Xen virtual block device frontend");
  881. MODULE_LICENSE("GPL");
  882. MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
  883. MODULE_ALIAS("xen:vbd");
  884. MODULE_ALIAS("xenblk");