xen-blkfront.c 34 KB

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