4 * XenLinux virtual block device driver.
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
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:
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:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
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
38 #include <linux/interrupt.h>
39 #include <linux/blkdev.h>
40 #include <linux/hdreg.h>
41 #include <linux/cdrom.h>
42 #include <linux/module.h>
43 #include <linux/slab.h>
44 #include <linux/smp_lock.h>
45 #include <linux/scatterlist.h>
48 #include <xen/xenbus.h>
49 #include <xen/grant_table.h>
50 #include <xen/events.h>
53 #include <xen/interface/grant_table.h>
54 #include <xen/interface/io/blkif.h>
55 #include <xen/interface/io/protocols.h>
57 #include <asm/xen/hypervisor.h>
60 BLKIF_STATE_DISCONNECTED,
61 BLKIF_STATE_CONNECTED,
62 BLKIF_STATE_SUSPENDED,
66 struct blkif_request req;
67 unsigned long request;
68 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
71 static const struct block_device_operations xlvbd_block_fops;
73 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
76 * We have one of these per vbd, whether ide, scsi or 'other'. They
77 * hang in private_data off the gendisk structure. We may end up
78 * putting all kinds of interesting stuff here :-)
83 struct xenbus_device *xbdev;
87 enum blkif_state connected;
89 struct blkif_front_ring ring;
90 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
91 unsigned int evtchn, irq;
92 struct request_queue *rq;
93 struct work_struct work;
94 struct gnttab_free_callback callback;
95 struct blk_shadow shadow[BLK_RING_SIZE];
96 unsigned long shadow_free;
101 * The number of people holding this device open. We won't allow a
102 * hot-unplug unless this is 0.
107 static DEFINE_SPINLOCK(blkif_io_lock);
109 static unsigned int nr_minors;
110 static unsigned long *minors;
111 static DEFINE_SPINLOCK(minor_lock);
113 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
114 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
115 #define GRANT_INVALID_REF 0
117 #define PARTS_PER_DISK 16
118 #define PARTS_PER_EXT_DISK 256
120 #define BLKIF_MAJOR(dev) ((dev)>>8)
121 #define BLKIF_MINOR(dev) ((dev) & 0xff)
124 #define EXTENDED (1<<EXT_SHIFT)
125 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
126 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
128 #define DEV_NAME "xvd" /* name in /dev */
130 static int get_id_from_freelist(struct blkfront_info *info)
132 unsigned long free = info->shadow_free;
133 BUG_ON(free >= BLK_RING_SIZE);
134 info->shadow_free = info->shadow[free].req.id;
135 info->shadow[free].req.id = 0x0fffffee; /* debug */
139 static void add_id_to_freelist(struct blkfront_info *info,
142 info->shadow[id].req.id = info->shadow_free;
143 info->shadow[id].request = 0;
144 info->shadow_free = id;
147 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
149 unsigned int end = minor + nr;
152 if (end > nr_minors) {
153 unsigned long *bitmap, *old;
155 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
160 spin_lock(&minor_lock);
161 if (end > nr_minors) {
163 memcpy(bitmap, minors,
164 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
166 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
169 spin_unlock(&minor_lock);
173 spin_lock(&minor_lock);
174 if (find_next_bit(minors, end, minor) >= end) {
175 for (; minor < end; ++minor)
176 __set_bit(minor, minors);
180 spin_unlock(&minor_lock);
185 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
187 unsigned int end = minor + nr;
189 BUG_ON(end > nr_minors);
190 spin_lock(&minor_lock);
191 for (; minor < end; ++minor)
192 __clear_bit(minor, minors);
193 spin_unlock(&minor_lock);
196 static void blkif_restart_queue_callback(void *arg)
198 struct blkfront_info *info = (struct blkfront_info *)arg;
199 schedule_work(&info->work);
202 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
204 /* We don't have real geometry info, but let's at least return
205 values consistent with the size of the device */
206 sector_t nsect = get_capacity(bd->bd_disk);
207 sector_t cylinders = nsect;
211 sector_div(cylinders, hg->heads * hg->sectors);
212 hg->cylinders = cylinders;
213 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
214 hg->cylinders = 0xffff;
218 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
219 unsigned command, unsigned long argument)
221 struct blkfront_info *info = bdev->bd_disk->private_data;
224 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
225 command, (long)argument);
228 case CDROMMULTISESSION:
229 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
230 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
231 if (put_user(0, (char __user *)(argument + i)))
235 case CDROM_GET_CAPABILITY: {
236 struct gendisk *gd = info->gd;
237 if (gd->flags & GENHD_FL_CD)
243 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
245 return -EINVAL; /* same return as native Linux */
252 * blkif_queue_request
256 * id: for guest use only.
257 * operation: BLKIF_OP_{READ,WRITE,PROBE}
258 * buffer: buffer to read/write into. this should be a
259 * virtual address in the guest os.
261 static int blkif_queue_request(struct request *req)
263 struct blkfront_info *info = req->rq_disk->private_data;
264 unsigned long buffer_mfn;
265 struct blkif_request *ring_req;
267 unsigned int fsect, lsect;
269 grant_ref_t gref_head;
270 struct scatterlist *sg;
272 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
275 if (gnttab_alloc_grant_references(
276 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
277 gnttab_request_free_callback(
279 blkif_restart_queue_callback,
281 BLKIF_MAX_SEGMENTS_PER_REQUEST);
285 /* Fill out a communications ring structure. */
286 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
287 id = get_id_from_freelist(info);
288 info->shadow[id].request = (unsigned long)req;
291 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
292 ring_req->handle = info->handle;
294 ring_req->operation = rq_data_dir(req) ?
295 BLKIF_OP_WRITE : BLKIF_OP_READ;
296 if (req->cmd_flags & REQ_HARDBARRIER)
297 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
299 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
300 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
302 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
303 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
304 fsect = sg->offset >> 9;
305 lsect = fsect + (sg->length >> 9) - 1;
306 /* install a grant reference. */
307 ref = gnttab_claim_grant_reference(&gref_head);
308 BUG_ON(ref == -ENOSPC);
310 gnttab_grant_foreign_access_ref(
312 info->xbdev->otherend_id,
316 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
318 (struct blkif_request_segment) {
321 .last_sect = lsect };
324 info->ring.req_prod_pvt++;
326 /* Keep a private copy so we can reissue requests when recovering. */
327 info->shadow[id].req = *ring_req;
329 gnttab_free_grant_references(gref_head);
335 static inline void flush_requests(struct blkfront_info *info)
339 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
342 notify_remote_via_irq(info->irq);
347 * read a block; request is in a request queue
349 static void do_blkif_request(struct request_queue *rq)
351 struct blkfront_info *info = NULL;
355 pr_debug("Entered do_blkif_request\n");
359 while ((req = blk_peek_request(rq)) != NULL) {
360 info = req->rq_disk->private_data;
362 if (RING_FULL(&info->ring))
365 blk_start_request(req);
367 if (req->cmd_type != REQ_TYPE_FS) {
368 __blk_end_request_all(req, -EIO);
372 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
373 "(%u/%u) buffer:%p [%s]\n",
374 req, req->cmd, (unsigned long)blk_rq_pos(req),
375 blk_rq_cur_sectors(req), blk_rq_sectors(req),
376 req->buffer, rq_data_dir(req) ? "write" : "read");
378 if (blkif_queue_request(req)) {
379 blk_requeue_request(rq, req);
381 /* Avoid pointless unplugs. */
390 flush_requests(info);
393 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
395 struct request_queue *rq;
397 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
401 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
403 /* Hard sector size and max sectors impersonate the equiv. hardware. */
404 blk_queue_logical_block_size(rq, sector_size);
405 blk_queue_max_hw_sectors(rq, 512);
407 /* Each segment in a request is up to an aligned page in size. */
408 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
409 blk_queue_max_segment_size(rq, PAGE_SIZE);
411 /* Ensure a merged request will fit in a single I/O ring slot. */
412 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
414 /* Make sure buffer addresses are sector-aligned. */
415 blk_queue_dma_alignment(rq, 511);
417 /* Make sure we don't use bounce buffers. */
418 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
426 static int xlvbd_barrier(struct blkfront_info *info)
430 err = blk_queue_ordered(info->rq,
431 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE);
436 printk(KERN_INFO "blkfront: %s: barriers %s\n",
438 info->feature_barrier ? "enabled" : "disabled");
443 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
444 struct blkfront_info *info,
445 u16 vdisk_info, u16 sector_size)
454 BUG_ON(info->gd != NULL);
455 BUG_ON(info->rq != NULL);
457 if ((info->vdevice>>EXT_SHIFT) > 1) {
458 /* this is above the extended range; something is wrong */
459 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
463 if (!VDEV_IS_EXTENDED(info->vdevice)) {
464 minor = BLKIF_MINOR(info->vdevice);
465 nr_parts = PARTS_PER_DISK;
467 minor = BLKIF_MINOR_EXT(info->vdevice);
468 nr_parts = PARTS_PER_EXT_DISK;
471 if ((minor % nr_parts) == 0)
472 nr_minors = nr_parts;
474 err = xlbd_reserve_minors(minor, nr_minors);
479 gd = alloc_disk(nr_minors);
483 offset = minor / nr_parts;
487 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
489 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
490 'a' + ((offset / 26)-1), 'a' + (offset % 26));
493 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
495 minor & (nr_parts - 1));
497 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
498 'a' + ((offset / 26) - 1),
500 minor & (nr_parts - 1));
503 gd->major = XENVBD_MAJOR;
504 gd->first_minor = minor;
505 gd->fops = &xlvbd_block_fops;
506 gd->private_data = info;
507 gd->driverfs_dev = &(info->xbdev->dev);
508 set_capacity(gd, capacity);
510 if (xlvbd_init_blk_queue(gd, sector_size)) {
515 info->rq = gd->queue;
518 if (info->feature_barrier)
521 if (vdisk_info & VDISK_READONLY)
524 if (vdisk_info & VDISK_REMOVABLE)
525 gd->flags |= GENHD_FL_REMOVABLE;
527 if (vdisk_info & VDISK_CDROM)
528 gd->flags |= GENHD_FL_CD;
533 xlbd_release_minors(minor, nr_minors);
538 static void xlvbd_release_gendisk(struct blkfront_info *info)
540 unsigned int minor, nr_minors;
543 if (info->rq == NULL)
546 spin_lock_irqsave(&blkif_io_lock, flags);
548 /* No more blkif_request(). */
549 blk_stop_queue(info->rq);
551 /* No more gnttab callback work. */
552 gnttab_cancel_free_callback(&info->callback);
553 spin_unlock_irqrestore(&blkif_io_lock, flags);
555 /* Flush gnttab callback work. Must be done with no locks held. */
556 flush_scheduled_work();
558 del_gendisk(info->gd);
560 minor = info->gd->first_minor;
561 nr_minors = info->gd->minors;
562 xlbd_release_minors(minor, nr_minors);
564 blk_cleanup_queue(info->rq);
571 static void kick_pending_request_queues(struct blkfront_info *info)
573 if (!RING_FULL(&info->ring)) {
574 /* Re-enable calldowns. */
575 blk_start_queue(info->rq);
576 /* Kick things off immediately. */
577 do_blkif_request(info->rq);
581 static void blkif_restart_queue(struct work_struct *work)
583 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
585 spin_lock_irq(&blkif_io_lock);
586 if (info->connected == BLKIF_STATE_CONNECTED)
587 kick_pending_request_queues(info);
588 spin_unlock_irq(&blkif_io_lock);
591 static void blkif_free(struct blkfront_info *info, int suspend)
593 /* Prevent new requests being issued until we fix things up. */
594 spin_lock_irq(&blkif_io_lock);
595 info->connected = suspend ?
596 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
597 /* No more blkif_request(). */
599 blk_stop_queue(info->rq);
600 /* No more gnttab callback work. */
601 gnttab_cancel_free_callback(&info->callback);
602 spin_unlock_irq(&blkif_io_lock);
604 /* Flush gnttab callback work. Must be done with no locks held. */
605 flush_scheduled_work();
607 /* Free resources associated with old device channel. */
608 if (info->ring_ref != GRANT_INVALID_REF) {
609 gnttab_end_foreign_access(info->ring_ref, 0,
610 (unsigned long)info->ring.sring);
611 info->ring_ref = GRANT_INVALID_REF;
612 info->ring.sring = NULL;
615 unbind_from_irqhandler(info->irq, info);
616 info->evtchn = info->irq = 0;
620 static void blkif_completion(struct blk_shadow *s)
623 for (i = 0; i < s->req.nr_segments; i++)
624 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
627 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
630 struct blkif_response *bret;
633 struct blkfront_info *info = (struct blkfront_info *)dev_id;
636 spin_lock_irqsave(&blkif_io_lock, flags);
638 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
639 spin_unlock_irqrestore(&blkif_io_lock, flags);
644 rp = info->ring.sring->rsp_prod;
645 rmb(); /* Ensure we see queued responses up to 'rp'. */
647 for (i = info->ring.rsp_cons; i != rp; i++) {
650 bret = RING_GET_RESPONSE(&info->ring, i);
652 req = (struct request *)info->shadow[id].request;
654 blkif_completion(&info->shadow[id]);
656 add_id_to_freelist(info, id);
658 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
659 switch (bret->operation) {
660 case BLKIF_OP_WRITE_BARRIER:
661 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
662 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
663 info->gd->disk_name);
665 info->feature_barrier = 0;
671 if (unlikely(bret->status != BLKIF_RSP_OKAY))
672 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
673 "request: %x\n", bret->status);
675 __blk_end_request_all(req, error);
682 info->ring.rsp_cons = i;
684 if (i != info->ring.req_prod_pvt) {
686 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
690 info->ring.sring->rsp_event = i + 1;
692 kick_pending_request_queues(info);
694 spin_unlock_irqrestore(&blkif_io_lock, flags);
700 static int setup_blkring(struct xenbus_device *dev,
701 struct blkfront_info *info)
703 struct blkif_sring *sring;
706 info->ring_ref = GRANT_INVALID_REF;
708 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
710 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
713 SHARED_RING_INIT(sring);
714 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
716 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
718 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
720 free_page((unsigned long)sring);
721 info->ring.sring = NULL;
724 info->ring_ref = err;
726 err = xenbus_alloc_evtchn(dev, &info->evtchn);
730 err = bind_evtchn_to_irqhandler(info->evtchn,
732 IRQF_SAMPLE_RANDOM, "blkif", info);
734 xenbus_dev_fatal(dev, err,
735 "bind_evtchn_to_irqhandler failed");
747 /* Common code used when first setting up, and when resuming. */
748 static int talk_to_blkback(struct xenbus_device *dev,
749 struct blkfront_info *info)
751 const char *message = NULL;
752 struct xenbus_transaction xbt;
755 /* Create shared ring, alloc event channel. */
756 err = setup_blkring(dev, info);
761 err = xenbus_transaction_start(&xbt);
763 xenbus_dev_fatal(dev, err, "starting transaction");
764 goto destroy_blkring;
767 err = xenbus_printf(xbt, dev->nodename,
768 "ring-ref", "%u", info->ring_ref);
770 message = "writing ring-ref";
771 goto abort_transaction;
773 err = xenbus_printf(xbt, dev->nodename,
774 "event-channel", "%u", info->evtchn);
776 message = "writing event-channel";
777 goto abort_transaction;
779 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
780 XEN_IO_PROTO_ABI_NATIVE);
782 message = "writing protocol";
783 goto abort_transaction;
786 err = xenbus_transaction_end(xbt, 0);
790 xenbus_dev_fatal(dev, err, "completing transaction");
791 goto destroy_blkring;
794 xenbus_switch_state(dev, XenbusStateInitialised);
799 xenbus_transaction_end(xbt, 1);
801 xenbus_dev_fatal(dev, err, "%s", message);
809 * Entry point to this code when a new device is created. Allocate the basic
810 * structures and the ring buffer for communication with the backend, and
811 * inform the backend of the appropriate details for those. Switch to
814 static int blkfront_probe(struct xenbus_device *dev,
815 const struct xenbus_device_id *id)
818 struct blkfront_info *info;
820 /* FIXME: Use dynamic device id if this is not set. */
821 err = xenbus_scanf(XBT_NIL, dev->nodename,
822 "virtual-device", "%i", &vdevice);
824 /* go looking in the extended area instead */
825 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
828 xenbus_dev_fatal(dev, err, "reading virtual-device");
833 info = kzalloc(sizeof(*info), GFP_KERNEL);
835 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
839 mutex_init(&info->mutex);
841 info->vdevice = vdevice;
842 info->connected = BLKIF_STATE_DISCONNECTED;
843 INIT_WORK(&info->work, blkif_restart_queue);
845 for (i = 0; i < BLK_RING_SIZE; i++)
846 info->shadow[i].req.id = i+1;
847 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
849 /* Front end dir is a number, which is used as the id. */
850 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
851 dev_set_drvdata(&dev->dev, info);
853 err = talk_to_blkback(dev, info);
856 dev_set_drvdata(&dev->dev, NULL);
864 static int blkif_recover(struct blkfront_info *info)
867 struct blkif_request *req;
868 struct blk_shadow *copy;
871 /* Stage 1: Make a safe copy of the shadow state. */
872 copy = kmalloc(sizeof(info->shadow),
873 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
876 memcpy(copy, info->shadow, sizeof(info->shadow));
878 /* Stage 2: Set up free list. */
879 memset(&info->shadow, 0, sizeof(info->shadow));
880 for (i = 0; i < BLK_RING_SIZE; i++)
881 info->shadow[i].req.id = i+1;
882 info->shadow_free = info->ring.req_prod_pvt;
883 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
885 /* Stage 3: Find pending requests and requeue them. */
886 for (i = 0; i < BLK_RING_SIZE; i++) {
888 if (copy[i].request == 0)
891 /* Grab a request slot and copy shadow state into it. */
892 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
895 /* We get a new request id, and must reset the shadow state. */
896 req->id = get_id_from_freelist(info);
897 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
899 /* Rewrite any grant references invalidated by susp/resume. */
900 for (j = 0; j < req->nr_segments; j++)
901 gnttab_grant_foreign_access_ref(
903 info->xbdev->otherend_id,
904 pfn_to_mfn(info->shadow[req->id].frame[j]),
907 info->shadow[req->id].request));
908 info->shadow[req->id].req = *req;
910 info->ring.req_prod_pvt++;
915 xenbus_switch_state(info->xbdev, XenbusStateConnected);
917 spin_lock_irq(&blkif_io_lock);
919 /* Now safe for us to use the shared ring */
920 info->connected = BLKIF_STATE_CONNECTED;
922 /* Send off requeued requests */
923 flush_requests(info);
925 /* Kick any other new requests queued since we resumed */
926 kick_pending_request_queues(info);
928 spin_unlock_irq(&blkif_io_lock);
934 * We are reconnecting to the backend, due to a suspend/resume, or a backend
935 * driver restart. We tear down our blkif structure and recreate it, but
936 * leave the device-layer structures intact so that this is transparent to the
937 * rest of the kernel.
939 static int blkfront_resume(struct xenbus_device *dev)
941 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
944 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
946 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
948 err = talk_to_blkback(dev, info);
949 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
950 err = blkif_recover(info);
956 blkfront_closing(struct blkfront_info *info)
958 struct xenbus_device *xbdev = info->xbdev;
959 struct block_device *bdev = NULL;
961 mutex_lock(&info->mutex);
963 if (xbdev->state == XenbusStateClosing) {
964 mutex_unlock(&info->mutex);
969 bdev = bdget_disk(info->gd, 0);
971 mutex_unlock(&info->mutex);
974 xenbus_frontend_closed(xbdev);
978 mutex_lock(&bdev->bd_mutex);
981 xenbus_dev_error(xbdev, -EBUSY,
982 "Device in use; refusing to close");
983 xenbus_switch_state(xbdev, XenbusStateClosing);
985 xlvbd_release_gendisk(info);
986 xenbus_frontend_closed(xbdev);
989 mutex_unlock(&bdev->bd_mutex);
994 * Invoked when the backend is finally 'ready' (and has told produced
995 * the details about the physical device - #sectors, size, etc).
997 static void blkfront_connect(struct blkfront_info *info)
999 unsigned long long sectors;
1000 unsigned long sector_size;
1004 switch (info->connected) {
1005 case BLKIF_STATE_CONNECTED:
1007 * Potentially, the back-end may be signalling
1008 * a capacity change; update the capacity.
1010 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1011 "sectors", "%Lu", §ors);
1012 if (XENBUS_EXIST_ERR(err))
1014 printk(KERN_INFO "Setting capacity to %Lu\n",
1016 set_capacity(info->gd, sectors);
1017 revalidate_disk(info->gd);
1020 case BLKIF_STATE_SUSPENDED:
1027 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1028 __func__, info->xbdev->otherend);
1030 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1031 "sectors", "%llu", §ors,
1032 "info", "%u", &binfo,
1033 "sector-size", "%lu", §or_size,
1036 xenbus_dev_fatal(info->xbdev, err,
1037 "reading backend fields at %s",
1038 info->xbdev->otherend);
1042 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1043 "feature-barrier", "%lu", &info->feature_barrier,
1046 info->feature_barrier = 0;
1048 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
1050 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1051 info->xbdev->otherend);
1055 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1057 /* Kick pending requests. */
1058 spin_lock_irq(&blkif_io_lock);
1059 info->connected = BLKIF_STATE_CONNECTED;
1060 kick_pending_request_queues(info);
1061 spin_unlock_irq(&blkif_io_lock);
1069 * Callback received when the backend's state changes.
1071 static void blkback_changed(struct xenbus_device *dev,
1072 enum xenbus_state backend_state)
1074 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1076 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
1078 switch (backend_state) {
1079 case XenbusStateInitialising:
1080 case XenbusStateInitWait:
1081 case XenbusStateInitialised:
1082 case XenbusStateUnknown:
1083 case XenbusStateClosed:
1086 case XenbusStateConnected:
1087 blkfront_connect(info);
1090 case XenbusStateClosing:
1091 blkfront_closing(info);
1096 static int blkfront_remove(struct xenbus_device *dev)
1098 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1100 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
1102 blkif_free(info, 0);
1104 if(info->users == 0)
1112 static int blkfront_is_ready(struct xenbus_device *dev)
1114 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1116 return info->is_ready && info->xbdev;
1119 static int blkif_open(struct block_device *bdev, fmode_t mode)
1121 struct gendisk *disk = bdev->bd_disk;
1122 struct blkfront_info *info;
1127 info = disk->private_data;
1134 mutex_lock(&info->mutex);
1137 /* xbdev is closed */
1140 mutex_unlock(&info->mutex);
1150 static int blkif_release(struct gendisk *disk, fmode_t mode)
1152 struct blkfront_info *info = disk->private_data;
1153 struct block_device *bdev;
1154 struct xenbus_device *xbdev;
1160 bdev = bdget_disk(disk, 0);
1164 * Check if we have been instructed to close. We will have
1165 * deferred this request, because the bdev was still open.
1168 mutex_lock(&info->mutex);
1169 xbdev = info->xbdev;
1171 if (xbdev && xbdev->state == XenbusStateClosing) {
1172 /* pending switch to state closed */
1173 xlvbd_release_gendisk(info);
1174 xenbus_frontend_closed(info->xbdev);
1177 mutex_unlock(&info->mutex);
1180 /* sudden device removal */
1181 xlvbd_release_gendisk(info);
1182 disk->private_data = NULL;
1191 static const struct block_device_operations xlvbd_block_fops =
1193 .owner = THIS_MODULE,
1195 .release = blkif_release,
1196 .getgeo = blkif_getgeo,
1197 .ioctl = blkif_ioctl,
1201 static const struct xenbus_device_id blkfront_ids[] = {
1206 static struct xenbus_driver blkfront = {
1208 .owner = THIS_MODULE,
1209 .ids = blkfront_ids,
1210 .probe = blkfront_probe,
1211 .remove = blkfront_remove,
1212 .resume = blkfront_resume,
1213 .otherend_changed = blkback_changed,
1214 .is_ready = blkfront_is_ready,
1217 static int __init xlblk_init(void)
1222 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1223 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1224 XENVBD_MAJOR, DEV_NAME);
1228 return xenbus_register_frontend(&blkfront);
1230 module_init(xlblk_init);
1233 static void __exit xlblk_exit(void)
1235 return xenbus_unregister_driver(&blkfront);
1237 module_exit(xlblk_exit);
1239 MODULE_DESCRIPTION("Xen virtual block device frontend");
1240 MODULE_LICENSE("GPL");
1241 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
1242 MODULE_ALIAS("xen:vbd");
1243 MODULE_ALIAS("xenblk");