xen-blkfront.c 37 KB

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