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
;
78 struct grant
**indirect_grants
;
79 struct scatterlist
*sg
;
88 static DEFINE_MUTEX(blkfront_mutex
);
89 static const struct block_device_operations xlvbd_block_fops
;
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
97 static unsigned int xen_blkif_max_segments
= 32;
98 module_param_named(max
, xen_blkif_max_segments
, int, S_IRUGO
);
99 MODULE_PARM_DESC(max
, "Maximum amount of segments in indirect requests (default is 32)");
101 #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
104 * We have one of these per vbd, whether ide, scsi or 'other'. They
105 * hang in private_data off the gendisk structure. We may end up
106 * putting all kinds of interesting stuff here :-)
112 struct xenbus_device
*xbdev
;
116 enum blkif_state connected
;
118 struct blkif_front_ring ring
;
119 unsigned int evtchn
, irq
;
120 struct request_queue
*rq
;
121 struct work_struct work
;
122 struct gnttab_free_callback callback
;
123 struct blk_shadow shadow
[BLK_RING_SIZE
];
124 struct list_head grants
;
125 struct list_head indirect_pages
;
126 unsigned int persistent_gnts_c
;
127 unsigned long shadow_free
;
128 unsigned int feature_flush
;
129 unsigned int flush_op
;
130 unsigned int feature_discard
:1;
131 unsigned int feature_secdiscard
:1;
132 unsigned int discard_granularity
;
133 unsigned int discard_alignment
;
134 unsigned int feature_persistent
:1;
135 unsigned int max_indirect_segments
;
139 static unsigned int nr_minors
;
140 static unsigned long *minors
;
141 static DEFINE_SPINLOCK(minor_lock
);
143 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
144 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
145 #define GRANT_INVALID_REF 0
147 #define PARTS_PER_DISK 16
148 #define PARTS_PER_EXT_DISK 256
150 #define BLKIF_MAJOR(dev) ((dev)>>8)
151 #define BLKIF_MINOR(dev) ((dev) & 0xff)
154 #define EXTENDED (1<<EXT_SHIFT)
155 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
156 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
157 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
158 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
159 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
160 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
162 #define DEV_NAME "xvd" /* name in /dev */
164 #define SEGS_PER_INDIRECT_FRAME \
165 (PAGE_SIZE/sizeof(struct blkif_request_segment_aligned))
166 #define INDIRECT_GREFS(_segs) \
167 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
169 static int blkfront_setup_indirect(struct blkfront_info
*info
);
171 static int get_id_from_freelist(struct blkfront_info
*info
)
173 unsigned long free
= info
->shadow_free
;
174 BUG_ON(free
>= BLK_RING_SIZE
);
175 info
->shadow_free
= info
->shadow
[free
].req
.u
.rw
.id
;
176 info
->shadow
[free
].req
.u
.rw
.id
= 0x0fffffee; /* debug */
180 static int add_id_to_freelist(struct blkfront_info
*info
,
183 if (info
->shadow
[id
].req
.u
.rw
.id
!= id
)
185 if (info
->shadow
[id
].request
== NULL
)
187 info
->shadow
[id
].req
.u
.rw
.id
= info
->shadow_free
;
188 info
->shadow
[id
].request
= NULL
;
189 info
->shadow_free
= id
;
193 static int fill_grant_buffer(struct blkfront_info
*info
, int num
)
195 struct page
*granted_page
;
196 struct grant
*gnt_list_entry
, *n
;
200 gnt_list_entry
= kzalloc(sizeof(struct grant
), GFP_NOIO
);
204 if (info
->feature_persistent
) {
205 granted_page
= alloc_page(GFP_NOIO
);
207 kfree(gnt_list_entry
);
210 gnt_list_entry
->pfn
= page_to_pfn(granted_page
);
213 gnt_list_entry
->gref
= GRANT_INVALID_REF
;
214 list_add(&gnt_list_entry
->node
, &info
->grants
);
221 list_for_each_entry_safe(gnt_list_entry
, n
,
222 &info
->grants
, node
) {
223 list_del(&gnt_list_entry
->node
);
224 if (info
->feature_persistent
)
225 __free_page(pfn_to_page(gnt_list_entry
->pfn
));
226 kfree(gnt_list_entry
);
233 static struct grant
*get_grant(grant_ref_t
*gref_head
,
235 struct blkfront_info
*info
)
237 struct grant
*gnt_list_entry
;
238 unsigned long buffer_mfn
;
240 BUG_ON(list_empty(&info
->grants
));
241 gnt_list_entry
= list_first_entry(&info
->grants
, struct grant
,
243 list_del(&gnt_list_entry
->node
);
245 if (gnt_list_entry
->gref
!= GRANT_INVALID_REF
) {
246 info
->persistent_gnts_c
--;
247 return gnt_list_entry
;
250 /* Assign a gref to this page */
251 gnt_list_entry
->gref
= gnttab_claim_grant_reference(gref_head
);
252 BUG_ON(gnt_list_entry
->gref
== -ENOSPC
);
253 if (!info
->feature_persistent
) {
255 gnt_list_entry
->pfn
= pfn
;
257 buffer_mfn
= pfn_to_mfn(gnt_list_entry
->pfn
);
258 gnttab_grant_foreign_access_ref(gnt_list_entry
->gref
,
259 info
->xbdev
->otherend_id
,
261 return gnt_list_entry
;
264 static const char *op_name(int op
)
266 static const char *const names
[] = {
267 [BLKIF_OP_READ
] = "read",
268 [BLKIF_OP_WRITE
] = "write",
269 [BLKIF_OP_WRITE_BARRIER
] = "barrier",
270 [BLKIF_OP_FLUSH_DISKCACHE
] = "flush",
271 [BLKIF_OP_DISCARD
] = "discard" };
273 if (op
< 0 || op
>= ARRAY_SIZE(names
))
281 static int xlbd_reserve_minors(unsigned int minor
, unsigned int nr
)
283 unsigned int end
= minor
+ nr
;
286 if (end
> nr_minors
) {
287 unsigned long *bitmap
, *old
;
289 bitmap
= kcalloc(BITS_TO_LONGS(end
), sizeof(*bitmap
),
294 spin_lock(&minor_lock
);
295 if (end
> nr_minors
) {
297 memcpy(bitmap
, minors
,
298 BITS_TO_LONGS(nr_minors
) * sizeof(*bitmap
));
300 nr_minors
= BITS_TO_LONGS(end
) * BITS_PER_LONG
;
303 spin_unlock(&minor_lock
);
307 spin_lock(&minor_lock
);
308 if (find_next_bit(minors
, end
, minor
) >= end
) {
309 bitmap_set(minors
, minor
, nr
);
313 spin_unlock(&minor_lock
);
318 static void xlbd_release_minors(unsigned int minor
, unsigned int nr
)
320 unsigned int end
= minor
+ nr
;
322 BUG_ON(end
> nr_minors
);
323 spin_lock(&minor_lock
);
324 bitmap_clear(minors
, minor
, nr
);
325 spin_unlock(&minor_lock
);
328 static void blkif_restart_queue_callback(void *arg
)
330 struct blkfront_info
*info
= (struct blkfront_info
*)arg
;
331 schedule_work(&info
->work
);
334 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
336 /* We don't have real geometry info, but let's at least return
337 values consistent with the size of the device */
338 sector_t nsect
= get_capacity(bd
->bd_disk
);
339 sector_t cylinders
= nsect
;
343 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
344 hg
->cylinders
= cylinders
;
345 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
346 hg
->cylinders
= 0xffff;
350 static int blkif_ioctl(struct block_device
*bdev
, fmode_t mode
,
351 unsigned command
, unsigned long argument
)
353 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
356 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
357 command
, (long)argument
);
360 case CDROMMULTISESSION
:
361 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
362 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
363 if (put_user(0, (char __user
*)(argument
+ i
)))
367 case CDROM_GET_CAPABILITY
: {
368 struct gendisk
*gd
= info
->gd
;
369 if (gd
->flags
& GENHD_FL_CD
)
375 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
377 return -EINVAL
; /* same return as native Linux */
384 * Generate a Xen blkfront IO request from a blk layer request. Reads
385 * and writes are handled as expected.
387 * @req: a request struct
389 static int blkif_queue_request(struct request
*req
)
391 struct blkfront_info
*info
= req
->rq_disk
->private_data
;
392 struct blkif_request
*ring_req
;
394 unsigned int fsect
, lsect
;
396 struct blkif_request_segment_aligned
*segments
= NULL
;
399 * Used to store if we are able to queue the request by just using
400 * existing persistent grants, or if we have to get new grants,
401 * as there are not sufficiently many free.
403 bool new_persistent_gnts
;
404 grant_ref_t gref_head
;
405 struct grant
*gnt_list_entry
= NULL
;
406 struct scatterlist
*sg
;
409 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
412 max_grefs
= info
->max_indirect_segments
?
413 info
->max_indirect_segments
+
414 INDIRECT_GREFS(info
->max_indirect_segments
) :
415 BLKIF_MAX_SEGMENTS_PER_REQUEST
;
417 /* Check if we have enough grants to allocate a requests */
418 if (info
->persistent_gnts_c
< max_grefs
) {
419 new_persistent_gnts
= 1;
420 if (gnttab_alloc_grant_references(
421 max_grefs
- info
->persistent_gnts_c
,
423 gnttab_request_free_callback(
425 blkif_restart_queue_callback
,
431 new_persistent_gnts
= 0;
433 /* Fill out a communications ring structure. */
434 ring_req
= RING_GET_REQUEST(&info
->ring
, info
->ring
.req_prod_pvt
);
435 id
= get_id_from_freelist(info
);
436 info
->shadow
[id
].request
= req
;
438 if (unlikely(req
->cmd_flags
& (REQ_DISCARD
| REQ_SECURE
))) {
439 ring_req
->operation
= BLKIF_OP_DISCARD
;
440 ring_req
->u
.discard
.nr_sectors
= blk_rq_sectors(req
);
441 ring_req
->u
.discard
.id
= id
;
442 ring_req
->u
.discard
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
443 if ((req
->cmd_flags
& REQ_SECURE
) && info
->feature_secdiscard
)
444 ring_req
->u
.discard
.flag
= BLKIF_DISCARD_SECURE
;
446 ring_req
->u
.discard
.flag
= 0;
448 BUG_ON(info
->max_indirect_segments
== 0 &&
449 req
->nr_phys_segments
> BLKIF_MAX_SEGMENTS_PER_REQUEST
);
450 BUG_ON(info
->max_indirect_segments
&&
451 req
->nr_phys_segments
> info
->max_indirect_segments
);
452 nseg
= blk_rq_map_sg(req
->q
, req
, info
->shadow
[id
].sg
);
453 ring_req
->u
.rw
.id
= id
;
454 if (nseg
> BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
456 * The indirect operation can only be a BLKIF_OP_READ or
459 BUG_ON(req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
));
460 ring_req
->operation
= BLKIF_OP_INDIRECT
;
461 ring_req
->u
.indirect
.indirect_op
= rq_data_dir(req
) ?
462 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
463 ring_req
->u
.indirect
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
464 ring_req
->u
.indirect
.handle
= info
->handle
;
465 ring_req
->u
.indirect
.nr_segments
= nseg
;
467 ring_req
->u
.rw
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
468 ring_req
->u
.rw
.handle
= info
->handle
;
469 ring_req
->operation
= rq_data_dir(req
) ?
470 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
471 if (req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) {
473 * Ideally we can do an unordered flush-to-disk. In case the
474 * backend onlysupports barriers, use that. A barrier request
475 * a superset of FUA, so we can implement it the same
476 * way. (It's also a FLUSH+FUA, since it is
477 * guaranteed ordered WRT previous writes.)
479 ring_req
->operation
= info
->flush_op
;
481 ring_req
->u
.rw
.nr_segments
= nseg
;
483 for_each_sg(info
->shadow
[id
].sg
, sg
, nseg
, i
) {
484 fsect
= sg
->offset
>> 9;
485 lsect
= fsect
+ (sg
->length
>> 9) - 1;
487 if ((ring_req
->operation
== BLKIF_OP_INDIRECT
) &&
488 (i
% SEGS_PER_INDIRECT_FRAME
== 0)) {
492 kunmap_atomic(segments
);
494 n
= i
/ SEGS_PER_INDIRECT_FRAME
;
495 if (!info
->feature_persistent
) {
496 struct page
*indirect_page
;
498 /* Fetch a pre-allocated page to use for indirect grefs */
499 BUG_ON(list_empty(&info
->indirect_pages
));
500 indirect_page
= list_first_entry(&info
->indirect_pages
,
502 list_del(&indirect_page
->lru
);
503 pfn
= page_to_pfn(indirect_page
);
505 gnt_list_entry
= get_grant(&gref_head
, pfn
, info
);
506 info
->shadow
[id
].indirect_grants
[n
] = gnt_list_entry
;
507 segments
= kmap_atomic(pfn_to_page(gnt_list_entry
->pfn
));
508 ring_req
->u
.indirect
.indirect_grefs
[n
] = gnt_list_entry
->gref
;
511 gnt_list_entry
= get_grant(&gref_head
, page_to_pfn(sg_page(sg
)), info
);
512 ref
= gnt_list_entry
->gref
;
514 info
->shadow
[id
].grants_used
[i
] = gnt_list_entry
;
516 if (rq_data_dir(req
) && info
->feature_persistent
) {
520 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
522 shared_data
= kmap_atomic(pfn_to_page(gnt_list_entry
->pfn
));
523 bvec_data
= kmap_atomic(sg_page(sg
));
526 * this does not wipe data stored outside the
527 * range sg->offset..sg->offset+sg->length.
528 * Therefore, blkback *could* see data from
529 * previous requests. This is OK as long as
530 * persistent grants are shared with just one
531 * domain. It may need refactoring if this
534 memcpy(shared_data
+ sg
->offset
,
535 bvec_data
+ sg
->offset
,
538 kunmap_atomic(bvec_data
);
539 kunmap_atomic(shared_data
);
541 if (ring_req
->operation
!= BLKIF_OP_INDIRECT
) {
542 ring_req
->u
.rw
.seg
[i
] =
543 (struct blkif_request_segment
) {
546 .last_sect
= lsect
};
548 n
= i
% SEGS_PER_INDIRECT_FRAME
;
550 (struct blkif_request_segment_aligned
) {
553 .last_sect
= lsect
};
557 kunmap_atomic(segments
);
560 info
->ring
.req_prod_pvt
++;
562 /* Keep a private copy so we can reissue requests when recovering. */
563 info
->shadow
[id
].req
= *ring_req
;
565 if (new_persistent_gnts
)
566 gnttab_free_grant_references(gref_head
);
572 static inline void flush_requests(struct blkfront_info
*info
)
576 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info
->ring
, notify
);
579 notify_remote_via_irq(info
->irq
);
584 * read a block; request is in a request queue
586 static void do_blkif_request(struct request_queue
*rq
)
588 struct blkfront_info
*info
= NULL
;
592 pr_debug("Entered do_blkif_request\n");
596 while ((req
= blk_peek_request(rq
)) != NULL
) {
597 info
= req
->rq_disk
->private_data
;
599 if (RING_FULL(&info
->ring
))
602 blk_start_request(req
);
604 if ((req
->cmd_type
!= REQ_TYPE_FS
) ||
605 ((req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) &&
607 __blk_end_request_all(req
, -EIO
);
611 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
612 "(%u/%u) buffer:%p [%s]\n",
613 req
, req
->cmd
, (unsigned long)blk_rq_pos(req
),
614 blk_rq_cur_sectors(req
), blk_rq_sectors(req
),
615 req
->buffer
, rq_data_dir(req
) ? "write" : "read");
617 if (blkif_queue_request(req
)) {
618 blk_requeue_request(rq
, req
);
620 /* Avoid pointless unplugs. */
629 flush_requests(info
);
632 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
,
633 unsigned int physical_sector_size
,
634 unsigned int segments
)
636 struct request_queue
*rq
;
637 struct blkfront_info
*info
= gd
->private_data
;
639 rq
= blk_init_queue(do_blkif_request
, &info
->io_lock
);
643 queue_flag_set_unlocked(QUEUE_FLAG_VIRT
, rq
);
645 if (info
->feature_discard
) {
646 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, rq
);
647 blk_queue_max_discard_sectors(rq
, get_capacity(gd
));
648 rq
->limits
.discard_granularity
= info
->discard_granularity
;
649 rq
->limits
.discard_alignment
= info
->discard_alignment
;
650 if (info
->feature_secdiscard
)
651 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD
, rq
);
654 /* Hard sector size and max sectors impersonate the equiv. hardware. */
655 blk_queue_logical_block_size(rq
, sector_size
);
656 blk_queue_physical_block_size(rq
, physical_sector_size
);
657 blk_queue_max_hw_sectors(rq
, (segments
* PAGE_SIZE
) / 512);
659 /* Each segment in a request is up to an aligned page in size. */
660 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
661 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
663 /* Ensure a merged request will fit in a single I/O ring slot. */
664 blk_queue_max_segments(rq
, segments
);
666 /* Make sure buffer addresses are sector-aligned. */
667 blk_queue_dma_alignment(rq
, 511);
669 /* Make sure we don't use bounce buffers. */
670 blk_queue_bounce_limit(rq
, BLK_BOUNCE_ANY
);
678 static void xlvbd_flush(struct blkfront_info
*info
)
680 blk_queue_flush(info
->rq
, info
->feature_flush
);
681 printk(KERN_INFO
"blkfront: %s: %s: %s %s %s %s %s\n",
683 info
->flush_op
== BLKIF_OP_WRITE_BARRIER
?
684 "barrier" : (info
->flush_op
== BLKIF_OP_FLUSH_DISKCACHE
?
685 "flush diskcache" : "barrier or flush"),
686 info
->feature_flush
? "enabled;" : "disabled;",
687 "persistent grants:",
688 info
->feature_persistent
? "enabled;" : "disabled;",
689 "indirect descriptors:",
690 info
->max_indirect_segments
? "enabled;" : "disabled;");
693 static int xen_translate_vdev(int vdevice
, int *minor
, unsigned int *offset
)
696 major
= BLKIF_MAJOR(vdevice
);
697 *minor
= BLKIF_MINOR(vdevice
);
700 *offset
= (*minor
/ 64) + EMULATED_HD_DISK_NAME_OFFSET
;
701 *minor
= ((*minor
/ 64) * PARTS_PER_DISK
) +
702 EMULATED_HD_DISK_MINOR_OFFSET
;
705 *offset
= (*minor
/ 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET
;
706 *minor
= (((*minor
/ 64) + 2) * PARTS_PER_DISK
) +
707 EMULATED_HD_DISK_MINOR_OFFSET
;
709 case XEN_SCSI_DISK0_MAJOR
:
710 *offset
= (*minor
/ PARTS_PER_DISK
) + EMULATED_SD_DISK_NAME_OFFSET
;
711 *minor
= *minor
+ EMULATED_SD_DISK_MINOR_OFFSET
;
713 case XEN_SCSI_DISK1_MAJOR
:
714 case XEN_SCSI_DISK2_MAJOR
:
715 case XEN_SCSI_DISK3_MAJOR
:
716 case XEN_SCSI_DISK4_MAJOR
:
717 case XEN_SCSI_DISK5_MAJOR
:
718 case XEN_SCSI_DISK6_MAJOR
:
719 case XEN_SCSI_DISK7_MAJOR
:
720 *offset
= (*minor
/ PARTS_PER_DISK
) +
721 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16) +
722 EMULATED_SD_DISK_NAME_OFFSET
;
724 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16 * PARTS_PER_DISK
) +
725 EMULATED_SD_DISK_MINOR_OFFSET
;
727 case XEN_SCSI_DISK8_MAJOR
:
728 case XEN_SCSI_DISK9_MAJOR
:
729 case XEN_SCSI_DISK10_MAJOR
:
730 case XEN_SCSI_DISK11_MAJOR
:
731 case XEN_SCSI_DISK12_MAJOR
:
732 case XEN_SCSI_DISK13_MAJOR
:
733 case XEN_SCSI_DISK14_MAJOR
:
734 case XEN_SCSI_DISK15_MAJOR
:
735 *offset
= (*minor
/ PARTS_PER_DISK
) +
736 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16) +
737 EMULATED_SD_DISK_NAME_OFFSET
;
739 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16 * PARTS_PER_DISK
) +
740 EMULATED_SD_DISK_MINOR_OFFSET
;
743 *offset
= *minor
/ PARTS_PER_DISK
;
746 printk(KERN_WARNING
"blkfront: your disk configuration is "
747 "incorrect, please use an xvd device instead\n");
753 static char *encode_disk_name(char *ptr
, unsigned int n
)
756 ptr
= encode_disk_name(ptr
, n
/ 26 - 1);
761 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
762 struct blkfront_info
*info
,
763 u16 vdisk_info
, u16 sector_size
,
764 unsigned int physical_sector_size
)
774 BUG_ON(info
->gd
!= NULL
);
775 BUG_ON(info
->rq
!= NULL
);
777 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
778 /* this is above the extended range; something is wrong */
779 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
783 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
784 err
= xen_translate_vdev(info
->vdevice
, &minor
, &offset
);
787 nr_parts
= PARTS_PER_DISK
;
789 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
790 nr_parts
= PARTS_PER_EXT_DISK
;
791 offset
= minor
/ nr_parts
;
792 if (xen_hvm_domain() && offset
< EMULATED_HD_DISK_NAME_OFFSET
+ 4)
793 printk(KERN_WARNING
"blkfront: vdevice 0x%x might conflict with "
794 "emulated IDE disks,\n\t choose an xvd device name"
795 "from xvde on\n", info
->vdevice
);
797 if (minor
>> MINORBITS
) {
798 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
799 info
->vdevice
, minor
);
803 if ((minor
% nr_parts
) == 0)
804 nr_minors
= nr_parts
;
806 err
= xlbd_reserve_minors(minor
, nr_minors
);
811 gd
= alloc_disk(nr_minors
);
815 strcpy(gd
->disk_name
, DEV_NAME
);
816 ptr
= encode_disk_name(gd
->disk_name
+ sizeof(DEV_NAME
) - 1, offset
);
817 BUG_ON(ptr
>= gd
->disk_name
+ DISK_NAME_LEN
);
821 snprintf(ptr
, gd
->disk_name
+ DISK_NAME_LEN
- ptr
,
822 "%d", minor
& (nr_parts
- 1));
824 gd
->major
= XENVBD_MAJOR
;
825 gd
->first_minor
= minor
;
826 gd
->fops
= &xlvbd_block_fops
;
827 gd
->private_data
= info
;
828 gd
->driverfs_dev
= &(info
->xbdev
->dev
);
829 set_capacity(gd
, capacity
);
831 if (xlvbd_init_blk_queue(gd
, sector_size
, physical_sector_size
,
832 info
->max_indirect_segments
? :
833 BLKIF_MAX_SEGMENTS_PER_REQUEST
)) {
838 info
->rq
= gd
->queue
;
843 if (vdisk_info
& VDISK_READONLY
)
846 if (vdisk_info
& VDISK_REMOVABLE
)
847 gd
->flags
|= GENHD_FL_REMOVABLE
;
849 if (vdisk_info
& VDISK_CDROM
)
850 gd
->flags
|= GENHD_FL_CD
;
855 xlbd_release_minors(minor
, nr_minors
);
860 static void xlvbd_release_gendisk(struct blkfront_info
*info
)
862 unsigned int minor
, nr_minors
;
865 if (info
->rq
== NULL
)
868 spin_lock_irqsave(&info
->io_lock
, flags
);
870 /* No more blkif_request(). */
871 blk_stop_queue(info
->rq
);
873 /* No more gnttab callback work. */
874 gnttab_cancel_free_callback(&info
->callback
);
875 spin_unlock_irqrestore(&info
->io_lock
, flags
);
877 /* Flush gnttab callback work. Must be done with no locks held. */
878 flush_work(&info
->work
);
880 del_gendisk(info
->gd
);
882 minor
= info
->gd
->first_minor
;
883 nr_minors
= info
->gd
->minors
;
884 xlbd_release_minors(minor
, nr_minors
);
886 blk_cleanup_queue(info
->rq
);
893 static void kick_pending_request_queues(struct blkfront_info
*info
)
895 if (!RING_FULL(&info
->ring
)) {
896 /* Re-enable calldowns. */
897 blk_start_queue(info
->rq
);
898 /* Kick things off immediately. */
899 do_blkif_request(info
->rq
);
903 static void blkif_restart_queue(struct work_struct
*work
)
905 struct blkfront_info
*info
= container_of(work
, struct blkfront_info
, work
);
907 spin_lock_irq(&info
->io_lock
);
908 if (info
->connected
== BLKIF_STATE_CONNECTED
)
909 kick_pending_request_queues(info
);
910 spin_unlock_irq(&info
->io_lock
);
913 static void blkif_free(struct blkfront_info
*info
, int suspend
)
915 struct grant
*persistent_gnt
;
919 /* Prevent new requests being issued until we fix things up. */
920 spin_lock_irq(&info
->io_lock
);
921 info
->connected
= suspend
?
922 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
923 /* No more blkif_request(). */
925 blk_stop_queue(info
->rq
);
927 /* Remove all persistent grants */
928 if (!list_empty(&info
->grants
)) {
929 list_for_each_entry_safe(persistent_gnt
, n
,
930 &info
->grants
, node
) {
931 list_del(&persistent_gnt
->node
);
932 if (persistent_gnt
->gref
!= GRANT_INVALID_REF
) {
933 gnttab_end_foreign_access(persistent_gnt
->gref
,
935 info
->persistent_gnts_c
--;
937 if (info
->feature_persistent
)
938 __free_page(pfn_to_page(persistent_gnt
->pfn
));
939 kfree(persistent_gnt
);
942 BUG_ON(info
->persistent_gnts_c
!= 0);
945 * Remove indirect pages, this only happens when using indirect
946 * descriptors but not persistent grants
948 if (!list_empty(&info
->indirect_pages
)) {
949 struct page
*indirect_page
, *n
;
951 BUG_ON(info
->feature_persistent
);
952 list_for_each_entry_safe(indirect_page
, n
, &info
->indirect_pages
, lru
) {
953 list_del(&indirect_page
->lru
);
954 __free_page(indirect_page
);
958 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
960 * Clear persistent grants present in requests already
963 if (!info
->shadow
[i
].request
)
966 segs
= info
->shadow
[i
].req
.operation
== BLKIF_OP_INDIRECT
?
967 info
->shadow
[i
].req
.u
.indirect
.nr_segments
:
968 info
->shadow
[i
].req
.u
.rw
.nr_segments
;
969 for (j
= 0; j
< segs
; j
++) {
970 persistent_gnt
= info
->shadow
[i
].grants_used
[j
];
971 gnttab_end_foreign_access(persistent_gnt
->gref
, 0, 0UL);
972 if (info
->feature_persistent
)
973 __free_page(pfn_to_page(persistent_gnt
->pfn
));
974 kfree(persistent_gnt
);
977 if (info
->shadow
[i
].req
.operation
!= BLKIF_OP_INDIRECT
)
979 * If this is not an indirect operation don't try to
980 * free indirect segments
984 for (j
= 0; j
< INDIRECT_GREFS(segs
); j
++) {
985 persistent_gnt
= info
->shadow
[i
].indirect_grants
[j
];
986 gnttab_end_foreign_access(persistent_gnt
->gref
, 0, 0UL);
987 __free_page(pfn_to_page(persistent_gnt
->pfn
));
988 kfree(persistent_gnt
);
992 kfree(info
->shadow
[i
].grants_used
);
993 info
->shadow
[i
].grants_used
= NULL
;
994 kfree(info
->shadow
[i
].indirect_grants
);
995 info
->shadow
[i
].indirect_grants
= NULL
;
996 kfree(info
->shadow
[i
].sg
);
997 info
->shadow
[i
].sg
= NULL
;
1000 /* No more gnttab callback work. */
1001 gnttab_cancel_free_callback(&info
->callback
);
1002 spin_unlock_irq(&info
->io_lock
);
1004 /* Flush gnttab callback work. Must be done with no locks held. */
1005 flush_work(&info
->work
);
1007 /* Free resources associated with old device channel. */
1008 if (info
->ring_ref
!= GRANT_INVALID_REF
) {
1009 gnttab_end_foreign_access(info
->ring_ref
, 0,
1010 (unsigned long)info
->ring
.sring
);
1011 info
->ring_ref
= GRANT_INVALID_REF
;
1012 info
->ring
.sring
= NULL
;
1015 unbind_from_irqhandler(info
->irq
, info
);
1016 info
->evtchn
= info
->irq
= 0;
1020 static void blkif_completion(struct blk_shadow
*s
, struct blkfront_info
*info
,
1021 struct blkif_response
*bret
)
1024 struct scatterlist
*sg
;
1029 nseg
= s
->req
.operation
== BLKIF_OP_INDIRECT
?
1030 s
->req
.u
.indirect
.nr_segments
: s
->req
.u
.rw
.nr_segments
;
1032 if (bret
->operation
== BLKIF_OP_READ
&& info
->feature_persistent
) {
1034 * Copy the data received from the backend into the bvec.
1035 * Since bv_offset can be different than 0, and bv_len different
1036 * than PAGE_SIZE, we have to keep track of the current offset,
1037 * to be sure we are copying the data from the right shared page.
1039 for_each_sg(s
->sg
, sg
, nseg
, i
) {
1040 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
1041 shared_data
= kmap_atomic(
1042 pfn_to_page(s
->grants_used
[i
]->pfn
));
1043 bvec_data
= kmap_atomic(sg_page(sg
));
1044 memcpy(bvec_data
+ sg
->offset
,
1045 shared_data
+ sg
->offset
,
1047 kunmap_atomic(bvec_data
);
1048 kunmap_atomic(shared_data
);
1051 /* Add the persistent grant into the list of free grants */
1052 for (i
= 0; i
< nseg
; i
++) {
1053 if (gnttab_query_foreign_access(s
->grants_used
[i
]->gref
)) {
1055 * If the grant is still mapped by the backend (the
1056 * backend has chosen to make this grant persistent)
1057 * we add it at the head of the list, so it will be
1060 if (!info
->feature_persistent
)
1061 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1062 s
->grants_used
[i
]->gref
);
1063 list_add(&s
->grants_used
[i
]->node
, &info
->grants
);
1064 info
->persistent_gnts_c
++;
1067 * If the grant is not mapped by the backend we end the
1068 * foreign access and add it to the tail of the list,
1069 * so it will not be picked again unless we run out of
1070 * persistent grants.
1072 gnttab_end_foreign_access(s
->grants_used
[i
]->gref
, 0, 0UL);
1073 s
->grants_used
[i
]->gref
= GRANT_INVALID_REF
;
1074 list_add_tail(&s
->grants_used
[i
]->node
, &info
->grants
);
1077 if (s
->req
.operation
== BLKIF_OP_INDIRECT
) {
1078 for (i
= 0; i
< INDIRECT_GREFS(nseg
); i
++) {
1079 if (gnttab_query_foreign_access(s
->indirect_grants
[i
]->gref
)) {
1080 if (!info
->feature_persistent
)
1081 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1082 s
->indirect_grants
[i
]->gref
);
1083 list_add(&s
->indirect_grants
[i
]->node
, &info
->grants
);
1084 info
->persistent_gnts_c
++;
1086 struct page
*indirect_page
;
1088 gnttab_end_foreign_access(s
->indirect_grants
[i
]->gref
, 0, 0UL);
1090 * Add the used indirect page back to the list of
1091 * available pages for indirect grefs.
1093 if (!info
->feature_persistent
) {
1094 indirect_page
= pfn_to_page(s
->indirect_grants
[i
]->pfn
);
1095 list_add(&indirect_page
->lru
, &info
->indirect_pages
);
1097 s
->indirect_grants
[i
]->gref
= GRANT_INVALID_REF
;
1098 list_add_tail(&s
->indirect_grants
[i
]->node
, &info
->grants
);
1104 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
1106 struct request
*req
;
1107 struct blkif_response
*bret
;
1109 unsigned long flags
;
1110 struct blkfront_info
*info
= (struct blkfront_info
*)dev_id
;
1113 spin_lock_irqsave(&info
->io_lock
, flags
);
1115 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
)) {
1116 spin_unlock_irqrestore(&info
->io_lock
, flags
);
1121 rp
= info
->ring
.sring
->rsp_prod
;
1122 rmb(); /* Ensure we see queued responses up to 'rp'. */
1124 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
1127 bret
= RING_GET_RESPONSE(&info
->ring
, i
);
1130 * The backend has messed up and given us an id that we would
1131 * never have given to it (we stamp it up to BLK_RING_SIZE -
1132 * look in get_id_from_freelist.
1134 if (id
>= BLK_RING_SIZE
) {
1135 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1136 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
1137 /* We can't safely get the 'struct request' as
1138 * the id is busted. */
1141 req
= info
->shadow
[id
].request
;
1143 if (bret
->operation
!= BLKIF_OP_DISCARD
)
1144 blkif_completion(&info
->shadow
[id
], info
, bret
);
1146 if (add_id_to_freelist(info
, id
)) {
1147 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1148 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
1152 error
= (bret
->status
== BLKIF_RSP_OKAY
) ? 0 : -EIO
;
1153 switch (bret
->operation
) {
1154 case BLKIF_OP_DISCARD
:
1155 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
1156 struct request_queue
*rq
= info
->rq
;
1157 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
1158 info
->gd
->disk_name
, op_name(bret
->operation
));
1159 error
= -EOPNOTSUPP
;
1160 info
->feature_discard
= 0;
1161 info
->feature_secdiscard
= 0;
1162 queue_flag_clear(QUEUE_FLAG_DISCARD
, rq
);
1163 queue_flag_clear(QUEUE_FLAG_SECDISCARD
, rq
);
1165 __blk_end_request_all(req
, error
);
1167 case BLKIF_OP_FLUSH_DISKCACHE
:
1168 case BLKIF_OP_WRITE_BARRIER
:
1169 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
1170 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
1171 info
->gd
->disk_name
, op_name(bret
->operation
));
1172 error
= -EOPNOTSUPP
;
1174 if (unlikely(bret
->status
== BLKIF_RSP_ERROR
&&
1175 info
->shadow
[id
].req
.u
.rw
.nr_segments
== 0)) {
1176 printk(KERN_WARNING
"blkfront: %s: empty %s op failed\n",
1177 info
->gd
->disk_name
, op_name(bret
->operation
));
1178 error
= -EOPNOTSUPP
;
1180 if (unlikely(error
)) {
1181 if (error
== -EOPNOTSUPP
)
1183 info
->feature_flush
= 0;
1189 case BLKIF_OP_WRITE
:
1190 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
1191 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
1192 "request: %x\n", bret
->status
);
1194 __blk_end_request_all(req
, error
);
1201 info
->ring
.rsp_cons
= i
;
1203 if (i
!= info
->ring
.req_prod_pvt
) {
1205 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
1209 info
->ring
.sring
->rsp_event
= i
+ 1;
1211 kick_pending_request_queues(info
);
1213 spin_unlock_irqrestore(&info
->io_lock
, flags
);
1219 static int setup_blkring(struct xenbus_device
*dev
,
1220 struct blkfront_info
*info
)
1222 struct blkif_sring
*sring
;
1225 info
->ring_ref
= GRANT_INVALID_REF
;
1227 sring
= (struct blkif_sring
*)__get_free_page(GFP_NOIO
| __GFP_HIGH
);
1229 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
1232 SHARED_RING_INIT(sring
);
1233 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
1235 err
= xenbus_grant_ring(dev
, virt_to_mfn(info
->ring
.sring
));
1237 free_page((unsigned long)sring
);
1238 info
->ring
.sring
= NULL
;
1241 info
->ring_ref
= err
;
1243 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
1247 err
= bind_evtchn_to_irqhandler(info
->evtchn
, blkif_interrupt
, 0,
1250 xenbus_dev_fatal(dev
, err
,
1251 "bind_evtchn_to_irqhandler failed");
1258 blkif_free(info
, 0);
1263 /* Common code used when first setting up, and when resuming. */
1264 static int talk_to_blkback(struct xenbus_device
*dev
,
1265 struct blkfront_info
*info
)
1267 const char *message
= NULL
;
1268 struct xenbus_transaction xbt
;
1271 /* Create shared ring, alloc event channel. */
1272 err
= setup_blkring(dev
, info
);
1277 err
= xenbus_transaction_start(&xbt
);
1279 xenbus_dev_fatal(dev
, err
, "starting transaction");
1280 goto destroy_blkring
;
1283 err
= xenbus_printf(xbt
, dev
->nodename
,
1284 "ring-ref", "%u", info
->ring_ref
);
1286 message
= "writing ring-ref";
1287 goto abort_transaction
;
1289 err
= xenbus_printf(xbt
, dev
->nodename
,
1290 "event-channel", "%u", info
->evtchn
);
1292 message
= "writing event-channel";
1293 goto abort_transaction
;
1295 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
1296 XEN_IO_PROTO_ABI_NATIVE
);
1298 message
= "writing protocol";
1299 goto abort_transaction
;
1301 err
= xenbus_printf(xbt
, dev
->nodename
,
1302 "feature-persistent", "%u", 1);
1305 "writing persistent grants feature to xenbus");
1307 err
= xenbus_transaction_end(xbt
, 0);
1311 xenbus_dev_fatal(dev
, err
, "completing transaction");
1312 goto destroy_blkring
;
1315 xenbus_switch_state(dev
, XenbusStateInitialised
);
1320 xenbus_transaction_end(xbt
, 1);
1322 xenbus_dev_fatal(dev
, err
, "%s", message
);
1324 blkif_free(info
, 0);
1330 * Entry point to this code when a new device is created. Allocate the basic
1331 * structures and the ring buffer for communication with the backend, and
1332 * inform the backend of the appropriate details for those. Switch to
1333 * Initialised state.
1335 static int blkfront_probe(struct xenbus_device
*dev
,
1336 const struct xenbus_device_id
*id
)
1338 int err
, vdevice
, i
;
1339 struct blkfront_info
*info
;
1341 /* FIXME: Use dynamic device id if this is not set. */
1342 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
1343 "virtual-device", "%i", &vdevice
);
1345 /* go looking in the extended area instead */
1346 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
1349 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
1354 if (xen_hvm_domain()) {
1357 /* no unplug has been done: do not hook devices != xen vbds */
1358 if (xen_has_pv_and_legacy_disk_devices()) {
1361 if (!VDEV_IS_EXTENDED(vdevice
))
1362 major
= BLKIF_MAJOR(vdevice
);
1364 major
= XENVBD_MAJOR
;
1366 if (major
!= XENVBD_MAJOR
) {
1368 "%s: HVM does not support vbd %d as xen block device\n",
1369 __FUNCTION__
, vdevice
);
1373 /* do not create a PV cdrom device if we are an HVM guest */
1374 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
1377 if (strncmp(type
, "cdrom", 5) == 0) {
1383 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1385 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
1389 mutex_init(&info
->mutex
);
1390 spin_lock_init(&info
->io_lock
);
1392 info
->vdevice
= vdevice
;
1393 INIT_LIST_HEAD(&info
->grants
);
1394 INIT_LIST_HEAD(&info
->indirect_pages
);
1395 info
->persistent_gnts_c
= 0;
1396 info
->connected
= BLKIF_STATE_DISCONNECTED
;
1397 INIT_WORK(&info
->work
, blkif_restart_queue
);
1399 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
1400 info
->shadow
[i
].req
.u
.rw
.id
= i
+1;
1401 info
->shadow
[BLK_RING_SIZE
-1].req
.u
.rw
.id
= 0x0fffffff;
1403 /* Front end dir is a number, which is used as the id. */
1404 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
1405 dev_set_drvdata(&dev
->dev
, info
);
1407 err
= talk_to_blkback(dev
, info
);
1410 dev_set_drvdata(&dev
->dev
, NULL
);
1418 * This is a clone of md_trim_bio, used to split a bio into smaller ones
1420 static void trim_bio(struct bio
*bio
, int offset
, int size
)
1422 /* 'bio' is a cloned bio which we need to trim to match
1423 * the given offset and size.
1424 * This requires adjusting bi_sector, bi_size, and bi_io_vec
1427 struct bio_vec
*bvec
;
1431 if (offset
== 0 && size
== bio
->bi_size
)
1434 bio
->bi_sector
+= offset
;
1435 bio
->bi_size
= size
;
1437 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
1439 while (bio
->bi_idx
< bio
->bi_vcnt
&&
1440 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
<= offset
) {
1441 /* remove this whole bio_vec */
1442 offset
-= bio
->bi_io_vec
[bio
->bi_idx
].bv_len
;
1445 if (bio
->bi_idx
< bio
->bi_vcnt
) {
1446 bio
->bi_io_vec
[bio
->bi_idx
].bv_offset
+= offset
;
1447 bio
->bi_io_vec
[bio
->bi_idx
].bv_len
-= offset
;
1449 /* avoid any complications with bi_idx being non-zero*/
1451 memmove(bio
->bi_io_vec
, bio
->bi_io_vec
+bio
->bi_idx
,
1452 (bio
->bi_vcnt
- bio
->bi_idx
) * sizeof(struct bio_vec
));
1453 bio
->bi_vcnt
-= bio
->bi_idx
;
1456 /* Make sure vcnt and last bv are not too big */
1457 bio_for_each_segment(bvec
, bio
, i
) {
1458 if (sofar
+ bvec
->bv_len
> size
)
1459 bvec
->bv_len
= size
- sofar
;
1460 if (bvec
->bv_len
== 0) {
1464 sofar
+= bvec
->bv_len
;
1468 static void split_bio_end(struct bio
*bio
, int error
)
1470 struct split_bio
*split_bio
= bio
->bi_private
;
1473 split_bio
->err
= error
;
1475 if (atomic_dec_and_test(&split_bio
->pending
)) {
1476 split_bio
->bio
->bi_phys_segments
= 0;
1477 bio_endio(split_bio
->bio
, split_bio
->err
);
1483 static int blkif_recover(struct blkfront_info
*info
)
1486 struct request
*req
, *n
;
1487 struct blk_shadow
*copy
;
1489 struct bio
*bio
, *cloned_bio
;
1490 struct bio_list bio_list
, merge_bio
;
1491 unsigned int segs
, offset
;
1493 struct split_bio
*split_bio
;
1494 struct list_head requests
;
1496 /* Stage 1: Make a safe copy of the shadow state. */
1497 copy
= kmemdup(info
->shadow
, sizeof(info
->shadow
),
1498 GFP_NOIO
| __GFP_REPEAT
| __GFP_HIGH
);
1502 /* Stage 2: Set up free list. */
1503 memset(&info
->shadow
, 0, sizeof(info
->shadow
));
1504 for (i
= 0; i
< BLK_RING_SIZE
; i
++)
1505 info
->shadow
[i
].req
.u
.rw
.id
= i
+1;
1506 info
->shadow_free
= info
->ring
.req_prod_pvt
;
1507 info
->shadow
[BLK_RING_SIZE
-1].req
.u
.rw
.id
= 0x0fffffff;
1509 rc
= blkfront_setup_indirect(info
);
1515 segs
= info
->max_indirect_segments
? : BLKIF_MAX_SEGMENTS_PER_REQUEST
;
1516 blk_queue_max_segments(info
->rq
, segs
);
1517 bio_list_init(&bio_list
);
1518 INIT_LIST_HEAD(&requests
);
1519 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
1521 if (!copy
[i
].request
)
1525 * Get the bios in the request so we can re-queue them.
1527 if (copy
[i
].request
->cmd_flags
&
1528 (REQ_FLUSH
| REQ_FUA
| REQ_DISCARD
| REQ_SECURE
)) {
1530 * Flush operations don't contain bios, so
1531 * we need to requeue the whole request
1533 list_add(©
[i
].request
->queuelist
, &requests
);
1536 merge_bio
.head
= copy
[i
].request
->bio
;
1537 merge_bio
.tail
= copy
[i
].request
->biotail
;
1538 bio_list_merge(&bio_list
, &merge_bio
);
1539 copy
[i
].request
->bio
= NULL
;
1540 blk_put_request(copy
[i
].request
);
1546 * Empty the queue, this is important because we might have
1547 * requests in the queue with more segments than what we
1550 spin_lock_irq(&info
->io_lock
);
1551 while ((req
= blk_fetch_request(info
->rq
)) != NULL
) {
1552 if (req
->cmd_flags
&
1553 (REQ_FLUSH
| REQ_FUA
| REQ_DISCARD
| REQ_SECURE
)) {
1554 list_add(&req
->queuelist
, &requests
);
1557 merge_bio
.head
= req
->bio
;
1558 merge_bio
.tail
= req
->biotail
;
1559 bio_list_merge(&bio_list
, &merge_bio
);
1561 if (req
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
))
1562 pr_alert("diskcache flush request found!\n");
1563 __blk_put_request(info
->rq
, req
);
1565 spin_unlock_irq(&info
->io_lock
);
1567 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1569 spin_lock_irq(&info
->io_lock
);
1571 /* Now safe for us to use the shared ring */
1572 info
->connected
= BLKIF_STATE_CONNECTED
;
1574 /* Kick any other new requests queued since we resumed */
1575 kick_pending_request_queues(info
);
1577 list_for_each_entry_safe(req
, n
, &requests
, queuelist
) {
1578 /* Requeue pending requests (flush or discard) */
1579 list_del_init(&req
->queuelist
);
1580 BUG_ON(req
->nr_phys_segments
> segs
);
1581 blk_requeue_request(info
->rq
, req
);
1583 spin_unlock_irq(&info
->io_lock
);
1585 while ((bio
= bio_list_pop(&bio_list
)) != NULL
) {
1586 /* Traverse the list of pending bios and re-queue them */
1587 if (bio_segments(bio
) > segs
) {
1589 * This bio has more segments than what we can
1590 * handle, we have to split it.
1592 pending
= (bio_segments(bio
) + segs
- 1) / segs
;
1593 split_bio
= kzalloc(sizeof(*split_bio
), GFP_NOIO
);
1594 BUG_ON(split_bio
== NULL
);
1595 atomic_set(&split_bio
->pending
, pending
);
1596 split_bio
->bio
= bio
;
1597 for (i
= 0; i
< pending
; i
++) {
1598 offset
= (i
* segs
* PAGE_SIZE
) >> 9;
1599 size
= min((unsigned int)(segs
* PAGE_SIZE
) >> 9,
1600 (unsigned int)(bio
->bi_size
>> 9) - offset
);
1601 cloned_bio
= bio_clone(bio
, GFP_NOIO
);
1602 BUG_ON(cloned_bio
== NULL
);
1603 trim_bio(cloned_bio
, offset
, size
);
1604 cloned_bio
->bi_private
= split_bio
;
1605 cloned_bio
->bi_end_io
= split_bio_end
;
1606 submit_bio(cloned_bio
->bi_rw
, cloned_bio
);
1609 * Now we have to wait for all those smaller bios to
1610 * end, so we can also end the "parent" bio.
1614 /* We don't need to split this bio */
1615 submit_bio(bio
->bi_rw
, bio
);
1622 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1623 * driver restart. We tear down our blkif structure and recreate it, but
1624 * leave the device-layer structures intact so that this is transparent to the
1625 * rest of the kernel.
1627 static int blkfront_resume(struct xenbus_device
*dev
)
1629 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1632 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
1634 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
1636 err
= talk_to_blkback(dev
, info
);
1639 * We have to wait for the backend to switch to
1640 * connected state, since we want to read which
1641 * features it supports.
1648 blkfront_closing(struct blkfront_info
*info
)
1650 struct xenbus_device
*xbdev
= info
->xbdev
;
1651 struct block_device
*bdev
= NULL
;
1653 mutex_lock(&info
->mutex
);
1655 if (xbdev
->state
== XenbusStateClosing
) {
1656 mutex_unlock(&info
->mutex
);
1661 bdev
= bdget_disk(info
->gd
, 0);
1663 mutex_unlock(&info
->mutex
);
1666 xenbus_frontend_closed(xbdev
);
1670 mutex_lock(&bdev
->bd_mutex
);
1672 if (bdev
->bd_openers
) {
1673 xenbus_dev_error(xbdev
, -EBUSY
,
1674 "Device in use; refusing to close");
1675 xenbus_switch_state(xbdev
, XenbusStateClosing
);
1677 xlvbd_release_gendisk(info
);
1678 xenbus_frontend_closed(xbdev
);
1681 mutex_unlock(&bdev
->bd_mutex
);
1685 static void blkfront_setup_discard(struct blkfront_info
*info
)
1689 unsigned int discard_granularity
;
1690 unsigned int discard_alignment
;
1691 unsigned int discard_secure
;
1693 type
= xenbus_read(XBT_NIL
, info
->xbdev
->otherend
, "type", NULL
);
1697 info
->feature_secdiscard
= 0;
1698 if (strncmp(type
, "phy", 3) == 0) {
1699 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1700 "discard-granularity", "%u", &discard_granularity
,
1701 "discard-alignment", "%u", &discard_alignment
,
1704 info
->feature_discard
= 1;
1705 info
->discard_granularity
= discard_granularity
;
1706 info
->discard_alignment
= discard_alignment
;
1708 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1709 "discard-secure", "%d", &discard_secure
,
1712 info
->feature_secdiscard
= discard_secure
;
1714 } else if (strncmp(type
, "file", 4) == 0)
1715 info
->feature_discard
= 1;
1720 static int blkfront_setup_indirect(struct blkfront_info
*info
)
1722 unsigned int indirect_segments
, segs
;
1725 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1726 "feature-max-indirect-segments", "%u", &indirect_segments
,
1729 info
->max_indirect_segments
= 0;
1730 segs
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
1732 info
->max_indirect_segments
= min(indirect_segments
,
1733 xen_blkif_max_segments
);
1734 segs
= info
->max_indirect_segments
;
1737 err
= fill_grant_buffer(info
, (segs
+ INDIRECT_GREFS(segs
)) * BLK_RING_SIZE
);
1741 if (!info
->feature_persistent
&& info
->max_indirect_segments
) {
1743 * We are using indirect descriptors but not persistent
1744 * grants, we need to allocate a set of pages that can be
1745 * used for mapping indirect grefs
1747 int num
= INDIRECT_GREFS(segs
) * BLK_RING_SIZE
;
1749 BUG_ON(!list_empty(&info
->indirect_pages
));
1750 for (i
= 0; i
< num
; i
++) {
1751 struct page
*indirect_page
= alloc_page(GFP_NOIO
);
1754 list_add(&indirect_page
->lru
, &info
->indirect_pages
);
1758 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
1759 info
->shadow
[i
].grants_used
= kzalloc(
1760 sizeof(info
->shadow
[i
].grants_used
[0]) * segs
,
1762 info
->shadow
[i
].sg
= kzalloc(sizeof(info
->shadow
[i
].sg
[0]) * segs
, GFP_NOIO
);
1763 if (info
->max_indirect_segments
)
1764 info
->shadow
[i
].indirect_grants
= kzalloc(
1765 sizeof(info
->shadow
[i
].indirect_grants
[0]) *
1766 INDIRECT_GREFS(segs
),
1768 if ((info
->shadow
[i
].grants_used
== NULL
) ||
1769 (info
->shadow
[i
].sg
== NULL
) ||
1770 (info
->max_indirect_segments
&&
1771 (info
->shadow
[i
].indirect_grants
== NULL
)))
1773 sg_init_table(info
->shadow
[i
].sg
, segs
);
1780 for (i
= 0; i
< BLK_RING_SIZE
; i
++) {
1781 kfree(info
->shadow
[i
].grants_used
);
1782 info
->shadow
[i
].grants_used
= NULL
;
1783 kfree(info
->shadow
[i
].sg
);
1784 info
->shadow
[i
].sg
= NULL
;
1785 kfree(info
->shadow
[i
].indirect_grants
);
1786 info
->shadow
[i
].indirect_grants
= NULL
;
1788 if (!list_empty(&info
->indirect_pages
)) {
1789 struct page
*indirect_page
, *n
;
1790 list_for_each_entry_safe(indirect_page
, n
, &info
->indirect_pages
, lru
) {
1791 list_del(&indirect_page
->lru
);
1792 __free_page(indirect_page
);
1799 * Invoked when the backend is finally 'ready' (and has told produced
1800 * the details about the physical device - #sectors, size, etc).
1802 static void blkfront_connect(struct blkfront_info
*info
)
1804 unsigned long long sectors
;
1805 unsigned long sector_size
;
1806 unsigned int physical_sector_size
;
1809 int barrier
, flush
, discard
, persistent
;
1811 switch (info
->connected
) {
1812 case BLKIF_STATE_CONNECTED
:
1814 * Potentially, the back-end may be signalling
1815 * a capacity change; update the capacity.
1817 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1818 "sectors", "%Lu", §ors
);
1819 if (XENBUS_EXIST_ERR(err
))
1821 printk(KERN_INFO
"Setting capacity to %Lu\n",
1823 set_capacity(info
->gd
, sectors
);
1824 revalidate_disk(info
->gd
);
1827 case BLKIF_STATE_SUSPENDED
:
1829 * If we are recovering from suspension, we need to wait
1830 * for the backend to announce it's features before
1831 * reconnecting, at least we need to know if the backend
1832 * supports indirect descriptors, and how many.
1834 blkif_recover(info
);
1841 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
1842 __func__
, info
->xbdev
->otherend
);
1844 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1845 "sectors", "%llu", §ors
,
1846 "info", "%u", &binfo
,
1847 "sector-size", "%lu", §or_size
,
1850 xenbus_dev_fatal(info
->xbdev
, err
,
1851 "reading backend fields at %s",
1852 info
->xbdev
->otherend
);
1857 * physcial-sector-size is a newer field, so old backends may not
1858 * provide this. Assume physical sector size to be the same as
1859 * sector_size in that case.
1861 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
1862 "physical-sector-size", "%u", &physical_sector_size
);
1864 physical_sector_size
= sector_size
;
1866 info
->feature_flush
= 0;
1869 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1870 "feature-barrier", "%d", &barrier
,
1874 * If there's no "feature-barrier" defined, then it means
1875 * we're dealing with a very old backend which writes
1876 * synchronously; nothing to do.
1878 * If there are barriers, then we use flush.
1880 if (!err
&& barrier
) {
1881 info
->feature_flush
= REQ_FLUSH
| REQ_FUA
;
1882 info
->flush_op
= BLKIF_OP_WRITE_BARRIER
;
1885 * And if there is "feature-flush-cache" use that above
1888 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1889 "feature-flush-cache", "%d", &flush
,
1892 if (!err
&& flush
) {
1893 info
->feature_flush
= REQ_FLUSH
;
1894 info
->flush_op
= BLKIF_OP_FLUSH_DISKCACHE
;
1897 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1898 "feature-discard", "%d", &discard
,
1901 if (!err
&& discard
)
1902 blkfront_setup_discard(info
);
1904 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
1905 "feature-persistent", "%u", &persistent
,
1908 info
->feature_persistent
= 0;
1910 info
->feature_persistent
= persistent
;
1912 err
= blkfront_setup_indirect(info
);
1914 xenbus_dev_fatal(info
->xbdev
, err
, "setup_indirect at %s",
1915 info
->xbdev
->otherend
);
1919 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
,
1920 physical_sector_size
);
1922 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
1923 info
->xbdev
->otherend
);
1927 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
1929 /* Kick pending requests. */
1930 spin_lock_irq(&info
->io_lock
);
1931 info
->connected
= BLKIF_STATE_CONNECTED
;
1932 kick_pending_request_queues(info
);
1933 spin_unlock_irq(&info
->io_lock
);
1941 * Callback received when the backend's state changes.
1943 static void blkback_changed(struct xenbus_device
*dev
,
1944 enum xenbus_state backend_state
)
1946 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
1948 dev_dbg(&dev
->dev
, "blkfront:blkback_changed to state %d.\n", backend_state
);
1950 switch (backend_state
) {
1951 case XenbusStateInitialising
:
1952 case XenbusStateInitWait
:
1953 case XenbusStateInitialised
:
1954 case XenbusStateReconfiguring
:
1955 case XenbusStateReconfigured
:
1956 case XenbusStateUnknown
:
1959 case XenbusStateConnected
:
1960 blkfront_connect(info
);
1963 case XenbusStateClosed
:
1964 if (dev
->state
== XenbusStateClosed
)
1966 /* Missed the backend's Closing state -- fallthrough */
1967 case XenbusStateClosing
:
1969 blkfront_closing(info
);
1974 static int blkfront_remove(struct xenbus_device
*xbdev
)
1976 struct blkfront_info
*info
= dev_get_drvdata(&xbdev
->dev
);
1977 struct block_device
*bdev
= NULL
;
1978 struct gendisk
*disk
;
1980 dev_dbg(&xbdev
->dev
, "%s removed", xbdev
->nodename
);
1982 blkif_free(info
, 0);
1984 mutex_lock(&info
->mutex
);
1988 bdev
= bdget_disk(disk
, 0);
1991 mutex_unlock(&info
->mutex
);
1999 * The xbdev was removed before we reached the Closed
2000 * state. See if it's safe to remove the disk. If the bdev
2001 * isn't closed yet, we let release take care of it.
2004 mutex_lock(&bdev
->bd_mutex
);
2005 info
= disk
->private_data
;
2007 dev_warn(disk_to_dev(disk
),
2008 "%s was hot-unplugged, %d stale handles\n",
2009 xbdev
->nodename
, bdev
->bd_openers
);
2011 if (info
&& !bdev
->bd_openers
) {
2012 xlvbd_release_gendisk(info
);
2013 disk
->private_data
= NULL
;
2017 mutex_unlock(&bdev
->bd_mutex
);
2023 static int blkfront_is_ready(struct xenbus_device
*dev
)
2025 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2027 return info
->is_ready
&& info
->xbdev
;
2030 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
2032 struct gendisk
*disk
= bdev
->bd_disk
;
2033 struct blkfront_info
*info
;
2036 mutex_lock(&blkfront_mutex
);
2038 info
= disk
->private_data
;
2045 mutex_lock(&info
->mutex
);
2048 /* xbdev is closed */
2051 mutex_unlock(&info
->mutex
);
2054 mutex_unlock(&blkfront_mutex
);
2058 static void blkif_release(struct gendisk
*disk
, fmode_t mode
)
2060 struct blkfront_info
*info
= disk
->private_data
;
2061 struct block_device
*bdev
;
2062 struct xenbus_device
*xbdev
;
2064 mutex_lock(&blkfront_mutex
);
2066 bdev
= bdget_disk(disk
, 0);
2068 if (bdev
->bd_openers
)
2072 * Check if we have been instructed to close. We will have
2073 * deferred this request, because the bdev was still open.
2076 mutex_lock(&info
->mutex
);
2077 xbdev
= info
->xbdev
;
2079 if (xbdev
&& xbdev
->state
== XenbusStateClosing
) {
2080 /* pending switch to state closed */
2081 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
2082 xlvbd_release_gendisk(info
);
2083 xenbus_frontend_closed(info
->xbdev
);
2086 mutex_unlock(&info
->mutex
);
2089 /* sudden device removal */
2090 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
2091 xlvbd_release_gendisk(info
);
2092 disk
->private_data
= NULL
;
2098 mutex_unlock(&blkfront_mutex
);
2101 static const struct block_device_operations xlvbd_block_fops
=
2103 .owner
= THIS_MODULE
,
2105 .release
= blkif_release
,
2106 .getgeo
= blkif_getgeo
,
2107 .ioctl
= blkif_ioctl
,
2111 static const struct xenbus_device_id blkfront_ids
[] = {
2116 static DEFINE_XENBUS_DRIVER(blkfront
, ,
2117 .probe
= blkfront_probe
,
2118 .remove
= blkfront_remove
,
2119 .resume
= blkfront_resume
,
2120 .otherend_changed
= blkback_changed
,
2121 .is_ready
= blkfront_is_ready
,
2124 static int __init
xlblk_init(void)
2131 if (!xen_has_pv_disk_devices())
2134 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
2135 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
2136 XENVBD_MAJOR
, DEV_NAME
);
2140 ret
= xenbus_register_frontend(&blkfront_driver
);
2142 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
2148 module_init(xlblk_init
);
2151 static void __exit
xlblk_exit(void)
2153 xenbus_unregister_driver(&blkfront_driver
);
2154 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
2157 module_exit(xlblk_exit
);
2159 MODULE_DESCRIPTION("Xen virtual block device frontend");
2160 MODULE_LICENSE("GPL");
2161 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
2162 MODULE_ALIAS("xen:vbd");
2163 MODULE_ALIAS("xenblk");