xen-blkfront.c 31 KB

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