xen-blkfront.c 27 KB

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