xen-blkfront.c 34 KB

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