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/module.h>
43 #include <xen/xenbus.h>
44 #include <xen/grant_table.h>
45 #include <xen/events.h>
48 #include <xen/interface/grant_table.h>
49 #include <xen/interface/io/blkif.h>
50 #include <xen/interface/io/protocols.h>
52 #include <asm/xen/hypervisor.h>
55 BLKIF_STATE_DISCONNECTED
,
56 BLKIF_STATE_CONNECTED
,
57 BLKIF_STATE_SUSPENDED
,
61 struct blkif_request req
;
62 unsigned long request
;
63 unsigned long frame
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
66 static struct block_device_operations xlvbd_block_fops
;
68 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
71 * We have one of these per vbd, whether ide, scsi or 'other'. They
72 * hang in private_data off the gendisk structure. We may end up
73 * putting all kinds of interesting stuff here :-)
77 struct xenbus_device
*xbdev
;
81 enum blkif_state connected
;
83 struct blkif_front_ring ring
;
84 unsigned int evtchn
, irq
;
85 struct request_queue
*rq
;
86 struct work_struct work
;
87 struct gnttab_free_callback callback
;
88 struct blk_shadow shadow
[BLK_RING_SIZE
];
89 unsigned long shadow_free
;
94 * The number of people holding this device open. We won't allow a
95 * hot-unplug unless this is 0.
100 static DEFINE_SPINLOCK(blkif_io_lock
);
102 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
103 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
104 #define GRANT_INVALID_REF 0
106 #define PARTS_PER_DISK 16
108 #define BLKIF_MAJOR(dev) ((dev)>>8)
109 #define BLKIF_MINOR(dev) ((dev) & 0xff)
111 #define DEV_NAME "xvd" /* name in /dev */
113 /* Information about our VBDs. */
115 static LIST_HEAD(vbds_list
);
117 static int get_id_from_freelist(struct blkfront_info
*info
)
119 unsigned long free
= info
->shadow_free
;
120 BUG_ON(free
> BLK_RING_SIZE
);
121 info
->shadow_free
= info
->shadow
[free
].req
.id
;
122 info
->shadow
[free
].req
.id
= 0x0fffffee; /* debug */
126 static void add_id_to_freelist(struct blkfront_info
*info
,
129 info
->shadow
[id
].req
.id
= info
->shadow_free
;
130 info
->shadow
[id
].request
= 0;
131 info
->shadow_free
= id
;
134 static void blkif_restart_queue_callback(void *arg
)
136 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
137 schedule_work(&info
->work
);
140 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
142 /* We don't have real geometry info, but let's at least return
143 values consistent with the size of the device */
144 sector_t nsect
= get_capacity(bd
->bd_disk
);
145 sector_t cylinders
= nsect
;
149 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
150 hg
->cylinders
= cylinders
;
151 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
152 hg
->cylinders
= 0xffff;
157 * blkif_queue_request
161 * id: for guest use only.
162 * operation: BLKIF_OP_{READ,WRITE,PROBE}
163 * buffer: buffer to read/write into. this should be a
164 * virtual address in the guest os.
166 static int blkif_queue_request(struct request
*req
)
168 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
169 unsigned long buffer_mfn
;
170 struct blkif_request
*ring_req
;
171 struct req_iterator iter
;
172 struct bio_vec
*bvec
;
174 unsigned int fsect
, lsect
;
176 grant_ref_t gref_head
;
178 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
181 if (gnttab_alloc_grant_references(
182 BLKIF_MAX_SEGMENTS_PER_REQUEST
, &gref_head
) < 0) {
183 gnttab_request_free_callback(
185 blkif_restart_queue_callback
,
187 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
191 /* Fill out a communications ring structure. */
192 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
193 id
= get_id_from_freelist(info
);
194 info
->shadow
[id
].request
= (unsigned long)req
;
197 ring_req
->sector_number
= (blkif_sector_t
)req
->sector
;
198 ring_req
->handle
= info
->handle
;
200 ring_req
->operation
= rq_data_dir(req
) ?
201 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
202 if (blk_barrier_rq(req
))
203 ring_req
->operation
= BLKIF_OP_WRITE_BARRIER
;
205 ring_req
->nr_segments
= 0;
206 rq_for_each_segment(bvec
, req
, iter
) {
207 BUG_ON(ring_req
->nr_segments
== BLKIF_MAX_SEGMENTS_PER_REQUEST
);
208 buffer_mfn
= pfn_to_mfn(page_to_pfn(bvec
->bv_page
));
209 fsect
= bvec
->bv_offset
>> 9;
210 lsect
= fsect
+ (bvec
->bv_len
>> 9) - 1;
211 /* install a grant reference. */
212 ref
= gnttab_claim_grant_reference(&gref_head
);
213 BUG_ON(ref
== -ENOSPC
);
215 gnttab_grant_foreign_access_ref(
217 info
->xbdev
->otherend_id
,
221 info
->shadow
[id
].frame
[ring_req
->nr_segments
] =
222 mfn_to_pfn(buffer_mfn
);
224 ring_req
->seg
[ring_req
->nr_segments
] =
225 (struct blkif_request_segment
) {
228 .last_sect
= lsect
};
230 ring_req
->nr_segments
++;
233 info
->ring
.req_prod_pvt
++;
235 /* Keep a private copy so we can reissue requests when recovering. */
236 info
->shadow
[id
].req
= *ring_req
;
238 gnttab_free_grant_references(gref_head
);
244 static inline void flush_requests(struct blkfront_info
*info
)
248 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info
->ring
, notify
);
251 notify_remote_via_irq(info
->irq
);
256 * read a block; request is in a request queue
258 static void do_blkif_request(struct request_queue
*rq
)
260 struct blkfront_info
*info
= NULL
;
264 pr_debug("Entered do_blkif_request\n");
268 while ((req
= elv_next_request(rq
)) != NULL
) {
269 info
= req
->rq_disk
->private_data
;
270 if (!blk_fs_request(req
)) {
275 if (RING_FULL(&info
->ring
))
278 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
279 "(%u/%li) buffer:%p [%s]\n",
280 req
, req
->cmd
, (unsigned long)req
->sector
,
281 req
->current_nr_sectors
,
282 req
->nr_sectors
, req
->buffer
,
283 rq_data_dir(req
) ? "write" : "read");
286 blkdev_dequeue_request(req
);
287 if (blkif_queue_request(req
)) {
288 blk_requeue_request(rq
, req
);
290 /* Avoid pointless unplugs. */
299 flush_requests(info
);
302 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
)
304 struct request_queue
*rq
;
306 rq
= blk_init_queue(do_blkif_request
, &blkif_io_lock
);
310 elevator_init(rq
, "noop");
312 /* Hard sector size and max sectors impersonate the equiv. hardware. */
313 blk_queue_hardsect_size(rq
, sector_size
);
314 blk_queue_max_sectors(rq
, 512);
316 /* Each segment in a request is up to an aligned page in size. */
317 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
318 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
320 /* Ensure a merged request will fit in a single I/O ring slot. */
321 blk_queue_max_phys_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
322 blk_queue_max_hw_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
324 /* Make sure buffer addresses are sector-aligned. */
325 blk_queue_dma_alignment(rq
, 511);
333 static int xlvbd_barrier(struct blkfront_info
*info
)
337 err
= blk_queue_ordered(info
->rq
,
338 info
->feature_barrier
? QUEUE_ORDERED_DRAIN
: QUEUE_ORDERED_NONE
,
344 printk(KERN_INFO
"blkfront: %s: barriers %s\n",
346 info
->feature_barrier
? "enabled" : "disabled");
351 static int xlvbd_alloc_gendisk(int minor
, blkif_sector_t capacity
,
352 int vdevice
, u16 vdisk_info
, u16 sector_size
,
353 struct blkfront_info
*info
)
359 BUG_ON(info
->gd
!= NULL
);
360 BUG_ON(info
->rq
!= NULL
);
362 if ((minor
% PARTS_PER_DISK
) == 0)
363 nr_minors
= PARTS_PER_DISK
;
365 gd
= alloc_disk(nr_minors
);
370 sprintf(gd
->disk_name
, "%s%c", DEV_NAME
,
371 'a' + minor
/ PARTS_PER_DISK
);
373 sprintf(gd
->disk_name
, "%s%c%d", DEV_NAME
,
374 'a' + minor
/ PARTS_PER_DISK
,
375 minor
% PARTS_PER_DISK
);
377 gd
->major
= XENVBD_MAJOR
;
378 gd
->first_minor
= minor
;
379 gd
->fops
= &xlvbd_block_fops
;
380 gd
->private_data
= info
;
381 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
382 set_capacity(gd
, capacity
);
384 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
389 info
->rq
= gd
->queue
;
392 if (info
->feature_barrier
)
395 if (vdisk_info
& VDISK_READONLY
)
398 if (vdisk_info
& VDISK_REMOVABLE
)
399 gd
->flags
|= GENHD_FL_REMOVABLE
;
401 if (vdisk_info
& VDISK_CDROM
)
402 gd
->flags
|= GENHD_FL_CD
;
410 static void kick_pending_request_queues(struct blkfront_info
*info
)
412 if (!RING_FULL(&info
->ring
)) {
413 /* Re-enable calldowns. */
414 blk_start_queue(info
->rq
);
415 /* Kick things off immediately. */
416 do_blkif_request(info
->rq
);
420 static void blkif_restart_queue(struct work_struct
*work
)
422 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
424 spin_lock_irq(&blkif_io_lock
);
425 if (info
->connected
== BLKIF_STATE_CONNECTED
)
426 kick_pending_request_queues(info
);
427 spin_unlock_irq(&blkif_io_lock
);
430 static void blkif_free(struct blkfront_info
*info
, int suspend
)
432 /* Prevent new requests being issued until we fix things up. */
433 spin_lock_irq(&blkif_io_lock
);
434 info
->connected
= suspend
?
435 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
436 /* No more blkif_request(). */
438 blk_stop_queue(info
->rq
);
439 /* No more gnttab callback work. */
440 gnttab_cancel_free_callback(&info
->callback
);
441 spin_unlock_irq(&blkif_io_lock
);
443 /* Flush gnttab callback work. Must be done with no locks held. */
444 flush_scheduled_work();
446 /* Free resources associated with old device channel. */
447 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
448 gnttab_end_foreign_access(info
->ring_ref
, 0,
449 (unsigned long)info
->ring
.sring
);
450 info
->ring_ref
= GRANT_INVALID_REF
;
451 info
->ring
.sring
= NULL
;
454 unbind_from_irqhandler(info
->irq
, info
);
455 info
->evtchn
= info
->irq
= 0;
459 static void blkif_completion(struct blk_shadow
*s
)
462 for (i
= 0; i
< s
->req
.nr_segments
; i
++)
463 gnttab_end_foreign_access(s
->req
.seg
[i
].gref
, 0, 0UL);
466 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
469 struct blkif_response
*bret
;
472 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
475 spin_lock_irqsave(&blkif_io_lock
, flags
);
477 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
478 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
483 rp
= info
->ring
.sring
->rsp_prod
;
484 rmb(); /* Ensure we see queued responses up to 'rp'. */
486 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
490 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
492 req
= (struct request
*)info
->shadow
[id
].request
;
494 blkif_completion(&info
->shadow
[id
]);
496 add_id_to_freelist(info
, id
);
498 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
499 switch (bret
->operation
) {
500 case BLKIF_OP_WRITE_BARRIER
:
501 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
502 printk(KERN_WARNING
"blkfront: %s: write barrier op failed\n",
503 info
->gd
->disk_name
);
505 info
->feature_barrier
= 0;
511 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
512 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
513 "request: %x\n", bret
->status
);
515 ret
= __blk_end_request(req
, error
, blk_rq_bytes(req
));
523 info
->ring
.rsp_cons
= i
;
525 if (i
!= info
->ring
.req_prod_pvt
) {
527 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
531 info
->ring
.sring
->rsp_event
= i
+ 1;
533 kick_pending_request_queues(info
);
535 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
541 static int setup_blkring(struct xenbus_device
*dev
,
542 struct blkfront_info
*info
)
544 struct blkif_sring
*sring
;
547 info
->ring_ref
= GRANT_INVALID_REF
;
549 sring
= (struct blkif_sring
*)__get_free_page(GFP_KERNEL
);
551 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
554 SHARED_RING_INIT(sring
);
555 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
557 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
559 free_page((unsigned long)sring
);
560 info
->ring
.sring
= NULL
;
563 info
->ring_ref
= err
;
565 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
569 err
= bind_evtchn_to_irqhandler(info
->evtchn
,
571 IRQF_SAMPLE_RANDOM
, "blkif", info
);
573 xenbus_dev_fatal(dev
, err
,
574 "bind_evtchn_to_irqhandler failed");
586 /* Common code used when first setting up, and when resuming. */
587 static int talk_to_backend(struct xenbus_device
*dev
,
588 struct blkfront_info
*info
)
590 const char *message
= NULL
;
591 struct xenbus_transaction xbt
;
594 /* Create shared ring, alloc event channel. */
595 err
= setup_blkring(dev
, info
);
600 err
= xenbus_transaction_start(&xbt
);
602 xenbus_dev_fatal(dev
, err
, "starting transaction");
603 goto destroy_blkring
;
606 err
= xenbus_printf(xbt
, dev
->nodename
,
607 "ring-ref", "%u", info
->ring_ref
);
609 message
= "writing ring-ref";
610 goto abort_transaction
;
612 err
= xenbus_printf(xbt
, dev
->nodename
,
613 "event-channel", "%u", info
->evtchn
);
615 message
= "writing event-channel";
616 goto abort_transaction
;
618 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
619 XEN_IO_PROTO_ABI_NATIVE
);
621 message
= "writing protocol";
622 goto abort_transaction
;
625 err
= xenbus_transaction_end(xbt
, 0);
629 xenbus_dev_fatal(dev
, err
, "completing transaction");
630 goto destroy_blkring
;
633 xenbus_switch_state(dev
, XenbusStateInitialised
);
638 xenbus_transaction_end(xbt
, 1);
640 xenbus_dev_fatal(dev
, err
, "%s", message
);
649 * Entry point to this code when a new device is created. Allocate the basic
650 * structures and the ring buffer for communication with the backend, and
651 * inform the backend of the appropriate details for those. Switch to
654 static int blkfront_probe(struct xenbus_device
*dev
,
655 const struct xenbus_device_id
*id
)
658 struct blkfront_info
*info
;
660 /* FIXME: Use dynamic device id if this is not set. */
661 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
662 "virtual-device", "%i", &vdevice
);
664 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
668 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
670 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
675 info
->vdevice
= vdevice
;
676 info
->connected
= BLKIF_STATE_DISCONNECTED
;
677 INIT_WORK(&info
->work
, blkif_restart_queue
);
679 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
680 info
->shadow
[i
].req
.id
= i
+1;
681 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
683 /* Front end dir is a number, which is used as the id. */
684 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
685 dev
->dev
.driver_data
= info
;
687 err
= talk_to_backend(dev
, info
);
690 dev
->dev
.driver_data
= NULL
;
698 static int blkif_recover(struct blkfront_info
*info
)
701 struct blkif_request
*req
;
702 struct blk_shadow
*copy
;
705 /* Stage 1: Make a safe copy of the shadow state. */
706 copy
= kmalloc(sizeof(info
->shadow
), GFP_KERNEL
);
709 memcpy(copy
, info
->shadow
, sizeof(info
->shadow
));
711 /* Stage 2: Set up free list. */
712 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
713 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
714 info
->shadow
[i
].req
.id
= i
+1;
715 info
->shadow_free
= info
->ring
.req_prod_pvt
;
716 info
->shadow
[BLK_RING_SIZE
-1].req
.id
= 0x0fffffff;
718 /* Stage 3: Find pending requests and requeue them. */
719 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
721 if (copy
[i
].request
== 0)
724 /* Grab a request slot and copy shadow state into it. */
725 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
728 /* We get a new request id, and must reset the shadow state. */
729 req
->id
= get_id_from_freelist(info
);
730 memcpy(&info
->shadow
[req
->id
], ©
[i
], sizeof(copy
[i
]));
732 /* Rewrite any grant references invalidated by susp/resume. */
733 for (j
= 0; j
< req
->nr_segments
; j
++)
734 gnttab_grant_foreign_access_ref(
736 info
->xbdev
->otherend_id
,
737 pfn_to_mfn(info
->shadow
[req
->id
].frame
[j
]),
740 info
->shadow
[req
->id
].request
));
741 info
->shadow
[req
->id
].req
= *req
;
743 info
->ring
.req_prod_pvt
++;
748 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
750 spin_lock_irq(&blkif_io_lock
);
752 /* Now safe for us to use the shared ring */
753 info
->connected
= BLKIF_STATE_CONNECTED
;
755 /* Send off requeued requests */
756 flush_requests(info
);
758 /* Kick any other new requests queued since we resumed */
759 kick_pending_request_queues(info
);
761 spin_unlock_irq(&blkif_io_lock
);
767 * We are reconnecting to the backend, due to a suspend/resume, or a backend
768 * driver restart. We tear down our blkif structure and recreate it, but
769 * leave the device-layer structures intact so that this is transparent to the
770 * rest of the kernel.
772 static int blkfront_resume(struct xenbus_device
*dev
)
774 struct blkfront_info
*info
= dev
->dev
.driver_data
;
777 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
779 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
781 err
= talk_to_backend(dev
, info
);
782 if (info
->connected
== BLKIF_STATE_SUSPENDED
&& !err
)
783 err
= blkif_recover(info
);
790 * Invoked when the backend is finally 'ready' (and has told produced
791 * the details about the physical device - #sectors, size, etc).
793 static void blkfront_connect(struct blkfront_info
*info
)
795 unsigned long long sectors
;
796 unsigned long sector_size
;
800 if ((info
->connected
== BLKIF_STATE_CONNECTED
) ||
801 (info
->connected
== BLKIF_STATE_SUSPENDED
) )
804 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
805 __func__
, info
->xbdev
->otherend
);
807 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
808 "sectors", "%llu", §ors
,
809 "info", "%u", &binfo
,
810 "sector-size", "%lu", §or_size
,
813 xenbus_dev_fatal(info
->xbdev
, err
,
814 "reading backend fields at %s",
815 info
->xbdev
->otherend
);
819 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
820 "feature-barrier", "%lu", &info
->feature_barrier
,
823 info
->feature_barrier
= 0;
825 err
= xlvbd_alloc_gendisk(BLKIF_MINOR(info
->vdevice
),
826 sectors
, info
->vdevice
,
827 binfo
, sector_size
, info
);
829 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
830 info
->xbdev
->otherend
);
834 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
836 /* Kick pending requests. */
837 spin_lock_irq(&blkif_io_lock
);
838 info
->connected
= BLKIF_STATE_CONNECTED
;
839 kick_pending_request_queues(info
);
840 spin_unlock_irq(&blkif_io_lock
);
848 * Handle the change of state of the backend to Closing. We must delete our
849 * device-layer structures now, to ensure that writes are flushed through to
850 * the backend. Once is this done, we can switch to Closed in
853 static void blkfront_closing(struct xenbus_device
*dev
)
855 struct blkfront_info
*info
= dev
->dev
.driver_data
;
858 dev_dbg(&dev
->dev
, "blkfront_closing: %s removed\n", dev
->nodename
);
860 if (info
->rq
== NULL
)
863 spin_lock_irqsave(&blkif_io_lock
, flags
);
865 del_gendisk(info
->gd
);
867 /* No more blkif_request(). */
868 blk_stop_queue(info
->rq
);
870 /* No more gnttab callback work. */
871 gnttab_cancel_free_callback(&info
->callback
);
872 spin_unlock_irqrestore(&blkif_io_lock
, flags
);
874 /* Flush gnttab callback work. Must be done with no locks held. */
875 flush_scheduled_work();
877 blk_cleanup_queue(info
->rq
);
881 xenbus_frontend_closed(dev
);
885 * Callback received when the backend's state changes.
887 static void backend_changed(struct xenbus_device
*dev
,
888 enum xenbus_state backend_state
)
890 struct blkfront_info
*info
= dev
->dev
.driver_data
;
891 struct block_device
*bd
;
893 dev_dbg(&dev
->dev
, "blkfront:backend_changed.\n");
895 switch (backend_state
) {
896 case XenbusStateInitialising
:
897 case XenbusStateInitWait
:
898 case XenbusStateInitialised
:
899 case XenbusStateUnknown
:
900 case XenbusStateClosed
:
903 case XenbusStateConnected
:
904 blkfront_connect(info
);
907 case XenbusStateClosing
:
908 bd
= bdget_disk(info
->gd
, 0);
910 xenbus_dev_fatal(dev
, -ENODEV
, "bdget failed");
912 mutex_lock(&bd
->bd_mutex
);
914 xenbus_dev_error(dev
, -EBUSY
,
915 "Device in use; refusing to close");
917 blkfront_closing(dev
);
918 mutex_unlock(&bd
->bd_mutex
);
924 static int blkfront_remove(struct xenbus_device
*dev
)
926 struct blkfront_info
*info
= dev
->dev
.driver_data
;
928 dev_dbg(&dev
->dev
, "blkfront_remove: %s removed\n", dev
->nodename
);
937 static int blkfront_is_ready(struct xenbus_device
*dev
)
939 struct blkfront_info
*info
= dev
->dev
.driver_data
;
941 return info
->is_ready
;
944 static int blkif_open(struct inode
*inode
, struct file
*filep
)
946 struct blkfront_info
*info
= inode
->i_bdev
->bd_disk
->private_data
;
951 static int blkif_release(struct inode
*inode
, struct file
*filep
)
953 struct blkfront_info
*info
= inode
->i_bdev
->bd_disk
->private_data
;
955 if (info
->users
== 0) {
956 /* Check whether we have been instructed to close. We will
957 have ignored this request initially, as the device was
959 struct xenbus_device
*dev
= info
->xbdev
;
960 enum xenbus_state state
= xenbus_read_driver_state(dev
->otherend
);
962 if (state
== XenbusStateClosing
)
963 blkfront_closing(dev
);
968 static struct block_device_operations xlvbd_block_fops
=
970 .owner
= THIS_MODULE
,
972 .release
= blkif_release
,
973 .getgeo
= blkif_getgeo
,
977 static struct xenbus_device_id blkfront_ids
[] = {
982 static struct xenbus_driver blkfront
= {
984 .owner
= THIS_MODULE
,
986 .probe
= blkfront_probe
,
987 .remove
= blkfront_remove
,
988 .resume
= blkfront_resume
,
989 .otherend_changed
= backend_changed
,
990 .is_ready
= blkfront_is_ready
,
993 static int __init
xlblk_init(void)
995 if (!is_running_on_xen())
998 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
999 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1000 XENVBD_MAJOR
, DEV_NAME
);
1004 return xenbus_register_frontend(&blkfront
);
1006 module_init(xlblk_init
);
1009 static void xlblk_exit(void)
1011 return xenbus_unregister_driver(&blkfront
);
1013 module_exit(xlblk_exit
);
1015 MODULE_DESCRIPTION("Xen virtual block device frontend");
1016 MODULE_LICENSE("GPL");
1017 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1018 MODULE_ALIAS("xen:vbd");
1019 MODULE_ALIAS("xenblk");