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>
46 #include <linux/bitmap.h>
47 #include <linux/list.h>
50 #include <xen/xenbus.h>
51 #include <xen/grant_table.h>
52 #include <xen/events.h>
54 #include <xen/platform_pci.h>
56 #include <xen/interface/grant_table.h>
57 #include <xen/interface/io/blkif.h>
58 #include <xen/interface/io/protocols.h>
60 #include <asm/xen/hypervisor.h>
63 BLKIF_STATE_DISCONNECTED
,
64 BLKIF_STATE_CONNECTED
,
65 BLKIF_STATE_SUSPENDED
,
71 struct list_head node
;
75 struct blkif_request req
;
76 struct request
*request
;
77 struct grant
*grants_used
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
78 struct scatterlist sg
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
81 static DEFINE_MUTEX(blkfront_mutex
);
82 static const struct block_device_operations xlvbd_block_fops
;
84 #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
87 * We have one of these per vbd, whether ide, scsi or 'other'. They
88 * hang in private_data off the gendisk structure. We may end up
89 * putting all kinds of interesting stuff here :-)
95 struct xenbus_device
*xbdev
;
99 enum blkif_state connected
;
101 struct blkif_front_ring ring
;
102 unsigned int evtchn
, irq
;
103 struct request_queue
*rq
;
104 struct work_struct work
;
105 struct gnttab_free_callback callback
;
106 struct blk_shadow shadow
[BLK_RING_SIZE
];
107 struct list_head grants
;
108 unsigned int persistent_gnts_c
;
109 unsigned long shadow_free
;
110 unsigned int feature_flush
;
111 unsigned int flush_op
;
112 unsigned int feature_discard
:1;
113 unsigned int feature_secdiscard
:1;
114 unsigned int discard_granularity
;
115 unsigned int discard_alignment
;
116 unsigned int feature_persistent
:1;
120 static unsigned int nr_minors
;
121 static unsigned long *minors
;
122 static DEFINE_SPINLOCK(minor_lock
);
124 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
125 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
126 #define GRANT_INVALID_REF 0
128 #define PARTS_PER_DISK 16
129 #define PARTS_PER_EXT_DISK 256
131 #define BLKIF_MAJOR(dev) ((dev)>>8)
132 #define BLKIF_MINOR(dev) ((dev) & 0xff)
135 #define EXTENDED (1<<EXT_SHIFT)
136 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
137 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
138 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
139 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
140 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
141 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
143 #define DEV_NAME "xvd" /* name in /dev */
145 static int get_id_from_freelist(struct blkfront_info
*info
)
147 unsigned long free
= info
->shadow_free
;
148 BUG_ON(free
>= BLK_RING_SIZE
);
149 info
->shadow_free
= info
->shadow
[free
].req
.u
.rw
.id
;
150 info
->shadow
[free
].req
.u
.rw
.id
= 0x0fffffee; /* debug */
154 static int add_id_to_freelist(struct blkfront_info
*info
,
157 if (info
->shadow
[id
].req
.u
.rw
.id
!= id
)
159 if (info
->shadow
[id
].request
== NULL
)
161 info
->shadow
[id
].req
.u
.rw
.id
= info
->shadow_free
;
162 info
->shadow
[id
].request
= NULL
;
163 info
->shadow_free
= id
;
167 static int fill_grant_buffer(struct blkfront_info
*info
, int num
)
169 struct page
*granted_page
;
170 struct grant
*gnt_list_entry
, *n
;
174 gnt_list_entry
= kzalloc(sizeof(struct grant
), GFP_NOIO
);
178 if (info
->feature_persistent
) {
179 granted_page
= alloc_page(GFP_NOIO
);
181 kfree(gnt_list_entry
);
184 gnt_list_entry
->pfn
= page_to_pfn(granted_page
);
187 gnt_list_entry
->gref
= GRANT_INVALID_REF
;
188 list_add(&gnt_list_entry
->node
, &info
->grants
);
195 list_for_each_entry_safe(gnt_list_entry
, n
,
196 &info
->grants
, node
) {
197 list_del(&gnt_list_entry
->node
);
198 if (info
->feature_persistent
)
199 __free_page(pfn_to_page(gnt_list_entry
->pfn
));
200 kfree(gnt_list_entry
);
207 static struct grant
*get_grant(grant_ref_t
*gref_head
,
209 struct blkfront_info
*info
)
211 struct grant
*gnt_list_entry
;
212 unsigned long buffer_mfn
;
214 BUG_ON(list_empty(&info
->grants
));
215 gnt_list_entry
= list_first_entry(&info
->grants
, struct grant
, node
);
216 list_del(&gnt_list_entry
->node
);
218 if (gnt_list_entry
->gref
!= GRANT_INVALID_REF
) {
219 info
->persistent_gnts_c
--;
220 return gnt_list_entry
;
223 /* Assign a gref to this page */
224 gnt_list_entry
->gref
= gnttab_claim_grant_reference(gref_head
);
225 BUG_ON(gnt_list_entry
->gref
== -ENOSPC
);
226 if (!info
->feature_persistent
) {
228 gnt_list_entry
->pfn
= pfn
;
230 buffer_mfn
= pfn_to_mfn(gnt_list_entry
->pfn
);
231 gnttab_grant_foreign_access_ref(gnt_list_entry
->gref
,
232 info
->xbdev
->otherend_id
,
234 return gnt_list_entry
;
237 static const char *op_name(int op
)
239 static const char *const names
[] = {
240 [BLKIF_OP_READ
] = "read",
241 [BLKIF_OP_WRITE
] = "write",
242 [BLKIF_OP_WRITE_BARRIER
] = "barrier",
243 [BLKIF_OP_FLUSH_DISKCACHE
] = "flush",
244 [BLKIF_OP_DISCARD
] = "discard" };
246 if (op
< 0 || op
>= ARRAY_SIZE(names
))
254 static int xlbd_reserve_minors(unsigned int minor
, unsigned int nr
)
256 unsigned int end
= minor
+ nr
;
259 if (end
> nr_minors
) {
260 unsigned long *bitmap
, *old
;
262 bitmap
= kcalloc(BITS_TO_LONGS(end
), sizeof(*bitmap
),
267 spin_lock(&minor_lock
);
268 if (end
> nr_minors
) {
270 memcpy(bitmap
, minors
,
271 BITS_TO_LONGS(nr_minors
) * sizeof(*bitmap
));
273 nr_minors
= BITS_TO_LONGS(end
) * BITS_PER_LONG
;
276 spin_unlock(&minor_lock
);
280 spin_lock(&minor_lock
);
281 if (find_next_bit(minors
, end
, minor
) >= end
) {
282 bitmap_set(minors
, minor
, nr
);
286 spin_unlock(&minor_lock
);
291 static void xlbd_release_minors(unsigned int minor
, unsigned int nr
)
293 unsigned int end
= minor
+ nr
;
295 BUG_ON(end
> nr_minors
);
296 spin_lock(&minor_lock
);
297 bitmap_clear(minors
, minor
, nr
);
298 spin_unlock(&minor_lock
);
301 static void blkif_restart_queue_callback(void *arg
)
303 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
304 schedule_work(&info
->work
);
307 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
309 /* We don't have real geometry info, but let's at least return
310 values consistent with the size of the device */
311 sector_t nsect
= get_capacity(bd
->bd_disk
);
312 sector_t cylinders
= nsect
;
316 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
317 hg
->cylinders
= cylinders
;
318 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
319 hg
->cylinders
= 0xffff;
323 static int blkif_ioctl(struct block_device
*bdev
, fmode_t mode
,
324 unsigned command
, unsigned long argument
)
326 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
329 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
330 command
, (long)argument
);
333 case CDROMMULTISESSION
:
334 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
335 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
336 if (put_user(0, (char __user
*)(argument
+ i
)))
340 case CDROM_GET_CAPABILITY
: {
341 struct gendisk
*gd
= info
->gd
;
342 if (gd
->flags
& GENHD_FL_CD
)
348 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
350 return -EINVAL
; /* same return as native Linux */
357 * Generate a Xen blkfront IO request from a blk layer request. Reads
358 * and writes are handled as expected.
360 * @req: a request struct
362 static int blkif_queue_request(struct request
*req
)
364 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
365 struct blkif_request
*ring_req
;
367 unsigned int fsect
, lsect
;
371 * Used to store if we are able to queue the request by just using
372 * existing persistent grants, or if we have to get new grants,
373 * as there are not sufficiently many free.
375 bool new_persistent_gnts
;
376 grant_ref_t gref_head
;
377 struct grant
*gnt_list_entry
= NULL
;
378 struct scatterlist
*sg
;
380 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
383 /* Check if we have enought grants to allocate a requests */
384 if (info
->persistent_gnts_c
< BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
385 new_persistent_gnts
= 1;
386 if (gnttab_alloc_grant_references(
387 BLKIF_MAX_SEGMENTS_PER_REQUEST
- info
->persistent_gnts_c
,
389 gnttab_request_free_callback(
391 blkif_restart_queue_callback
,
393 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
397 new_persistent_gnts
= 0;
399 /* Fill out a communications ring structure. */
400 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
401 id
= get_id_from_freelist(info
);
402 info
->shadow
[id
].request
= req
;
404 ring_req
->u
.rw
.id
= id
;
405 ring_req
->u
.rw
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
406 ring_req
->u
.rw
.handle
= info
->handle
;
408 ring_req
->operation
= rq_data_dir(req
) ?
409 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
411 if (req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) {
413 * Ideally we can do an unordered flush-to-disk. In case the
414 * backend onlysupports barriers, use that. A barrier request
415 * a superset of FUA, so we can implement it the same
416 * way. (It's also a FLUSH+FUA, since it is
417 * guaranteed ordered WRT previous writes.)
419 ring_req
->operation
= info
->flush_op
;
422 if (unlikely(req
->cmd_flags
& (REQ_DISCARD
| REQ_SECURE
))) {
423 /* id, sector_number and handle are set above. */
424 ring_req
->operation
= BLKIF_OP_DISCARD
;
425 ring_req
->u
.discard
.nr_sectors
= blk_rq_sectors(req
);
426 if ((req
->cmd_flags
& REQ_SECURE
) && info
->feature_secdiscard
)
427 ring_req
->u
.discard
.flag
= BLKIF_DISCARD_SECURE
;
429 ring_req
->u
.discard
.flag
= 0;
431 ring_req
->u
.rw
.nr_segments
= blk_rq_map_sg(req
->q
, req
,
432 info
->shadow
[id
].sg
);
433 BUG_ON(ring_req
->u
.rw
.nr_segments
>
434 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
436 for_each_sg(info
->shadow
[id
].sg
, sg
, ring_req
->u
.rw
.nr_segments
, i
) {
437 fsect
= sg
->offset
>> 9;
438 lsect
= fsect
+ (sg
->length
>> 9) - 1;
440 gnt_list_entry
= get_grant(&gref_head
, page_to_pfn(sg_page(sg
)), info
);
441 ref
= gnt_list_entry
->gref
;
443 info
->shadow
[id
].grants_used
[i
] = gnt_list_entry
;
445 if (rq_data_dir(req
) && info
->feature_persistent
) {
449 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
451 shared_data
= kmap_atomic(
452 pfn_to_page(gnt_list_entry
->pfn
));
453 bvec_data
= kmap_atomic(sg_page(sg
));
456 * this does not wipe data stored outside the
457 * range sg->offset..sg->offset+sg->length.
458 * Therefore, blkback *could* see data from
459 * previous requests. This is OK as long as
460 * persistent grants are shared with just one
461 * domain. It may need refactoring if this
464 memcpy(shared_data
+ sg
->offset
,
465 bvec_data
+ sg
->offset
,
468 kunmap_atomic(bvec_data
);
469 kunmap_atomic(shared_data
);
472 ring_req
->u
.rw
.seg
[i
] =
473 (struct blkif_request_segment
) {
476 .last_sect
= lsect
};
480 info
->ring
.req_prod_pvt
++;
482 /* Keep a private copy so we can reissue requests when recovering. */
483 info
->shadow
[id
].req
= *ring_req
;
485 if (new_persistent_gnts
)
486 gnttab_free_grant_references(gref_head
);
492 static inline void flush_requests(struct blkfront_info
*info
)
496 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info
->ring
, notify
);
499 notify_remote_via_irq(info
->irq
);
504 * read a block; request is in a request queue
506 static void do_blkif_request(struct request_queue
*rq
)
508 struct blkfront_info
*info
= NULL
;
512 pr_debug("Entered do_blkif_request\n");
516 while ((req
= blk_peek_request(rq
)) != NULL
) {
517 info
= req
->rq_disk
->private_data
;
519 if (RING_FULL(&info
->ring
))
522 blk_start_request(req
);
524 if ((req
->cmd_type
!= REQ_TYPE_FS
) ||
525 ((req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) &&
527 __blk_end_request_all(req
, -EIO
);
531 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
532 "(%u/%u) buffer:%p [%s]\n",
533 req
, req
->cmd
, (unsigned long)blk_rq_pos(req
),
534 blk_rq_cur_sectors(req
), blk_rq_sectors(req
),
535 req
->buffer
, rq_data_dir(req
) ? "write" : "read");
537 if (blkif_queue_request(req
)) {
538 blk_requeue_request(rq
, req
);
540 /* Avoid pointless unplugs. */
549 flush_requests(info
);
552 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
)
554 struct request_queue
*rq
;
555 struct blkfront_info
*info
= gd
->private_data
;
557 rq
= blk_init_queue(do_blkif_request
, &info
->io_lock
);
561 queue_flag_set_unlocked(QUEUE_FLAG_VIRT
, rq
);
563 if (info
->feature_discard
) {
564 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, rq
);
565 blk_queue_max_discard_sectors(rq
, get_capacity(gd
));
566 rq
->limits
.discard_granularity
= info
->discard_granularity
;
567 rq
->limits
.discard_alignment
= info
->discard_alignment
;
568 if (info
->feature_secdiscard
)
569 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD
, rq
);
572 /* Hard sector size and max sectors impersonate the equiv. hardware. */
573 blk_queue_logical_block_size(rq
, sector_size
);
574 blk_queue_max_hw_sectors(rq
, 512);
576 /* Each segment in a request is up to an aligned page in size. */
577 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
578 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
580 /* Ensure a merged request will fit in a single I/O ring slot. */
581 blk_queue_max_segments(rq
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
583 /* Make sure buffer addresses are sector-aligned. */
584 blk_queue_dma_alignment(rq
, 511);
586 /* Make sure we don't use bounce buffers. */
587 blk_queue_bounce_limit(rq
, BLK_BOUNCE_ANY
);
595 static void xlvbd_flush(struct blkfront_info
*info
)
597 blk_queue_flush(info
->rq
, info
->feature_flush
);
598 printk(KERN_INFO
"blkfront: %s: %s: %s %s\n",
600 info
->flush_op
== BLKIF_OP_WRITE_BARRIER
?
601 "barrier" : (info
->flush_op
== BLKIF_OP_FLUSH_DISKCACHE
?
602 "flush diskcache" : "barrier or flush"),
603 info
->feature_flush
? "enabled" : "disabled",
604 info
->feature_persistent
? "using persistent grants" : "");
607 static int xen_translate_vdev(int vdevice
, int *minor
, unsigned int *offset
)
610 major
= BLKIF_MAJOR(vdevice
);
611 *minor
= BLKIF_MINOR(vdevice
);
614 *offset
= (*minor
/ 64) + EMULATED_HD_DISK_NAME_OFFSET
;
615 *minor
= ((*minor
/ 64) * PARTS_PER_DISK
) +
616 EMULATED_HD_DISK_MINOR_OFFSET
;
619 *offset
= (*minor
/ 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET
;
620 *minor
= (((*minor
/ 64) + 2) * PARTS_PER_DISK
) +
621 EMULATED_HD_DISK_MINOR_OFFSET
;
623 case XEN_SCSI_DISK0_MAJOR
:
624 *offset
= (*minor
/ PARTS_PER_DISK
) + EMULATED_SD_DISK_NAME_OFFSET
;
625 *minor
= *minor
+ EMULATED_SD_DISK_MINOR_OFFSET
;
627 case XEN_SCSI_DISK1_MAJOR
:
628 case XEN_SCSI_DISK2_MAJOR
:
629 case XEN_SCSI_DISK3_MAJOR
:
630 case XEN_SCSI_DISK4_MAJOR
:
631 case XEN_SCSI_DISK5_MAJOR
:
632 case XEN_SCSI_DISK6_MAJOR
:
633 case XEN_SCSI_DISK7_MAJOR
:
634 *offset
= (*minor
/ PARTS_PER_DISK
) +
635 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16) +
636 EMULATED_SD_DISK_NAME_OFFSET
;
638 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16 * PARTS_PER_DISK
) +
639 EMULATED_SD_DISK_MINOR_OFFSET
;
641 case XEN_SCSI_DISK8_MAJOR
:
642 case XEN_SCSI_DISK9_MAJOR
:
643 case XEN_SCSI_DISK10_MAJOR
:
644 case XEN_SCSI_DISK11_MAJOR
:
645 case XEN_SCSI_DISK12_MAJOR
:
646 case XEN_SCSI_DISK13_MAJOR
:
647 case XEN_SCSI_DISK14_MAJOR
:
648 case XEN_SCSI_DISK15_MAJOR
:
649 *offset
= (*minor
/ PARTS_PER_DISK
) +
650 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16) +
651 EMULATED_SD_DISK_NAME_OFFSET
;
653 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16 * PARTS_PER_DISK
) +
654 EMULATED_SD_DISK_MINOR_OFFSET
;
657 *offset
= *minor
/ PARTS_PER_DISK
;
660 printk(KERN_WARNING
"blkfront: your disk configuration is "
661 "incorrect, please use an xvd device instead\n");
667 static char *encode_disk_name(char *ptr
, unsigned int n
)
670 ptr
= encode_disk_name(ptr
, n
/ 26 - 1);
675 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
676 struct blkfront_info
*info
,
677 u16 vdisk_info
, u16 sector_size
)
687 BUG_ON(info
->gd
!= NULL
);
688 BUG_ON(info
->rq
!= NULL
);
690 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
691 /* this is above the extended range; something is wrong */
692 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
696 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
697 err
= xen_translate_vdev(info
->vdevice
, &minor
, &offset
);
700 nr_parts
= PARTS_PER_DISK
;
702 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
703 nr_parts
= PARTS_PER_EXT_DISK
;
704 offset
= minor
/ nr_parts
;
705 if (xen_hvm_domain() && offset
< EMULATED_HD_DISK_NAME_OFFSET
+ 4)
706 printk(KERN_WARNING
"blkfront: vdevice 0x%x might conflict with "
707 "emulated IDE disks,\n\t choose an xvd device name"
708 "from xvde on\n", info
->vdevice
);
710 if (minor
>> MINORBITS
) {
711 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
712 info
->vdevice
, minor
);
716 if ((minor
% nr_parts
) == 0)
717 nr_minors
= nr_parts
;
719 err
= xlbd_reserve_minors(minor
, nr_minors
);
724 gd
= alloc_disk(nr_minors
);
728 strcpy(gd
->disk_name
, DEV_NAME
);
729 ptr
= encode_disk_name(gd
->disk_name
+ sizeof(DEV_NAME
) - 1, offset
);
730 BUG_ON(ptr
>= gd
->disk_name
+ DISK_NAME_LEN
);
734 snprintf(ptr
, gd
->disk_name
+ DISK_NAME_LEN
- ptr
,
735 "%d", minor
& (nr_parts
- 1));
737 gd
->major
= XENVBD_MAJOR
;
738 gd
->first_minor
= minor
;
739 gd
->fops
= &xlvbd_block_fops
;
740 gd
->private_data
= info
;
741 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
742 set_capacity(gd
, capacity
);
744 if (xlvbd_init_blk_queue(gd
, sector_size
)) {
749 info
->rq
= gd
->queue
;
754 if (vdisk_info
& VDISK_READONLY
)
757 if (vdisk_info
& VDISK_REMOVABLE
)
758 gd
->flags
|= GENHD_FL_REMOVABLE
;
760 if (vdisk_info
& VDISK_CDROM
)
761 gd
->flags
|= GENHD_FL_CD
;
766 xlbd_release_minors(minor
, nr_minors
);
771 static void xlvbd_release_gendisk(struct blkfront_info
*info
)
773 unsigned int minor
, nr_minors
;
776 if (info
->rq
== NULL
)
779 spin_lock_irqsave(&info
->io_lock
, flags
);
781 /* No more blkif_request(). */
782 blk_stop_queue(info
->rq
);
784 /* No more gnttab callback work. */
785 gnttab_cancel_free_callback(&info
->callback
);
786 spin_unlock_irqrestore(&info
->io_lock
, flags
);
788 /* Flush gnttab callback work. Must be done with no locks held. */
789 flush_work(&info
->work
);
791 del_gendisk(info
->gd
);
793 minor
= info
->gd
->first_minor
;
794 nr_minors
= info
->gd
->minors
;
795 xlbd_release_minors(minor
, nr_minors
);
797 blk_cleanup_queue(info
->rq
);
804 static void kick_pending_request_queues(struct blkfront_info
*info
)
806 if (!RING_FULL(&info
->ring
)) {
807 /* Re-enable calldowns. */
808 blk_start_queue(info
->rq
);
809 /* Kick things off immediately. */
810 do_blkif_request(info
->rq
);
814 static void blkif_restart_queue(struct work_struct
*work
)
816 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
818 spin_lock_irq(&info
->io_lock
);
819 if (info
->connected
== BLKIF_STATE_CONNECTED
)
820 kick_pending_request_queues(info
);
821 spin_unlock_irq(&info
->io_lock
);
824 static void blkif_free(struct blkfront_info
*info
, int suspend
)
826 struct grant
*persistent_gnt
;
829 /* Prevent new requests being issued until we fix things up. */
830 spin_lock_irq(&info
->io_lock
);
831 info
->connected
= suspend
?
832 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
833 /* No more blkif_request(). */
835 blk_stop_queue(info
->rq
);
837 /* Remove all persistent grants */
838 if (!list_empty(&info
->grants
)) {
839 list_for_each_entry_safe(persistent_gnt
, n
,
840 &info
->grants
, node
) {
841 list_del(&persistent_gnt
->node
);
842 if (persistent_gnt
->gref
!= GRANT_INVALID_REF
) {
843 gnttab_end_foreign_access(persistent_gnt
->gref
,
845 info
->persistent_gnts_c
--;
847 if (info
->feature_persistent
)
848 __free_page(pfn_to_page(persistent_gnt
->pfn
));
849 kfree(persistent_gnt
);
852 BUG_ON(info
->persistent_gnts_c
!= 0);
854 /* No more gnttab callback work. */
855 gnttab_cancel_free_callback(&info
->callback
);
856 spin_unlock_irq(&info
->io_lock
);
858 /* Flush gnttab callback work. Must be done with no locks held. */
859 flush_work(&info
->work
);
861 /* Free resources associated with old device channel. */
862 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
863 gnttab_end_foreign_access(info
->ring_ref
, 0,
864 (unsigned long)info
->ring
.sring
);
865 info
->ring_ref
= GRANT_INVALID_REF
;
866 info
->ring
.sring
= NULL
;
869 unbind_from_irqhandler(info
->irq
, info
);
870 info
->evtchn
= info
->irq
= 0;
874 static void blkif_completion(struct blk_shadow
*s
, struct blkfront_info
*info
,
875 struct blkif_response
*bret
)
878 struct scatterlist
*sg
;
883 nseg
= s
->req
.u
.rw
.nr_segments
;
885 if (bret
->operation
== BLKIF_OP_READ
&& info
->feature_persistent
) {
887 * Copy the data received from the backend into the bvec.
888 * Since bv_offset can be different than 0, and bv_len different
889 * than PAGE_SIZE, we have to keep track of the current offset,
890 * to be sure we are copying the data from the right shared page.
892 for_each_sg(s
->sg
, sg
, nseg
, i
) {
893 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
894 shared_data
= kmap_atomic(
895 pfn_to_page(s
->grants_used
[i
]->pfn
));
896 bvec_data
= kmap_atomic(sg_page(sg
));
897 memcpy(bvec_data
+ sg
->offset
,
898 shared_data
+ sg
->offset
,
900 kunmap_atomic(bvec_data
);
901 kunmap_atomic(shared_data
);
904 /* Add the persistent grant into the list of free grants */
905 for (i
= 0; i
< nseg
; i
++) {
906 if (gnttab_query_foreign_access(s
->grants_used
[i
]->gref
)) {
908 * If the grant is still mapped by the backend (the
909 * backend has chosen to make this grant persistent)
910 * we add it at the head of the list, so it will be
913 if (!info
->feature_persistent
)
914 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
915 s
->grants_used
[i
]->gref
);
916 list_add(&s
->grants_used
[i
]->node
, &info
->grants
);
917 info
->persistent_gnts_c
++;
920 * If the grant is not mapped by the backend we end the
921 * foreign access and add it to the tail of the list,
922 * so it will not be picked again unless we run out of
925 gnttab_end_foreign_access(s
->grants_used
[i
]->gref
, 0, 0UL);
926 s
->grants_used
[i
]->gref
= GRANT_INVALID_REF
;
927 list_add_tail(&s
->grants_used
[i
]->node
, &info
->grants
);
932 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
935 struct blkif_response
*bret
;
938 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
941 spin_lock_irqsave(&info
->io_lock
, flags
);
943 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
944 spin_unlock_irqrestore(&info
->io_lock
, flags
);
949 rp
= info
->ring
.sring
->rsp_prod
;
950 rmb(); /* Ensure we see queued responses up to 'rp'. */
952 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
955 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
958 * The backend has messed up and given us an id that we would
959 * never have given to it (we stamp it up to BLK_RING_SIZE -
960 * look in get_id_from_freelist.
962 if (id
>= BLK_RING_SIZE
) {
963 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
964 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
965 /* We can't safely get the 'struct request' as
966 * the id is busted. */
969 req
= info
->shadow
[id
].request
;
971 if (bret
->operation
!= BLKIF_OP_DISCARD
)
972 blkif_completion(&info
->shadow
[id
], info
, bret
);
974 if (add_id_to_freelist(info
, id
)) {
975 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
976 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
980 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
981 switch (bret
->operation
) {
982 case BLKIF_OP_DISCARD
:
983 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
984 struct request_queue
*rq
= info
->rq
;
985 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
986 info
->gd
->disk_name
, op_name(bret
->operation
));
988 info
->feature_discard
= 0;
989 info
->feature_secdiscard
= 0;
990 queue_flag_clear(QUEUE_FLAG_DISCARD
, rq
);
991 queue_flag_clear(QUEUE_FLAG_SECDISCARD
, rq
);
993 __blk_end_request_all(req
, error
);
995 case BLKIF_OP_FLUSH_DISKCACHE
:
996 case BLKIF_OP_WRITE_BARRIER
:
997 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
998 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
999 info
->gd
->disk_name
, op_name(bret
->operation
));
1000 error
= -EOPNOTSUPP
;
1002 if (unlikely(bret
->status
== BLKIF_RSP_ERROR
&&
1003 info
->shadow
[id
].req
.u
.rw
.nr_segments
== 0)) {
1004 printk(KERN_WARNING
"blkfront: %s: empty %s op failed\n",
1005 info
->gd
->disk_name
, op_name(bret
->operation
));
1006 error
= -EOPNOTSUPP
;
1008 if (unlikely(error
)) {
1009 if (error
== -EOPNOTSUPP
)
1011 info
->feature_flush
= 0;
1017 case BLKIF_OP_WRITE
:
1018 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
1019 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
1020 "request: %x\n", bret
->status
);
1022 __blk_end_request_all(req
, error
);
1029 info
->ring
.rsp_cons
= i
;
1031 if (i
!= info
->ring
.req_prod_pvt
) {
1033 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
1037 info
->ring
.sring
->rsp_event
= i
+ 1;
1039 kick_pending_request_queues(info
);
1041 spin_unlock_irqrestore(&info
->io_lock
, flags
);
1047 static int setup_blkring(struct xenbus_device
*dev
,
1048 struct blkfront_info
*info
)
1050 struct blkif_sring
*sring
;
1053 info
->ring_ref
= GRANT_INVALID_REF
;
1055 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
1057 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
1060 SHARED_RING_INIT(sring
);
1061 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
1063 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
1064 sg_init_table(info
->shadow
[i
].sg
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
1066 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
1068 free_page((unsigned long)sring
);
1069 info
->ring
.sring
= NULL
;
1072 info
->ring_ref
= err
;
1074 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
1078 err
= bind_evtchn_to_irqhandler(info
->evtchn
, blkif_interrupt
, 0,
1081 xenbus_dev_fatal(dev
, err
,
1082 "bind_evtchn_to_irqhandler failed");
1089 blkif_free(info
, 0);
1094 /* Common code used when first setting up, and when resuming. */
1095 static int talk_to_blkback(struct xenbus_device
*dev
,
1096 struct blkfront_info
*info
)
1098 const char *message
= NULL
;
1099 struct xenbus_transaction xbt
;
1102 /* Create shared ring, alloc event channel. */
1103 err
= setup_blkring(dev
, info
);
1108 err
= xenbus_transaction_start(&xbt
);
1110 xenbus_dev_fatal(dev
, err
, "starting transaction");
1111 goto destroy_blkring
;
1114 err
= xenbus_printf(xbt
, dev
->nodename
,
1115 "ring-ref", "%u", info
->ring_ref
);
1117 message
= "writing ring-ref";
1118 goto abort_transaction
;
1120 err
= xenbus_printf(xbt
, dev
->nodename
,
1121 "event-channel", "%u", info
->evtchn
);
1123 message
= "writing event-channel";
1124 goto abort_transaction
;
1126 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
1127 XEN_IO_PROTO_ABI_NATIVE
);
1129 message
= "writing protocol";
1130 goto abort_transaction
;
1132 err
= xenbus_printf(xbt
, dev
->nodename
,
1133 "feature-persistent", "%u", 1);
1136 "writing persistent grants feature to xenbus");
1138 err
= xenbus_transaction_end(xbt
, 0);
1142 xenbus_dev_fatal(dev
, err
, "completing transaction");
1143 goto destroy_blkring
;
1146 xenbus_switch_state(dev
, XenbusStateInitialised
);
1151 xenbus_transaction_end(xbt
, 1);
1153 xenbus_dev_fatal(dev
, err
, "%s", message
);
1155 blkif_free(info
, 0);
1161 * Entry point to this code when a new device is created. Allocate the basic
1162 * structures and the ring buffer for communication with the backend, and
1163 * inform the backend of the appropriate details for those. Switch to
1164 * Initialised state.
1166 static int blkfront_probe(struct xenbus_device
*dev
,
1167 const struct xenbus_device_id
*id
)
1169 int err
, vdevice
, i
;
1170 struct blkfront_info
*info
;
1172 /* FIXME: Use dynamic device id if this is not set. */
1173 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
1174 "virtual-device", "%i", &vdevice
);
1176 /* go looking in the extended area instead */
1177 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
1180 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
1185 if (xen_hvm_domain()) {
1188 /* no unplug has been done: do not hook devices != xen vbds */
1189 if (xen_platform_pci_unplug
& XEN_UNPLUG_UNNECESSARY
) {
1192 if (!VDEV_IS_EXTENDED(vdevice
))
1193 major
= BLKIF_MAJOR(vdevice
);
1195 major
= XENVBD_MAJOR
;
1197 if (major
!= XENVBD_MAJOR
) {
1199 "%s: HVM does not support vbd %d as xen block device\n",
1200 __FUNCTION__
, vdevice
);
1204 /* do not create a PV cdrom device if we are an HVM guest */
1205 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
1208 if (strncmp(type
, "cdrom", 5) == 0) {
1214 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1216 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
1220 mutex_init(&info
->mutex
);
1221 spin_lock_init(&info
->io_lock
);
1223 info
->vdevice
= vdevice
;
1224 INIT_LIST_HEAD(&info
->grants
);
1225 info
->persistent_gnts_c
= 0;
1226 info
->connected
= BLKIF_STATE_DISCONNECTED
;
1227 INIT_WORK(&info
->work
, blkif_restart_queue
);
1229 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
1230 info
->shadow
[i
].req
.u
.rw
.id
= i
+1;
1231 info
->shadow
[BLK_RING_SIZE
-1].req
.u
.rw
.id
= 0x0fffffff;
1233 /* Front end dir is a number, which is used as the id. */
1234 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
1235 dev_set_drvdata(&dev
->dev
, info
);
1237 err
= talk_to_blkback(dev
, info
);
1240 dev_set_drvdata(&dev
->dev
, NULL
);
1248 static int blkif_recover(struct blkfront_info
*info
)
1251 struct blkif_request
*req
;
1252 struct blk_shadow
*copy
;
1253 unsigned int persistent
;
1256 /* Stage 1: Make a safe copy of the shadow state. */
1257 copy
= kmemdup(info
->shadow
, sizeof(info
->shadow
),
1258 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
1262 /* Stage 2: Set up free list. */
1263 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
1264 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
1265 info
->shadow
[i
].req
.u
.rw
.id
= i
+1;
1266 info
->shadow_free
= info
->ring
.req_prod_pvt
;
1267 info
->shadow
[BLK_RING_SIZE
-1].req
.u
.rw
.id
= 0x0fffffff;
1269 /* Check if the backend supports persistent grants */
1270 rc
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1271 "feature-persistent", "%u", &persistent
,
1274 info
->feature_persistent
= 0;
1276 info
->feature_persistent
= persistent
;
1278 /* Allocate memory for grants */
1279 rc
= fill_grant_buffer(info
, BLK_RING_SIZE
*
1280 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
1282 xenbus_dev_fatal(info
->xbdev
, rc
, "setting grant buffer failed");
1287 /* Stage 3: Find pending requests and requeue them. */
1288 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
1290 if (!copy
[i
].request
)
1293 /* Grab a request slot and copy shadow state into it. */
1294 req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
1297 /* We get a new request id, and must reset the shadow state. */
1298 req
->u
.rw
.id
= get_id_from_freelist(info
);
1299 memcpy(&info
->shadow
[req
->u
.rw
.id
], ©
[i
], sizeof(copy
[i
]));
1301 if (req
->operation
!= BLKIF_OP_DISCARD
) {
1302 /* Rewrite any grant references invalidated by susp/resume. */
1303 for (j
= 0; j
< req
->u
.rw
.nr_segments
; j
++)
1304 gnttab_grant_foreign_access_ref(
1305 req
->u
.rw
.seg
[j
].gref
,
1306 info
->xbdev
->otherend_id
,
1307 pfn_to_mfn(copy
[i
].grants_used
[j
]->pfn
),
1310 info
->shadow
[req
->u
.rw
.id
].req
= *req
;
1312 info
->ring
.req_prod_pvt
++;
1317 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1319 spin_lock_irq(&info
->io_lock
);
1321 /* Now safe for us to use the shared ring */
1322 info
->connected
= BLKIF_STATE_CONNECTED
;
1324 /* Send off requeued requests */
1325 flush_requests(info
);
1327 /* Kick any other new requests queued since we resumed */
1328 kick_pending_request_queues(info
);
1330 spin_unlock_irq(&info
->io_lock
);
1336 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1337 * driver restart. We tear down our blkif structure and recreate it, but
1338 * leave the device-layer structures intact so that this is transparent to the
1339 * rest of the kernel.
1341 static int blkfront_resume(struct xenbus_device
*dev
)
1343 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1346 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
1348 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
1350 err
= talk_to_blkback(dev
, info
);
1353 * We have to wait for the backend to switch to
1354 * connected state, since we want to read which
1355 * features it supports.
1362 blkfront_closing(struct blkfront_info
*info
)
1364 struct xenbus_device
*xbdev
= info
->xbdev
;
1365 struct block_device
*bdev
= NULL
;
1367 mutex_lock(&info
->mutex
);
1369 if (xbdev
->state
== XenbusStateClosing
) {
1370 mutex_unlock(&info
->mutex
);
1375 bdev
= bdget_disk(info
->gd
, 0);
1377 mutex_unlock(&info
->mutex
);
1380 xenbus_frontend_closed(xbdev
);
1384 mutex_lock(&bdev
->bd_mutex
);
1386 if (bdev
->bd_openers
) {
1387 xenbus_dev_error(xbdev
, -EBUSY
,
1388 "Device in use; refusing to close");
1389 xenbus_switch_state(xbdev
, XenbusStateClosing
);
1391 xlvbd_release_gendisk(info
);
1392 xenbus_frontend_closed(xbdev
);
1395 mutex_unlock(&bdev
->bd_mutex
);
1399 static void blkfront_setup_discard(struct blkfront_info
*info
)
1403 unsigned int discard_granularity
;
1404 unsigned int discard_alignment
;
1405 unsigned int discard_secure
;
1407 type
= xenbus_read(XBT_NIL
, info
->xbdev
->otherend
, "type", NULL
);
1411 info
->feature_secdiscard
= 0;
1412 if (strncmp(type
, "phy", 3) == 0) {
1413 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1414 "discard-granularity", "%u", &discard_granularity
,
1415 "discard-alignment", "%u", &discard_alignment
,
1418 info
->feature_discard
= 1;
1419 info
->discard_granularity
= discard_granularity
;
1420 info
->discard_alignment
= discard_alignment
;
1422 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1423 "discard-secure", "%d", &discard_secure
,
1426 info
->feature_secdiscard
= discard_secure
;
1428 } else if (strncmp(type
, "file", 4) == 0)
1429 info
->feature_discard
= 1;
1435 * Invoked when the backend is finally 'ready' (and has told produced
1436 * the details about the physical device - #sectors, size, etc).
1438 static void blkfront_connect(struct blkfront_info
*info
)
1440 unsigned long long sectors
;
1441 unsigned long sector_size
;
1444 int barrier
, flush
, discard
, persistent
;
1446 switch (info
->connected
) {
1447 case BLKIF_STATE_CONNECTED
:
1449 * Potentially, the back-end may be signalling
1450 * a capacity change; update the capacity.
1452 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1453 "sectors", "%Lu", §ors
);
1454 if (XENBUS_EXIST_ERR(err
))
1456 printk(KERN_INFO
"Setting capacity to %Lu\n",
1458 set_capacity(info
->gd
, sectors
);
1459 revalidate_disk(info
->gd
);
1462 case BLKIF_STATE_SUSPENDED
:
1464 * If we are recovering from suspension, we need to wait
1465 * for the backend to announce it's features before
1466 * reconnecting, we need to know if the backend supports
1467 * persistent grants.
1469 blkif_recover(info
);
1476 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
1477 __func__
, info
->xbdev
->otherend
);
1479 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1480 "sectors", "%llu", §ors
,
1481 "info", "%u", &binfo
,
1482 "sector-size", "%lu", §or_size
,
1485 xenbus_dev_fatal(info
->xbdev
, err
,
1486 "reading backend fields at %s",
1487 info
->xbdev
->otherend
);
1491 info
->feature_flush
= 0;
1494 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1495 "feature-barrier", "%d", &barrier
,
1499 * If there's no "feature-barrier" defined, then it means
1500 * we're dealing with a very old backend which writes
1501 * synchronously; nothing to do.
1503 * If there are barriers, then we use flush.
1505 if (!err
&& barrier
) {
1506 info
->feature_flush
= REQ_FLUSH
| REQ_FUA
;
1507 info
->flush_op
= BLKIF_OP_WRITE_BARRIER
;
1510 * And if there is "feature-flush-cache" use that above
1513 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1514 "feature-flush-cache", "%d", &flush
,
1517 if (!err
&& flush
) {
1518 info
->feature_flush
= REQ_FLUSH
;
1519 info
->flush_op
= BLKIF_OP_FLUSH_DISKCACHE
;
1522 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1523 "feature-discard", "%d", &discard
,
1526 if (!err
&& discard
)
1527 blkfront_setup_discard(info
);
1529 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1530 "feature-persistent", "%u", &persistent
,
1533 info
->feature_persistent
= 0;
1535 info
->feature_persistent
= persistent
;
1537 /* Allocate memory for grants */
1538 err
= fill_grant_buffer(info
, BLK_RING_SIZE
*
1539 BLKIF_MAX_SEGMENTS_PER_REQUEST
);
1541 xenbus_dev_fatal(info
->xbdev
, err
, "setting grant buffer failed");
1545 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
);
1547 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
1548 info
->xbdev
->otherend
);
1552 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1554 /* Kick pending requests. */
1555 spin_lock_irq(&info
->io_lock
);
1556 info
->connected
= BLKIF_STATE_CONNECTED
;
1557 kick_pending_request_queues(info
);
1558 spin_unlock_irq(&info
->io_lock
);
1566 * Callback received when the backend's state changes.
1568 static void blkback_changed(struct xenbus_device
*dev
,
1569 enum xenbus_state backend_state
)
1571 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1573 dev_dbg(&dev
->dev
, "blkfront:blkback_changed to state %d.\n", backend_state
);
1575 switch (backend_state
) {
1576 case XenbusStateInitialising
:
1577 case XenbusStateInitWait
:
1578 case XenbusStateInitialised
:
1579 case XenbusStateReconfiguring
:
1580 case XenbusStateReconfigured
:
1581 case XenbusStateUnknown
:
1584 case XenbusStateConnected
:
1585 blkfront_connect(info
);
1588 case XenbusStateClosed
:
1589 if (dev
->state
== XenbusStateClosed
)
1591 /* Missed the backend's Closing state -- fallthrough */
1592 case XenbusStateClosing
:
1594 blkfront_closing(info
);
1599 static int blkfront_remove(struct xenbus_device
*xbdev
)
1601 struct blkfront_info
*info
= dev_get_drvdata(&xbdev
->dev
);
1602 struct block_device
*bdev
= NULL
;
1603 struct gendisk
*disk
;
1605 dev_dbg(&xbdev
->dev
, "%s removed", xbdev
->nodename
);
1607 blkif_free(info
, 0);
1609 mutex_lock(&info
->mutex
);
1613 bdev
= bdget_disk(disk
, 0);
1616 mutex_unlock(&info
->mutex
);
1624 * The xbdev was removed before we reached the Closed
1625 * state. See if it's safe to remove the disk. If the bdev
1626 * isn't closed yet, we let release take care of it.
1629 mutex_lock(&bdev
->bd_mutex
);
1630 info
= disk
->private_data
;
1632 dev_warn(disk_to_dev(disk
),
1633 "%s was hot-unplugged, %d stale handles\n",
1634 xbdev
->nodename
, bdev
->bd_openers
);
1636 if (info
&& !bdev
->bd_openers
) {
1637 xlvbd_release_gendisk(info
);
1638 disk
->private_data
= NULL
;
1642 mutex_unlock(&bdev
->bd_mutex
);
1648 static int blkfront_is_ready(struct xenbus_device
*dev
)
1650 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1652 return info
->is_ready
&& info
->xbdev
;
1655 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
1657 struct gendisk
*disk
= bdev
->bd_disk
;
1658 struct blkfront_info
*info
;
1661 mutex_lock(&blkfront_mutex
);
1663 info
= disk
->private_data
;
1670 mutex_lock(&info
->mutex
);
1673 /* xbdev is closed */
1676 mutex_unlock(&info
->mutex
);
1679 mutex_unlock(&blkfront_mutex
);
1683 static void blkif_release(struct gendisk
*disk
, fmode_t mode
)
1685 struct blkfront_info
*info
= disk
->private_data
;
1686 struct block_device
*bdev
;
1687 struct xenbus_device
*xbdev
;
1689 mutex_lock(&blkfront_mutex
);
1691 bdev
= bdget_disk(disk
, 0);
1693 if (bdev
->bd_openers
)
1697 * Check if we have been instructed to close. We will have
1698 * deferred this request, because the bdev was still open.
1701 mutex_lock(&info
->mutex
);
1702 xbdev
= info
->xbdev
;
1704 if (xbdev
&& xbdev
->state
== XenbusStateClosing
) {
1705 /* pending switch to state closed */
1706 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1707 xlvbd_release_gendisk(info
);
1708 xenbus_frontend_closed(info
->xbdev
);
1711 mutex_unlock(&info
->mutex
);
1714 /* sudden device removal */
1715 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
1716 xlvbd_release_gendisk(info
);
1717 disk
->private_data
= NULL
;
1723 mutex_unlock(&blkfront_mutex
);
1726 static const struct block_device_operations xlvbd_block_fops
=
1728 .owner
= THIS_MODULE
,
1730 .release
= blkif_release
,
1731 .getgeo
= blkif_getgeo
,
1732 .ioctl
= blkif_ioctl
,
1736 static const struct xenbus_device_id blkfront_ids
[] = {
1741 static DEFINE_XENBUS_DRIVER(blkfront
, ,
1742 .probe
= blkfront_probe
,
1743 .remove
= blkfront_remove
,
1744 .resume
= blkfront_resume
,
1745 .otherend_changed
= blkback_changed
,
1746 .is_ready
= blkfront_is_ready
,
1749 static int __init
xlblk_init(void)
1756 if (xen_hvm_domain() && !xen_platform_pci_unplug
)
1759 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
1760 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
1761 XENVBD_MAJOR
, DEV_NAME
);
1765 ret
= xenbus_register_frontend(&blkfront_driver
);
1767 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
1773 module_init(xlblk_init
);
1776 static void __exit
xlblk_exit(void)
1778 xenbus_unregister_driver(&blkfront_driver
);
1779 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
1782 module_exit(xlblk_exit
);
1784 MODULE_DESCRIPTION("Xen virtual block device frontend");
1785 MODULE_LICENSE("GPL");
1786 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
1787 MODULE_ALIAS("xen:vbd");
1788 MODULE_ALIAS("xenblk");