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
;
292 if (req
->cmd_flags
& REQ_HARDBARRIER
)
293 ring_req
->operation
= BLKIF_OP_WRITE_BARRIER
;
295 ring_req
->nr_segments
= blk_rq_map_sg(req
->q
, req
, info
->sg
);
296 BUG_ON(ring_req
->nr_segments
> BLKIF_MAX_SEGMENTS_PER_REQUEST
);
298 for_each_sg(info
->sg
, sg
, ring_req
->nr_segments
, i
) {
299 buffer_mfn
= pfn_to_mfn(page_to_pfn(sg_page(sg
)));
300 fsect
= sg
->offset
>> 9;
301 lsect
= fsect
+ (sg
->length
>> 9) - 1;
302 /* install a grant reference. */
303 ref
= gnttab_claim_grant_reference(&gref_head
);
304 BUG_ON(ref
== -ENOSPC
);
306 gnttab_grant_foreign_access_ref(
308 info
->xbdev
->otherend_id
,
312 info
->shadow
[id
].frame
[i
] = mfn_to_pfn(buffer_mfn
);
314 (struct blkif_request_segment
) {
317 .last_sect
= lsect
};
320 info
->ring
.req_prod_pvt
++;
322 /* Keep a private copy so we can reissue requests when recovering. */
323 info
->shadow
[id
].req
= *ring_req
;
325 gnttab_free_grant_references(gref_head
);
331 static inline void flush_requests(struct blkfront_info
*info
)
335 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info
->ring
, notify
);
338 notify_remote_via_irq(info
->irq
);
343 * read a block; request is in a request queue
345 static void do_blkif_request(struct request_queue
*rq
)
347 struct blkfront_info
*info
= NULL
;
351 pr_debug("Entered do_blkif_request\n");
355 while ((req
= blk_peek_request(rq
)) != NULL
) {
356 info
= req
->rq_disk
->private_data
;
358 if (RING_FULL(&info
->ring
))
361 blk_start_request(req
);
363 if (req
->cmd_type
!= REQ_TYPE_FS
) {
364 __blk_end_request_all(req
, -EIO
);
368 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
369 "(%u/%u) buffer:%p [%s]\n",
370 req
, req
->cmd
, (unsigned long)blk_rq_pos(req
),
371 blk_rq_cur_sectors(req
), blk_rq_sectors(req
),
372 req
->buffer
, rq_data_dir(req
) ? "write" : "read");
374 if (blkif_queue_request(req
)) {
375 blk_requeue_request(rq
, req
);
377 /* Avoid pointless unplugs. */
386 flush_requests(info
);
389 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
)
391 struct request_queue
*rq
;
393 rq
= blk_init_queue(do_blkif_request
, &blkif_io_lock
);
397 queue_flag_set_unlocked(QUEUE_FLAG_VIRT
, rq
);
399 /* Hard sector size and max sectors impersonate the equiv. hardware. */
400 blk_queue_logical_block_size(rq
, sector_size
);
401 blk_queue_max_hw_sectors(rq
, 512);
403 /* Each segment in a request is up to an aligned page in size. */
404 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
405 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
407 /* Ensure a merged request will fit in a single I/O ring slot. */
408 blk_queue_max_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
410 /* Make sure buffer addresses are sector-aligned. */
411 blk_queue_dma_alignment(rq
, 511);
413 /* Make sure we don't use bounce buffers. */
414 blk_queue_bounce_limit(rq
, BLK_BOUNCE_ANY
);
422 static void xlvbd_flush(struct blkfront_info
*info
)
424 blk_queue_flush(info
->rq
, info
->feature_flush
);
425 printk(KERN_INFO
"blkfront: %s: barriers %s\n",
427 info
->feature_flush
? "enabled" : "disabled");
431 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
432 struct blkfront_info
*info
,
433 u16 vdisk_info
, u16 sector_size
)
442 BUG_ON(info
->gd
!= NULL
);
443 BUG_ON(info
->rq
!= NULL
);
445 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
446 /* this is above the extended range; something is wrong */
447 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
451 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
452 minor
= BLKIF_MINOR(info
->vdevice
);
453 nr_parts
= PARTS_PER_DISK
;
455 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
456 nr_parts
= PARTS_PER_EXT_DISK
;
459 if ((minor
% nr_parts
) == 0)
460 nr_minors
= nr_parts
;
462 err
= xlbd_reserve_minors(minor
, nr_minors
);
467 gd
= alloc_disk(nr_minors
);
471 offset
= minor
/ nr_parts
;
475 sprintf(gd
->disk_name
, "%s%c", DEV_NAME
, 'a' + offset
);
477 sprintf(gd
->disk_name
, "%s%c%c", DEV_NAME
,
478 'a' + ((offset
/ 26)-1), 'a' + (offset
% 26));
481 sprintf(gd
->disk_name
, "%s%c%d", DEV_NAME
,
483 minor
& (nr_parts
- 1));
485 sprintf(gd
->disk_name
, "%s%c%c%d", DEV_NAME
,
486 'a' + ((offset
/ 26) - 1),
488 minor
& (nr_parts
- 1));
491 gd
->major
= XENVBD_MAJOR
;
492 gd
->first_minor
= minor
;
493 gd
->fops
= &xlvbd_block_fops
;
494 gd
->private_data
= info
;
495 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
496 set_capacity(gd
, capacity
);
498 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
503 info
->rq
= gd
->queue
;
508 if (vdisk_info
& VDISK_READONLY
)
511 if (vdisk_info
& VDISK_REMOVABLE
)
512 gd
->flags
|= GENHD_FL_REMOVABLE
;
514 if (vdisk_info
& VDISK_CDROM
)
515 gd
->flags
|= GENHD_FL_CD
;
520 xlbd_release_minors(minor
, nr_minors
);
525 static void xlvbd_release_gendisk(struct blkfront_info
*info
)
527 unsigned int minor
, nr_minors
;
530 if (info
->rq
== NULL
)
533 spin_lock_irqsave(&blkif_io_lock
, flags
);
535 /* No more blkif_request(). */
536 blk_stop_queue(info
->rq
);
538 /* No more gnttab callback work. */
539 gnttab_cancel_free_callback(&info
->callback
);
540 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
542 /* Flush gnttab callback work. Must be done with no locks held. */
543 flush_scheduled_work();
545 del_gendisk(info
->gd
);
547 minor
= info
->gd
->first_minor
;
548 nr_minors
= info
->gd
->minors
;
549 xlbd_release_minors(minor
, nr_minors
);
551 blk_cleanup_queue(info
->rq
);
558 static void kick_pending_request_queues(struct blkfront_info
*info
)
560 if (!RING_FULL(&info
->ring
)) {
561 /* Re-enable calldowns. */
562 blk_start_queue(info
->rq
);
563 /* Kick things off immediately. */
564 do_blkif_request(info
->rq
);
568 static void blkif_restart_queue(struct work_struct
*work
)
570 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
572 spin_lock_irq(&blkif_io_lock
);
573 if (info
->connected
== BLKIF_STATE_CONNECTED
)
574 kick_pending_request_queues(info
);
575 spin_unlock_irq(&blkif_io_lock
);
578 static void blkif_free(struct blkfront_info
*info
, int suspend
)
580 /* Prevent new requests being issued until we fix things up. */
581 spin_lock_irq(&blkif_io_lock
);
582 info
->connected
= suspend
?
583 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
584 /* No more blkif_request(). */
586 blk_stop_queue(info
->rq
);
587 /* No more gnttab callback work. */
588 gnttab_cancel_free_callback(&info
->callback
);
589 spin_unlock_irq(&blkif_io_lock
);
591 /* Flush gnttab callback work. Must be done with no locks held. */
592 flush_scheduled_work();
594 /* Free resources associated with old device channel. */
595 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
596 gnttab_end_foreign_access(info
->ring_ref
, 0,
597 (unsigned long)info
->ring
.sring
);
598 info
->ring_ref
= GRANT_INVALID_REF
;
599 info
->ring
.sring
= NULL
;
602 unbind_from_irqhandler(info
->irq
, info
);
603 info
->evtchn
= info
->irq
= 0;
607 static void blkif_completion(struct blk_shadow
*s
)
610 for (i
= 0; i
< s
->req
.nr_segments
; i
++)
611 gnttab_end_foreign_access(s
->req
.seg
[i
].gref
, 0, 0UL);
614 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
617 struct blkif_response
*bret
;
620 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
623 spin_lock_irqsave(&blkif_io_lock
, flags
);
625 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
626 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
631 rp
= info
->ring
.sring
->rsp_prod
;
632 rmb(); /* Ensure we see queued responses up to 'rp'. */
634 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
637 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
639 req
= (struct request
*)info
->shadow
[id
].request
;
641 blkif_completion(&info
->shadow
[id
]);
643 add_id_to_freelist(info
, id
);
645 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
646 switch (bret
->operation
) {
647 case BLKIF_OP_WRITE_BARRIER
:
648 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
649 printk(KERN_WARNING
"blkfront: %s: write barrier op failed\n",
650 info
->gd
->disk_name
);
652 info
->feature_flush
= 0;
658 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
659 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
660 "request: %x\n", bret
->status
);
662 __blk_end_request_all(req
, error
);
669 info
->ring
.rsp_cons
= i
;
671 if (i
!= info
->ring
.req_prod_pvt
) {
673 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
677 info
->ring
.sring
->rsp_event
= i
+ 1;
679 kick_pending_request_queues(info
);
681 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
687 static int setup_blkring(struct xenbus_device
*dev
,
688 struct blkfront_info
*info
)
690 struct blkif_sring
*sring
;
693 info
->ring_ref
= GRANT_INVALID_REF
;
695 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
697 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
700 SHARED_RING_INIT(sring
);
701 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
703 sg_init_table(info
->sg
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
705 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
707 free_page((unsigned long)sring
);
708 info
->ring
.sring
= NULL
;
711 info
->ring_ref
= err
;
713 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
717 err
= bind_evtchn_to_irqhandler(info
->evtchn
,
719 IRQF_SAMPLE_RANDOM
, "blkif", info
);
721 xenbus_dev_fatal(dev
, err
,
722 "bind_evtchn_to_irqhandler failed");
734 /* Common code used when first setting up, and when resuming. */
735 static int talk_to_blkback(struct xenbus_device
*dev
,
736 struct blkfront_info
*info
)
738 const char *message
= NULL
;
739 struct xenbus_transaction xbt
;
742 /* Create shared ring, alloc event channel. */
743 err
= setup_blkring(dev
, info
);
748 err
= xenbus_transaction_start(&xbt
);
750 xenbus_dev_fatal(dev
, err
, "starting transaction");
751 goto destroy_blkring
;
754 err
= xenbus_printf(xbt
, dev
->nodename
,
755 "ring-ref", "%u", info
->ring_ref
);
757 message
= "writing ring-ref";
758 goto abort_transaction
;
760 err
= xenbus_printf(xbt
, dev
->nodename
,
761 "event-channel", "%u", info
->evtchn
);
763 message
= "writing event-channel";
764 goto abort_transaction
;
766 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
767 XEN_IO_PROTO_ABI_NATIVE
);
769 message
= "writing protocol";
770 goto abort_transaction
;
773 err
= xenbus_transaction_end(xbt
, 0);
777 xenbus_dev_fatal(dev
, err
, "completing transaction");
778 goto destroy_blkring
;
781 xenbus_switch_state(dev
, XenbusStateInitialised
);
786 xenbus_transaction_end(xbt
, 1);
788 xenbus_dev_fatal(dev
, err
, "%s", message
);
796 * Entry point to this code when a new device is created. Allocate the basic
797 * structures and the ring buffer for communication with the backend, and
798 * inform the backend of the appropriate details for those. Switch to
801 static int blkfront_probe(struct xenbus_device
*dev
,
802 const struct xenbus_device_id
*id
)
805 struct blkfront_info
*info
;
807 /* FIXME: Use dynamic device id if this is not set. */
808 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
809 "virtual-device", "%i", &vdevice
);
811 /* go looking in the extended area instead */
812 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
815 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
820 if (xen_hvm_domain()) {
823 /* no unplug has been done: do not hook devices != xen vbds */
824 if (xen_platform_pci_unplug
& XEN_UNPLUG_UNNECESSARY
) {
827 if (!VDEV_IS_EXTENDED(vdevice
))
828 major
= BLKIF_MAJOR(vdevice
);
830 major
= XENVBD_MAJOR
;
832 if (major
!= XENVBD_MAJOR
) {
834 "%s: HVM does not support vbd %d as xen block device\n",
835 __FUNCTION__
, vdevice
);
839 /* do not create a PV cdrom device if we are an HVM guest */
840 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
843 if (strncmp(type
, "cdrom", 5) == 0) {
849 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
851 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
855 mutex_init(&info
->mutex
);
857 info
->vdevice
= vdevice
;
858 info
->connected
= BLKIF_STATE_DISCONNECTED
;
859 INIT_WORK(&info
->work
, blkif_restart_queue
);
861 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
862 info
->shadow
[i
].req
.id
= i
+1;
863 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
865 /* Front end dir is a number, which is used as the id. */
866 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
867 dev_set_drvdata(&dev
->dev
, info
);
869 err
= talk_to_blkback(dev
, info
);
872 dev_set_drvdata(&dev
->dev
, NULL
);
880 static int blkif_recover(struct blkfront_info
*info
)
883 struct blkif_request
*req
;
884 struct blk_shadow
*copy
;
887 /* Stage 1: Make a safe copy of the shadow state. */
888 copy
= kmalloc(sizeof(info
->shadow
),
889 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
892 memcpy(copy
, info
->shadow
, sizeof(info
->shadow
));
894 /* Stage 2: Set up free list. */
895 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
896 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
897 info
->shadow
[i
].req
.id
= i
+1;
898 info
->shadow_free
= info
->ring
.req_prod_pvt
;
899 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
901 /* Stage 3: Find pending requests and requeue them. */
902 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
904 if (copy
[i
].request
== 0)
907 /* Grab a request slot and copy shadow state into it. */
908 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
911 /* We get a new request id, and must reset the shadow state. */
912 req
->id
= get_id_from_freelist(info
);
913 memcpy(&info
->shadow
[req
->id
], ©
[i
], sizeof(copy
[i
]));
915 /* Rewrite any grant references invalidated by susp/resume. */
916 for (j
= 0; j
< req
->nr_segments
; j
++)
917 gnttab_grant_foreign_access_ref(
919 info
->xbdev
->otherend_id
,
920 pfn_to_mfn(info
->shadow
[req
->id
].frame
[j
]),
923 info
->shadow
[req
->id
].request
));
924 info
->shadow
[req
->id
].req
= *req
;
926 info
->ring
.req_prod_pvt
++;
931 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
933 spin_lock_irq(&blkif_io_lock
);
935 /* Now safe for us to use the shared ring */
936 info
->connected
= BLKIF_STATE_CONNECTED
;
938 /* Send off requeued requests */
939 flush_requests(info
);
941 /* Kick any other new requests queued since we resumed */
942 kick_pending_request_queues(info
);
944 spin_unlock_irq(&blkif_io_lock
);
950 * We are reconnecting to the backend, due to a suspend/resume, or a backend
951 * driver restart. We tear down our blkif structure and recreate it, but
952 * leave the device-layer structures intact so that this is transparent to the
953 * rest of the kernel.
955 static int blkfront_resume(struct xenbus_device
*dev
)
957 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
960 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
962 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
964 err
= talk_to_blkback(dev
, info
);
965 if (info
->connected
== BLKIF_STATE_SUSPENDED
&& !err
)
966 err
= blkif_recover(info
);
972 blkfront_closing(struct blkfront_info
*info
)
974 struct xenbus_device
*xbdev
= info
->xbdev
;
975 struct block_device
*bdev
= NULL
;
977 mutex_lock(&info
->mutex
);
979 if (xbdev
->state
== XenbusStateClosing
) {
980 mutex_unlock(&info
->mutex
);
985 bdev
= bdget_disk(info
->gd
, 0);
987 mutex_unlock(&info
->mutex
);
990 xenbus_frontend_closed(xbdev
);
994 mutex_lock(&bdev
->bd_mutex
);
996 if (bdev
->bd_openers
) {
997 xenbus_dev_error(xbdev
, -EBUSY
,
998 "Device in use; refusing to close");
999 xenbus_switch_state(xbdev
, XenbusStateClosing
);
1001 xlvbd_release_gendisk(info
);
1002 xenbus_frontend_closed(xbdev
);
1005 mutex_unlock(&bdev
->bd_mutex
);
1010 * Invoked when the backend is finally 'ready' (and has told produced
1011 * the details about the physical device - #sectors, size, etc).
1013 static void blkfront_connect(struct blkfront_info
*info
)
1015 unsigned long long sectors
;
1016 unsigned long sector_size
;
1021 switch (info
->connected
) {
1022 case BLKIF_STATE_CONNECTED
:
1024 * Potentially, the back-end may be signalling
1025 * a capacity change; update the capacity.
1027 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1028 "sectors", "%Lu", §ors
);
1029 if (XENBUS_EXIST_ERR(err
))
1031 printk(KERN_INFO
"Setting capacity to %Lu\n",
1033 set_capacity(info
->gd
, sectors
);
1034 revalidate_disk(info
->gd
);
1037 case BLKIF_STATE_SUSPENDED
:
1044 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
1045 __func__
, info
->xbdev
->otherend
);
1047 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1048 "sectors", "%llu", §ors
,
1049 "info", "%u", &binfo
,
1050 "sector-size", "%lu", §or_size
,
1053 xenbus_dev_fatal(info
->xbdev
, err
,
1054 "reading backend fields at %s",
1055 info
->xbdev
->otherend
);
1059 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1060 "feature-barrier", "%lu", &barrier
,
1064 * If there's no "feature-barrier" defined, then it means
1065 * we're dealing with a very old backend which writes
1066 * synchronously; nothing to do.
1068 * If there are barriers, then we use flush.
1070 info
->feature_flush
= 0;
1073 * The driver doesn't properly handled empty flushes, so
1074 * lets disable barrier support for now.
1077 if (!err
&& barrier
)
1078 info
->feature_flush
= REQ_FLUSH
;
1081 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
);
1083 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
1084 info
->xbdev
->otherend
);
1088 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1090 /* Kick pending requests. */
1091 spin_lock_irq(&blkif_io_lock
);
1092 info
->connected
= BLKIF_STATE_CONNECTED
;
1093 kick_pending_request_queues(info
);
1094 spin_unlock_irq(&blkif_io_lock
);
1102 * Callback received when the backend's state changes.
1104 static void blkback_changed(struct xenbus_device
*dev
,
1105 enum xenbus_state backend_state
)
1107 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1109 dev_dbg(&dev
->dev
, "blkfront:blkback_changed to state %d.\n", backend_state
);
1111 switch (backend_state
) {
1112 case XenbusStateInitialising
:
1113 case XenbusStateInitWait
:
1114 case XenbusStateInitialised
:
1115 case XenbusStateReconfiguring
:
1116 case XenbusStateReconfigured
:
1117 case XenbusStateUnknown
:
1118 case XenbusStateClosed
:
1121 case XenbusStateConnected
:
1122 blkfront_connect(info
);
1125 case XenbusStateClosing
:
1126 blkfront_closing(info
);
1131 static int blkfront_remove(struct xenbus_device
*xbdev
)
1133 struct blkfront_info
*info
= dev_get_drvdata(&xbdev
->dev
);
1134 struct block_device
*bdev
= NULL
;
1135 struct gendisk
*disk
;
1137 dev_dbg(&xbdev
->dev
, "%s removed", xbdev
->nodename
);
1139 blkif_free(info
, 0);
1141 mutex_lock(&info
->mutex
);
1145 bdev
= bdget_disk(disk
, 0);
1148 mutex_unlock(&info
->mutex
);
1156 * The xbdev was removed before we reached the Closed
1157 * state. See if it's safe to remove the disk. If the bdev
1158 * isn't closed yet, we let release take care of it.
1161 mutex_lock(&bdev
->bd_mutex
);
1162 info
= disk
->private_data
;
1164 dev_warn(disk_to_dev(disk
),
1165 "%s was hot-unplugged, %d stale handles\n",
1166 xbdev
->nodename
, bdev
->bd_openers
);
1168 if (info
&& !bdev
->bd_openers
) {
1169 xlvbd_release_gendisk(info
);
1170 disk
->private_data
= NULL
;
1174 mutex_unlock(&bdev
->bd_mutex
);
1180 static int blkfront_is_ready(struct xenbus_device
*dev
)
1182 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1184 return info
->is_ready
&& info
->xbdev
;
1187 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
1189 struct gendisk
*disk
= bdev
->bd_disk
;
1190 struct blkfront_info
*info
;
1193 mutex_lock(&blkfront_mutex
);
1195 info
= disk
->private_data
;
1202 mutex_lock(&info
->mutex
);
1205 /* xbdev is closed */
1208 mutex_unlock(&info
->mutex
);
1211 mutex_unlock(&blkfront_mutex
);
1215 static int blkif_release(struct gendisk
*disk
, fmode_t mode
)
1217 struct blkfront_info
*info
= disk
->private_data
;
1218 struct block_device
*bdev
;
1219 struct xenbus_device
*xbdev
;
1221 mutex_lock(&blkfront_mutex
);
1223 bdev
= bdget_disk(disk
, 0);
1226 if (bdev
->bd_openers
)
1230 * Check if we have been instructed to close. We will have
1231 * deferred this request, because the bdev was still open.
1234 mutex_lock(&info
->mutex
);
1235 xbdev
= info
->xbdev
;
1237 if (xbdev
&& xbdev
->state
== XenbusStateClosing
) {
1238 /* pending switch to state closed */
1239 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1240 xlvbd_release_gendisk(info
);
1241 xenbus_frontend_closed(info
->xbdev
);
1244 mutex_unlock(&info
->mutex
);
1247 /* sudden device removal */
1248 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1249 xlvbd_release_gendisk(info
);
1250 disk
->private_data
= NULL
;
1255 mutex_unlock(&blkfront_mutex
);
1259 static const struct block_device_operations xlvbd_block_fops
=
1261 .owner
= THIS_MODULE
,
1263 .release
= blkif_release
,
1264 .getgeo
= blkif_getgeo
,
1265 .ioctl
= blkif_ioctl
,
1269 static const struct xenbus_device_id blkfront_ids
[] = {
1274 static struct xenbus_driver blkfront
= {
1276 .owner
= THIS_MODULE
,
1277 .ids
= blkfront_ids
,
1278 .probe
= blkfront_probe
,
1279 .remove
= blkfront_remove
,
1280 .resume
= blkfront_resume
,
1281 .otherend_changed
= blkback_changed
,
1282 .is_ready
= blkfront_is_ready
,
1285 static int __init
xlblk_init(void)
1290 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
1291 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1292 XENVBD_MAJOR
, DEV_NAME
);
1296 return xenbus_register_frontend(&blkfront
);
1298 module_init(xlblk_init
);
1301 static void __exit
xlblk_exit(void)
1303 return xenbus_unregister_driver(&blkfront
);
1305 module_exit(xlblk_exit
);
1307 MODULE_DESCRIPTION("Xen virtual block device frontend");
1308 MODULE_LICENSE("GPL");
1309 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1310 MODULE_ALIAS("xen:vbd");
1311 MODULE_ALIAS("xenblk");