xen-blkfront.c 29 KB

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