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/scatterlist.h>
46 #include <xen/xenbus.h>
47 #include <xen/grant_table.h>
48 #include <xen/events.h>
51 #include <xen/interface/grant_table.h>
52 #include <xen/interface/io/blkif.h>
53 #include <xen/interface/io/protocols.h>
55 #include <asm/xen/hypervisor.h>
58 BLKIF_STATE_DISCONNECTED,
59 BLKIF_STATE_CONNECTED,
60 BLKIF_STATE_SUSPENDED,
64 struct blkif_request req;
65 unsigned long request;
66 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
69 static const struct block_device_operations xlvbd_block_fops;
71 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
74 * We have one of these per vbd, whether ide, scsi or 'other'. They
75 * hang in private_data off the gendisk structure. We may end up
76 * putting all kinds of interesting stuff here :-)
80 struct xenbus_device *xbdev;
84 enum blkif_state connected;
86 struct blkif_front_ring ring;
87 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
88 unsigned int evtchn, irq;
89 struct request_queue *rq;
90 struct work_struct work;
91 struct gnttab_free_callback callback;
92 struct blk_shadow shadow[BLK_RING_SIZE];
93 unsigned long shadow_free;
98 * The number of people holding this device open. We won't allow a
99 * hot-unplug unless this is 0.
104 static DEFINE_SPINLOCK(blkif_io_lock);
106 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
107 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
108 #define GRANT_INVALID_REF 0
110 #define PARTS_PER_DISK 16
111 #define PARTS_PER_EXT_DISK 256
113 #define BLKIF_MAJOR(dev) ((dev)>>8)
114 #define BLKIF_MINOR(dev) ((dev) & 0xff)
117 #define EXTENDED (1<<EXT_SHIFT)
118 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
119 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
121 #define DEV_NAME "xvd" /* name in /dev */
123 static int get_id_from_freelist(struct blkfront_info *info)
125 unsigned long free = info->shadow_free;
126 BUG_ON(free >= BLK_RING_SIZE);
127 info->shadow_free = info->shadow[free].req.id;
128 info->shadow[free].req.id = 0x0fffffee; /* debug */
132 static void add_id_to_freelist(struct blkfront_info *info,
135 info->shadow[id].req.id = info->shadow_free;
136 info->shadow[id].request = 0;
137 info->shadow_free = id;
140 static void blkif_restart_queue_callback(void *arg)
142 struct blkfront_info *info = (struct blkfront_info *)arg;
143 schedule_work(&info->work);
146 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
148 /* We don't have real geometry info, but let's at least return
149 values consistent with the size of the device */
150 sector_t nsect = get_capacity(bd->bd_disk);
151 sector_t cylinders = nsect;
155 sector_div(cylinders, hg->heads * hg->sectors);
156 hg->cylinders = cylinders;
157 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
158 hg->cylinders = 0xffff;
162 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
163 unsigned command, unsigned long argument)
165 struct blkfront_info *info = bdev->bd_disk->private_data;
168 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
169 command, (long)argument);
172 case CDROMMULTISESSION:
173 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
174 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
175 if (put_user(0, (char __user *)(argument + i)))
179 case CDROM_GET_CAPABILITY: {
180 struct gendisk *gd = info->gd;
181 if (gd->flags & GENHD_FL_CD)
187 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
189 return -EINVAL; /* same return as native Linux */
196 * blkif_queue_request
200 * id: for guest use only.
201 * operation: BLKIF_OP_{READ,WRITE,PROBE}
202 * buffer: buffer to read/write into. this should be a
203 * virtual address in the guest os.
205 static int blkif_queue_request(struct request *req)
207 struct blkfront_info *info = req->rq_disk->private_data;
208 unsigned long buffer_mfn;
209 struct blkif_request *ring_req;
211 unsigned int fsect, lsect;
213 grant_ref_t gref_head;
214 struct scatterlist *sg;
216 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
219 if (gnttab_alloc_grant_references(
220 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
221 gnttab_request_free_callback(
223 blkif_restart_queue_callback,
225 BLKIF_MAX_SEGMENTS_PER_REQUEST);
229 /* Fill out a communications ring structure. */
230 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
231 id = get_id_from_freelist(info);
232 info->shadow[id].request = (unsigned long)req;
235 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
236 ring_req->handle = info->handle;
238 ring_req->operation = rq_data_dir(req) ?
239 BLKIF_OP_WRITE : BLKIF_OP_READ;
240 if (blk_barrier_rq(req))
241 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
243 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
244 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
246 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
247 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
248 fsect = sg->offset >> 9;
249 lsect = fsect + (sg->length >> 9) - 1;
250 /* install a grant reference. */
251 ref = gnttab_claim_grant_reference(&gref_head);
252 BUG_ON(ref == -ENOSPC);
254 gnttab_grant_foreign_access_ref(
256 info->xbdev->otherend_id,
260 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
262 (struct blkif_request_segment) {
265 .last_sect = lsect };
268 info->ring.req_prod_pvt++;
270 /* Keep a private copy so we can reissue requests when recovering. */
271 info->shadow[id].req = *ring_req;
273 gnttab_free_grant_references(gref_head);
279 static inline void flush_requests(struct blkfront_info *info)
283 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
286 notify_remote_via_irq(info->irq);
291 * read a block; request is in a request queue
293 static void do_blkif_request(struct request_queue *rq)
295 struct blkfront_info *info = NULL;
299 pr_debug("Entered do_blkif_request\n");
303 while ((req = blk_peek_request(rq)) != NULL) {
304 info = req->rq_disk->private_data;
306 if (RING_FULL(&info->ring))
309 blk_start_request(req);
311 if (!blk_fs_request(req)) {
312 __blk_end_request_all(req, -EIO);
316 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
317 "(%u/%u) buffer:%p [%s]\n",
318 req, req->cmd, (unsigned long)blk_rq_pos(req),
319 blk_rq_cur_sectors(req), blk_rq_sectors(req),
320 req->buffer, rq_data_dir(req) ? "write" : "read");
322 if (blkif_queue_request(req)) {
323 blk_requeue_request(rq, req);
325 /* Avoid pointless unplugs. */
334 flush_requests(info);
337 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
339 struct request_queue *rq;
341 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
345 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
347 /* Hard sector size and max sectors impersonate the equiv. hardware. */
348 blk_queue_logical_block_size(rq, sector_size);
349 blk_queue_max_sectors(rq, 512);
351 /* Each segment in a request is up to an aligned page in size. */
352 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
353 blk_queue_max_segment_size(rq, PAGE_SIZE);
355 /* Ensure a merged request will fit in a single I/O ring slot. */
356 blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
357 blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
359 /* Make sure buffer addresses are sector-aligned. */
360 blk_queue_dma_alignment(rq, 511);
362 /* Make sure we don't use bounce buffers. */
363 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
371 static int xlvbd_barrier(struct blkfront_info *info)
375 err = blk_queue_ordered(info->rq,
376 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE,
382 printk(KERN_INFO "blkfront: %s: barriers %s\n",
384 info->feature_barrier ? "enabled" : "disabled");
389 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
390 struct blkfront_info *info,
391 u16 vdisk_info, u16 sector_size)
400 BUG_ON(info->gd != NULL);
401 BUG_ON(info->rq != NULL);
403 if ((info->vdevice>>EXT_SHIFT) > 1) {
404 /* this is above the extended range; something is wrong */
405 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
409 if (!VDEV_IS_EXTENDED(info->vdevice)) {
410 minor = BLKIF_MINOR(info->vdevice);
411 nr_parts = PARTS_PER_DISK;
413 minor = BLKIF_MINOR_EXT(info->vdevice);
414 nr_parts = PARTS_PER_EXT_DISK;
417 if ((minor % nr_parts) == 0)
418 nr_minors = nr_parts;
420 gd = alloc_disk(nr_minors);
424 offset = minor / nr_parts;
428 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
430 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
431 'a' + ((offset / 26)-1), 'a' + (offset % 26));
434 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
436 minor & (nr_parts - 1));
438 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
439 'a' + ((offset / 26) - 1),
441 minor & (nr_parts - 1));
444 gd->major = XENVBD_MAJOR;
445 gd->first_minor = minor;
446 gd->fops = &xlvbd_block_fops;
447 gd->private_data = info;
448 gd->driverfs_dev = &(info->xbdev->dev);
449 set_capacity(gd, capacity);
451 if (xlvbd_init_blk_queue(gd, sector_size)) {
456 info->rq = gd->queue;
459 if (info->feature_barrier)
462 if (vdisk_info & VDISK_READONLY)
465 if (vdisk_info & VDISK_REMOVABLE)
466 gd->flags |= GENHD_FL_REMOVABLE;
468 if (vdisk_info & VDISK_CDROM)
469 gd->flags |= GENHD_FL_CD;
477 static void kick_pending_request_queues(struct blkfront_info *info)
479 if (!RING_FULL(&info->ring)) {
480 /* Re-enable calldowns. */
481 blk_start_queue(info->rq);
482 /* Kick things off immediately. */
483 do_blkif_request(info->rq);
487 static void blkif_restart_queue(struct work_struct *work)
489 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
491 spin_lock_irq(&blkif_io_lock);
492 if (info->connected == BLKIF_STATE_CONNECTED)
493 kick_pending_request_queues(info);
494 spin_unlock_irq(&blkif_io_lock);
497 static void blkif_free(struct blkfront_info *info, int suspend)
499 /* Prevent new requests being issued until we fix things up. */
500 spin_lock_irq(&blkif_io_lock);
501 info->connected = suspend ?
502 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
503 /* No more blkif_request(). */
505 blk_stop_queue(info->rq);
506 /* No more gnttab callback work. */
507 gnttab_cancel_free_callback(&info->callback);
508 spin_unlock_irq(&blkif_io_lock);
510 /* Flush gnttab callback work. Must be done with no locks held. */
511 flush_scheduled_work();
513 /* Free resources associated with old device channel. */
514 if (info->ring_ref != GRANT_INVALID_REF) {
515 gnttab_end_foreign_access(info->ring_ref, 0,
516 (unsigned long)info->ring.sring);
517 info->ring_ref = GRANT_INVALID_REF;
518 info->ring.sring = NULL;
521 unbind_from_irqhandler(info->irq, info);
522 info->evtchn = info->irq = 0;
526 static void blkif_completion(struct blk_shadow *s)
529 for (i = 0; i < s->req.nr_segments; i++)
530 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
533 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
536 struct blkif_response *bret;
539 struct blkfront_info *info = (struct blkfront_info *)dev_id;
542 spin_lock_irqsave(&blkif_io_lock, flags);
544 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
545 spin_unlock_irqrestore(&blkif_io_lock, flags);
550 rp = info->ring.sring->rsp_prod;
551 rmb(); /* Ensure we see queued responses up to 'rp'. */
553 for (i = info->ring.rsp_cons; i != rp; i++) {
556 bret = RING_GET_RESPONSE(&info->ring, i);
558 req = (struct request *)info->shadow[id].request;
560 blkif_completion(&info->shadow[id]);
562 add_id_to_freelist(info, id);
564 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
565 switch (bret->operation) {
566 case BLKIF_OP_WRITE_BARRIER:
567 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
568 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
569 info->gd->disk_name);
571 info->feature_barrier = 0;
577 if (unlikely(bret->status != BLKIF_RSP_OKAY))
578 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
579 "request: %x\n", bret->status);
581 __blk_end_request_all(req, error);
588 info->ring.rsp_cons = i;
590 if (i != info->ring.req_prod_pvt) {
592 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
596 info->ring.sring->rsp_event = i + 1;
598 kick_pending_request_queues(info);
600 spin_unlock_irqrestore(&blkif_io_lock, flags);
606 static int setup_blkring(struct xenbus_device *dev,
607 struct blkfront_info *info)
609 struct blkif_sring *sring;
612 info->ring_ref = GRANT_INVALID_REF;
614 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
616 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
619 SHARED_RING_INIT(sring);
620 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
622 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
624 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
626 free_page((unsigned long)sring);
627 info->ring.sring = NULL;
630 info->ring_ref = err;
632 err = xenbus_alloc_evtchn(dev, &info->evtchn);
636 err = bind_evtchn_to_irqhandler(info->evtchn,
638 IRQF_SAMPLE_RANDOM, "blkif", info);
640 xenbus_dev_fatal(dev, err,
641 "bind_evtchn_to_irqhandler failed");
653 /* Common code used when first setting up, and when resuming. */
654 static int talk_to_backend(struct xenbus_device *dev,
655 struct blkfront_info *info)
657 const char *message = NULL;
658 struct xenbus_transaction xbt;
661 /* Create shared ring, alloc event channel. */
662 err = setup_blkring(dev, info);
667 err = xenbus_transaction_start(&xbt);
669 xenbus_dev_fatal(dev, err, "starting transaction");
670 goto destroy_blkring;
673 err = xenbus_printf(xbt, dev->nodename,
674 "ring-ref", "%u", info->ring_ref);
676 message = "writing ring-ref";
677 goto abort_transaction;
679 err = xenbus_printf(xbt, dev->nodename,
680 "event-channel", "%u", info->evtchn);
682 message = "writing event-channel";
683 goto abort_transaction;
685 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
686 XEN_IO_PROTO_ABI_NATIVE);
688 message = "writing protocol";
689 goto abort_transaction;
692 err = xenbus_transaction_end(xbt, 0);
696 xenbus_dev_fatal(dev, err, "completing transaction");
697 goto destroy_blkring;
700 xenbus_switch_state(dev, XenbusStateInitialised);
705 xenbus_transaction_end(xbt, 1);
707 xenbus_dev_fatal(dev, err, "%s", message);
716 * Entry point to this code when a new device is created. Allocate the basic
717 * structures and the ring buffer for communication with the backend, and
718 * inform the backend of the appropriate details for those. Switch to
721 static int blkfront_probe(struct xenbus_device *dev,
722 const struct xenbus_device_id *id)
725 struct blkfront_info *info;
727 /* FIXME: Use dynamic device id if this is not set. */
728 err = xenbus_scanf(XBT_NIL, dev->nodename,
729 "virtual-device", "%i", &vdevice);
731 /* go looking in the extended area instead */
732 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
735 xenbus_dev_fatal(dev, err, "reading virtual-device");
740 info = kzalloc(sizeof(*info), GFP_KERNEL);
742 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
747 info->vdevice = vdevice;
748 info->connected = BLKIF_STATE_DISCONNECTED;
749 INIT_WORK(&info->work, blkif_restart_queue);
751 for (i = 0; i < BLK_RING_SIZE; i++)
752 info->shadow[i].req.id = i+1;
753 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
755 /* Front end dir is a number, which is used as the id. */
756 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
757 dev_set_drvdata(&dev->dev, info);
759 err = talk_to_backend(dev, info);
762 dev_set_drvdata(&dev->dev, NULL);
770 static int blkif_recover(struct blkfront_info *info)
773 struct blkif_request *req;
774 struct blk_shadow *copy;
777 /* Stage 1: Make a safe copy of the shadow state. */
778 copy = kmalloc(sizeof(info->shadow),
779 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
782 memcpy(copy, info->shadow, sizeof(info->shadow));
784 /* Stage 2: Set up free list. */
785 memset(&info->shadow, 0, sizeof(info->shadow));
786 for (i = 0; i < BLK_RING_SIZE; i++)
787 info->shadow[i].req.id = i+1;
788 info->shadow_free = info->ring.req_prod_pvt;
789 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
791 /* Stage 3: Find pending requests and requeue them. */
792 for (i = 0; i < BLK_RING_SIZE; i++) {
794 if (copy[i].request == 0)
797 /* Grab a request slot and copy shadow state into it. */
798 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
801 /* We get a new request id, and must reset the shadow state. */
802 req->id = get_id_from_freelist(info);
803 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
805 /* Rewrite any grant references invalidated by susp/resume. */
806 for (j = 0; j < req->nr_segments; j++)
807 gnttab_grant_foreign_access_ref(
809 info->xbdev->otherend_id,
810 pfn_to_mfn(info->shadow[req->id].frame[j]),
813 info->shadow[req->id].request));
814 info->shadow[req->id].req = *req;
816 info->ring.req_prod_pvt++;
821 xenbus_switch_state(info->xbdev, XenbusStateConnected);
823 spin_lock_irq(&blkif_io_lock);
825 /* Now safe for us to use the shared ring */
826 info->connected = BLKIF_STATE_CONNECTED;
828 /* Send off requeued requests */
829 flush_requests(info);
831 /* Kick any other new requests queued since we resumed */
832 kick_pending_request_queues(info);
834 spin_unlock_irq(&blkif_io_lock);
840 * We are reconnecting to the backend, due to a suspend/resume, or a backend
841 * driver restart. We tear down our blkif structure and recreate it, but
842 * leave the device-layer structures intact so that this is transparent to the
843 * rest of the kernel.
845 static int blkfront_resume(struct xenbus_device *dev)
847 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
850 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
852 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
854 err = talk_to_backend(dev, info);
855 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
856 err = blkif_recover(info);
863 * Invoked when the backend is finally 'ready' (and has told produced
864 * the details about the physical device - #sectors, size, etc).
866 static void blkfront_connect(struct blkfront_info *info)
868 unsigned long long sectors;
869 unsigned long sector_size;
873 if ((info->connected == BLKIF_STATE_CONNECTED) ||
874 (info->connected == BLKIF_STATE_SUSPENDED) )
877 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
878 __func__, info->xbdev->otherend);
880 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
881 "sectors", "%llu", §ors,
882 "info", "%u", &binfo,
883 "sector-size", "%lu", §or_size,
886 xenbus_dev_fatal(info->xbdev, err,
887 "reading backend fields at %s",
888 info->xbdev->otherend);
892 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
893 "feature-barrier", "%lu", &info->feature_barrier,
896 info->feature_barrier = 0;
898 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
900 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
901 info->xbdev->otherend);
905 xenbus_switch_state(info->xbdev, XenbusStateConnected);
907 /* Kick pending requests. */
908 spin_lock_irq(&blkif_io_lock);
909 info->connected = BLKIF_STATE_CONNECTED;
910 kick_pending_request_queues(info);
911 spin_unlock_irq(&blkif_io_lock);
919 * Handle the change of state of the backend to Closing. We must delete our
920 * device-layer structures now, to ensure that writes are flushed through to
921 * the backend. Once is this done, we can switch to Closed in
924 static void blkfront_closing(struct xenbus_device *dev)
926 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
929 dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
931 if (info->rq == NULL)
934 spin_lock_irqsave(&blkif_io_lock, flags);
936 /* No more blkif_request(). */
937 blk_stop_queue(info->rq);
939 /* No more gnttab callback work. */
940 gnttab_cancel_free_callback(&info->callback);
941 spin_unlock_irqrestore(&blkif_io_lock, flags);
943 /* Flush gnttab callback work. Must be done with no locks held. */
944 flush_scheduled_work();
946 blk_cleanup_queue(info->rq);
949 del_gendisk(info->gd);
952 xenbus_frontend_closed(dev);
956 * Callback received when the backend's state changes.
958 static void backend_changed(struct xenbus_device *dev,
959 enum xenbus_state backend_state)
961 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
962 struct block_device *bd;
964 dev_dbg(&dev->dev, "blkfront:backend_changed.\n");
966 switch (backend_state) {
967 case XenbusStateInitialising:
968 case XenbusStateInitWait:
969 case XenbusStateInitialised:
970 case XenbusStateUnknown:
971 case XenbusStateClosed:
974 case XenbusStateConnected:
975 blkfront_connect(info);
978 case XenbusStateClosing:
979 if (info->gd == NULL) {
980 xenbus_frontend_closed(dev);
983 bd = bdget_disk(info->gd, 0);
985 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
987 mutex_lock(&bd->bd_mutex);
989 xenbus_dev_error(dev, -EBUSY,
990 "Device in use; refusing to close");
992 blkfront_closing(dev);
993 mutex_unlock(&bd->bd_mutex);
999 static int blkfront_remove(struct xenbus_device *dev)
1001 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1003 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
1005 blkif_free(info, 0);
1012 static int blkfront_is_ready(struct xenbus_device *dev)
1014 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1016 return info->is_ready;
1019 static int blkif_open(struct block_device *bdev, fmode_t mode)
1021 struct blkfront_info *info = bdev->bd_disk->private_data;
1026 static int blkif_release(struct gendisk *disk, fmode_t mode)
1028 struct blkfront_info *info = disk->private_data;
1030 if (info->users == 0) {
1031 /* Check whether we have been instructed to close. We will
1032 have ignored this request initially, as the device was
1034 struct xenbus_device *dev = info->xbdev;
1035 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
1037 if (state == XenbusStateClosing && info->is_ready)
1038 blkfront_closing(dev);
1043 static const struct block_device_operations xlvbd_block_fops =
1045 .owner = THIS_MODULE,
1047 .release = blkif_release,
1048 .getgeo = blkif_getgeo,
1049 .locked_ioctl = blkif_ioctl,
1053 static struct xenbus_device_id blkfront_ids[] = {
1058 static struct xenbus_driver blkfront = {
1060 .owner = THIS_MODULE,
1061 .ids = blkfront_ids,
1062 .probe = blkfront_probe,
1063 .remove = blkfront_remove,
1064 .resume = blkfront_resume,
1065 .otherend_changed = backend_changed,
1066 .is_ready = blkfront_is_ready,
1069 static int __init xlblk_init(void)
1074 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1075 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1076 XENVBD_MAJOR, DEV_NAME);
1080 return xenbus_register_frontend(&blkfront);
1082 module_init(xlblk_init);
1085 static void __exit xlblk_exit(void)
1087 return xenbus_unregister_driver(&blkfront);
1089 module_exit(xlblk_exit);
1091 MODULE_DESCRIPTION("Xen virtual block device frontend");
1092 MODULE_LICENSE("GPL");
1093 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
1094 MODULE_ALIAS("xen:vbd");
1095 MODULE_ALIAS("xenblk");