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/mutex.h>
45 #include <linux/scatterlist.h>
48 #include <xen/xenbus.h>
49 #include <xen/grant_table.h>
50 #include <xen/events.h>
52 #include <xen/platform_pci.h>
54 #include <xen/interface/grant_table.h>
55 #include <xen/interface/io/blkif.h>
56 #include <xen/interface/io/protocols.h>
58 #include <asm/xen/hypervisor.h>
61 BLKIF_STATE_DISCONNECTED
,
62 BLKIF_STATE_CONNECTED
,
63 BLKIF_STATE_SUSPENDED
,
67 struct blkif_request req
;
68 unsigned long request
;
69 unsigned long frame
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
72 static DEFINE_MUTEX(blkfront_mutex
);
73 static const struct block_device_operations xlvbd_block_fops
;
75 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
78 * We have one of these per vbd, whether ide, scsi or 'other'. They
79 * hang in private_data off the gendisk structure. We may end up
80 * putting all kinds of interesting stuff here :-)
85 struct xenbus_device
*xbdev
;
89 enum blkif_state connected
;
91 struct blkif_front_ring ring
;
92 struct scatterlist sg
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
93 unsigned int evtchn
, irq
;
94 struct request_queue
*rq
;
95 struct work_struct work
;
96 struct gnttab_free_callback callback
;
97 struct blk_shadow shadow
[BLK_RING_SIZE
];
98 unsigned long shadow_free
;
99 unsigned int feature_flush
;
103 static DEFINE_SPINLOCK(blkif_io_lock
);
105 static unsigned int nr_minors
;
106 static unsigned long *minors
;
107 static DEFINE_SPINLOCK(minor_lock
);
109 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
110 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
111 #define GRANT_INVALID_REF 0
113 #define PARTS_PER_DISK 16
114 #define PARTS_PER_EXT_DISK 256
116 #define BLKIF_MAJOR(dev) ((dev)>>8)
117 #define BLKIF_MINOR(dev) ((dev) & 0xff)
120 #define EXTENDED (1<<EXT_SHIFT)
121 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
122 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
124 #define DEV_NAME "xvd" /* name in /dev */
126 static int get_id_from_freelist(struct blkfront_info
*info
)
128 unsigned long free
= info
->shadow_free
;
129 BUG_ON(free
>= BLK_RING_SIZE
);
130 info
->shadow_free
= info
->shadow
[free
].req
.id
;
131 info
->shadow
[free
].req
.id
= 0x0fffffee; /* debug */
135 static void add_id_to_freelist(struct blkfront_info
*info
,
138 info
->shadow
[id
].req
.id
= info
->shadow_free
;
139 info
->shadow
[id
].request
= 0;
140 info
->shadow_free
= id
;
143 static int xlbd_reserve_minors(unsigned int minor
, unsigned int nr
)
145 unsigned int end
= minor
+ nr
;
148 if (end
> nr_minors
) {
149 unsigned long *bitmap
, *old
;
151 bitmap
= kzalloc(BITS_TO_LONGS(end
) * sizeof(*bitmap
),
156 spin_lock(&minor_lock
);
157 if (end
> nr_minors
) {
159 memcpy(bitmap
, minors
,
160 BITS_TO_LONGS(nr_minors
) * sizeof(*bitmap
));
162 nr_minors
= BITS_TO_LONGS(end
) * BITS_PER_LONG
;
165 spin_unlock(&minor_lock
);
169 spin_lock(&minor_lock
);
170 if (find_next_bit(minors
, end
, minor
) >= end
) {
171 for (; minor
< end
; ++minor
)
172 __set_bit(minor
, minors
);
176 spin_unlock(&minor_lock
);
181 static void xlbd_release_minors(unsigned int minor
, unsigned int nr
)
183 unsigned int end
= minor
+ nr
;
185 BUG_ON(end
> nr_minors
);
186 spin_lock(&minor_lock
);
187 for (; minor
< end
; ++minor
)
188 __clear_bit(minor
, minors
);
189 spin_unlock(&minor_lock
);
192 static void blkif_restart_queue_callback(void *arg
)
194 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
195 schedule_work(&info
->work
);
198 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
200 /* We don't have real geometry info, but let's at least return
201 values consistent with the size of the device */
202 sector_t nsect
= get_capacity(bd
->bd_disk
);
203 sector_t cylinders
= nsect
;
207 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
208 hg
->cylinders
= cylinders
;
209 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
210 hg
->cylinders
= 0xffff;
214 static int blkif_ioctl(struct block_device
*bdev
, fmode_t mode
,
215 unsigned command
, unsigned long argument
)
217 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
220 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
221 command
, (long)argument
);
224 case CDROMMULTISESSION
:
225 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
226 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
227 if (put_user(0, (char __user
*)(argument
+ i
)))
231 case CDROM_GET_CAPABILITY
: {
232 struct gendisk
*gd
= info
->gd
;
233 if (gd
->flags
& GENHD_FL_CD
)
239 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
241 return -EINVAL
; /* same return as native Linux */
248 * blkif_queue_request
252 * id: for guest use only.
253 * operation: BLKIF_OP_{READ,WRITE,PROBE}
254 * buffer: buffer to read/write into. this should be a
255 * virtual address in the guest os.
257 static int blkif_queue_request(struct request
*req
)
259 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
260 unsigned long buffer_mfn
;
261 struct blkif_request
*ring_req
;
263 unsigned int fsect
, lsect
;
265 grant_ref_t gref_head
;
266 struct scatterlist
*sg
;
268 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
271 if (gnttab_alloc_grant_references(
272 BLKIF_MAX_SEGMENTS_PER_REQUEST
, &gref_head
) < 0) {
273 gnttab_request_free_callback(
275 blkif_restart_queue_callback
,
277 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
281 /* Fill out a communications ring structure. */
282 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
283 id
= get_id_from_freelist(info
);
284 info
->shadow
[id
].request
= (unsigned long)req
;
287 ring_req
->sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
288 ring_req
->handle
= info
->handle
;
290 ring_req
->operation
= rq_data_dir(req
) ?
291 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
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 void xlvbd_flush(struct blkfront_info
*info
)
422 blk_queue_flush(info
->rq
, info
->feature_flush
);
423 printk(KERN_INFO
"blkfront: %s: barriers %s\n",
425 info
->feature_flush
? "enabled" : "disabled");
429 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
430 struct blkfront_info
*info
,
431 u16 vdisk_info
, u16 sector_size
)
440 BUG_ON(info
->gd
!= NULL
);
441 BUG_ON(info
->rq
!= NULL
);
443 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
444 /* this is above the extended range; something is wrong */
445 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
449 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
450 minor
= BLKIF_MINOR(info
->vdevice
);
451 nr_parts
= PARTS_PER_DISK
;
453 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
454 nr_parts
= PARTS_PER_EXT_DISK
;
457 if ((minor
% nr_parts
) == 0)
458 nr_minors
= nr_parts
;
460 err
= xlbd_reserve_minors(minor
, nr_minors
);
465 gd
= alloc_disk(nr_minors
);
469 offset
= minor
/ nr_parts
;
473 sprintf(gd
->disk_name
, "%s%c", DEV_NAME
, 'a' + offset
);
475 sprintf(gd
->disk_name
, "%s%c%c", DEV_NAME
,
476 'a' + ((offset
/ 26)-1), 'a' + (offset
% 26));
479 sprintf(gd
->disk_name
, "%s%c%d", DEV_NAME
,
481 minor
& (nr_parts
- 1));
483 sprintf(gd
->disk_name
, "%s%c%c%d", DEV_NAME
,
484 'a' + ((offset
/ 26) - 1),
486 minor
& (nr_parts
- 1));
489 gd
->major
= XENVBD_MAJOR
;
490 gd
->first_minor
= minor
;
491 gd
->fops
= &xlvbd_block_fops
;
492 gd
->private_data
= info
;
493 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
494 set_capacity(gd
, capacity
);
496 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
501 info
->rq
= gd
->queue
;
506 if (vdisk_info
& VDISK_READONLY
)
509 if (vdisk_info
& VDISK_REMOVABLE
)
510 gd
->flags
|= GENHD_FL_REMOVABLE
;
512 if (vdisk_info
& VDISK_CDROM
)
513 gd
->flags
|= GENHD_FL_CD
;
518 xlbd_release_minors(minor
, nr_minors
);
523 static void xlvbd_release_gendisk(struct blkfront_info
*info
)
525 unsigned int minor
, nr_minors
;
528 if (info
->rq
== NULL
)
531 spin_lock_irqsave(&blkif_io_lock
, flags
);
533 /* No more blkif_request(). */
534 blk_stop_queue(info
->rq
);
536 /* No more gnttab callback work. */
537 gnttab_cancel_free_callback(&info
->callback
);
538 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
540 /* Flush gnttab callback work. Must be done with no locks held. */
541 flush_scheduled_work();
543 del_gendisk(info
->gd
);
545 minor
= info
->gd
->first_minor
;
546 nr_minors
= info
->gd
->minors
;
547 xlbd_release_minors(minor
, nr_minors
);
549 blk_cleanup_queue(info
->rq
);
556 static void kick_pending_request_queues(struct blkfront_info
*info
)
558 if (!RING_FULL(&info
->ring
)) {
559 /* Re-enable calldowns. */
560 blk_start_queue(info
->rq
);
561 /* Kick things off immediately. */
562 do_blkif_request(info
->rq
);
566 static void blkif_restart_queue(struct work_struct
*work
)
568 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
570 spin_lock_irq(&blkif_io_lock
);
571 if (info
->connected
== BLKIF_STATE_CONNECTED
)
572 kick_pending_request_queues(info
);
573 spin_unlock_irq(&blkif_io_lock
);
576 static void blkif_free(struct blkfront_info
*info
, int suspend
)
578 /* Prevent new requests being issued until we fix things up. */
579 spin_lock_irq(&blkif_io_lock
);
580 info
->connected
= suspend
?
581 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
582 /* No more blkif_request(). */
584 blk_stop_queue(info
->rq
);
585 /* No more gnttab callback work. */
586 gnttab_cancel_free_callback(&info
->callback
);
587 spin_unlock_irq(&blkif_io_lock
);
589 /* Flush gnttab callback work. Must be done with no locks held. */
590 flush_scheduled_work();
592 /* Free resources associated with old device channel. */
593 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
594 gnttab_end_foreign_access(info
->ring_ref
, 0,
595 (unsigned long)info
->ring
.sring
);
596 info
->ring_ref
= GRANT_INVALID_REF
;
597 info
->ring
.sring
= NULL
;
600 unbind_from_irqhandler(info
->irq
, info
);
601 info
->evtchn
= info
->irq
= 0;
605 static void blkif_completion(struct blk_shadow
*s
)
608 for (i
= 0; i
< s
->req
.nr_segments
; i
++)
609 gnttab_end_foreign_access(s
->req
.seg
[i
].gref
, 0, 0UL);
612 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
615 struct blkif_response
*bret
;
618 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
621 spin_lock_irqsave(&blkif_io_lock
, flags
);
623 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
624 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
629 rp
= info
->ring
.sring
->rsp_prod
;
630 rmb(); /* Ensure we see queued responses up to 'rp'. */
632 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
635 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
637 req
= (struct request
*)info
->shadow
[id
].request
;
639 blkif_completion(&info
->shadow
[id
]);
641 add_id_to_freelist(info
, id
);
643 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
644 switch (bret
->operation
) {
645 case BLKIF_OP_WRITE_BARRIER
:
646 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
647 printk(KERN_WARNING
"blkfront: %s: write barrier op failed\n",
648 info
->gd
->disk_name
);
650 info
->feature_flush
= 0;
656 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
657 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
658 "request: %x\n", bret
->status
);
660 __blk_end_request_all(req
, error
);
667 info
->ring
.rsp_cons
= i
;
669 if (i
!= info
->ring
.req_prod_pvt
) {
671 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
675 info
->ring
.sring
->rsp_event
= i
+ 1;
677 kick_pending_request_queues(info
);
679 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
685 static int setup_blkring(struct xenbus_device
*dev
,
686 struct blkfront_info
*info
)
688 struct blkif_sring
*sring
;
691 info
->ring_ref
= GRANT_INVALID_REF
;
693 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
695 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
698 SHARED_RING_INIT(sring
);
699 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
701 sg_init_table(info
->sg
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
703 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
705 free_page((unsigned long)sring
);
706 info
->ring
.sring
= NULL
;
709 info
->ring_ref
= err
;
711 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
715 err
= bind_evtchn_to_irqhandler(info
->evtchn
,
717 IRQF_SAMPLE_RANDOM
, "blkif", info
);
719 xenbus_dev_fatal(dev
, err
,
720 "bind_evtchn_to_irqhandler failed");
732 /* Common code used when first setting up, and when resuming. */
733 static int talk_to_blkback(struct xenbus_device
*dev
,
734 struct blkfront_info
*info
)
736 const char *message
= NULL
;
737 struct xenbus_transaction xbt
;
740 /* Create shared ring, alloc event channel. */
741 err
= setup_blkring(dev
, info
);
746 err
= xenbus_transaction_start(&xbt
);
748 xenbus_dev_fatal(dev
, err
, "starting transaction");
749 goto destroy_blkring
;
752 err
= xenbus_printf(xbt
, dev
->nodename
,
753 "ring-ref", "%u", info
->ring_ref
);
755 message
= "writing ring-ref";
756 goto abort_transaction
;
758 err
= xenbus_printf(xbt
, dev
->nodename
,
759 "event-channel", "%u", info
->evtchn
);
761 message
= "writing event-channel";
762 goto abort_transaction
;
764 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
765 XEN_IO_PROTO_ABI_NATIVE
);
767 message
= "writing protocol";
768 goto abort_transaction
;
771 err
= xenbus_transaction_end(xbt
, 0);
775 xenbus_dev_fatal(dev
, err
, "completing transaction");
776 goto destroy_blkring
;
779 xenbus_switch_state(dev
, XenbusStateInitialised
);
784 xenbus_transaction_end(xbt
, 1);
786 xenbus_dev_fatal(dev
, err
, "%s", message
);
794 * Entry point to this code when a new device is created. Allocate the basic
795 * structures and the ring buffer for communication with the backend, and
796 * inform the backend of the appropriate details for those. Switch to
799 static int blkfront_probe(struct xenbus_device
*dev
,
800 const struct xenbus_device_id
*id
)
803 struct blkfront_info
*info
;
805 /* FIXME: Use dynamic device id if this is not set. */
806 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
807 "virtual-device", "%i", &vdevice
);
809 /* go looking in the extended area instead */
810 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
813 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
818 if (xen_hvm_domain()) {
821 /* no unplug has been done: do not hook devices != xen vbds */
822 if (xen_platform_pci_unplug
& XEN_UNPLUG_UNNECESSARY
) {
825 if (!VDEV_IS_EXTENDED(vdevice
))
826 major
= BLKIF_MAJOR(vdevice
);
828 major
= XENVBD_MAJOR
;
830 if (major
!= XENVBD_MAJOR
) {
832 "%s: HVM does not support vbd %d as xen block device\n",
833 __FUNCTION__
, vdevice
);
837 /* do not create a PV cdrom device if we are an HVM guest */
838 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
841 if (strncmp(type
, "cdrom", 5) == 0) {
847 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
849 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
853 mutex_init(&info
->mutex
);
855 info
->vdevice
= vdevice
;
856 info
->connected
= BLKIF_STATE_DISCONNECTED
;
857 INIT_WORK(&info
->work
, blkif_restart_queue
);
859 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
860 info
->shadow
[i
].req
.id
= i
+1;
861 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
863 /* Front end dir is a number, which is used as the id. */
864 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
865 dev_set_drvdata(&dev
->dev
, info
);
867 err
= talk_to_blkback(dev
, info
);
870 dev_set_drvdata(&dev
->dev
, NULL
);
878 static int blkif_recover(struct blkfront_info
*info
)
881 struct blkif_request
*req
;
882 struct blk_shadow
*copy
;
885 /* Stage 1: Make a safe copy of the shadow state. */
886 copy
= kmalloc(sizeof(info
->shadow
),
887 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
890 memcpy(copy
, info
->shadow
, sizeof(info
->shadow
));
892 /* Stage 2: Set up free list. */
893 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
894 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
895 info
->shadow
[i
].req
.id
= i
+1;
896 info
->shadow_free
= info
->ring
.req_prod_pvt
;
897 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
899 /* Stage 3: Find pending requests and requeue them. */
900 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
902 if (copy
[i
].request
== 0)
905 /* Grab a request slot and copy shadow state into it. */
906 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
909 /* We get a new request id, and must reset the shadow state. */
910 req
->id
= get_id_from_freelist(info
);
911 memcpy(&info
->shadow
[req
->id
], ©
[i
], sizeof(copy
[i
]));
913 /* Rewrite any grant references invalidated by susp/resume. */
914 for (j
= 0; j
< req
->nr_segments
; j
++)
915 gnttab_grant_foreign_access_ref(
917 info
->xbdev
->otherend_id
,
918 pfn_to_mfn(info
->shadow
[req
->id
].frame
[j
]),
921 info
->shadow
[req
->id
].request
));
922 info
->shadow
[req
->id
].req
= *req
;
924 info
->ring
.req_prod_pvt
++;
929 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
931 spin_lock_irq(&blkif_io_lock
);
933 /* Now safe for us to use the shared ring */
934 info
->connected
= BLKIF_STATE_CONNECTED
;
936 /* Send off requeued requests */
937 flush_requests(info
);
939 /* Kick any other new requests queued since we resumed */
940 kick_pending_request_queues(info
);
942 spin_unlock_irq(&blkif_io_lock
);
948 * We are reconnecting to the backend, due to a suspend/resume, or a backend
949 * driver restart. We tear down our blkif structure and recreate it, but
950 * leave the device-layer structures intact so that this is transparent to the
951 * rest of the kernel.
953 static int blkfront_resume(struct xenbus_device
*dev
)
955 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
958 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
960 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
962 err
= talk_to_blkback(dev
, info
);
963 if (info
->connected
== BLKIF_STATE_SUSPENDED
&& !err
)
964 err
= blkif_recover(info
);
970 blkfront_closing(struct blkfront_info
*info
)
972 struct xenbus_device
*xbdev
= info
->xbdev
;
973 struct block_device
*bdev
= NULL
;
975 mutex_lock(&info
->mutex
);
977 if (xbdev
->state
== XenbusStateClosing
) {
978 mutex_unlock(&info
->mutex
);
983 bdev
= bdget_disk(info
->gd
, 0);
985 mutex_unlock(&info
->mutex
);
988 xenbus_frontend_closed(xbdev
);
992 mutex_lock(&bdev
->bd_mutex
);
994 if (bdev
->bd_openers
) {
995 xenbus_dev_error(xbdev
, -EBUSY
,
996 "Device in use; refusing to close");
997 xenbus_switch_state(xbdev
, XenbusStateClosing
);
999 xlvbd_release_gendisk(info
);
1000 xenbus_frontend_closed(xbdev
);
1003 mutex_unlock(&bdev
->bd_mutex
);
1008 * Invoked when the backend is finally 'ready' (and has told produced
1009 * the details about the physical device - #sectors, size, etc).
1011 static void blkfront_connect(struct blkfront_info
*info
)
1013 unsigned long long sectors
;
1014 unsigned long sector_size
;
1019 switch (info
->connected
) {
1020 case BLKIF_STATE_CONNECTED
:
1022 * Potentially, the back-end may be signalling
1023 * a capacity change; update the capacity.
1025 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1026 "sectors", "%Lu", §ors
);
1027 if (XENBUS_EXIST_ERR(err
))
1029 printk(KERN_INFO
"Setting capacity to %Lu\n",
1031 set_capacity(info
->gd
, sectors
);
1032 revalidate_disk(info
->gd
);
1035 case BLKIF_STATE_SUSPENDED
:
1042 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
1043 __func__
, info
->xbdev
->otherend
);
1045 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1046 "sectors", "%llu", §ors
,
1047 "info", "%u", &binfo
,
1048 "sector-size", "%lu", §or_size
,
1051 xenbus_dev_fatal(info
->xbdev
, err
,
1052 "reading backend fields at %s",
1053 info
->xbdev
->otherend
);
1057 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1058 "feature-barrier", "%lu", &barrier
,
1062 * If there's no "feature-barrier" defined, then it means
1063 * we're dealing with a very old backend which writes
1064 * synchronously; nothing to do.
1066 * If there are barriers, then we use flush.
1068 info
->feature_flush
= 0;
1071 * The driver doesn't properly handled empty flushes, so
1072 * lets disable barrier support for now.
1075 if (!err
&& barrier
)
1076 info
->feature_flush
= REQ_FLUSH
;
1079 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
);
1081 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
1082 info
->xbdev
->otherend
);
1086 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1088 /* Kick pending requests. */
1089 spin_lock_irq(&blkif_io_lock
);
1090 info
->connected
= BLKIF_STATE_CONNECTED
;
1091 kick_pending_request_queues(info
);
1092 spin_unlock_irq(&blkif_io_lock
);
1100 * Callback received when the backend's state changes.
1102 static void blkback_changed(struct xenbus_device
*dev
,
1103 enum xenbus_state backend_state
)
1105 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1107 dev_dbg(&dev
->dev
, "blkfront:blkback_changed to state %d.\n", backend_state
);
1109 switch (backend_state
) {
1110 case XenbusStateInitialising
:
1111 case XenbusStateInitWait
:
1112 case XenbusStateInitialised
:
1113 case XenbusStateReconfiguring
:
1114 case XenbusStateReconfigured
:
1115 case XenbusStateUnknown
:
1116 case XenbusStateClosed
:
1119 case XenbusStateConnected
:
1120 blkfront_connect(info
);
1123 case XenbusStateClosing
:
1124 blkfront_closing(info
);
1129 static int blkfront_remove(struct xenbus_device
*xbdev
)
1131 struct blkfront_info
*info
= dev_get_drvdata(&xbdev
->dev
);
1132 struct block_device
*bdev
= NULL
;
1133 struct gendisk
*disk
;
1135 dev_dbg(&xbdev
->dev
, "%s removed", xbdev
->nodename
);
1137 blkif_free(info
, 0);
1139 mutex_lock(&info
->mutex
);
1143 bdev
= bdget_disk(disk
, 0);
1146 mutex_unlock(&info
->mutex
);
1154 * The xbdev was removed before we reached the Closed
1155 * state. See if it's safe to remove the disk. If the bdev
1156 * isn't closed yet, we let release take care of it.
1159 mutex_lock(&bdev
->bd_mutex
);
1160 info
= disk
->private_data
;
1162 dev_warn(disk_to_dev(disk
),
1163 "%s was hot-unplugged, %d stale handles\n",
1164 xbdev
->nodename
, bdev
->bd_openers
);
1166 if (info
&& !bdev
->bd_openers
) {
1167 xlvbd_release_gendisk(info
);
1168 disk
->private_data
= NULL
;
1172 mutex_unlock(&bdev
->bd_mutex
);
1178 static int blkfront_is_ready(struct xenbus_device
*dev
)
1180 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1182 return info
->is_ready
&& info
->xbdev
;
1185 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
1187 struct gendisk
*disk
= bdev
->bd_disk
;
1188 struct blkfront_info
*info
;
1191 mutex_lock(&blkfront_mutex
);
1193 info
= disk
->private_data
;
1200 mutex_lock(&info
->mutex
);
1203 /* xbdev is closed */
1206 mutex_unlock(&info
->mutex
);
1209 mutex_unlock(&blkfront_mutex
);
1213 static int blkif_release(struct gendisk
*disk
, fmode_t mode
)
1215 struct blkfront_info
*info
= disk
->private_data
;
1216 struct block_device
*bdev
;
1217 struct xenbus_device
*xbdev
;
1219 mutex_lock(&blkfront_mutex
);
1221 bdev
= bdget_disk(disk
, 0);
1224 if (bdev
->bd_openers
)
1228 * Check if we have been instructed to close. We will have
1229 * deferred this request, because the bdev was still open.
1232 mutex_lock(&info
->mutex
);
1233 xbdev
= info
->xbdev
;
1235 if (xbdev
&& xbdev
->state
== XenbusStateClosing
) {
1236 /* pending switch to state closed */
1237 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1238 xlvbd_release_gendisk(info
);
1239 xenbus_frontend_closed(info
->xbdev
);
1242 mutex_unlock(&info
->mutex
);
1245 /* sudden device removal */
1246 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1247 xlvbd_release_gendisk(info
);
1248 disk
->private_data
= NULL
;
1253 mutex_unlock(&blkfront_mutex
);
1257 static const struct block_device_operations xlvbd_block_fops
=
1259 .owner
= THIS_MODULE
,
1261 .release
= blkif_release
,
1262 .getgeo
= blkif_getgeo
,
1263 .ioctl
= blkif_ioctl
,
1267 static const struct xenbus_device_id blkfront_ids
[] = {
1272 static struct xenbus_driver blkfront
= {
1274 .owner
= THIS_MODULE
,
1275 .ids
= blkfront_ids
,
1276 .probe
= blkfront_probe
,
1277 .remove
= blkfront_remove
,
1278 .resume
= blkfront_resume
,
1279 .otherend_changed
= blkback_changed
,
1280 .is_ready
= blkfront_is_ready
,
1283 static int __init
xlblk_init(void)
1288 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
1289 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1290 XENVBD_MAJOR
, DEV_NAME
);
1294 return xenbus_register_frontend(&blkfront
);
1296 module_init(xlblk_init
);
1299 static void __exit
xlblk_exit(void)
1301 return xenbus_unregister_driver(&blkfront
);
1303 module_exit(xlblk_exit
);
1305 MODULE_DESCRIPTION("Xen virtual block device frontend");
1306 MODULE_LICENSE("GPL");
1307 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1308 MODULE_ALIAS("xen:vbd");
1309 MODULE_ALIAS("xenblk");