xen-blkfront.c 30 KB

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