xen-blkfront.c 26 KB

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