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>
44 #include <xen/xenbus.h>
45 #include <xen/grant_table.h>
46 #include <xen/events.h>
49 #include <xen/interface/grant_table.h>
50 #include <xen/interface/io/blkif.h>
51 #include <xen/interface/io/protocols.h>
53 #include <asm/xen/hypervisor.h>
56 BLKIF_STATE_DISCONNECTED
,
57 BLKIF_STATE_CONNECTED
,
58 BLKIF_STATE_SUSPENDED
,
62 struct blkif_request req
;
63 unsigned long request
;
64 unsigned long frame
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
67 static struct block_device_operations xlvbd_block_fops
;
69 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
72 * We have one of these per vbd, whether ide, scsi or 'other'. They
73 * hang in private_data off the gendisk structure. We may end up
74 * putting all kinds of interesting stuff here :-)
78 struct xenbus_device
*xbdev
;
82 enum blkif_state connected
;
84 struct blkif_front_ring ring
;
85 unsigned int evtchn
, irq
;
86 struct request_queue
*rq
;
87 struct work_struct work
;
88 struct gnttab_free_callback callback
;
89 struct blk_shadow shadow
[BLK_RING_SIZE
];
90 unsigned long shadow_free
;
95 * The number of people holding this device open. We won't allow a
96 * hot-unplug unless this is 0.
101 static DEFINE_SPINLOCK(blkif_io_lock
);
103 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
104 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
105 #define GRANT_INVALID_REF 0
107 #define PARTS_PER_DISK 16
108 #define PARTS_PER_EXT_DISK 256
110 #define BLKIF_MAJOR(dev) ((dev)>>8)
111 #define BLKIF_MINOR(dev) ((dev) & 0xff)
114 #define EXTENDED (1<<EXT_SHIFT)
115 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
116 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
118 #define DEV_NAME "xvd" /* name in /dev */
120 static int get_id_from_freelist(struct blkfront_info
*info
)
122 unsigned long free
= info
->shadow_free
;
123 BUG_ON(free
> BLK_RING_SIZE
);
124 info
->shadow_free
= info
->shadow
[free
].req
.id
;
125 info
->shadow
[free
].req
.id
= 0x0fffffee; /* debug */
129 static void add_id_to_freelist(struct blkfront_info
*info
,
132 info
->shadow
[id
].req
.id
= info
->shadow_free
;
133 info
->shadow
[id
].request
= 0;
134 info
->shadow_free
= id
;
137 static void blkif_restart_queue_callback(void *arg
)
139 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
140 schedule_work(&info
->work
);
143 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
145 /* We don't have real geometry info, but let's at least return
146 values consistent with the size of the device */
147 sector_t nsect
= get_capacity(bd
->bd_disk
);
148 sector_t cylinders
= nsect
;
152 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
153 hg
->cylinders
= cylinders
;
154 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
155 hg
->cylinders
= 0xffff;
159 static int blkif_ioctl(struct inode
*inode
, struct file
*filep
,
160 unsigned command
, unsigned long argument
)
162 struct blkfront_info
*info
=
163 inode
->i_bdev
->bd_disk
->private_data
;
166 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
167 command
, (long)argument
);
170 case CDROMMULTISESSION
:
171 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
172 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
173 if (put_user(0, (char __user
*)(argument
+ i
)))
177 case CDROM_GET_CAPABILITY
: {
178 struct gendisk
*gd
= info
->gd
;
179 if (gd
->flags
& GENHD_FL_CD
)
185 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
187 return -EINVAL
; /* same return as native Linux */
194 * blkif_queue_request
198 * id: for guest use only.
199 * operation: BLKIF_OP_{READ,WRITE,PROBE}
200 * buffer: buffer to read/write into. this should be a
201 * virtual address in the guest os.
203 static int blkif_queue_request(struct request
*req
)
205 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
206 unsigned long buffer_mfn
;
207 struct blkif_request
*ring_req
;
208 struct req_iterator iter
;
209 struct bio_vec
*bvec
;
211 unsigned int fsect
, lsect
;
213 grant_ref_t gref_head
;
215 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
218 if (gnttab_alloc_grant_references(
219 BLKIF_MAX_SEGMENTS_PER_REQUEST
, &gref_head
) < 0) {
220 gnttab_request_free_callback(
222 blkif_restart_queue_callback
,
224 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
228 /* Fill out a communications ring structure. */
229 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
230 id
= get_id_from_freelist(info
);
231 info
->shadow
[id
].request
= (unsigned long)req
;
234 ring_req
->sector_number
= (blkif_sector_t
)req
->sector
;
235 ring_req
->handle
= info
->handle
;
237 ring_req
->operation
= rq_data_dir(req
) ?
238 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
239 if (blk_barrier_rq(req
))
240 ring_req
->operation
= BLKIF_OP_WRITE_BARRIER
;
242 ring_req
->nr_segments
= 0;
243 rq_for_each_segment(bvec
, req
, iter
) {
244 BUG_ON(ring_req
->nr_segments
== BLKIF_MAX_SEGMENTS_PER_REQUEST
);
245 buffer_mfn
= pfn_to_mfn(page_to_pfn(bvec
->bv_page
));
246 fsect
= bvec
->bv_offset
>> 9;
247 lsect
= fsect
+ (bvec
->bv_len
>> 9) - 1;
248 /* install a grant reference. */
249 ref
= gnttab_claim_grant_reference(&gref_head
);
250 BUG_ON(ref
== -ENOSPC
);
252 gnttab_grant_foreign_access_ref(
254 info
->xbdev
->otherend_id
,
258 info
->shadow
[id
].frame
[ring_req
->nr_segments
] =
259 mfn_to_pfn(buffer_mfn
);
261 ring_req
->seg
[ring_req
->nr_segments
] =
262 (struct blkif_request_segment
) {
265 .last_sect
= lsect
};
267 ring_req
->nr_segments
++;
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
= elv_next_request(rq
)) != NULL
) {
306 info
= req
->rq_disk
->private_data
;
307 if (!blk_fs_request(req
)) {
312 if (RING_FULL(&info
->ring
))
315 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
316 "(%u/%li) buffer:%p [%s]\n",
317 req
, req
->cmd
, (unsigned long)req
->sector
,
318 req
->current_nr_sectors
,
319 req
->nr_sectors
, req
->buffer
,
320 rq_data_dir(req
) ? "write" : "read");
323 blkdev_dequeue_request(req
);
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 elevator_init(rq
, "noop");
349 /* Hard sector size and max sectors impersonate the equiv. hardware. */
350 blk_queue_hardsect_size(rq
, sector_size
);
351 blk_queue_max_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_phys_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
359 blk_queue_max_hw_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
361 /* Make sure buffer addresses are sector-aligned. */
362 blk_queue_dma_alignment(rq
, 511);
364 /* Make sure we don't use bounce buffers. */
365 blk_queue_bounce_limit(rq
, BLK_BOUNCE_ANY
);
373 static int xlvbd_barrier(struct blkfront_info
*info
)
377 err
= blk_queue_ordered(info
->rq
,
378 info
->feature_barrier
? QUEUE_ORDERED_DRAIN
: QUEUE_ORDERED_NONE
,
384 printk(KERN_INFO
"blkfront: %s: barriers %s\n",
386 info
->feature_barrier
? "enabled" : "disabled");
391 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
392 struct blkfront_info
*info
,
393 u16 vdisk_info
, u16 sector_size
)
402 BUG_ON(info
->gd
!= NULL
);
403 BUG_ON(info
->rq
!= NULL
);
405 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
406 /* this is above the extended range; something is wrong */
407 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
411 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
412 minor
= BLKIF_MINOR(info
->vdevice
);
413 nr_parts
= PARTS_PER_DISK
;
415 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
416 nr_parts
= PARTS_PER_EXT_DISK
;
419 if ((minor
% nr_parts
) == 0)
420 nr_minors
= nr_parts
;
422 gd
= alloc_disk(nr_minors
);
426 offset
= minor
/ nr_parts
;
430 sprintf(gd
->disk_name
, "%s%c", DEV_NAME
, 'a' + offset
);
432 sprintf(gd
->disk_name
, "%s%c%c", DEV_NAME
,
433 'a' + ((offset
/ 26)-1), 'a' + (offset
% 26));
436 sprintf(gd
->disk_name
, "%s%c%d", DEV_NAME
,
438 minor
& (nr_parts
- 1));
440 sprintf(gd
->disk_name
, "%s%c%c%d", DEV_NAME
,
441 'a' + ((offset
/ 26) - 1),
443 minor
& (nr_parts
- 1));
446 gd
->major
= XENVBD_MAJOR
;
447 gd
->first_minor
= minor
;
448 gd
->fops
= &xlvbd_block_fops
;
449 gd
->private_data
= info
;
450 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
451 set_capacity(gd
, capacity
);
453 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
458 info
->rq
= gd
->queue
;
461 if (info
->feature_barrier
)
464 if (vdisk_info
& VDISK_READONLY
)
467 if (vdisk_info
& VDISK_REMOVABLE
)
468 gd
->flags
|= GENHD_FL_REMOVABLE
;
470 if (vdisk_info
& VDISK_CDROM
)
471 gd
->flags
|= GENHD_FL_CD
;
479 static void kick_pending_request_queues(struct blkfront_info
*info
)
481 if (!RING_FULL(&info
->ring
)) {
482 /* Re-enable calldowns. */
483 blk_start_queue(info
->rq
);
484 /* Kick things off immediately. */
485 do_blkif_request(info
->rq
);
489 static void blkif_restart_queue(struct work_struct
*work
)
491 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
493 spin_lock_irq(&blkif_io_lock
);
494 if (info
->connected
== BLKIF_STATE_CONNECTED
)
495 kick_pending_request_queues(info
);
496 spin_unlock_irq(&blkif_io_lock
);
499 static void blkif_free(struct blkfront_info
*info
, int suspend
)
501 /* Prevent new requests being issued until we fix things up. */
502 spin_lock_irq(&blkif_io_lock
);
503 info
->connected
= suspend
?
504 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
505 /* No more blkif_request(). */
507 blk_stop_queue(info
->rq
);
508 /* No more gnttab callback work. */
509 gnttab_cancel_free_callback(&info
->callback
);
510 spin_unlock_irq(&blkif_io_lock
);
512 /* Flush gnttab callback work. Must be done with no locks held. */
513 flush_scheduled_work();
515 /* Free resources associated with old device channel. */
516 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
517 gnttab_end_foreign_access(info
->ring_ref
, 0,
518 (unsigned long)info
->ring
.sring
);
519 info
->ring_ref
= GRANT_INVALID_REF
;
520 info
->ring
.sring
= NULL
;
523 unbind_from_irqhandler(info
->irq
, info
);
524 info
->evtchn
= info
->irq
= 0;
528 static void blkif_completion(struct blk_shadow
*s
)
531 for (i
= 0; i
< s
->req
.nr_segments
; i
++)
532 gnttab_end_foreign_access(s
->req
.seg
[i
].gref
, 0, 0UL);
535 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
538 struct blkif_response
*bret
;
541 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
544 spin_lock_irqsave(&blkif_io_lock
, flags
);
546 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
547 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
552 rp
= info
->ring
.sring
->rsp_prod
;
553 rmb(); /* Ensure we see queued responses up to 'rp'. */
555 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
559 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
561 req
= (struct request
*)info
->shadow
[id
].request
;
563 blkif_completion(&info
->shadow
[id
]);
565 add_id_to_freelist(info
, id
);
567 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
568 switch (bret
->operation
) {
569 case BLKIF_OP_WRITE_BARRIER
:
570 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
571 printk(KERN_WARNING
"blkfront: %s: write barrier op failed\n",
572 info
->gd
->disk_name
);
574 info
->feature_barrier
= 0;
580 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
581 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
582 "request: %x\n", bret
->status
);
584 ret
= __blk_end_request(req
, error
, blk_rq_bytes(req
));
592 info
->ring
.rsp_cons
= i
;
594 if (i
!= info
->ring
.req_prod_pvt
) {
596 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
600 info
->ring
.sring
->rsp_event
= i
+ 1;
602 kick_pending_request_queues(info
);
604 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
610 static int setup_blkring(struct xenbus_device
*dev
,
611 struct blkfront_info
*info
)
613 struct blkif_sring
*sring
;
616 info
->ring_ref
= GRANT_INVALID_REF
;
618 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
620 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
623 SHARED_RING_INIT(sring
);
624 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
626 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
628 free_page((unsigned long)sring
);
629 info
->ring
.sring
= NULL
;
632 info
->ring_ref
= err
;
634 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
638 err
= bind_evtchn_to_irqhandler(info
->evtchn
,
640 IRQF_SAMPLE_RANDOM
, "blkif", info
);
642 xenbus_dev_fatal(dev
, err
,
643 "bind_evtchn_to_irqhandler failed");
655 /* Common code used when first setting up, and when resuming. */
656 static int talk_to_backend(struct xenbus_device
*dev
,
657 struct blkfront_info
*info
)
659 const char *message
= NULL
;
660 struct xenbus_transaction xbt
;
663 /* Create shared ring, alloc event channel. */
664 err
= setup_blkring(dev
, info
);
669 err
= xenbus_transaction_start(&xbt
);
671 xenbus_dev_fatal(dev
, err
, "starting transaction");
672 goto destroy_blkring
;
675 err
= xenbus_printf(xbt
, dev
->nodename
,
676 "ring-ref", "%u", info
->ring_ref
);
678 message
= "writing ring-ref";
679 goto abort_transaction
;
681 err
= xenbus_printf(xbt
, dev
->nodename
,
682 "event-channel", "%u", info
->evtchn
);
684 message
= "writing event-channel";
685 goto abort_transaction
;
687 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
688 XEN_IO_PROTO_ABI_NATIVE
);
690 message
= "writing protocol";
691 goto abort_transaction
;
694 err
= xenbus_transaction_end(xbt
, 0);
698 xenbus_dev_fatal(dev
, err
, "completing transaction");
699 goto destroy_blkring
;
702 xenbus_switch_state(dev
, XenbusStateInitialised
);
707 xenbus_transaction_end(xbt
, 1);
709 xenbus_dev_fatal(dev
, err
, "%s", message
);
718 * Entry point to this code when a new device is created. Allocate the basic
719 * structures and the ring buffer for communication with the backend, and
720 * inform the backend of the appropriate details for those. Switch to
723 static int blkfront_probe(struct xenbus_device
*dev
,
724 const struct xenbus_device_id
*id
)
727 struct blkfront_info
*info
;
729 /* FIXME: Use dynamic device id if this is not set. */
730 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
731 "virtual-device", "%i", &vdevice
);
733 /* go looking in the extended area instead */
734 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
737 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
742 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
744 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
749 info
->vdevice
= vdevice
;
750 info
->connected
= BLKIF_STATE_DISCONNECTED
;
751 INIT_WORK(&info
->work
, blkif_restart_queue
);
753 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
754 info
->shadow
[i
].req
.id
= i
+1;
755 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
757 /* Front end dir is a number, which is used as the id. */
758 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
759 dev
->dev
.driver_data
= info
;
761 err
= talk_to_backend(dev
, info
);
764 dev
->dev
.driver_data
= NULL
;
772 static int blkif_recover(struct blkfront_info
*info
)
775 struct blkif_request
*req
;
776 struct blk_shadow
*copy
;
779 /* Stage 1: Make a safe copy of the shadow state. */
780 copy
= kmalloc(sizeof(info
->shadow
),
781 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
784 memcpy(copy
, info
->shadow
, sizeof(info
->shadow
));
786 /* Stage 2: Set up free list. */
787 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
788 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
789 info
->shadow
[i
].req
.id
= i
+1;
790 info
->shadow_free
= info
->ring
.req_prod_pvt
;
791 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
793 /* Stage 3: Find pending requests and requeue them. */
794 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
796 if (copy
[i
].request
== 0)
799 /* Grab a request slot and copy shadow state into it. */
800 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
803 /* We get a new request id, and must reset the shadow state. */
804 req
->id
= get_id_from_freelist(info
);
805 memcpy(&info
->shadow
[req
->id
], ©
[i
], sizeof(copy
[i
]));
807 /* Rewrite any grant references invalidated by susp/resume. */
808 for (j
= 0; j
< req
->nr_segments
; j
++)
809 gnttab_grant_foreign_access_ref(
811 info
->xbdev
->otherend_id
,
812 pfn_to_mfn(info
->shadow
[req
->id
].frame
[j
]),
815 info
->shadow
[req
->id
].request
));
816 info
->shadow
[req
->id
].req
= *req
;
818 info
->ring
.req_prod_pvt
++;
823 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
825 spin_lock_irq(&blkif_io_lock
);
827 /* Now safe for us to use the shared ring */
828 info
->connected
= BLKIF_STATE_CONNECTED
;
830 /* Send off requeued requests */
831 flush_requests(info
);
833 /* Kick any other new requests queued since we resumed */
834 kick_pending_request_queues(info
);
836 spin_unlock_irq(&blkif_io_lock
);
842 * We are reconnecting to the backend, due to a suspend/resume, or a backend
843 * driver restart. We tear down our blkif structure and recreate it, but
844 * leave the device-layer structures intact so that this is transparent to the
845 * rest of the kernel.
847 static int blkfront_resume(struct xenbus_device
*dev
)
849 struct blkfront_info
*info
= dev
->dev
.driver_data
;
852 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
854 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
856 err
= talk_to_backend(dev
, info
);
857 if (info
->connected
== BLKIF_STATE_SUSPENDED
&& !err
)
858 err
= blkif_recover(info
);
865 * Invoked when the backend is finally 'ready' (and has told produced
866 * the details about the physical device - #sectors, size, etc).
868 static void blkfront_connect(struct blkfront_info
*info
)
870 unsigned long long sectors
;
871 unsigned long sector_size
;
875 if ((info
->connected
== BLKIF_STATE_CONNECTED
) ||
876 (info
->connected
== BLKIF_STATE_SUSPENDED
) )
879 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
880 __func__
, info
->xbdev
->otherend
);
882 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
883 "sectors", "%llu", §ors
,
884 "info", "%u", &binfo
,
885 "sector-size", "%lu", §or_size
,
888 xenbus_dev_fatal(info
->xbdev
, err
,
889 "reading backend fields at %s",
890 info
->xbdev
->otherend
);
894 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
895 "feature-barrier", "%lu", &info
->feature_barrier
,
898 info
->feature_barrier
= 0;
900 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
);
902 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
903 info
->xbdev
->otherend
);
907 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
909 /* Kick pending requests. */
910 spin_lock_irq(&blkif_io_lock
);
911 info
->connected
= BLKIF_STATE_CONNECTED
;
912 kick_pending_request_queues(info
);
913 spin_unlock_irq(&blkif_io_lock
);
921 * Handle the change of state of the backend to Closing. We must delete our
922 * device-layer structures now, to ensure that writes are flushed through to
923 * the backend. Once is this done, we can switch to Closed in
926 static void blkfront_closing(struct xenbus_device
*dev
)
928 struct blkfront_info
*info
= dev
->dev
.driver_data
;
931 dev_dbg(&dev
->dev
, "blkfront_closing: %s removed\n", dev
->nodename
);
933 if (info
->rq
== NULL
)
936 spin_lock_irqsave(&blkif_io_lock
, flags
);
938 del_gendisk(info
->gd
);
940 /* No more blkif_request(). */
941 blk_stop_queue(info
->rq
);
943 /* No more gnttab callback work. */
944 gnttab_cancel_free_callback(&info
->callback
);
945 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
947 /* Flush gnttab callback work. Must be done with no locks held. */
948 flush_scheduled_work();
950 blk_cleanup_queue(info
->rq
);
954 xenbus_frontend_closed(dev
);
958 * Callback received when the backend's state changes.
960 static void backend_changed(struct xenbus_device
*dev
,
961 enum xenbus_state backend_state
)
963 struct blkfront_info
*info
= dev
->dev
.driver_data
;
964 struct block_device
*bd
;
966 dev_dbg(&dev
->dev
, "blkfront:backend_changed.\n");
968 switch (backend_state
) {
969 case XenbusStateInitialising
:
970 case XenbusStateInitWait
:
971 case XenbusStateInitialised
:
972 case XenbusStateUnknown
:
973 case XenbusStateClosed
:
976 case XenbusStateConnected
:
977 blkfront_connect(info
);
980 case XenbusStateClosing
:
981 bd
= bdget_disk(info
->gd
, 0);
983 xenbus_dev_fatal(dev
, -ENODEV
, "bdget failed");
985 mutex_lock(&bd
->bd_mutex
);
987 xenbus_dev_error(dev
, -EBUSY
,
988 "Device in use; refusing to close");
990 blkfront_closing(dev
);
991 mutex_unlock(&bd
->bd_mutex
);
997 static int blkfront_remove(struct xenbus_device
*dev
)
999 struct blkfront_info
*info
= dev
->dev
.driver_data
;
1001 dev_dbg(&dev
->dev
, "blkfront_remove: %s removed\n", dev
->nodename
);
1003 blkif_free(info
, 0);
1010 static int blkfront_is_ready(struct xenbus_device
*dev
)
1012 struct blkfront_info
*info
= dev
->dev
.driver_data
;
1014 return info
->is_ready
;
1017 static int blkif_open(struct inode
*inode
, struct file
*filep
)
1019 struct blkfront_info
*info
= inode
->i_bdev
->bd_disk
->private_data
;
1024 static int blkif_release(struct inode
*inode
, struct file
*filep
)
1026 struct blkfront_info
*info
= inode
->i_bdev
->bd_disk
->private_data
;
1028 if (info
->users
== 0) {
1029 /* Check whether we have been instructed to close. We will
1030 have ignored this request initially, as the device was
1032 struct xenbus_device
*dev
= info
->xbdev
;
1033 enum xenbus_state state
= xenbus_read_driver_state(dev
->otherend
);
1035 if (state
== XenbusStateClosing
&& info
->is_ready
)
1036 blkfront_closing(dev
);
1041 static struct block_device_operations xlvbd_block_fops
=
1043 .owner
= THIS_MODULE
,
1045 .release
= blkif_release
,
1046 .getgeo
= blkif_getgeo
,
1047 .ioctl
= blkif_ioctl
,
1051 static struct xenbus_device_id blkfront_ids
[] = {
1056 static struct xenbus_driver blkfront
= {
1058 .owner
= THIS_MODULE
,
1059 .ids
= blkfront_ids
,
1060 .probe
= blkfront_probe
,
1061 .remove
= blkfront_remove
,
1062 .resume
= blkfront_resume
,
1063 .otherend_changed
= backend_changed
,
1064 .is_ready
= blkfront_is_ready
,
1067 static int __init
xlblk_init(void)
1072 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
1073 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1074 XENVBD_MAJOR
, DEV_NAME
);
1078 return xenbus_register_frontend(&blkfront
);
1080 module_init(xlblk_init
);
1083 static void __exit
xlblk_exit(void)
1085 return xenbus_unregister_driver(&blkfront
);
1087 module_exit(xlblk_exit
);
1089 MODULE_DESCRIPTION("Xen virtual block device frontend");
1090 MODULE_LICENSE("GPL");
1091 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1092 MODULE_ALIAS("xen:vbd");
1093 MODULE_ALIAS("xenblk");