xen-blkfront.c 30 KB

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