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/scatterlist.h>
47 #include <xen/xenbus.h>
48 #include <xen/grant_table.h>
49 #include <xen/events.h>
51 #include <xen/platform_pci.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 :-)
82 struct xenbus_device
*xbdev
;
86 enum blkif_state connected
;
88 struct blkif_front_ring ring
;
89 struct scatterlist sg
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
90 unsigned int evtchn
, irq
;
91 struct request_queue
*rq
;
92 struct work_struct work
;
93 struct gnttab_free_callback callback
;
94 struct blk_shadow shadow
[BLK_RING_SIZE
];
95 unsigned long shadow_free
;
100 * The number of people holding this device open. We won't allow a
101 * hot-unplug unless this is 0.
106 static DEFINE_SPINLOCK(blkif_io_lock
);
108 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
109 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
110 #define GRANT_INVALID_REF 0
112 #define PARTS_PER_DISK 16
113 #define PARTS_PER_EXT_DISK 256
115 #define BLKIF_MAJOR(dev) ((dev)>>8)
116 #define BLKIF_MINOR(dev) ((dev) & 0xff)
119 #define EXTENDED (1<<EXT_SHIFT)
120 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
121 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
123 #define DEV_NAME "xvd" /* name in /dev */
125 static int get_id_from_freelist(struct blkfront_info
*info
)
127 unsigned long free
= info
->shadow_free
;
128 BUG_ON(free
>= BLK_RING_SIZE
);
129 info
->shadow_free
= info
->shadow
[free
].req
.id
;
130 info
->shadow
[free
].req
.id
= 0x0fffffee; /* debug */
134 static void add_id_to_freelist(struct blkfront_info
*info
,
137 info
->shadow
[id
].req
.id
= info
->shadow_free
;
138 info
->shadow
[id
].request
= 0;
139 info
->shadow_free
= id
;
142 static void blkif_restart_queue_callback(void *arg
)
144 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
145 schedule_work(&info
->work
);
148 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
150 /* We don't have real geometry info, but let's at least return
151 values consistent with the size of the device */
152 sector_t nsect
= get_capacity(bd
->bd_disk
);
153 sector_t cylinders
= nsect
;
157 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
158 hg
->cylinders
= cylinders
;
159 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
160 hg
->cylinders
= 0xffff;
164 static int blkif_ioctl(struct block_device
*bdev
, fmode_t mode
,
165 unsigned command
, unsigned long argument
)
167 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
170 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
171 command
, (long)argument
);
174 case CDROMMULTISESSION
:
175 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
176 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
177 if (put_user(0, (char __user
*)(argument
+ i
)))
181 case CDROM_GET_CAPABILITY
: {
182 struct gendisk
*gd
= info
->gd
;
183 if (gd
->flags
& GENHD_FL_CD
)
189 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
191 return -EINVAL
; /* same return as native Linux */
198 * blkif_queue_request
202 * id: for guest use only.
203 * operation: BLKIF_OP_{READ,WRITE,PROBE}
204 * buffer: buffer to read/write into. this should be a
205 * virtual address in the guest os.
207 static int blkif_queue_request(struct request
*req
)
209 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
210 unsigned long buffer_mfn
;
211 struct blkif_request
*ring_req
;
213 unsigned int fsect
, lsect
;
215 grant_ref_t gref_head
;
216 struct scatterlist
*sg
;
218 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
221 if (gnttab_alloc_grant_references(
222 BLKIF_MAX_SEGMENTS_PER_REQUEST
, &gref_head
) < 0) {
223 gnttab_request_free_callback(
225 blkif_restart_queue_callback
,
227 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
231 /* Fill out a communications ring structure. */
232 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
233 id
= get_id_from_freelist(info
);
234 info
->shadow
[id
].request
= (unsigned long)req
;
237 ring_req
->sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
238 ring_req
->handle
= info
->handle
;
240 ring_req
->operation
= rq_data_dir(req
) ?
241 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
242 if (blk_barrier_rq(req
))
243 ring_req
->operation
= BLKIF_OP_WRITE_BARRIER
;
245 ring_req
->nr_segments
= blk_rq_map_sg(req
->q
, req
, info
->sg
);
246 BUG_ON(ring_req
->nr_segments
> BLKIF_MAX_SEGMENTS_PER_REQUEST
);
248 for_each_sg(info
->sg
, sg
, ring_req
->nr_segments
, i
) {
249 buffer_mfn
= pfn_to_mfn(page_to_pfn(sg_page(sg
)));
250 fsect
= sg
->offset
>> 9;
251 lsect
= fsect
+ (sg
->length
>> 9) - 1;
252 /* install a grant reference. */
253 ref
= gnttab_claim_grant_reference(&gref_head
);
254 BUG_ON(ref
== -ENOSPC
);
256 gnttab_grant_foreign_access_ref(
258 info
->xbdev
->otherend_id
,
262 info
->shadow
[id
].frame
[i
] = mfn_to_pfn(buffer_mfn
);
264 (struct blkif_request_segment
) {
267 .last_sect
= lsect
};
270 info
->ring
.req_prod_pvt
++;
272 /* Keep a private copy so we can reissue requests when recovering. */
273 info
->shadow
[id
].req
= *ring_req
;
275 gnttab_free_grant_references(gref_head
);
281 static inline void flush_requests(struct blkfront_info
*info
)
285 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info
->ring
, notify
);
288 notify_remote_via_irq(info
->irq
);
293 * read a block; request is in a request queue
295 static void do_blkif_request(struct request_queue
*rq
)
297 struct blkfront_info
*info
= NULL
;
301 pr_debug("Entered do_blkif_request\n");
305 while ((req
= blk_peek_request(rq
)) != NULL
) {
306 info
= req
->rq_disk
->private_data
;
308 if (RING_FULL(&info
->ring
))
311 blk_start_request(req
);
313 if (!blk_fs_request(req
)) {
314 __blk_end_request_all(req
, -EIO
);
318 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
319 "(%u/%u) buffer:%p [%s]\n",
320 req
, req
->cmd
, (unsigned long)blk_rq_pos(req
),
321 blk_rq_cur_sectors(req
), blk_rq_sectors(req
),
322 req
->buffer
, rq_data_dir(req
) ? "write" : "read");
324 if (blkif_queue_request(req
)) {
325 blk_requeue_request(rq
, req
);
327 /* Avoid pointless unplugs. */
336 flush_requests(info
);
339 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
)
341 struct request_queue
*rq
;
343 rq
= blk_init_queue(do_blkif_request
, &blkif_io_lock
);
347 queue_flag_set_unlocked(QUEUE_FLAG_VIRT
, rq
);
349 /* Hard sector size and max sectors impersonate the equiv. hardware. */
350 blk_queue_logical_block_size(rq
, sector_size
);
351 blk_queue_max_hw_sectors(rq
, 512);
353 /* Each segment in a request is up to an aligned page in size. */
354 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
355 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
357 /* Ensure a merged request will fit in a single I/O ring slot. */
358 blk_queue_max_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
360 /* Make sure buffer addresses are sector-aligned. */
361 blk_queue_dma_alignment(rq
, 511);
363 /* Make sure we don't use bounce buffers. */
364 blk_queue_bounce_limit(rq
, BLK_BOUNCE_ANY
);
372 static int xlvbd_barrier(struct blkfront_info
*info
)
376 err
= blk_queue_ordered(info
->rq
,
377 info
->feature_barrier
? QUEUE_ORDERED_DRAIN
: QUEUE_ORDERED_NONE
,
383 printk(KERN_INFO
"blkfront: %s: barriers %s\n",
385 info
->feature_barrier
? "enabled" : "disabled");
390 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
391 struct blkfront_info
*info
,
392 u16 vdisk_info
, u16 sector_size
)
401 BUG_ON(info
->gd
!= NULL
);
402 BUG_ON(info
->rq
!= NULL
);
404 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
405 /* this is above the extended range; something is wrong */
406 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
410 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
411 minor
= BLKIF_MINOR(info
->vdevice
);
412 nr_parts
= PARTS_PER_DISK
;
414 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
415 nr_parts
= PARTS_PER_EXT_DISK
;
418 if ((minor
% nr_parts
) == 0)
419 nr_minors
= nr_parts
;
421 gd
= alloc_disk(nr_minors
);
425 offset
= minor
/ nr_parts
;
429 sprintf(gd
->disk_name
, "%s%c", DEV_NAME
, 'a' + offset
);
431 sprintf(gd
->disk_name
, "%s%c%c", DEV_NAME
,
432 'a' + ((offset
/ 26)-1), 'a' + (offset
% 26));
435 sprintf(gd
->disk_name
, "%s%c%d", DEV_NAME
,
437 minor
& (nr_parts
- 1));
439 sprintf(gd
->disk_name
, "%s%c%c%d", DEV_NAME
,
440 'a' + ((offset
/ 26) - 1),
442 minor
& (nr_parts
- 1));
445 gd
->major
= XENVBD_MAJOR
;
446 gd
->first_minor
= minor
;
447 gd
->fops
= &xlvbd_block_fops
;
448 gd
->private_data
= info
;
449 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
450 set_capacity(gd
, capacity
);
452 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
457 info
->rq
= gd
->queue
;
460 if (info
->feature_barrier
)
463 if (vdisk_info
& VDISK_READONLY
)
466 if (vdisk_info
& VDISK_REMOVABLE
)
467 gd
->flags
|= GENHD_FL_REMOVABLE
;
469 if (vdisk_info
& VDISK_CDROM
)
470 gd
->flags
|= GENHD_FL_CD
;
478 static void kick_pending_request_queues(struct blkfront_info
*info
)
480 if (!RING_FULL(&info
->ring
)) {
481 /* Re-enable calldowns. */
482 blk_start_queue(info
->rq
);
483 /* Kick things off immediately. */
484 do_blkif_request(info
->rq
);
488 static void blkif_restart_queue(struct work_struct
*work
)
490 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
492 spin_lock_irq(&blkif_io_lock
);
493 if (info
->connected
== BLKIF_STATE_CONNECTED
)
494 kick_pending_request_queues(info
);
495 spin_unlock_irq(&blkif_io_lock
);
498 static void blkif_free(struct blkfront_info
*info
, int suspend
)
500 /* Prevent new requests being issued until we fix things up. */
501 spin_lock_irq(&blkif_io_lock
);
502 info
->connected
= suspend
?
503 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
504 /* No more blkif_request(). */
506 blk_stop_queue(info
->rq
);
507 /* No more gnttab callback work. */
508 gnttab_cancel_free_callback(&info
->callback
);
509 spin_unlock_irq(&blkif_io_lock
);
511 /* Flush gnttab callback work. Must be done with no locks held. */
512 flush_scheduled_work();
514 /* Free resources associated with old device channel. */
515 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
516 gnttab_end_foreign_access(info
->ring_ref
, 0,
517 (unsigned long)info
->ring
.sring
);
518 info
->ring_ref
= GRANT_INVALID_REF
;
519 info
->ring
.sring
= NULL
;
522 unbind_from_irqhandler(info
->irq
, info
);
523 info
->evtchn
= info
->irq
= 0;
527 static void blkif_completion(struct blk_shadow
*s
)
530 for (i
= 0; i
< s
->req
.nr_segments
; i
++)
531 gnttab_end_foreign_access(s
->req
.seg
[i
].gref
, 0, 0UL);
534 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
537 struct blkif_response
*bret
;
540 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
543 spin_lock_irqsave(&blkif_io_lock
, flags
);
545 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
546 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
551 rp
= info
->ring
.sring
->rsp_prod
;
552 rmb(); /* Ensure we see queued responses up to 'rp'. */
554 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
557 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
559 req
= (struct request
*)info
->shadow
[id
].request
;
561 blkif_completion(&info
->shadow
[id
]);
563 add_id_to_freelist(info
, id
);
565 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
566 switch (bret
->operation
) {
567 case BLKIF_OP_WRITE_BARRIER
:
568 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
569 printk(KERN_WARNING
"blkfront: %s: write barrier op failed\n",
570 info
->gd
->disk_name
);
572 info
->feature_barrier
= 0;
578 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
579 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
580 "request: %x\n", bret
->status
);
582 __blk_end_request_all(req
, error
);
589 info
->ring
.rsp_cons
= i
;
591 if (i
!= info
->ring
.req_prod_pvt
) {
593 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
597 info
->ring
.sring
->rsp_event
= i
+ 1;
599 kick_pending_request_queues(info
);
601 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
607 static int setup_blkring(struct xenbus_device
*dev
,
608 struct blkfront_info
*info
)
610 struct blkif_sring
*sring
;
613 info
->ring_ref
= GRANT_INVALID_REF
;
615 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
617 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
620 SHARED_RING_INIT(sring
);
621 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
623 sg_init_table(info
->sg
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
625 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
627 free_page((unsigned long)sring
);
628 info
->ring
.sring
= NULL
;
631 info
->ring_ref
= err
;
633 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
637 err
= bind_evtchn_to_irqhandler(info
->evtchn
,
639 IRQF_SAMPLE_RANDOM
, "blkif", info
);
641 xenbus_dev_fatal(dev
, err
,
642 "bind_evtchn_to_irqhandler failed");
654 /* Common code used when first setting up, and when resuming. */
655 static int talk_to_backend(struct xenbus_device
*dev
,
656 struct blkfront_info
*info
)
658 const char *message
= NULL
;
659 struct xenbus_transaction xbt
;
662 /* Create shared ring, alloc event channel. */
663 err
= setup_blkring(dev
, info
);
668 err
= xenbus_transaction_start(&xbt
);
670 xenbus_dev_fatal(dev
, err
, "starting transaction");
671 goto destroy_blkring
;
674 err
= xenbus_printf(xbt
, dev
->nodename
,
675 "ring-ref", "%u", info
->ring_ref
);
677 message
= "writing ring-ref";
678 goto abort_transaction
;
680 err
= xenbus_printf(xbt
, dev
->nodename
,
681 "event-channel", "%u", info
->evtchn
);
683 message
= "writing event-channel";
684 goto abort_transaction
;
686 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
687 XEN_IO_PROTO_ABI_NATIVE
);
689 message
= "writing protocol";
690 goto abort_transaction
;
693 err
= xenbus_transaction_end(xbt
, 0);
697 xenbus_dev_fatal(dev
, err
, "completing transaction");
698 goto destroy_blkring
;
701 xenbus_switch_state(dev
, XenbusStateInitialised
);
706 xenbus_transaction_end(xbt
, 1);
708 xenbus_dev_fatal(dev
, err
, "%s", message
);
717 * Entry point to this code when a new device is created. Allocate the basic
718 * structures and the ring buffer for communication with the backend, and
719 * inform the backend of the appropriate details for those. Switch to
722 static int blkfront_probe(struct xenbus_device
*dev
,
723 const struct xenbus_device_id
*id
)
726 struct blkfront_info
*info
;
728 /* FIXME: Use dynamic device id if this is not set. */
729 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
730 "virtual-device", "%i", &vdevice
);
732 /* go looking in the extended area instead */
733 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
736 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
741 if (xen_hvm_domain()) {
744 /* no unplug has been done: do not hook devices != xen vbds */
745 if (xen_platform_pci_unplug
& XEN_UNPLUG_IGNORE
) {
748 if (!VDEV_IS_EXTENDED(vdevice
))
749 major
= BLKIF_MAJOR(vdevice
);
751 major
= XENVBD_MAJOR
;
753 if (major
!= XENVBD_MAJOR
) {
755 "%s: HVM does not support vbd %d as xen block device\n",
756 __FUNCTION__
, vdevice
);
760 /* do not create a PV cdrom device if we are an HVM guest */
761 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
764 if (strncmp(type
, "cdrom", 5) == 0) {
770 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
772 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
777 info
->vdevice
= vdevice
;
778 info
->connected
= BLKIF_STATE_DISCONNECTED
;
779 INIT_WORK(&info
->work
, blkif_restart_queue
);
781 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
782 info
->shadow
[i
].req
.id
= i
+1;
783 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
785 /* Front end dir is a number, which is used as the id. */
786 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
787 dev_set_drvdata(&dev
->dev
, info
);
789 err
= talk_to_backend(dev
, info
);
792 dev_set_drvdata(&dev
->dev
, NULL
);
800 static int blkif_recover(struct blkfront_info
*info
)
803 struct blkif_request
*req
;
804 struct blk_shadow
*copy
;
807 /* Stage 1: Make a safe copy of the shadow state. */
808 copy
= kmalloc(sizeof(info
->shadow
),
809 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
812 memcpy(copy
, info
->shadow
, sizeof(info
->shadow
));
814 /* Stage 2: Set up free list. */
815 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
816 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
817 info
->shadow
[i
].req
.id
= i
+1;
818 info
->shadow_free
= info
->ring
.req_prod_pvt
;
819 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
821 /* Stage 3: Find pending requests and requeue them. */
822 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
824 if (copy
[i
].request
== 0)
827 /* Grab a request slot and copy shadow state into it. */
828 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
831 /* We get a new request id, and must reset the shadow state. */
832 req
->id
= get_id_from_freelist(info
);
833 memcpy(&info
->shadow
[req
->id
], ©
[i
], sizeof(copy
[i
]));
835 /* Rewrite any grant references invalidated by susp/resume. */
836 for (j
= 0; j
< req
->nr_segments
; j
++)
837 gnttab_grant_foreign_access_ref(
839 info
->xbdev
->otherend_id
,
840 pfn_to_mfn(info
->shadow
[req
->id
].frame
[j
]),
843 info
->shadow
[req
->id
].request
));
844 info
->shadow
[req
->id
].req
= *req
;
846 info
->ring
.req_prod_pvt
++;
851 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
853 spin_lock_irq(&blkif_io_lock
);
855 /* Now safe for us to use the shared ring */
856 info
->connected
= BLKIF_STATE_CONNECTED
;
858 /* Send off requeued requests */
859 flush_requests(info
);
861 /* Kick any other new requests queued since we resumed */
862 kick_pending_request_queues(info
);
864 spin_unlock_irq(&blkif_io_lock
);
870 * We are reconnecting to the backend, due to a suspend/resume, or a backend
871 * driver restart. We tear down our blkif structure and recreate it, but
872 * leave the device-layer structures intact so that this is transparent to the
873 * rest of the kernel.
875 static int blkfront_resume(struct xenbus_device
*dev
)
877 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
880 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
882 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
884 err
= talk_to_backend(dev
, info
);
885 if (info
->connected
== BLKIF_STATE_SUSPENDED
&& !err
)
886 err
= blkif_recover(info
);
893 * Invoked when the backend is finally 'ready' (and has told produced
894 * the details about the physical device - #sectors, size, etc).
896 static void blkfront_connect(struct blkfront_info
*info
)
898 unsigned long long sectors
;
899 unsigned long sector_size
;
903 if ((info
->connected
== BLKIF_STATE_CONNECTED
) ||
904 (info
->connected
== BLKIF_STATE_SUSPENDED
) )
907 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
908 __func__
, info
->xbdev
->otherend
);
910 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
911 "sectors", "%llu", §ors
,
912 "info", "%u", &binfo
,
913 "sector-size", "%lu", §or_size
,
916 xenbus_dev_fatal(info
->xbdev
, err
,
917 "reading backend fields at %s",
918 info
->xbdev
->otherend
);
922 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
923 "feature-barrier", "%lu", &info
->feature_barrier
,
926 info
->feature_barrier
= 0;
928 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
);
930 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
931 info
->xbdev
->otherend
);
935 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
937 /* Kick pending requests. */
938 spin_lock_irq(&blkif_io_lock
);
939 info
->connected
= BLKIF_STATE_CONNECTED
;
940 kick_pending_request_queues(info
);
941 spin_unlock_irq(&blkif_io_lock
);
949 * Handle the change of state of the backend to Closing. We must delete our
950 * device-layer structures now, to ensure that writes are flushed through to
951 * the backend. Once is this done, we can switch to Closed in
954 static void blkfront_closing(struct xenbus_device
*dev
)
956 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
959 dev_dbg(&dev
->dev
, "blkfront_closing: %s removed\n", dev
->nodename
);
961 if (info
->rq
== NULL
)
964 spin_lock_irqsave(&blkif_io_lock
, flags
);
966 /* No more blkif_request(). */
967 blk_stop_queue(info
->rq
);
969 /* No more gnttab callback work. */
970 gnttab_cancel_free_callback(&info
->callback
);
971 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
973 /* Flush gnttab callback work. Must be done with no locks held. */
974 flush_scheduled_work();
976 blk_cleanup_queue(info
->rq
);
979 del_gendisk(info
->gd
);
982 xenbus_frontend_closed(dev
);
986 * Callback received when the backend's state changes.
988 static void backend_changed(struct xenbus_device
*dev
,
989 enum xenbus_state backend_state
)
991 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
992 struct block_device
*bd
;
994 dev_dbg(&dev
->dev
, "blkfront:backend_changed.\n");
996 switch (backend_state
) {
997 case XenbusStateInitialising
:
998 case XenbusStateInitWait
:
999 case XenbusStateInitialised
:
1000 case XenbusStateUnknown
:
1001 case XenbusStateClosed
:
1004 case XenbusStateConnected
:
1005 blkfront_connect(info
);
1008 case XenbusStateClosing
:
1009 if (info
->gd
== NULL
) {
1010 xenbus_frontend_closed(dev
);
1013 bd
= bdget_disk(info
->gd
, 0);
1015 xenbus_dev_fatal(dev
, -ENODEV
, "bdget failed");
1017 mutex_lock(&bd
->bd_mutex
);
1018 if (info
->users
> 0)
1019 xenbus_dev_error(dev
, -EBUSY
,
1020 "Device in use; refusing to close");
1022 blkfront_closing(dev
);
1023 mutex_unlock(&bd
->bd_mutex
);
1029 static int blkfront_remove(struct xenbus_device
*dev
)
1031 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1033 dev_dbg(&dev
->dev
, "blkfront_remove: %s removed\n", dev
->nodename
);
1035 blkif_free(info
, 0);
1042 static int blkfront_is_ready(struct xenbus_device
*dev
)
1044 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1046 return info
->is_ready
;
1049 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
1051 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
1056 static int blkif_release(struct gendisk
*disk
, fmode_t mode
)
1058 struct blkfront_info
*info
= disk
->private_data
;
1060 if (info
->users
== 0) {
1061 /* Check whether we have been instructed to close. We will
1062 have ignored this request initially, as the device was
1064 struct xenbus_device
*dev
= info
->xbdev
;
1065 enum xenbus_state state
= xenbus_read_driver_state(dev
->otherend
);
1067 if (state
== XenbusStateClosing
&& info
->is_ready
)
1068 blkfront_closing(dev
);
1073 static const struct block_device_operations xlvbd_block_fops
=
1075 .owner
= THIS_MODULE
,
1077 .release
= blkif_release
,
1078 .getgeo
= blkif_getgeo
,
1079 .locked_ioctl
= blkif_ioctl
,
1083 static const struct xenbus_device_id blkfront_ids
[] = {
1088 static struct xenbus_driver blkfront
= {
1090 .owner
= THIS_MODULE
,
1091 .ids
= blkfront_ids
,
1092 .probe
= blkfront_probe
,
1093 .remove
= blkfront_remove
,
1094 .resume
= blkfront_resume
,
1095 .otherend_changed
= backend_changed
,
1096 .is_ready
= blkfront_is_ready
,
1099 static int __init
xlblk_init(void)
1104 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
1105 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1106 XENVBD_MAJOR
, DEV_NAME
);
1110 return xenbus_register_frontend(&blkfront
);
1112 module_init(xlblk_init
);
1115 static void __exit
xlblk_exit(void)
1117 return xenbus_unregister_driver(&blkfront
);
1119 module_exit(xlblk_exit
);
1121 MODULE_DESCRIPTION("Xen virtual block device frontend");
1122 MODULE_LICENSE("GPL");
1123 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1124 MODULE_ALIAS("xen:vbd");
1125 MODULE_ALIAS("xenblk");