xen-blkfront.c 31 KB

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