xen-blkfront.c 27 KB

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