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 static DEFINE_SPINLOCK(blkif_io_lock);
103 static unsigned int nr_minors;
104 static unsigned long *minors;
105 static DEFINE_SPINLOCK(minor_lock);
107 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
108 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
109 #define GRANT_INVALID_REF 0
111 #define PARTS_PER_DISK 16
112 #define PARTS_PER_EXT_DISK 256
114 #define BLKIF_MAJOR(dev) ((dev)>>8)
115 #define BLKIF_MINOR(dev) ((dev) & 0xff)
118 #define EXTENDED (1<<EXT_SHIFT)
119 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
120 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
122 #define DEV_NAME "xvd" /* name in /dev */
124 static int get_id_from_freelist(struct blkfront_info *info)
126 unsigned long free = info->shadow_free;
127 BUG_ON(free >= BLK_RING_SIZE);
128 info->shadow_free = info->shadow[free].req.id;
129 info->shadow[free].req.id = 0x0fffffee; /* debug */
133 static void add_id_to_freelist(struct blkfront_info *info,
136 info->shadow[id].req.id = info->shadow_free;
137 info->shadow[id].request = 0;
138 info->shadow_free = id;
141 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
143 unsigned int end = minor + nr;
146 if (end > nr_minors) {
147 unsigned long *bitmap, *old;
149 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
154 spin_lock(&minor_lock);
155 if (end > nr_minors) {
157 memcpy(bitmap, minors,
158 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
160 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
163 spin_unlock(&minor_lock);
167 spin_lock(&minor_lock);
168 if (find_next_bit(minors, end, minor) >= end) {
169 for (; minor < end; ++minor)
170 __set_bit(minor, minors);
174 spin_unlock(&minor_lock);
179 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
181 unsigned int end = minor + nr;
183 BUG_ON(end > nr_minors);
184 spin_lock(&minor_lock);
185 for (; minor < end; ++minor)
186 __clear_bit(minor, minors);
187 spin_unlock(&minor_lock);
190 static void blkif_restart_queue_callback(void *arg)
192 struct blkfront_info *info = (struct blkfront_info *)arg;
193 schedule_work(&info->work);
196 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
198 /* We don't have real geometry info, but let's at least return
199 values consistent with the size of the device */
200 sector_t nsect = get_capacity(bd->bd_disk);
201 sector_t cylinders = nsect;
205 sector_div(cylinders, hg->heads * hg->sectors);
206 hg->cylinders = cylinders;
207 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
208 hg->cylinders = 0xffff;
212 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
213 unsigned command, unsigned long argument)
215 struct blkfront_info *info = bdev->bd_disk->private_data;
218 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
219 command, (long)argument);
222 case CDROMMULTISESSION:
223 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
224 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
225 if (put_user(0, (char __user *)(argument + i)))
229 case CDROM_GET_CAPABILITY: {
230 struct gendisk *gd = info->gd;
231 if (gd->flags & GENHD_FL_CD)
237 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
239 return -EINVAL; /* same return as native Linux */
246 * blkif_queue_request
250 * id: for guest use only.
251 * operation: BLKIF_OP_{READ,WRITE,PROBE}
252 * buffer: buffer to read/write into. this should be a
253 * virtual address in the guest os.
255 static int blkif_queue_request(struct request *req)
257 struct blkfront_info *info = req->rq_disk->private_data;
258 unsigned long buffer_mfn;
259 struct blkif_request *ring_req;
261 unsigned int fsect, lsect;
263 grant_ref_t gref_head;
264 struct scatterlist *sg;
266 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
269 if (gnttab_alloc_grant_references(
270 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
271 gnttab_request_free_callback(
273 blkif_restart_queue_callback,
275 BLKIF_MAX_SEGMENTS_PER_REQUEST);
279 /* Fill out a communications ring structure. */
280 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
281 id = get_id_from_freelist(info);
282 info->shadow[id].request = (unsigned long)req;
285 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
286 ring_req->handle = info->handle;
288 ring_req->operation = rq_data_dir(req) ?
289 BLKIF_OP_WRITE : BLKIF_OP_READ;
290 if (req->cmd_flags & REQ_HARDBARRIER)
291 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
293 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
294 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
296 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
297 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
298 fsect = sg->offset >> 9;
299 lsect = fsect + (sg->length >> 9) - 1;
300 /* install a grant reference. */
301 ref = gnttab_claim_grant_reference(&gref_head);
302 BUG_ON(ref == -ENOSPC);
304 gnttab_grant_foreign_access_ref(
306 info->xbdev->otherend_id,
310 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
312 (struct blkif_request_segment) {
315 .last_sect = lsect };
318 info->ring.req_prod_pvt++;
320 /* Keep a private copy so we can reissue requests when recovering. */
321 info->shadow[id].req = *ring_req;
323 gnttab_free_grant_references(gref_head);
329 static inline void flush_requests(struct blkfront_info *info)
333 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
336 notify_remote_via_irq(info->irq);
341 * read a block; request is in a request queue
343 static void do_blkif_request(struct request_queue *rq)
345 struct blkfront_info *info = NULL;
349 pr_debug("Entered do_blkif_request\n");
353 while ((req = blk_peek_request(rq)) != NULL) {
354 info = req->rq_disk->private_data;
356 if (RING_FULL(&info->ring))
359 blk_start_request(req);
361 if (req->cmd_type != REQ_TYPE_FS) {
362 __blk_end_request_all(req, -EIO);
366 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
367 "(%u/%u) buffer:%p [%s]\n",
368 req, req->cmd, (unsigned long)blk_rq_pos(req),
369 blk_rq_cur_sectors(req), blk_rq_sectors(req),
370 req->buffer, rq_data_dir(req) ? "write" : "read");
372 if (blkif_queue_request(req)) {
373 blk_requeue_request(rq, req);
375 /* Avoid pointless unplugs. */
384 flush_requests(info);
387 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
389 struct request_queue *rq;
391 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
395 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
397 /* Hard sector size and max sectors impersonate the equiv. hardware. */
398 blk_queue_logical_block_size(rq, sector_size);
399 blk_queue_max_hw_sectors(rq, 512);
401 /* Each segment in a request is up to an aligned page in size. */
402 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
403 blk_queue_max_segment_size(rq, PAGE_SIZE);
405 /* Ensure a merged request will fit in a single I/O ring slot. */
406 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
408 /* Make sure buffer addresses are sector-aligned. */
409 blk_queue_dma_alignment(rq, 511);
411 /* Make sure we don't use bounce buffers. */
412 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
420 static int xlvbd_barrier(struct blkfront_info *info)
425 switch (info->feature_barrier) {
426 case QUEUE_ORDERED_DRAIN: barrier = "enabled (drain)"; break;
427 case QUEUE_ORDERED_TAG: barrier = "enabled (tag)"; break;
428 case QUEUE_ORDERED_NONE: barrier = "disabled"; break;
429 default: return -EINVAL;
432 err = blk_queue_ordered(info->rq, info->feature_barrier);
437 printk(KERN_INFO "blkfront: %s: barriers %s\n",
438 info->gd->disk_name, barrier);
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;
520 if (vdisk_info & VDISK_READONLY)
523 if (vdisk_info & VDISK_REMOVABLE)
524 gd->flags |= GENHD_FL_REMOVABLE;
526 if (vdisk_info & VDISK_CDROM)
527 gd->flags |= GENHD_FL_CD;
532 xlbd_release_minors(minor, nr_minors);
537 static void xlvbd_release_gendisk(struct blkfront_info *info)
539 unsigned int minor, nr_minors;
542 if (info->rq == NULL)
545 spin_lock_irqsave(&blkif_io_lock, flags);
547 /* No more blkif_request(). */
548 blk_stop_queue(info->rq);
550 /* No more gnttab callback work. */
551 gnttab_cancel_free_callback(&info->callback);
552 spin_unlock_irqrestore(&blkif_io_lock, flags);
554 /* Flush gnttab callback work. Must be done with no locks held. */
555 flush_scheduled_work();
557 del_gendisk(info->gd);
559 minor = info->gd->first_minor;
560 nr_minors = info->gd->minors;
561 xlbd_release_minors(minor, nr_minors);
563 blk_cleanup_queue(info->rq);
570 static void kick_pending_request_queues(struct blkfront_info *info)
572 if (!RING_FULL(&info->ring)) {
573 /* Re-enable calldowns. */
574 blk_start_queue(info->rq);
575 /* Kick things off immediately. */
576 do_blkif_request(info->rq);
580 static void blkif_restart_queue(struct work_struct *work)
582 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
584 spin_lock_irq(&blkif_io_lock);
585 if (info->connected == BLKIF_STATE_CONNECTED)
586 kick_pending_request_queues(info);
587 spin_unlock_irq(&blkif_io_lock);
590 static void blkif_free(struct blkfront_info *info, int suspend)
592 /* Prevent new requests being issued until we fix things up. */
593 spin_lock_irq(&blkif_io_lock);
594 info->connected = suspend ?
595 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
596 /* No more blkif_request(). */
598 blk_stop_queue(info->rq);
599 /* No more gnttab callback work. */
600 gnttab_cancel_free_callback(&info->callback);
601 spin_unlock_irq(&blkif_io_lock);
603 /* Flush gnttab callback work. Must be done with no locks held. */
604 flush_scheduled_work();
606 /* Free resources associated with old device channel. */
607 if (info->ring_ref != GRANT_INVALID_REF) {
608 gnttab_end_foreign_access(info->ring_ref, 0,
609 (unsigned long)info->ring.sring);
610 info->ring_ref = GRANT_INVALID_REF;
611 info->ring.sring = NULL;
614 unbind_from_irqhandler(info->irq, info);
615 info->evtchn = info->irq = 0;
619 static void blkif_completion(struct blk_shadow *s)
622 for (i = 0; i < s->req.nr_segments; i++)
623 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
626 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
629 struct blkif_response *bret;
632 struct blkfront_info *info = (struct blkfront_info *)dev_id;
635 spin_lock_irqsave(&blkif_io_lock, flags);
637 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
638 spin_unlock_irqrestore(&blkif_io_lock, flags);
643 rp = info->ring.sring->rsp_prod;
644 rmb(); /* Ensure we see queued responses up to 'rp'. */
646 for (i = info->ring.rsp_cons; i != rp; i++) {
649 bret = RING_GET_RESPONSE(&info->ring, i);
651 req = (struct request *)info->shadow[id].request;
653 blkif_completion(&info->shadow[id]);
655 add_id_to_freelist(info, id);
657 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
658 switch (bret->operation) {
659 case BLKIF_OP_WRITE_BARRIER:
660 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
661 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
662 info->gd->disk_name);
664 info->feature_barrier = QUEUE_ORDERED_NONE;
670 if (unlikely(bret->status != BLKIF_RSP_OKAY))
671 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
672 "request: %x\n", bret->status);
674 __blk_end_request_all(req, error);
681 info->ring.rsp_cons = i;
683 if (i != info->ring.req_prod_pvt) {
685 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
689 info->ring.sring->rsp_event = i + 1;
691 kick_pending_request_queues(info);
693 spin_unlock_irqrestore(&blkif_io_lock, flags);
699 static int setup_blkring(struct xenbus_device *dev,
700 struct blkfront_info *info)
702 struct blkif_sring *sring;
705 info->ring_ref = GRANT_INVALID_REF;
707 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
709 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
712 SHARED_RING_INIT(sring);
713 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
715 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
717 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
719 free_page((unsigned long)sring);
720 info->ring.sring = NULL;
723 info->ring_ref = err;
725 err = xenbus_alloc_evtchn(dev, &info->evtchn);
729 err = bind_evtchn_to_irqhandler(info->evtchn,
731 IRQF_SAMPLE_RANDOM, "blkif", info);
733 xenbus_dev_fatal(dev, err,
734 "bind_evtchn_to_irqhandler failed");
746 /* Common code used when first setting up, and when resuming. */
747 static int talk_to_blkback(struct xenbus_device *dev,
748 struct blkfront_info *info)
750 const char *message = NULL;
751 struct xenbus_transaction xbt;
754 /* Create shared ring, alloc event channel. */
755 err = setup_blkring(dev, info);
760 err = xenbus_transaction_start(&xbt);
762 xenbus_dev_fatal(dev, err, "starting transaction");
763 goto destroy_blkring;
766 err = xenbus_printf(xbt, dev->nodename,
767 "ring-ref", "%u", info->ring_ref);
769 message = "writing ring-ref";
770 goto abort_transaction;
772 err = xenbus_printf(xbt, dev->nodename,
773 "event-channel", "%u", info->evtchn);
775 message = "writing event-channel";
776 goto abort_transaction;
778 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
779 XEN_IO_PROTO_ABI_NATIVE);
781 message = "writing protocol";
782 goto abort_transaction;
785 err = xenbus_transaction_end(xbt, 0);
789 xenbus_dev_fatal(dev, err, "completing transaction");
790 goto destroy_blkring;
793 xenbus_switch_state(dev, XenbusStateInitialised);
798 xenbus_transaction_end(xbt, 1);
800 xenbus_dev_fatal(dev, err, "%s", message);
808 * Entry point to this code when a new device is created. Allocate the basic
809 * structures and the ring buffer for communication with the backend, and
810 * inform the backend of the appropriate details for those. Switch to
813 static int blkfront_probe(struct xenbus_device *dev,
814 const struct xenbus_device_id *id)
817 struct blkfront_info *info;
819 /* FIXME: Use dynamic device id if this is not set. */
820 err = xenbus_scanf(XBT_NIL, dev->nodename,
821 "virtual-device", "%i", &vdevice);
823 /* go looking in the extended area instead */
824 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
827 xenbus_dev_fatal(dev, err, "reading virtual-device");
832 info = kzalloc(sizeof(*info), GFP_KERNEL);
834 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
838 mutex_init(&info->mutex);
840 info->vdevice = vdevice;
841 info->connected = BLKIF_STATE_DISCONNECTED;
842 INIT_WORK(&info->work, blkif_restart_queue);
844 for (i = 0; i < BLK_RING_SIZE; i++)
845 info->shadow[i].req.id = i+1;
846 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
848 /* Front end dir is a number, which is used as the id. */
849 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
850 dev_set_drvdata(&dev->dev, info);
852 err = talk_to_blkback(dev, info);
855 dev_set_drvdata(&dev->dev, NULL);
863 static int blkif_recover(struct blkfront_info *info)
866 struct blkif_request *req;
867 struct blk_shadow *copy;
870 /* Stage 1: Make a safe copy of the shadow state. */
871 copy = kmalloc(sizeof(info->shadow),
872 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
875 memcpy(copy, info->shadow, sizeof(info->shadow));
877 /* Stage 2: Set up free list. */
878 memset(&info->shadow, 0, sizeof(info->shadow));
879 for (i = 0; i < BLK_RING_SIZE; i++)
880 info->shadow[i].req.id = i+1;
881 info->shadow_free = info->ring.req_prod_pvt;
882 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
884 /* Stage 3: Find pending requests and requeue them. */
885 for (i = 0; i < BLK_RING_SIZE; i++) {
887 if (copy[i].request == 0)
890 /* Grab a request slot and copy shadow state into it. */
891 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
894 /* We get a new request id, and must reset the shadow state. */
895 req->id = get_id_from_freelist(info);
896 memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i]));
898 /* Rewrite any grant references invalidated by susp/resume. */
899 for (j = 0; j < req->nr_segments; j++)
900 gnttab_grant_foreign_access_ref(
902 info->xbdev->otherend_id,
903 pfn_to_mfn(info->shadow[req->id].frame[j]),
906 info->shadow[req->id].request));
907 info->shadow[req->id].req = *req;
909 info->ring.req_prod_pvt++;
914 xenbus_switch_state(info->xbdev, XenbusStateConnected);
916 spin_lock_irq(&blkif_io_lock);
918 /* Now safe for us to use the shared ring */
919 info->connected = BLKIF_STATE_CONNECTED;
921 /* Send off requeued requests */
922 flush_requests(info);
924 /* Kick any other new requests queued since we resumed */
925 kick_pending_request_queues(info);
927 spin_unlock_irq(&blkif_io_lock);
933 * We are reconnecting to the backend, due to a suspend/resume, or a backend
934 * driver restart. We tear down our blkif structure and recreate it, but
935 * leave the device-layer structures intact so that this is transparent to the
936 * rest of the kernel.
938 static int blkfront_resume(struct xenbus_device *dev)
940 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
943 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
945 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
947 err = talk_to_blkback(dev, info);
948 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
949 err = blkif_recover(info);
955 blkfront_closing(struct blkfront_info *info)
957 struct xenbus_device *xbdev = info->xbdev;
958 struct block_device *bdev = NULL;
960 mutex_lock(&info->mutex);
962 if (xbdev->state == XenbusStateClosing) {
963 mutex_unlock(&info->mutex);
968 bdev = bdget_disk(info->gd, 0);
970 mutex_unlock(&info->mutex);
973 xenbus_frontend_closed(xbdev);
977 mutex_lock(&bdev->bd_mutex);
979 if (bdev->bd_openers) {
980 xenbus_dev_error(xbdev, -EBUSY,
981 "Device in use; refusing to close");
982 xenbus_switch_state(xbdev, XenbusStateClosing);
984 xlvbd_release_gendisk(info);
985 xenbus_frontend_closed(xbdev);
988 mutex_unlock(&bdev->bd_mutex);
993 * Invoked when the backend is finally 'ready' (and has told produced
994 * the details about the physical device - #sectors, size, etc).
996 static void blkfront_connect(struct blkfront_info *info)
998 unsigned long long sectors;
999 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", &barrier,
1047 * If there's no "feature-barrier" defined, then it means
1048 * we're dealing with a very old backend which writes
1049 * synchronously; draining will do what needs to get done.
1051 * If there are barriers, then we can do full queued writes
1052 * with tagged barriers.
1054 * If barriers are not supported, then there's no much we can
1055 * do, so just set ordering to NONE.
1058 info->feature_barrier = QUEUE_ORDERED_DRAIN;
1060 info->feature_barrier = QUEUE_ORDERED_TAG;
1062 info->feature_barrier = QUEUE_ORDERED_NONE;
1064 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
1066 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1067 info->xbdev->otherend);
1071 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1073 /* Kick pending requests. */
1074 spin_lock_irq(&blkif_io_lock);
1075 info->connected = BLKIF_STATE_CONNECTED;
1076 kick_pending_request_queues(info);
1077 spin_unlock_irq(&blkif_io_lock);
1085 * Callback received when the backend's state changes.
1087 static void blkback_changed(struct xenbus_device *dev,
1088 enum xenbus_state backend_state)
1090 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1092 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
1094 switch (backend_state) {
1095 case XenbusStateInitialising:
1096 case XenbusStateInitWait:
1097 case XenbusStateInitialised:
1098 case XenbusStateUnknown:
1099 case XenbusStateClosed:
1102 case XenbusStateConnected:
1103 blkfront_connect(info);
1106 case XenbusStateClosing:
1107 blkfront_closing(info);
1112 static int blkfront_remove(struct xenbus_device *xbdev)
1114 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1115 struct block_device *bdev = NULL;
1116 struct gendisk *disk;
1118 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
1120 blkif_free(info, 0);
1122 mutex_lock(&info->mutex);
1126 bdev = bdget_disk(disk, 0);
1129 mutex_unlock(&info->mutex);
1137 * The xbdev was removed before we reached the Closed
1138 * state. See if it's safe to remove the disk. If the bdev
1139 * isn't closed yet, we let release take care of it.
1142 mutex_lock(&bdev->bd_mutex);
1143 info = disk->private_data;
1145 dev_warn(disk_to_dev(disk),
1146 "%s was hot-unplugged, %d stale handles\n",
1147 xbdev->nodename, bdev->bd_openers);
1149 if (info && !bdev->bd_openers) {
1150 xlvbd_release_gendisk(info);
1151 disk->private_data = NULL;
1155 mutex_unlock(&bdev->bd_mutex);
1161 static int blkfront_is_ready(struct xenbus_device *dev)
1163 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1165 return info->is_ready && info->xbdev;
1168 static int blkif_open(struct block_device *bdev, fmode_t mode)
1170 struct gendisk *disk = bdev->bd_disk;
1171 struct blkfront_info *info;
1176 info = disk->private_data;
1183 mutex_lock(&info->mutex);
1186 /* xbdev is closed */
1189 mutex_unlock(&info->mutex);
1196 static int blkif_release(struct gendisk *disk, fmode_t mode)
1198 struct blkfront_info *info = disk->private_data;
1199 struct block_device *bdev;
1200 struct xenbus_device *xbdev;
1204 bdev = bdget_disk(disk, 0);
1207 if (bdev->bd_openers)
1211 * Check if we have been instructed to close. We will have
1212 * deferred this request, because the bdev was still open.
1215 mutex_lock(&info->mutex);
1216 xbdev = info->xbdev;
1218 if (xbdev && xbdev->state == XenbusStateClosing) {
1219 /* pending switch to state closed */
1220 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
1221 xlvbd_release_gendisk(info);
1222 xenbus_frontend_closed(info->xbdev);
1225 mutex_unlock(&info->mutex);
1228 /* sudden device removal */
1229 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
1230 xlvbd_release_gendisk(info);
1231 disk->private_data = NULL;
1240 static const struct block_device_operations xlvbd_block_fops =
1242 .owner = THIS_MODULE,
1244 .release = blkif_release,
1245 .getgeo = blkif_getgeo,
1246 .ioctl = blkif_ioctl,
1250 static const struct xenbus_device_id blkfront_ids[] = {
1255 static struct xenbus_driver blkfront = {
1257 .owner = THIS_MODULE,
1258 .ids = blkfront_ids,
1259 .probe = blkfront_probe,
1260 .remove = blkfront_remove,
1261 .resume = blkfront_resume,
1262 .otherend_changed = blkback_changed,
1263 .is_ready = blkfront_is_ready,
1266 static int __init xlblk_init(void)
1271 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1272 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1273 XENVBD_MAJOR, DEV_NAME);
1277 return xenbus_register_frontend(&blkfront);
1279 module_init(xlblk_init);
1282 static void __exit xlblk_exit(void)
1284 return xenbus_unregister_driver(&blkfront);
1286 module_exit(xlblk_exit);
1288 MODULE_DESCRIPTION("Xen virtual block device frontend");
1289 MODULE_LICENSE("GPL");
1290 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
1291 MODULE_ALIAS("xen:vbd");
1292 MODULE_ALIAS("xenblk");