xen-blkfront.c 24 KB

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