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/blk-mq.h>
41 #include <linux/hdreg.h>
42 #include <linux/cdrom.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/mutex.h>
46 #include <linux/scatterlist.h>
47 #include <linux/bitmap.h>
48 #include <linux/list.h>
51 #include <xen/xenbus.h>
52 #include <xen/grant_table.h>
53 #include <xen/events.h>
55 #include <xen/platform_pci.h>
57 #include <xen/interface/grant_table.h>
58 #include <xen/interface/io/blkif.h>
59 #include <xen/interface/io/protocols.h>
61 #include <asm/xen/hypervisor.h>
64 * The minimal size of segment supported by the block framework is PAGE_SIZE.
65 * When Linux is using a different page size than Xen, it may not be possible
66 * to put all the data in a single segment.
67 * This can happen when the backend doesn't support indirect descriptor and
68 * therefore the maximum amount of data that a request can carry is
69 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
71 * Note that we only support one extra request. So the Linux page size
72 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
75 #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
78 BLKIF_STATE_DISCONNECTED
,
79 BLKIF_STATE_CONNECTED
,
80 BLKIF_STATE_SUSPENDED
,
86 struct list_head node
;
97 struct blkif_request req
;
98 struct request
*request
;
99 struct grant
**grants_used
;
100 struct grant
**indirect_grants
;
101 struct scatterlist
*sg
;
103 enum blk_req_status status
;
105 #define NO_ASSOCIATED_ID ~0UL
107 * Id of the sibling if we ever need 2 requests when handling a
110 unsigned long associated_id
;
117 static inline struct blkif_req
*blkif_req(struct request
*rq
)
119 return blk_mq_rq_to_pdu(rq
);
122 static DEFINE_MUTEX(blkfront_mutex
);
123 static const struct block_device_operations xlvbd_block_fops
;
126 * Maximum number of segments in indirect requests, the actual value used by
127 * the frontend driver is the minimum of this value and the value provided
128 * by the backend driver.
131 static unsigned int xen_blkif_max_segments
= 32;
132 module_param_named(max_indirect_segments
, xen_blkif_max_segments
, uint
,
134 MODULE_PARM_DESC(max_indirect_segments
,
135 "Maximum amount of segments in indirect requests (default is 32)");
137 static unsigned int xen_blkif_max_queues
= 4;
138 module_param_named(max_queues
, xen_blkif_max_queues
, uint
, S_IRUGO
);
139 MODULE_PARM_DESC(max_queues
, "Maximum number of hardware queues/rings used per virtual disk");
142 * Maximum order of pages to be used for the shared ring between front and
143 * backend, 4KB page granularity is used.
145 static unsigned int xen_blkif_max_ring_order
;
146 module_param_named(max_ring_page_order
, xen_blkif_max_ring_order
, int, S_IRUGO
);
147 MODULE_PARM_DESC(max_ring_page_order
, "Maximum order of pages to be used for the shared ring");
149 #define BLK_RING_SIZE(info) \
150 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
152 #define BLK_MAX_RING_SIZE \
153 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
156 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
157 * characters are enough. Define to 20 to keep consistent with backend.
159 #define RINGREF_NAME_LEN (20)
161 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
163 #define QUEUE_NAME_LEN (17)
167 * Every blkfront device can associate with one or more blkfront_ring_info,
168 * depending on how many hardware queues/rings to be used.
170 struct blkfront_ring_info
{
171 /* Lock to protect data in every ring buffer. */
172 spinlock_t ring_lock
;
173 struct blkif_front_ring ring
;
174 unsigned int ring_ref
[XENBUS_MAX_RING_GRANTS
];
175 unsigned int evtchn
, irq
;
176 struct work_struct work
;
177 struct gnttab_free_callback callback
;
178 struct blk_shadow shadow
[BLK_MAX_RING_SIZE
];
179 struct list_head indirect_pages
;
180 struct list_head grants
;
181 unsigned int persistent_gnts_c
;
182 unsigned long shadow_free
;
183 struct blkfront_info
*dev_info
;
187 * We have one of these per vbd, whether ide, scsi or 'other'. They
188 * hang in private_data off the gendisk structure. We may end up
189 * putting all kinds of interesting stuff here :-)
194 struct xenbus_device
*xbdev
;
197 unsigned int physical_sector_size
;
200 enum blkif_state connected
;
201 /* Number of pages per ring buffer. */
202 unsigned int nr_ring_pages
;
203 struct request_queue
*rq
;
204 unsigned int feature_flush
:1;
205 unsigned int feature_fua
:1;
206 unsigned int feature_discard
:1;
207 unsigned int feature_secdiscard
:1;
208 unsigned int feature_persistent
:1;
209 unsigned int discard_granularity
;
210 unsigned int discard_alignment
;
211 /* Number of 4KB segments handled */
212 unsigned int max_indirect_segments
;
214 struct blk_mq_tag_set tag_set
;
215 struct blkfront_ring_info
*rinfo
;
216 unsigned int nr_rings
;
217 /* Save uncomplete reqs and bios for migration. */
218 struct list_head requests
;
219 struct bio_list bio_list
;
222 static unsigned int nr_minors
;
223 static unsigned long *minors
;
224 static DEFINE_SPINLOCK(minor_lock
);
226 #define GRANT_INVALID_REF 0
228 #define PARTS_PER_DISK 16
229 #define PARTS_PER_EXT_DISK 256
231 #define BLKIF_MAJOR(dev) ((dev)>>8)
232 #define BLKIF_MINOR(dev) ((dev) & 0xff)
235 #define EXTENDED (1<<EXT_SHIFT)
236 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
237 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
238 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
239 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
240 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
241 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
243 #define DEV_NAME "xvd" /* name in /dev */
246 * Grants are always the same size as a Xen page (i.e 4KB).
247 * A physical segment is always the same size as a Linux page.
248 * Number of grants per physical segment
250 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
252 #define GRANTS_PER_INDIRECT_FRAME \
253 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
255 #define PSEGS_PER_INDIRECT_FRAME \
256 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
258 #define INDIRECT_GREFS(_grants) \
259 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
261 #define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
263 static int blkfront_setup_indirect(struct blkfront_ring_info
*rinfo
);
264 static void blkfront_gather_backend_features(struct blkfront_info
*info
);
265 static int negotiate_mq(struct blkfront_info
*info
);
267 static int get_id_from_freelist(struct blkfront_ring_info
*rinfo
)
269 unsigned long free
= rinfo
->shadow_free
;
271 BUG_ON(free
>= BLK_RING_SIZE(rinfo
->dev_info
));
272 rinfo
->shadow_free
= rinfo
->shadow
[free
].req
.u
.rw
.id
;
273 rinfo
->shadow
[free
].req
.u
.rw
.id
= 0x0fffffee; /* debug */
277 static int add_id_to_freelist(struct blkfront_ring_info
*rinfo
,
280 if (rinfo
->shadow
[id
].req
.u
.rw
.id
!= id
)
282 if (rinfo
->shadow
[id
].request
== NULL
)
284 rinfo
->shadow
[id
].req
.u
.rw
.id
= rinfo
->shadow_free
;
285 rinfo
->shadow
[id
].request
= NULL
;
286 rinfo
->shadow_free
= id
;
290 static int fill_grant_buffer(struct blkfront_ring_info
*rinfo
, int num
)
292 struct blkfront_info
*info
= rinfo
->dev_info
;
293 struct page
*granted_page
;
294 struct grant
*gnt_list_entry
, *n
;
298 gnt_list_entry
= kzalloc(sizeof(struct grant
), GFP_NOIO
);
302 if (info
->feature_persistent
) {
303 granted_page
= alloc_page(GFP_NOIO
);
305 kfree(gnt_list_entry
);
308 gnt_list_entry
->page
= granted_page
;
311 gnt_list_entry
->gref
= GRANT_INVALID_REF
;
312 list_add(&gnt_list_entry
->node
, &rinfo
->grants
);
319 list_for_each_entry_safe(gnt_list_entry
, n
,
320 &rinfo
->grants
, node
) {
321 list_del(&gnt_list_entry
->node
);
322 if (info
->feature_persistent
)
323 __free_page(gnt_list_entry
->page
);
324 kfree(gnt_list_entry
);
331 static struct grant
*get_free_grant(struct blkfront_ring_info
*rinfo
)
333 struct grant
*gnt_list_entry
;
335 BUG_ON(list_empty(&rinfo
->grants
));
336 gnt_list_entry
= list_first_entry(&rinfo
->grants
, struct grant
,
338 list_del(&gnt_list_entry
->node
);
340 if (gnt_list_entry
->gref
!= GRANT_INVALID_REF
)
341 rinfo
->persistent_gnts_c
--;
343 return gnt_list_entry
;
346 static inline void grant_foreign_access(const struct grant
*gnt_list_entry
,
347 const struct blkfront_info
*info
)
349 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry
->gref
,
350 info
->xbdev
->otherend_id
,
351 gnt_list_entry
->page
,
355 static struct grant
*get_grant(grant_ref_t
*gref_head
,
357 struct blkfront_ring_info
*rinfo
)
359 struct grant
*gnt_list_entry
= get_free_grant(rinfo
);
360 struct blkfront_info
*info
= rinfo
->dev_info
;
362 if (gnt_list_entry
->gref
!= GRANT_INVALID_REF
)
363 return gnt_list_entry
;
365 /* Assign a gref to this page */
366 gnt_list_entry
->gref
= gnttab_claim_grant_reference(gref_head
);
367 BUG_ON(gnt_list_entry
->gref
== -ENOSPC
);
368 if (info
->feature_persistent
)
369 grant_foreign_access(gnt_list_entry
, info
);
371 /* Grant access to the GFN passed by the caller */
372 gnttab_grant_foreign_access_ref(gnt_list_entry
->gref
,
373 info
->xbdev
->otherend_id
,
377 return gnt_list_entry
;
380 static struct grant
*get_indirect_grant(grant_ref_t
*gref_head
,
381 struct blkfront_ring_info
*rinfo
)
383 struct grant
*gnt_list_entry
= get_free_grant(rinfo
);
384 struct blkfront_info
*info
= rinfo
->dev_info
;
386 if (gnt_list_entry
->gref
!= GRANT_INVALID_REF
)
387 return gnt_list_entry
;
389 /* Assign a gref to this page */
390 gnt_list_entry
->gref
= gnttab_claim_grant_reference(gref_head
);
391 BUG_ON(gnt_list_entry
->gref
== -ENOSPC
);
392 if (!info
->feature_persistent
) {
393 struct page
*indirect_page
;
395 /* Fetch a pre-allocated page to use for indirect grefs */
396 BUG_ON(list_empty(&rinfo
->indirect_pages
));
397 indirect_page
= list_first_entry(&rinfo
->indirect_pages
,
399 list_del(&indirect_page
->lru
);
400 gnt_list_entry
->page
= indirect_page
;
402 grant_foreign_access(gnt_list_entry
, info
);
404 return gnt_list_entry
;
407 static const char *op_name(int op
)
409 static const char *const names
[] = {
410 [BLKIF_OP_READ
] = "read",
411 [BLKIF_OP_WRITE
] = "write",
412 [BLKIF_OP_WRITE_BARRIER
] = "barrier",
413 [BLKIF_OP_FLUSH_DISKCACHE
] = "flush",
414 [BLKIF_OP_DISCARD
] = "discard" };
416 if (op
< 0 || op
>= ARRAY_SIZE(names
))
424 static int xlbd_reserve_minors(unsigned int minor
, unsigned int nr
)
426 unsigned int end
= minor
+ nr
;
429 if (end
> nr_minors
) {
430 unsigned long *bitmap
, *old
;
432 bitmap
= kcalloc(BITS_TO_LONGS(end
), sizeof(*bitmap
),
437 spin_lock(&minor_lock
);
438 if (end
> nr_minors
) {
440 memcpy(bitmap
, minors
,
441 BITS_TO_LONGS(nr_minors
) * sizeof(*bitmap
));
443 nr_minors
= BITS_TO_LONGS(end
) * BITS_PER_LONG
;
446 spin_unlock(&minor_lock
);
450 spin_lock(&minor_lock
);
451 if (find_next_bit(minors
, end
, minor
) >= end
) {
452 bitmap_set(minors
, minor
, nr
);
456 spin_unlock(&minor_lock
);
461 static void xlbd_release_minors(unsigned int minor
, unsigned int nr
)
463 unsigned int end
= minor
+ nr
;
465 BUG_ON(end
> nr_minors
);
466 spin_lock(&minor_lock
);
467 bitmap_clear(minors
, minor
, nr
);
468 spin_unlock(&minor_lock
);
471 static void blkif_restart_queue_callback(void *arg
)
473 struct blkfront_ring_info
*rinfo
= (struct blkfront_ring_info
*)arg
;
474 schedule_work(&rinfo
->work
);
477 static int blkif_getgeo(struct block_device
*bd
, struct hd_geometry
*hg
)
479 /* We don't have real geometry info, but let's at least return
480 values consistent with the size of the device */
481 sector_t nsect
= get_capacity(bd
->bd_disk
);
482 sector_t cylinders
= nsect
;
486 sector_div(cylinders
, hg
->heads
* hg
->sectors
);
487 hg
->cylinders
= cylinders
;
488 if ((sector_t
)(hg
->cylinders
+ 1) * hg
->heads
* hg
->sectors
< nsect
)
489 hg
->cylinders
= 0xffff;
493 static int blkif_ioctl(struct block_device
*bdev
, fmode_t mode
,
494 unsigned command
, unsigned long argument
)
496 struct blkfront_info
*info
= bdev
->bd_disk
->private_data
;
499 dev_dbg(&info
->xbdev
->dev
, "command: 0x%x, argument: 0x%lx\n",
500 command
, (long)argument
);
503 case CDROMMULTISESSION
:
504 dev_dbg(&info
->xbdev
->dev
, "FIXME: support multisession CDs later\n");
505 for (i
= 0; i
< sizeof(struct cdrom_multisession
); i
++)
506 if (put_user(0, (char __user
*)(argument
+ i
)))
510 case CDROM_GET_CAPABILITY
: {
511 struct gendisk
*gd
= info
->gd
;
512 if (gd
->flags
& GENHD_FL_CD
)
518 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
520 return -EINVAL
; /* same return as native Linux */
526 static unsigned long blkif_ring_get_request(struct blkfront_ring_info
*rinfo
,
528 struct blkif_request
**ring_req
)
532 *ring_req
= RING_GET_REQUEST(&rinfo
->ring
, rinfo
->ring
.req_prod_pvt
);
533 rinfo
->ring
.req_prod_pvt
++;
535 id
= get_id_from_freelist(rinfo
);
536 rinfo
->shadow
[id
].request
= req
;
537 rinfo
->shadow
[id
].status
= REQ_WAITING
;
538 rinfo
->shadow
[id
].associated_id
= NO_ASSOCIATED_ID
;
540 (*ring_req
)->u
.rw
.id
= id
;
545 static int blkif_queue_discard_req(struct request
*req
, struct blkfront_ring_info
*rinfo
)
547 struct blkfront_info
*info
= rinfo
->dev_info
;
548 struct blkif_request
*ring_req
;
551 /* Fill out a communications ring structure. */
552 id
= blkif_ring_get_request(rinfo
, req
, &ring_req
);
554 ring_req
->operation
= BLKIF_OP_DISCARD
;
555 ring_req
->u
.discard
.nr_sectors
= blk_rq_sectors(req
);
556 ring_req
->u
.discard
.id
= id
;
557 ring_req
->u
.discard
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
558 if (req_op(req
) == REQ_OP_SECURE_ERASE
&& info
->feature_secdiscard
)
559 ring_req
->u
.discard
.flag
= BLKIF_DISCARD_SECURE
;
561 ring_req
->u
.discard
.flag
= 0;
563 /* Keep a private copy so we can reissue requests when recovering. */
564 rinfo
->shadow
[id
].req
= *ring_req
;
569 struct setup_rw_req
{
570 unsigned int grant_idx
;
571 struct blkif_request_segment
*segments
;
572 struct blkfront_ring_info
*rinfo
;
573 struct blkif_request
*ring_req
;
574 grant_ref_t gref_head
;
576 /* Only used when persistent grant is used and it's a read request */
578 unsigned int bvec_off
;
581 bool require_extra_req
;
582 struct blkif_request
*extra_ring_req
;
585 static void blkif_setup_rw_req_grant(unsigned long gfn
, unsigned int offset
,
586 unsigned int len
, void *data
)
588 struct setup_rw_req
*setup
= data
;
590 struct grant
*gnt_list_entry
;
591 unsigned int fsect
, lsect
;
592 /* Convenient aliases */
593 unsigned int grant_idx
= setup
->grant_idx
;
594 struct blkif_request
*ring_req
= setup
->ring_req
;
595 struct blkfront_ring_info
*rinfo
= setup
->rinfo
;
597 * We always use the shadow of the first request to store the list
598 * of grant associated to the block I/O request. This made the
599 * completion more easy to handle even if the block I/O request is
602 struct blk_shadow
*shadow
= &rinfo
->shadow
[setup
->id
];
604 if (unlikely(setup
->require_extra_req
&&
605 grant_idx
>= BLKIF_MAX_SEGMENTS_PER_REQUEST
)) {
607 * We are using the second request, setup grant_idx
608 * to be the index of the segment array.
610 grant_idx
-= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
611 ring_req
= setup
->extra_ring_req
;
614 if ((ring_req
->operation
== BLKIF_OP_INDIRECT
) &&
615 (grant_idx
% GRANTS_PER_INDIRECT_FRAME
== 0)) {
617 kunmap_atomic(setup
->segments
);
619 n
= grant_idx
/ GRANTS_PER_INDIRECT_FRAME
;
620 gnt_list_entry
= get_indirect_grant(&setup
->gref_head
, rinfo
);
621 shadow
->indirect_grants
[n
] = gnt_list_entry
;
622 setup
->segments
= kmap_atomic(gnt_list_entry
->page
);
623 ring_req
->u
.indirect
.indirect_grefs
[n
] = gnt_list_entry
->gref
;
626 gnt_list_entry
= get_grant(&setup
->gref_head
, gfn
, rinfo
);
627 ref
= gnt_list_entry
->gref
;
629 * All the grants are stored in the shadow of the first
630 * request. Therefore we have to use the global index.
632 shadow
->grants_used
[setup
->grant_idx
] = gnt_list_entry
;
634 if (setup
->need_copy
) {
637 shared_data
= kmap_atomic(gnt_list_entry
->page
);
639 * this does not wipe data stored outside the
640 * range sg->offset..sg->offset+sg->length.
641 * Therefore, blkback *could* see data from
642 * previous requests. This is OK as long as
643 * persistent grants are shared with just one
644 * domain. It may need refactoring if this
647 memcpy(shared_data
+ offset
,
648 setup
->bvec_data
+ setup
->bvec_off
,
651 kunmap_atomic(shared_data
);
652 setup
->bvec_off
+= len
;
656 lsect
= fsect
+ (len
>> 9) - 1;
657 if (ring_req
->operation
!= BLKIF_OP_INDIRECT
) {
658 ring_req
->u
.rw
.seg
[grant_idx
] =
659 (struct blkif_request_segment
) {
662 .last_sect
= lsect
};
664 setup
->segments
[grant_idx
% GRANTS_PER_INDIRECT_FRAME
] =
665 (struct blkif_request_segment
) {
668 .last_sect
= lsect
};
671 (setup
->grant_idx
)++;
674 static void blkif_setup_extra_req(struct blkif_request
*first
,
675 struct blkif_request
*second
)
677 uint16_t nr_segments
= first
->u
.rw
.nr_segments
;
680 * The second request is only present when the first request uses
681 * all its segments. It's always the continuity of the first one.
683 first
->u
.rw
.nr_segments
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
685 second
->u
.rw
.nr_segments
= nr_segments
- BLKIF_MAX_SEGMENTS_PER_REQUEST
;
686 second
->u
.rw
.sector_number
= first
->u
.rw
.sector_number
+
687 (BLKIF_MAX_SEGMENTS_PER_REQUEST
* XEN_PAGE_SIZE
) / 512;
689 second
->u
.rw
.handle
= first
->u
.rw
.handle
;
690 second
->operation
= first
->operation
;
693 static int blkif_queue_rw_req(struct request
*req
, struct blkfront_ring_info
*rinfo
)
695 struct blkfront_info
*info
= rinfo
->dev_info
;
696 struct blkif_request
*ring_req
, *extra_ring_req
= NULL
;
697 unsigned long id
, extra_id
= NO_ASSOCIATED_ID
;
698 bool require_extra_req
= false;
700 struct setup_rw_req setup
= {
704 .need_copy
= rq_data_dir(req
) && info
->feature_persistent
,
708 * Used to store if we are able to queue the request by just using
709 * existing persistent grants, or if we have to get new grants,
710 * as there are not sufficiently many free.
712 bool new_persistent_gnts
= false;
713 struct scatterlist
*sg
;
714 int num_sg
, max_grefs
, num_grant
;
716 max_grefs
= req
->nr_phys_segments
* GRANTS_PER_PSEG
;
717 if (max_grefs
> BLKIF_MAX_SEGMENTS_PER_REQUEST
)
719 * If we are using indirect segments we need to account
720 * for the indirect grefs used in the request.
722 max_grefs
+= INDIRECT_GREFS(max_grefs
);
724 /* Check if we have enough persistent grants to allocate a requests */
725 if (rinfo
->persistent_gnts_c
< max_grefs
) {
726 new_persistent_gnts
= true;
728 if (gnttab_alloc_grant_references(
729 max_grefs
- rinfo
->persistent_gnts_c
,
730 &setup
.gref_head
) < 0) {
731 gnttab_request_free_callback(
733 blkif_restart_queue_callback
,
735 max_grefs
- rinfo
->persistent_gnts_c
);
740 /* Fill out a communications ring structure. */
741 id
= blkif_ring_get_request(rinfo
, req
, &ring_req
);
743 num_sg
= blk_rq_map_sg(req
->q
, req
, rinfo
->shadow
[id
].sg
);
745 /* Calculate the number of grant used */
746 for_each_sg(rinfo
->shadow
[id
].sg
, sg
, num_sg
, i
)
747 num_grant
+= gnttab_count_grant(sg
->offset
, sg
->length
);
749 require_extra_req
= info
->max_indirect_segments
== 0 &&
750 num_grant
> BLKIF_MAX_SEGMENTS_PER_REQUEST
;
751 BUG_ON(!HAS_EXTRA_REQ
&& require_extra_req
);
753 rinfo
->shadow
[id
].num_sg
= num_sg
;
754 if (num_grant
> BLKIF_MAX_SEGMENTS_PER_REQUEST
&&
755 likely(!require_extra_req
)) {
757 * The indirect operation can only be a BLKIF_OP_READ or
760 BUG_ON(req_op(req
) == REQ_OP_FLUSH
|| req
->cmd_flags
& REQ_FUA
);
761 ring_req
->operation
= BLKIF_OP_INDIRECT
;
762 ring_req
->u
.indirect
.indirect_op
= rq_data_dir(req
) ?
763 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
764 ring_req
->u
.indirect
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
765 ring_req
->u
.indirect
.handle
= info
->handle
;
766 ring_req
->u
.indirect
.nr_segments
= num_grant
;
768 ring_req
->u
.rw
.sector_number
= (blkif_sector_t
)blk_rq_pos(req
);
769 ring_req
->u
.rw
.handle
= info
->handle
;
770 ring_req
->operation
= rq_data_dir(req
) ?
771 BLKIF_OP_WRITE
: BLKIF_OP_READ
;
772 if (req_op(req
) == REQ_OP_FLUSH
|| req
->cmd_flags
& REQ_FUA
) {
774 * Ideally we can do an unordered flush-to-disk.
775 * In case the backend onlysupports barriers, use that.
776 * A barrier request a superset of FUA, so we can
777 * implement it the same way. (It's also a FLUSH+FUA,
778 * since it is guaranteed ordered WRT previous writes.)
780 if (info
->feature_flush
&& info
->feature_fua
)
781 ring_req
->operation
=
782 BLKIF_OP_WRITE_BARRIER
;
783 else if (info
->feature_flush
)
784 ring_req
->operation
=
785 BLKIF_OP_FLUSH_DISKCACHE
;
787 ring_req
->operation
= 0;
789 ring_req
->u
.rw
.nr_segments
= num_grant
;
790 if (unlikely(require_extra_req
)) {
791 extra_id
= blkif_ring_get_request(rinfo
, req
,
794 * Only the first request contains the scatter-gather
797 rinfo
->shadow
[extra_id
].num_sg
= 0;
799 blkif_setup_extra_req(ring_req
, extra_ring_req
);
801 /* Link the 2 requests together */
802 rinfo
->shadow
[extra_id
].associated_id
= id
;
803 rinfo
->shadow
[id
].associated_id
= extra_id
;
807 setup
.ring_req
= ring_req
;
810 setup
.require_extra_req
= require_extra_req
;
811 if (unlikely(require_extra_req
))
812 setup
.extra_ring_req
= extra_ring_req
;
814 for_each_sg(rinfo
->shadow
[id
].sg
, sg
, num_sg
, i
) {
815 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
817 if (setup
.need_copy
) {
818 setup
.bvec_off
= sg
->offset
;
819 setup
.bvec_data
= kmap_atomic(sg_page(sg
));
822 gnttab_foreach_grant_in_range(sg_page(sg
),
825 blkif_setup_rw_req_grant
,
829 kunmap_atomic(setup
.bvec_data
);
832 kunmap_atomic(setup
.segments
);
834 /* Keep a private copy so we can reissue requests when recovering. */
835 rinfo
->shadow
[id
].req
= *ring_req
;
836 if (unlikely(require_extra_req
))
837 rinfo
->shadow
[extra_id
].req
= *extra_ring_req
;
839 if (new_persistent_gnts
)
840 gnttab_free_grant_references(setup
.gref_head
);
846 * Generate a Xen blkfront IO request from a blk layer request. Reads
847 * and writes are handled as expected.
849 * @req: a request struct
851 static int blkif_queue_request(struct request
*req
, struct blkfront_ring_info
*rinfo
)
853 if (unlikely(rinfo
->dev_info
->connected
!= BLKIF_STATE_CONNECTED
))
856 if (unlikely(req_op(req
) == REQ_OP_DISCARD
||
857 req_op(req
) == REQ_OP_SECURE_ERASE
))
858 return blkif_queue_discard_req(req
, rinfo
);
860 return blkif_queue_rw_req(req
, rinfo
);
863 static inline void flush_requests(struct blkfront_ring_info
*rinfo
)
867 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo
->ring
, notify
);
870 notify_remote_via_irq(rinfo
->irq
);
873 static inline bool blkif_request_flush_invalid(struct request
*req
,
874 struct blkfront_info
*info
)
876 return (blk_rq_is_passthrough(req
) ||
877 ((req_op(req
) == REQ_OP_FLUSH
) &&
878 !info
->feature_flush
) ||
879 ((req
->cmd_flags
& REQ_FUA
) &&
880 !info
->feature_fua
));
883 static blk_status_t
blkif_queue_rq(struct blk_mq_hw_ctx
*hctx
,
884 const struct blk_mq_queue_data
*qd
)
887 int qid
= hctx
->queue_num
;
888 struct blkfront_info
*info
= hctx
->queue
->queuedata
;
889 struct blkfront_ring_info
*rinfo
= NULL
;
891 BUG_ON(info
->nr_rings
<= qid
);
892 rinfo
= &info
->rinfo
[qid
];
893 blk_mq_start_request(qd
->rq
);
894 spin_lock_irqsave(&rinfo
->ring_lock
, flags
);
895 if (RING_FULL(&rinfo
->ring
))
898 if (blkif_request_flush_invalid(qd
->rq
, rinfo
->dev_info
))
901 if (blkif_queue_request(qd
->rq
, rinfo
))
904 flush_requests(rinfo
);
905 spin_unlock_irqrestore(&rinfo
->ring_lock
, flags
);
909 spin_unlock_irqrestore(&rinfo
->ring_lock
, flags
);
910 return BLK_STS_IOERR
;
913 blk_mq_stop_hw_queue(hctx
);
914 spin_unlock_irqrestore(&rinfo
->ring_lock
, flags
);
915 return BLK_STS_DEV_RESOURCE
;
918 static void blkif_complete_rq(struct request
*rq
)
920 blk_mq_end_request(rq
, blkif_req(rq
)->error
);
923 static const struct blk_mq_ops blkfront_mq_ops
= {
924 .queue_rq
= blkif_queue_rq
,
925 .complete
= blkif_complete_rq
,
928 static void blkif_set_queue_limits(struct blkfront_info
*info
)
930 struct request_queue
*rq
= info
->rq
;
931 struct gendisk
*gd
= info
->gd
;
932 unsigned int segments
= info
->max_indirect_segments
? :
933 BLKIF_MAX_SEGMENTS_PER_REQUEST
;
935 queue_flag_set_unlocked(QUEUE_FLAG_VIRT
, rq
);
937 if (info
->feature_discard
) {
938 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, rq
);
939 blk_queue_max_discard_sectors(rq
, get_capacity(gd
));
940 rq
->limits
.discard_granularity
= info
->discard_granularity
;
941 rq
->limits
.discard_alignment
= info
->discard_alignment
;
942 if (info
->feature_secdiscard
)
943 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE
, rq
);
946 /* Hard sector size and max sectors impersonate the equiv. hardware. */
947 blk_queue_logical_block_size(rq
, info
->sector_size
);
948 blk_queue_physical_block_size(rq
, info
->physical_sector_size
);
949 blk_queue_max_hw_sectors(rq
, (segments
* XEN_PAGE_SIZE
) / 512);
951 /* Each segment in a request is up to an aligned page in size. */
952 blk_queue_segment_boundary(rq
, PAGE_SIZE
- 1);
953 blk_queue_max_segment_size(rq
, PAGE_SIZE
);
955 /* Ensure a merged request will fit in a single I/O ring slot. */
956 blk_queue_max_segments(rq
, segments
/ GRANTS_PER_PSEG
);
958 /* Make sure buffer addresses are sector-aligned. */
959 blk_queue_dma_alignment(rq
, 511);
962 static int xlvbd_init_blk_queue(struct gendisk
*gd
, u16 sector_size
,
963 unsigned int physical_sector_size
)
965 struct request_queue
*rq
;
966 struct blkfront_info
*info
= gd
->private_data
;
968 memset(&info
->tag_set
, 0, sizeof(info
->tag_set
));
969 info
->tag_set
.ops
= &blkfront_mq_ops
;
970 info
->tag_set
.nr_hw_queues
= info
->nr_rings
;
971 if (HAS_EXTRA_REQ
&& info
->max_indirect_segments
== 0) {
973 * When indirect descriptior is not supported, the I/O request
974 * will be split between multiple request in the ring.
975 * To avoid problems when sending the request, divide by
976 * 2 the depth of the queue.
978 info
->tag_set
.queue_depth
= BLK_RING_SIZE(info
) / 2;
980 info
->tag_set
.queue_depth
= BLK_RING_SIZE(info
);
981 info
->tag_set
.numa_node
= NUMA_NO_NODE
;
982 info
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_SG_MERGE
;
983 info
->tag_set
.cmd_size
= sizeof(struct blkif_req
);
984 info
->tag_set
.driver_data
= info
;
986 if (blk_mq_alloc_tag_set(&info
->tag_set
))
988 rq
= blk_mq_init_queue(&info
->tag_set
);
990 blk_mq_free_tag_set(&info
->tag_set
);
994 rq
->queuedata
= info
;
995 info
->rq
= gd
->queue
= rq
;
997 info
->sector_size
= sector_size
;
998 info
->physical_sector_size
= physical_sector_size
;
999 blkif_set_queue_limits(info
);
1004 static const char *flush_info(struct blkfront_info
*info
)
1006 if (info
->feature_flush
&& info
->feature_fua
)
1007 return "barrier: enabled;";
1008 else if (info
->feature_flush
)
1009 return "flush diskcache: enabled;";
1011 return "barrier or flush: disabled;";
1014 static void xlvbd_flush(struct blkfront_info
*info
)
1016 blk_queue_write_cache(info
->rq
, info
->feature_flush
? true : false,
1017 info
->feature_fua
? true : false);
1018 pr_info("blkfront: %s: %s %s %s %s %s\n",
1019 info
->gd
->disk_name
, flush_info(info
),
1020 "persistent grants:", info
->feature_persistent
?
1021 "enabled;" : "disabled;", "indirect descriptors:",
1022 info
->max_indirect_segments
? "enabled;" : "disabled;");
1025 static int xen_translate_vdev(int vdevice
, int *minor
, unsigned int *offset
)
1028 major
= BLKIF_MAJOR(vdevice
);
1029 *minor
= BLKIF_MINOR(vdevice
);
1031 case XEN_IDE0_MAJOR
:
1032 *offset
= (*minor
/ 64) + EMULATED_HD_DISK_NAME_OFFSET
;
1033 *minor
= ((*minor
/ 64) * PARTS_PER_DISK
) +
1034 EMULATED_HD_DISK_MINOR_OFFSET
;
1036 case XEN_IDE1_MAJOR
:
1037 *offset
= (*minor
/ 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET
;
1038 *minor
= (((*minor
/ 64) + 2) * PARTS_PER_DISK
) +
1039 EMULATED_HD_DISK_MINOR_OFFSET
;
1041 case XEN_SCSI_DISK0_MAJOR
:
1042 *offset
= (*minor
/ PARTS_PER_DISK
) + EMULATED_SD_DISK_NAME_OFFSET
;
1043 *minor
= *minor
+ EMULATED_SD_DISK_MINOR_OFFSET
;
1045 case XEN_SCSI_DISK1_MAJOR
:
1046 case XEN_SCSI_DISK2_MAJOR
:
1047 case XEN_SCSI_DISK3_MAJOR
:
1048 case XEN_SCSI_DISK4_MAJOR
:
1049 case XEN_SCSI_DISK5_MAJOR
:
1050 case XEN_SCSI_DISK6_MAJOR
:
1051 case XEN_SCSI_DISK7_MAJOR
:
1052 *offset
= (*minor
/ PARTS_PER_DISK
) +
1053 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16) +
1054 EMULATED_SD_DISK_NAME_OFFSET
;
1056 ((major
- XEN_SCSI_DISK1_MAJOR
+ 1) * 16 * PARTS_PER_DISK
) +
1057 EMULATED_SD_DISK_MINOR_OFFSET
;
1059 case XEN_SCSI_DISK8_MAJOR
:
1060 case XEN_SCSI_DISK9_MAJOR
:
1061 case XEN_SCSI_DISK10_MAJOR
:
1062 case XEN_SCSI_DISK11_MAJOR
:
1063 case XEN_SCSI_DISK12_MAJOR
:
1064 case XEN_SCSI_DISK13_MAJOR
:
1065 case XEN_SCSI_DISK14_MAJOR
:
1066 case XEN_SCSI_DISK15_MAJOR
:
1067 *offset
= (*minor
/ PARTS_PER_DISK
) +
1068 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16) +
1069 EMULATED_SD_DISK_NAME_OFFSET
;
1071 ((major
- XEN_SCSI_DISK8_MAJOR
+ 8) * 16 * PARTS_PER_DISK
) +
1072 EMULATED_SD_DISK_MINOR_OFFSET
;
1075 *offset
= *minor
/ PARTS_PER_DISK
;
1078 printk(KERN_WARNING
"blkfront: your disk configuration is "
1079 "incorrect, please use an xvd device instead\n");
1085 static char *encode_disk_name(char *ptr
, unsigned int n
)
1088 ptr
= encode_disk_name(ptr
, n
/ 26 - 1);
1089 *ptr
= 'a' + n
% 26;
1093 static int xlvbd_alloc_gendisk(blkif_sector_t capacity
,
1094 struct blkfront_info
*info
,
1095 u16 vdisk_info
, u16 sector_size
,
1096 unsigned int physical_sector_size
)
1101 unsigned int offset
;
1106 BUG_ON(info
->gd
!= NULL
);
1107 BUG_ON(info
->rq
!= NULL
);
1109 if ((info
->vdevice
>>EXT_SHIFT
) > 1) {
1110 /* this is above the extended range; something is wrong */
1111 printk(KERN_WARNING
"blkfront: vdevice 0x%x is above the extended range; ignoring\n", info
->vdevice
);
1115 if (!VDEV_IS_EXTENDED(info
->vdevice
)) {
1116 err
= xen_translate_vdev(info
->vdevice
, &minor
, &offset
);
1119 nr_parts
= PARTS_PER_DISK
;
1121 minor
= BLKIF_MINOR_EXT(info
->vdevice
);
1122 nr_parts
= PARTS_PER_EXT_DISK
;
1123 offset
= minor
/ nr_parts
;
1124 if (xen_hvm_domain() && offset
< EMULATED_HD_DISK_NAME_OFFSET
+ 4)
1125 printk(KERN_WARNING
"blkfront: vdevice 0x%x might conflict with "
1126 "emulated IDE disks,\n\t choose an xvd device name"
1127 "from xvde on\n", info
->vdevice
);
1129 if (minor
>> MINORBITS
) {
1130 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1131 info
->vdevice
, minor
);
1135 if ((minor
% nr_parts
) == 0)
1136 nr_minors
= nr_parts
;
1138 err
= xlbd_reserve_minors(minor
, nr_minors
);
1143 gd
= alloc_disk(nr_minors
);
1147 strcpy(gd
->disk_name
, DEV_NAME
);
1148 ptr
= encode_disk_name(gd
->disk_name
+ sizeof(DEV_NAME
) - 1, offset
);
1149 BUG_ON(ptr
>= gd
->disk_name
+ DISK_NAME_LEN
);
1153 snprintf(ptr
, gd
->disk_name
+ DISK_NAME_LEN
- ptr
,
1154 "%d", minor
& (nr_parts
- 1));
1156 gd
->major
= XENVBD_MAJOR
;
1157 gd
->first_minor
= minor
;
1158 gd
->fops
= &xlvbd_block_fops
;
1159 gd
->private_data
= info
;
1160 set_capacity(gd
, capacity
);
1162 if (xlvbd_init_blk_queue(gd
, sector_size
, physical_sector_size
)) {
1169 if (vdisk_info
& VDISK_READONLY
)
1172 if (vdisk_info
& VDISK_REMOVABLE
)
1173 gd
->flags
|= GENHD_FL_REMOVABLE
;
1175 if (vdisk_info
& VDISK_CDROM
)
1176 gd
->flags
|= GENHD_FL_CD
;
1181 xlbd_release_minors(minor
, nr_minors
);
1186 static void xlvbd_release_gendisk(struct blkfront_info
*info
)
1188 unsigned int minor
, nr_minors
, i
;
1190 if (info
->rq
== NULL
)
1193 /* No more blkif_request(). */
1194 blk_mq_stop_hw_queues(info
->rq
);
1196 for (i
= 0; i
< info
->nr_rings
; i
++) {
1197 struct blkfront_ring_info
*rinfo
= &info
->rinfo
[i
];
1199 /* No more gnttab callback work. */
1200 gnttab_cancel_free_callback(&rinfo
->callback
);
1202 /* Flush gnttab callback work. Must be done with no locks held. */
1203 flush_work(&rinfo
->work
);
1206 del_gendisk(info
->gd
);
1208 minor
= info
->gd
->first_minor
;
1209 nr_minors
= info
->gd
->minors
;
1210 xlbd_release_minors(minor
, nr_minors
);
1212 blk_cleanup_queue(info
->rq
);
1213 blk_mq_free_tag_set(&info
->tag_set
);
1220 /* Already hold rinfo->ring_lock. */
1221 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info
*rinfo
)
1223 if (!RING_FULL(&rinfo
->ring
))
1224 blk_mq_start_stopped_hw_queues(rinfo
->dev_info
->rq
, true);
1227 static void kick_pending_request_queues(struct blkfront_ring_info
*rinfo
)
1229 unsigned long flags
;
1231 spin_lock_irqsave(&rinfo
->ring_lock
, flags
);
1232 kick_pending_request_queues_locked(rinfo
);
1233 spin_unlock_irqrestore(&rinfo
->ring_lock
, flags
);
1236 static void blkif_restart_queue(struct work_struct
*work
)
1238 struct blkfront_ring_info
*rinfo
= container_of(work
, struct blkfront_ring_info
, work
);
1240 if (rinfo
->dev_info
->connected
== BLKIF_STATE_CONNECTED
)
1241 kick_pending_request_queues(rinfo
);
1244 static void blkif_free_ring(struct blkfront_ring_info
*rinfo
)
1246 struct grant
*persistent_gnt
, *n
;
1247 struct blkfront_info
*info
= rinfo
->dev_info
;
1251 * Remove indirect pages, this only happens when using indirect
1252 * descriptors but not persistent grants
1254 if (!list_empty(&rinfo
->indirect_pages
)) {
1255 struct page
*indirect_page
, *n
;
1257 BUG_ON(info
->feature_persistent
);
1258 list_for_each_entry_safe(indirect_page
, n
, &rinfo
->indirect_pages
, lru
) {
1259 list_del(&indirect_page
->lru
);
1260 __free_page(indirect_page
);
1264 /* Remove all persistent grants. */
1265 if (!list_empty(&rinfo
->grants
)) {
1266 list_for_each_entry_safe(persistent_gnt
, n
,
1267 &rinfo
->grants
, node
) {
1268 list_del(&persistent_gnt
->node
);
1269 if (persistent_gnt
->gref
!= GRANT_INVALID_REF
) {
1270 gnttab_end_foreign_access(persistent_gnt
->gref
,
1272 rinfo
->persistent_gnts_c
--;
1274 if (info
->feature_persistent
)
1275 __free_page(persistent_gnt
->page
);
1276 kfree(persistent_gnt
);
1279 BUG_ON(rinfo
->persistent_gnts_c
!= 0);
1281 for (i
= 0; i
< BLK_RING_SIZE(info
); i
++) {
1283 * Clear persistent grants present in requests already
1284 * on the shared ring
1286 if (!rinfo
->shadow
[i
].request
)
1289 segs
= rinfo
->shadow
[i
].req
.operation
== BLKIF_OP_INDIRECT
?
1290 rinfo
->shadow
[i
].req
.u
.indirect
.nr_segments
:
1291 rinfo
->shadow
[i
].req
.u
.rw
.nr_segments
;
1292 for (j
= 0; j
< segs
; j
++) {
1293 persistent_gnt
= rinfo
->shadow
[i
].grants_used
[j
];
1294 gnttab_end_foreign_access(persistent_gnt
->gref
, 0, 0UL);
1295 if (info
->feature_persistent
)
1296 __free_page(persistent_gnt
->page
);
1297 kfree(persistent_gnt
);
1300 if (rinfo
->shadow
[i
].req
.operation
!= BLKIF_OP_INDIRECT
)
1302 * If this is not an indirect operation don't try to
1303 * free indirect segments
1307 for (j
= 0; j
< INDIRECT_GREFS(segs
); j
++) {
1308 persistent_gnt
= rinfo
->shadow
[i
].indirect_grants
[j
];
1309 gnttab_end_foreign_access(persistent_gnt
->gref
, 0, 0UL);
1310 __free_page(persistent_gnt
->page
);
1311 kfree(persistent_gnt
);
1315 kfree(rinfo
->shadow
[i
].grants_used
);
1316 rinfo
->shadow
[i
].grants_used
= NULL
;
1317 kfree(rinfo
->shadow
[i
].indirect_grants
);
1318 rinfo
->shadow
[i
].indirect_grants
= NULL
;
1319 kfree(rinfo
->shadow
[i
].sg
);
1320 rinfo
->shadow
[i
].sg
= NULL
;
1323 /* No more gnttab callback work. */
1324 gnttab_cancel_free_callback(&rinfo
->callback
);
1326 /* Flush gnttab callback work. Must be done with no locks held. */
1327 flush_work(&rinfo
->work
);
1329 /* Free resources associated with old device channel. */
1330 for (i
= 0; i
< info
->nr_ring_pages
; i
++) {
1331 if (rinfo
->ring_ref
[i
] != GRANT_INVALID_REF
) {
1332 gnttab_end_foreign_access(rinfo
->ring_ref
[i
], 0, 0);
1333 rinfo
->ring_ref
[i
] = GRANT_INVALID_REF
;
1336 free_pages((unsigned long)rinfo
->ring
.sring
, get_order(info
->nr_ring_pages
* XEN_PAGE_SIZE
));
1337 rinfo
->ring
.sring
= NULL
;
1340 unbind_from_irqhandler(rinfo
->irq
, rinfo
);
1341 rinfo
->evtchn
= rinfo
->irq
= 0;
1344 static void blkif_free(struct blkfront_info
*info
, int suspend
)
1348 /* Prevent new requests being issued until we fix things up. */
1349 info
->connected
= suspend
?
1350 BLKIF_STATE_SUSPENDED
: BLKIF_STATE_DISCONNECTED
;
1351 /* No more blkif_request(). */
1353 blk_mq_stop_hw_queues(info
->rq
);
1355 for (i
= 0; i
< info
->nr_rings
; i
++)
1356 blkif_free_ring(&info
->rinfo
[i
]);
1363 struct copy_from_grant
{
1364 const struct blk_shadow
*s
;
1365 unsigned int grant_idx
;
1366 unsigned int bvec_offset
;
1370 static void blkif_copy_from_grant(unsigned long gfn
, unsigned int offset
,
1371 unsigned int len
, void *data
)
1373 struct copy_from_grant
*info
= data
;
1375 /* Convenient aliases */
1376 const struct blk_shadow
*s
= info
->s
;
1378 shared_data
= kmap_atomic(s
->grants_used
[info
->grant_idx
]->page
);
1380 memcpy(info
->bvec_data
+ info
->bvec_offset
,
1381 shared_data
+ offset
, len
);
1383 info
->bvec_offset
+= len
;
1386 kunmap_atomic(shared_data
);
1389 static enum blk_req_status
blkif_rsp_to_req_status(int rsp
)
1393 case BLKIF_RSP_OKAY
:
1395 case BLKIF_RSP_EOPNOTSUPP
:
1396 return REQ_EOPNOTSUPP
;
1397 case BLKIF_RSP_ERROR
:
1405 * Get the final status of the block request based on two ring response
1407 static int blkif_get_final_status(enum blk_req_status s1
,
1408 enum blk_req_status s2
)
1410 BUG_ON(s1
== REQ_WAITING
);
1411 BUG_ON(s2
== REQ_WAITING
);
1413 if (s1
== REQ_ERROR
|| s2
== REQ_ERROR
)
1414 return BLKIF_RSP_ERROR
;
1415 else if (s1
== REQ_EOPNOTSUPP
|| s2
== REQ_EOPNOTSUPP
)
1416 return BLKIF_RSP_EOPNOTSUPP
;
1417 return BLKIF_RSP_OKAY
;
1420 static bool blkif_completion(unsigned long *id
,
1421 struct blkfront_ring_info
*rinfo
,
1422 struct blkif_response
*bret
)
1425 struct scatterlist
*sg
;
1426 int num_sg
, num_grant
;
1427 struct blkfront_info
*info
= rinfo
->dev_info
;
1428 struct blk_shadow
*s
= &rinfo
->shadow
[*id
];
1429 struct copy_from_grant data
= {
1433 num_grant
= s
->req
.operation
== BLKIF_OP_INDIRECT
?
1434 s
->req
.u
.indirect
.nr_segments
: s
->req
.u
.rw
.nr_segments
;
1436 /* The I/O request may be split in two. */
1437 if (unlikely(s
->associated_id
!= NO_ASSOCIATED_ID
)) {
1438 struct blk_shadow
*s2
= &rinfo
->shadow
[s
->associated_id
];
1440 /* Keep the status of the current response in shadow. */
1441 s
->status
= blkif_rsp_to_req_status(bret
->status
);
1443 /* Wait the second response if not yet here. */
1444 if (s2
->status
== REQ_WAITING
)
1447 bret
->status
= blkif_get_final_status(s
->status
,
1451 * All the grants is stored in the first shadow in order
1452 * to make the completion code simpler.
1454 num_grant
+= s2
->req
.u
.rw
.nr_segments
;
1457 * The two responses may not come in order. Only the
1458 * first request will store the scatter-gather list.
1460 if (s2
->num_sg
!= 0) {
1461 /* Update "id" with the ID of the first response. */
1462 *id
= s
->associated_id
;
1467 * We don't need anymore the second request, so recycling
1470 if (add_id_to_freelist(rinfo
, s
->associated_id
))
1471 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1472 info
->gd
->disk_name
, s
->associated_id
);
1478 if (bret
->operation
== BLKIF_OP_READ
&& info
->feature_persistent
) {
1479 for_each_sg(s
->sg
, sg
, num_sg
, i
) {
1480 BUG_ON(sg
->offset
+ sg
->length
> PAGE_SIZE
);
1482 data
.bvec_offset
= sg
->offset
;
1483 data
.bvec_data
= kmap_atomic(sg_page(sg
));
1485 gnttab_foreach_grant_in_range(sg_page(sg
),
1488 blkif_copy_from_grant
,
1491 kunmap_atomic(data
.bvec_data
);
1494 /* Add the persistent grant into the list of free grants */
1495 for (i
= 0; i
< num_grant
; i
++) {
1496 if (gnttab_query_foreign_access(s
->grants_used
[i
]->gref
)) {
1498 * If the grant is still mapped by the backend (the
1499 * backend has chosen to make this grant persistent)
1500 * we add it at the head of the list, so it will be
1503 if (!info
->feature_persistent
)
1504 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1505 s
->grants_used
[i
]->gref
);
1506 list_add(&s
->grants_used
[i
]->node
, &rinfo
->grants
);
1507 rinfo
->persistent_gnts_c
++;
1510 * If the grant is not mapped by the backend we end the
1511 * foreign access and add it to the tail of the list,
1512 * so it will not be picked again unless we run out of
1513 * persistent grants.
1515 gnttab_end_foreign_access(s
->grants_used
[i
]->gref
, 0, 0UL);
1516 s
->grants_used
[i
]->gref
= GRANT_INVALID_REF
;
1517 list_add_tail(&s
->grants_used
[i
]->node
, &rinfo
->grants
);
1520 if (s
->req
.operation
== BLKIF_OP_INDIRECT
) {
1521 for (i
= 0; i
< INDIRECT_GREFS(num_grant
); i
++) {
1522 if (gnttab_query_foreign_access(s
->indirect_grants
[i
]->gref
)) {
1523 if (!info
->feature_persistent
)
1524 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1525 s
->indirect_grants
[i
]->gref
);
1526 list_add(&s
->indirect_grants
[i
]->node
, &rinfo
->grants
);
1527 rinfo
->persistent_gnts_c
++;
1529 struct page
*indirect_page
;
1531 gnttab_end_foreign_access(s
->indirect_grants
[i
]->gref
, 0, 0UL);
1533 * Add the used indirect page back to the list of
1534 * available pages for indirect grefs.
1536 if (!info
->feature_persistent
) {
1537 indirect_page
= s
->indirect_grants
[i
]->page
;
1538 list_add(&indirect_page
->lru
, &rinfo
->indirect_pages
);
1540 s
->indirect_grants
[i
]->gref
= GRANT_INVALID_REF
;
1541 list_add_tail(&s
->indirect_grants
[i
]->node
, &rinfo
->grants
);
1549 static irqreturn_t
blkif_interrupt(int irq
, void *dev_id
)
1551 struct request
*req
;
1552 struct blkif_response
*bret
;
1554 unsigned long flags
;
1555 struct blkfront_ring_info
*rinfo
= (struct blkfront_ring_info
*)dev_id
;
1556 struct blkfront_info
*info
= rinfo
->dev_info
;
1558 if (unlikely(info
->connected
!= BLKIF_STATE_CONNECTED
))
1561 spin_lock_irqsave(&rinfo
->ring_lock
, flags
);
1563 rp
= rinfo
->ring
.sring
->rsp_prod
;
1564 rmb(); /* Ensure we see queued responses up to 'rp'. */
1566 for (i
= rinfo
->ring
.rsp_cons
; i
!= rp
; i
++) {
1569 bret
= RING_GET_RESPONSE(&rinfo
->ring
, i
);
1572 * The backend has messed up and given us an id that we would
1573 * never have given to it (we stamp it up to BLK_RING_SIZE -
1574 * look in get_id_from_freelist.
1576 if (id
>= BLK_RING_SIZE(info
)) {
1577 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1578 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
1579 /* We can't safely get the 'struct request' as
1580 * the id is busted. */
1583 req
= rinfo
->shadow
[id
].request
;
1585 if (bret
->operation
!= BLKIF_OP_DISCARD
) {
1587 * We may need to wait for an extra response if the
1588 * I/O request is split in 2
1590 if (!blkif_completion(&id
, rinfo
, bret
))
1594 if (add_id_to_freelist(rinfo
, id
)) {
1595 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1596 info
->gd
->disk_name
, op_name(bret
->operation
), id
);
1600 if (bret
->status
== BLKIF_RSP_OKAY
)
1601 blkif_req(req
)->error
= BLK_STS_OK
;
1603 blkif_req(req
)->error
= BLK_STS_IOERR
;
1605 switch (bret
->operation
) {
1606 case BLKIF_OP_DISCARD
:
1607 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
1608 struct request_queue
*rq
= info
->rq
;
1609 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
1610 info
->gd
->disk_name
, op_name(bret
->operation
));
1611 blkif_req(req
)->error
= BLK_STS_NOTSUPP
;
1612 info
->feature_discard
= 0;
1613 info
->feature_secdiscard
= 0;
1614 queue_flag_clear(QUEUE_FLAG_DISCARD
, rq
);
1615 queue_flag_clear(QUEUE_FLAG_SECERASE
, rq
);
1618 case BLKIF_OP_FLUSH_DISKCACHE
:
1619 case BLKIF_OP_WRITE_BARRIER
:
1620 if (unlikely(bret
->status
== BLKIF_RSP_EOPNOTSUPP
)) {
1621 printk(KERN_WARNING
"blkfront: %s: %s op failed\n",
1622 info
->gd
->disk_name
, op_name(bret
->operation
));
1623 blkif_req(req
)->error
= BLK_STS_NOTSUPP
;
1625 if (unlikely(bret
->status
== BLKIF_RSP_ERROR
&&
1626 rinfo
->shadow
[id
].req
.u
.rw
.nr_segments
== 0)) {
1627 printk(KERN_WARNING
"blkfront: %s: empty %s op failed\n",
1628 info
->gd
->disk_name
, op_name(bret
->operation
));
1629 blkif_req(req
)->error
= BLK_STS_NOTSUPP
;
1631 if (unlikely(blkif_req(req
)->error
)) {
1632 if (blkif_req(req
)->error
== BLK_STS_NOTSUPP
)
1633 blkif_req(req
)->error
= BLK_STS_OK
;
1634 info
->feature_fua
= 0;
1635 info
->feature_flush
= 0;
1640 case BLKIF_OP_WRITE
:
1641 if (unlikely(bret
->status
!= BLKIF_RSP_OKAY
))
1642 dev_dbg(&info
->xbdev
->dev
, "Bad return from blkdev data "
1643 "request: %x\n", bret
->status
);
1650 blk_mq_complete_request(req
);
1653 rinfo
->ring
.rsp_cons
= i
;
1655 if (i
!= rinfo
->ring
.req_prod_pvt
) {
1657 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo
->ring
, more_to_do
);
1661 rinfo
->ring
.sring
->rsp_event
= i
+ 1;
1663 kick_pending_request_queues_locked(rinfo
);
1665 spin_unlock_irqrestore(&rinfo
->ring_lock
, flags
);
1671 static int setup_blkring(struct xenbus_device
*dev
,
1672 struct blkfront_ring_info
*rinfo
)
1674 struct blkif_sring
*sring
;
1676 struct blkfront_info
*info
= rinfo
->dev_info
;
1677 unsigned long ring_size
= info
->nr_ring_pages
* XEN_PAGE_SIZE
;
1678 grant_ref_t gref
[XENBUS_MAX_RING_GRANTS
];
1680 for (i
= 0; i
< info
->nr_ring_pages
; i
++)
1681 rinfo
->ring_ref
[i
] = GRANT_INVALID_REF
;
1683 sring
= (struct blkif_sring
*)__get_free_pages(GFP_NOIO
| __GFP_HIGH
,
1684 get_order(ring_size
));
1686 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
1689 SHARED_RING_INIT(sring
);
1690 FRONT_RING_INIT(&rinfo
->ring
, sring
, ring_size
);
1692 err
= xenbus_grant_ring(dev
, rinfo
->ring
.sring
, info
->nr_ring_pages
, gref
);
1694 free_pages((unsigned long)sring
, get_order(ring_size
));
1695 rinfo
->ring
.sring
= NULL
;
1698 for (i
= 0; i
< info
->nr_ring_pages
; i
++)
1699 rinfo
->ring_ref
[i
] = gref
[i
];
1701 err
= xenbus_alloc_evtchn(dev
, &rinfo
->evtchn
);
1705 err
= bind_evtchn_to_irqhandler(rinfo
->evtchn
, blkif_interrupt
, 0,
1708 xenbus_dev_fatal(dev
, err
,
1709 "bind_evtchn_to_irqhandler failed");
1716 blkif_free(info
, 0);
1721 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1722 * ring buffer may have multi pages depending on ->nr_ring_pages.
1724 static int write_per_ring_nodes(struct xenbus_transaction xbt
,
1725 struct blkfront_ring_info
*rinfo
, const char *dir
)
1729 const char *message
= NULL
;
1730 struct blkfront_info
*info
= rinfo
->dev_info
;
1732 if (info
->nr_ring_pages
== 1) {
1733 err
= xenbus_printf(xbt
, dir
, "ring-ref", "%u", rinfo
->ring_ref
[0]);
1735 message
= "writing ring-ref";
1736 goto abort_transaction
;
1739 for (i
= 0; i
< info
->nr_ring_pages
; i
++) {
1740 char ring_ref_name
[RINGREF_NAME_LEN
];
1742 snprintf(ring_ref_name
, RINGREF_NAME_LEN
, "ring-ref%u", i
);
1743 err
= xenbus_printf(xbt
, dir
, ring_ref_name
,
1744 "%u", rinfo
->ring_ref
[i
]);
1746 message
= "writing ring-ref";
1747 goto abort_transaction
;
1752 err
= xenbus_printf(xbt
, dir
, "event-channel", "%u", rinfo
->evtchn
);
1754 message
= "writing event-channel";
1755 goto abort_transaction
;
1761 xenbus_transaction_end(xbt
, 1);
1763 xenbus_dev_fatal(info
->xbdev
, err
, "%s", message
);
1768 /* Common code used when first setting up, and when resuming. */
1769 static int talk_to_blkback(struct xenbus_device
*dev
,
1770 struct blkfront_info
*info
)
1772 const char *message
= NULL
;
1773 struct xenbus_transaction xbt
;
1775 unsigned int i
, max_page_order
;
1776 unsigned int ring_page_order
;
1781 max_page_order
= xenbus_read_unsigned(info
->xbdev
->otherend
,
1782 "max-ring-page-order", 0);
1783 ring_page_order
= min(xen_blkif_max_ring_order
, max_page_order
);
1784 info
->nr_ring_pages
= 1 << ring_page_order
;
1786 err
= negotiate_mq(info
);
1788 goto destroy_blkring
;
1790 for (i
= 0; i
< info
->nr_rings
; i
++) {
1791 struct blkfront_ring_info
*rinfo
= &info
->rinfo
[i
];
1793 /* Create shared ring, alloc event channel. */
1794 err
= setup_blkring(dev
, rinfo
);
1796 goto destroy_blkring
;
1800 err
= xenbus_transaction_start(&xbt
);
1802 xenbus_dev_fatal(dev
, err
, "starting transaction");
1803 goto destroy_blkring
;
1806 if (info
->nr_ring_pages
> 1) {
1807 err
= xenbus_printf(xbt
, dev
->nodename
, "ring-page-order", "%u",
1810 message
= "writing ring-page-order";
1811 goto abort_transaction
;
1815 /* We already got the number of queues/rings in _probe */
1816 if (info
->nr_rings
== 1) {
1817 err
= write_per_ring_nodes(xbt
, &info
->rinfo
[0], dev
->nodename
);
1819 goto destroy_blkring
;
1824 err
= xenbus_printf(xbt
, dev
->nodename
, "multi-queue-num-queues", "%u",
1827 message
= "writing multi-queue-num-queues";
1828 goto abort_transaction
;
1831 pathsize
= strlen(dev
->nodename
) + QUEUE_NAME_LEN
;
1832 path
= kmalloc(pathsize
, GFP_KERNEL
);
1835 message
= "ENOMEM while writing ring references";
1836 goto abort_transaction
;
1839 for (i
= 0; i
< info
->nr_rings
; i
++) {
1840 memset(path
, 0, pathsize
);
1841 snprintf(path
, pathsize
, "%s/queue-%u", dev
->nodename
, i
);
1842 err
= write_per_ring_nodes(xbt
, &info
->rinfo
[i
], path
);
1845 goto destroy_blkring
;
1850 err
= xenbus_printf(xbt
, dev
->nodename
, "protocol", "%s",
1851 XEN_IO_PROTO_ABI_NATIVE
);
1853 message
= "writing protocol";
1854 goto abort_transaction
;
1856 err
= xenbus_printf(xbt
, dev
->nodename
,
1857 "feature-persistent", "%u", 1);
1860 "writing persistent grants feature to xenbus");
1862 err
= xenbus_transaction_end(xbt
, 0);
1866 xenbus_dev_fatal(dev
, err
, "completing transaction");
1867 goto destroy_blkring
;
1870 for (i
= 0; i
< info
->nr_rings
; i
++) {
1872 struct blkfront_ring_info
*rinfo
= &info
->rinfo
[i
];
1874 for (j
= 0; j
< BLK_RING_SIZE(info
); j
++)
1875 rinfo
->shadow
[j
].req
.u
.rw
.id
= j
+ 1;
1876 rinfo
->shadow
[BLK_RING_SIZE(info
)-1].req
.u
.rw
.id
= 0x0fffffff;
1878 xenbus_switch_state(dev
, XenbusStateInitialised
);
1883 xenbus_transaction_end(xbt
, 1);
1885 xenbus_dev_fatal(dev
, err
, "%s", message
);
1887 blkif_free(info
, 0);
1890 dev_set_drvdata(&dev
->dev
, NULL
);
1895 static int negotiate_mq(struct blkfront_info
*info
)
1897 unsigned int backend_max_queues
;
1900 BUG_ON(info
->nr_rings
);
1902 /* Check if backend supports multiple queues. */
1903 backend_max_queues
= xenbus_read_unsigned(info
->xbdev
->otherend
,
1904 "multi-queue-max-queues", 1);
1905 info
->nr_rings
= min(backend_max_queues
, xen_blkif_max_queues
);
1906 /* We need at least one ring. */
1907 if (!info
->nr_rings
)
1910 info
->rinfo
= kzalloc(sizeof(struct blkfront_ring_info
) * info
->nr_rings
, GFP_KERNEL
);
1912 xenbus_dev_fatal(info
->xbdev
, -ENOMEM
, "allocating ring_info structure");
1916 for (i
= 0; i
< info
->nr_rings
; i
++) {
1917 struct blkfront_ring_info
*rinfo
;
1919 rinfo
= &info
->rinfo
[i
];
1920 INIT_LIST_HEAD(&rinfo
->indirect_pages
);
1921 INIT_LIST_HEAD(&rinfo
->grants
);
1922 rinfo
->dev_info
= info
;
1923 INIT_WORK(&rinfo
->work
, blkif_restart_queue
);
1924 spin_lock_init(&rinfo
->ring_lock
);
1929 * Entry point to this code when a new device is created. Allocate the basic
1930 * structures and the ring buffer for communication with the backend, and
1931 * inform the backend of the appropriate details for those. Switch to
1932 * Initialised state.
1934 static int blkfront_probe(struct xenbus_device
*dev
,
1935 const struct xenbus_device_id
*id
)
1938 struct blkfront_info
*info
;
1940 /* FIXME: Use dynamic device id if this is not set. */
1941 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
,
1942 "virtual-device", "%i", &vdevice
);
1944 /* go looking in the extended area instead */
1945 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "virtual-device-ext",
1948 xenbus_dev_fatal(dev
, err
, "reading virtual-device");
1953 if (xen_hvm_domain()) {
1956 /* no unplug has been done: do not hook devices != xen vbds */
1957 if (xen_has_pv_and_legacy_disk_devices()) {
1960 if (!VDEV_IS_EXTENDED(vdevice
))
1961 major
= BLKIF_MAJOR(vdevice
);
1963 major
= XENVBD_MAJOR
;
1965 if (major
!= XENVBD_MAJOR
) {
1967 "%s: HVM does not support vbd %d as xen block device\n",
1972 /* do not create a PV cdrom device if we are an HVM guest */
1973 type
= xenbus_read(XBT_NIL
, dev
->nodename
, "device-type", &len
);
1976 if (strncmp(type
, "cdrom", 5) == 0) {
1982 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1984 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
1990 mutex_init(&info
->mutex
);
1991 info
->vdevice
= vdevice
;
1992 info
->connected
= BLKIF_STATE_DISCONNECTED
;
1994 /* Front end dir is a number, which is used as the id. */
1995 info
->handle
= simple_strtoul(strrchr(dev
->nodename
, '/')+1, NULL
, 0);
1996 dev_set_drvdata(&dev
->dev
, info
);
2001 static int blkif_recover(struct blkfront_info
*info
)
2003 unsigned int r_index
;
2004 struct request
*req
, *n
;
2009 blkfront_gather_backend_features(info
);
2010 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2011 blkif_set_queue_limits(info
);
2012 segs
= info
->max_indirect_segments
? : BLKIF_MAX_SEGMENTS_PER_REQUEST
;
2013 blk_queue_max_segments(info
->rq
, segs
/ GRANTS_PER_PSEG
);
2015 for (r_index
= 0; r_index
< info
->nr_rings
; r_index
++) {
2016 struct blkfront_ring_info
*rinfo
= &info
->rinfo
[r_index
];
2018 rc
= blkfront_setup_indirect(rinfo
);
2022 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
2024 /* Now safe for us to use the shared ring */
2025 info
->connected
= BLKIF_STATE_CONNECTED
;
2027 for (r_index
= 0; r_index
< info
->nr_rings
; r_index
++) {
2028 struct blkfront_ring_info
*rinfo
;
2030 rinfo
= &info
->rinfo
[r_index
];
2031 /* Kick any other new requests queued since we resumed */
2032 kick_pending_request_queues(rinfo
);
2035 list_for_each_entry_safe(req
, n
, &info
->requests
, queuelist
) {
2036 /* Requeue pending requests (flush or discard) */
2037 list_del_init(&req
->queuelist
);
2038 BUG_ON(req
->nr_phys_segments
> segs
);
2039 blk_mq_requeue_request(req
, false);
2041 blk_mq_start_stopped_hw_queues(info
->rq
, true);
2042 blk_mq_kick_requeue_list(info
->rq
);
2044 while ((bio
= bio_list_pop(&info
->bio_list
)) != NULL
) {
2045 /* Traverse the list of pending bios and re-queue them */
2053 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2054 * driver restart. We tear down our blkif structure and recreate it, but
2055 * leave the device-layer structures intact so that this is transparent to the
2056 * rest of the kernel.
2058 static int blkfront_resume(struct xenbus_device
*dev
)
2060 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2064 dev_dbg(&dev
->dev
, "blkfront_resume: %s\n", dev
->nodename
);
2066 bio_list_init(&info
->bio_list
);
2067 INIT_LIST_HEAD(&info
->requests
);
2068 for (i
= 0; i
< info
->nr_rings
; i
++) {
2069 struct blkfront_ring_info
*rinfo
= &info
->rinfo
[i
];
2070 struct bio_list merge_bio
;
2071 struct blk_shadow
*shadow
= rinfo
->shadow
;
2073 for (j
= 0; j
< BLK_RING_SIZE(info
); j
++) {
2075 if (!shadow
[j
].request
)
2079 * Get the bios in the request so we can re-queue them.
2081 if (req_op(shadow
[j
].request
) == REQ_OP_FLUSH
||
2082 req_op(shadow
[j
].request
) == REQ_OP_DISCARD
||
2083 req_op(shadow
[j
].request
) == REQ_OP_SECURE_ERASE
||
2084 shadow
[j
].request
->cmd_flags
& REQ_FUA
) {
2086 * Flush operations don't contain bios, so
2087 * we need to requeue the whole request
2089 * XXX: but this doesn't make any sense for a
2090 * write with the FUA flag set..
2092 list_add(&shadow
[j
].request
->queuelist
, &info
->requests
);
2095 merge_bio
.head
= shadow
[j
].request
->bio
;
2096 merge_bio
.tail
= shadow
[j
].request
->biotail
;
2097 bio_list_merge(&info
->bio_list
, &merge_bio
);
2098 shadow
[j
].request
->bio
= NULL
;
2099 blk_mq_end_request(shadow
[j
].request
, BLK_STS_OK
);
2103 blkif_free(info
, info
->connected
== BLKIF_STATE_CONNECTED
);
2105 err
= talk_to_blkback(dev
, info
);
2107 blk_mq_update_nr_hw_queues(&info
->tag_set
, info
->nr_rings
);
2110 * We have to wait for the backend to switch to
2111 * connected state, since we want to read which
2112 * features it supports.
2118 static void blkfront_closing(struct blkfront_info
*info
)
2120 struct xenbus_device
*xbdev
= info
->xbdev
;
2121 struct block_device
*bdev
= NULL
;
2123 mutex_lock(&info
->mutex
);
2125 if (xbdev
->state
== XenbusStateClosing
) {
2126 mutex_unlock(&info
->mutex
);
2131 bdev
= bdget_disk(info
->gd
, 0);
2133 mutex_unlock(&info
->mutex
);
2136 xenbus_frontend_closed(xbdev
);
2140 mutex_lock(&bdev
->bd_mutex
);
2142 if (bdev
->bd_openers
) {
2143 xenbus_dev_error(xbdev
, -EBUSY
,
2144 "Device in use; refusing to close");
2145 xenbus_switch_state(xbdev
, XenbusStateClosing
);
2147 xlvbd_release_gendisk(info
);
2148 xenbus_frontend_closed(xbdev
);
2151 mutex_unlock(&bdev
->bd_mutex
);
2155 static void blkfront_setup_discard(struct blkfront_info
*info
)
2158 unsigned int discard_granularity
;
2159 unsigned int discard_alignment
;
2161 info
->feature_discard
= 1;
2162 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
2163 "discard-granularity", "%u", &discard_granularity
,
2164 "discard-alignment", "%u", &discard_alignment
,
2167 info
->discard_granularity
= discard_granularity
;
2168 info
->discard_alignment
= discard_alignment
;
2170 info
->feature_secdiscard
=
2171 !!xenbus_read_unsigned(info
->xbdev
->otherend
, "discard-secure",
2175 static int blkfront_setup_indirect(struct blkfront_ring_info
*rinfo
)
2177 unsigned int psegs
, grants
;
2179 struct blkfront_info
*info
= rinfo
->dev_info
;
2181 if (info
->max_indirect_segments
== 0) {
2183 grants
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
2186 * When an extra req is required, the maximum
2187 * grants supported is related to the size of the
2188 * Linux block segment.
2190 grants
= GRANTS_PER_PSEG
;
2194 grants
= info
->max_indirect_segments
;
2195 psegs
= DIV_ROUND_UP(grants
, GRANTS_PER_PSEG
);
2197 err
= fill_grant_buffer(rinfo
,
2198 (grants
+ INDIRECT_GREFS(grants
)) * BLK_RING_SIZE(info
));
2202 if (!info
->feature_persistent
&& info
->max_indirect_segments
) {
2204 * We are using indirect descriptors but not persistent
2205 * grants, we need to allocate a set of pages that can be
2206 * used for mapping indirect grefs
2208 int num
= INDIRECT_GREFS(grants
) * BLK_RING_SIZE(info
);
2210 BUG_ON(!list_empty(&rinfo
->indirect_pages
));
2211 for (i
= 0; i
< num
; i
++) {
2212 struct page
*indirect_page
= alloc_page(GFP_NOIO
);
2215 list_add(&indirect_page
->lru
, &rinfo
->indirect_pages
);
2219 for (i
= 0; i
< BLK_RING_SIZE(info
); i
++) {
2220 rinfo
->shadow
[i
].grants_used
= kzalloc(
2221 sizeof(rinfo
->shadow
[i
].grants_used
[0]) * grants
,
2223 rinfo
->shadow
[i
].sg
= kzalloc(sizeof(rinfo
->shadow
[i
].sg
[0]) * psegs
, GFP_NOIO
);
2224 if (info
->max_indirect_segments
)
2225 rinfo
->shadow
[i
].indirect_grants
= kzalloc(
2226 sizeof(rinfo
->shadow
[i
].indirect_grants
[0]) *
2227 INDIRECT_GREFS(grants
),
2229 if ((rinfo
->shadow
[i
].grants_used
== NULL
) ||
2230 (rinfo
->shadow
[i
].sg
== NULL
) ||
2231 (info
->max_indirect_segments
&&
2232 (rinfo
->shadow
[i
].indirect_grants
== NULL
)))
2234 sg_init_table(rinfo
->shadow
[i
].sg
, psegs
);
2241 for (i
= 0; i
< BLK_RING_SIZE(info
); i
++) {
2242 kfree(rinfo
->shadow
[i
].grants_used
);
2243 rinfo
->shadow
[i
].grants_used
= NULL
;
2244 kfree(rinfo
->shadow
[i
].sg
);
2245 rinfo
->shadow
[i
].sg
= NULL
;
2246 kfree(rinfo
->shadow
[i
].indirect_grants
);
2247 rinfo
->shadow
[i
].indirect_grants
= NULL
;
2249 if (!list_empty(&rinfo
->indirect_pages
)) {
2250 struct page
*indirect_page
, *n
;
2251 list_for_each_entry_safe(indirect_page
, n
, &rinfo
->indirect_pages
, lru
) {
2252 list_del(&indirect_page
->lru
);
2253 __free_page(indirect_page
);
2260 * Gather all backend feature-*
2262 static void blkfront_gather_backend_features(struct blkfront_info
*info
)
2264 unsigned int indirect_segments
;
2266 info
->feature_flush
= 0;
2267 info
->feature_fua
= 0;
2270 * If there's no "feature-barrier" defined, then it means
2271 * we're dealing with a very old backend which writes
2272 * synchronously; nothing to do.
2274 * If there are barriers, then we use flush.
2276 if (xenbus_read_unsigned(info
->xbdev
->otherend
, "feature-barrier", 0)) {
2277 info
->feature_flush
= 1;
2278 info
->feature_fua
= 1;
2282 * And if there is "feature-flush-cache" use that above
2285 if (xenbus_read_unsigned(info
->xbdev
->otherend
, "feature-flush-cache",
2287 info
->feature_flush
= 1;
2288 info
->feature_fua
= 0;
2291 if (xenbus_read_unsigned(info
->xbdev
->otherend
, "feature-discard", 0))
2292 blkfront_setup_discard(info
);
2294 info
->feature_persistent
=
2295 !!xenbus_read_unsigned(info
->xbdev
->otherend
,
2296 "feature-persistent", 0);
2298 indirect_segments
= xenbus_read_unsigned(info
->xbdev
->otherend
,
2299 "feature-max-indirect-segments", 0);
2300 if (indirect_segments
> xen_blkif_max_segments
)
2301 indirect_segments
= xen_blkif_max_segments
;
2302 if (indirect_segments
<= BLKIF_MAX_SEGMENTS_PER_REQUEST
)
2303 indirect_segments
= 0;
2304 info
->max_indirect_segments
= indirect_segments
;
2308 * Invoked when the backend is finally 'ready' (and has told produced
2309 * the details about the physical device - #sectors, size, etc).
2311 static void blkfront_connect(struct blkfront_info
*info
)
2313 unsigned long long sectors
;
2314 unsigned long sector_size
;
2315 unsigned int physical_sector_size
;
2317 char *envp
[] = { "RESIZE=1", NULL
};
2320 switch (info
->connected
) {
2321 case BLKIF_STATE_CONNECTED
:
2323 * Potentially, the back-end may be signalling
2324 * a capacity change; update the capacity.
2326 err
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
2327 "sectors", "%Lu", §ors
);
2328 if (XENBUS_EXIST_ERR(err
))
2330 printk(KERN_INFO
"Setting capacity to %Lu\n",
2332 set_capacity(info
->gd
, sectors
);
2333 revalidate_disk(info
->gd
);
2334 kobject_uevent_env(&disk_to_dev(info
->gd
)->kobj
,
2338 case BLKIF_STATE_SUSPENDED
:
2340 * If we are recovering from suspension, we need to wait
2341 * for the backend to announce it's features before
2342 * reconnecting, at least we need to know if the backend
2343 * supports indirect descriptors, and how many.
2345 blkif_recover(info
);
2352 dev_dbg(&info
->xbdev
->dev
, "%s:%s.\n",
2353 __func__
, info
->xbdev
->otherend
);
2355 err
= xenbus_gather(XBT_NIL
, info
->xbdev
->otherend
,
2356 "sectors", "%llu", §ors
,
2357 "info", "%u", &binfo
,
2358 "sector-size", "%lu", §or_size
,
2361 xenbus_dev_fatal(info
->xbdev
, err
,
2362 "reading backend fields at %s",
2363 info
->xbdev
->otherend
);
2368 * physcial-sector-size is a newer field, so old backends may not
2369 * provide this. Assume physical sector size to be the same as
2370 * sector_size in that case.
2372 physical_sector_size
= xenbus_read_unsigned(info
->xbdev
->otherend
,
2373 "physical-sector-size",
2375 blkfront_gather_backend_features(info
);
2376 for (i
= 0; i
< info
->nr_rings
; i
++) {
2377 err
= blkfront_setup_indirect(&info
->rinfo
[i
]);
2379 xenbus_dev_fatal(info
->xbdev
, err
, "setup_indirect at %s",
2380 info
->xbdev
->otherend
);
2381 blkif_free(info
, 0);
2386 err
= xlvbd_alloc_gendisk(sectors
, info
, binfo
, sector_size
,
2387 physical_sector_size
);
2389 xenbus_dev_fatal(info
->xbdev
, err
, "xlvbd_add at %s",
2390 info
->xbdev
->otherend
);
2394 xenbus_switch_state(info
->xbdev
, XenbusStateConnected
);
2396 /* Kick pending requests. */
2397 info
->connected
= BLKIF_STATE_CONNECTED
;
2398 for (i
= 0; i
< info
->nr_rings
; i
++)
2399 kick_pending_request_queues(&info
->rinfo
[i
]);
2401 device_add_disk(&info
->xbdev
->dev
, info
->gd
);
2407 blkif_free(info
, 0);
2412 * Callback received when the backend's state changes.
2414 static void blkback_changed(struct xenbus_device
*dev
,
2415 enum xenbus_state backend_state
)
2417 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2419 dev_dbg(&dev
->dev
, "blkfront:blkback_changed to state %d.\n", backend_state
);
2421 switch (backend_state
) {
2422 case XenbusStateInitWait
:
2423 if (dev
->state
!= XenbusStateInitialising
)
2425 if (talk_to_blkback(dev
, info
))
2427 case XenbusStateInitialising
:
2428 case XenbusStateInitialised
:
2429 case XenbusStateReconfiguring
:
2430 case XenbusStateReconfigured
:
2431 case XenbusStateUnknown
:
2434 case XenbusStateConnected
:
2436 * talk_to_blkback sets state to XenbusStateInitialised
2437 * and blkfront_connect sets it to XenbusStateConnected
2438 * (if connection went OK).
2440 * If the backend (or toolstack) decides to poke at backend
2441 * state (and re-trigger the watch by setting the state repeatedly
2442 * to XenbusStateConnected (4)) we need to deal with this.
2443 * This is allowed as this is used to communicate to the guest
2444 * that the size of disk has changed!
2446 if ((dev
->state
!= XenbusStateInitialised
) &&
2447 (dev
->state
!= XenbusStateConnected
)) {
2448 if (talk_to_blkback(dev
, info
))
2452 blkfront_connect(info
);
2455 case XenbusStateClosed
:
2456 if (dev
->state
== XenbusStateClosed
)
2459 case XenbusStateClosing
:
2461 blkfront_closing(info
);
2466 static int blkfront_remove(struct xenbus_device
*xbdev
)
2468 struct blkfront_info
*info
= dev_get_drvdata(&xbdev
->dev
);
2469 struct block_device
*bdev
= NULL
;
2470 struct gendisk
*disk
;
2472 dev_dbg(&xbdev
->dev
, "%s removed", xbdev
->nodename
);
2474 blkif_free(info
, 0);
2476 mutex_lock(&info
->mutex
);
2480 bdev
= bdget_disk(disk
, 0);
2483 mutex_unlock(&info
->mutex
);
2491 * The xbdev was removed before we reached the Closed
2492 * state. See if it's safe to remove the disk. If the bdev
2493 * isn't closed yet, we let release take care of it.
2496 mutex_lock(&bdev
->bd_mutex
);
2497 info
= disk
->private_data
;
2499 dev_warn(disk_to_dev(disk
),
2500 "%s was hot-unplugged, %d stale handles\n",
2501 xbdev
->nodename
, bdev
->bd_openers
);
2503 if (info
&& !bdev
->bd_openers
) {
2504 xlvbd_release_gendisk(info
);
2505 disk
->private_data
= NULL
;
2509 mutex_unlock(&bdev
->bd_mutex
);
2515 static int blkfront_is_ready(struct xenbus_device
*dev
)
2517 struct blkfront_info
*info
= dev_get_drvdata(&dev
->dev
);
2519 return info
->is_ready
&& info
->xbdev
;
2522 static int blkif_open(struct block_device
*bdev
, fmode_t mode
)
2524 struct gendisk
*disk
= bdev
->bd_disk
;
2525 struct blkfront_info
*info
;
2528 mutex_lock(&blkfront_mutex
);
2530 info
= disk
->private_data
;
2537 mutex_lock(&info
->mutex
);
2540 /* xbdev is closed */
2543 mutex_unlock(&info
->mutex
);
2546 mutex_unlock(&blkfront_mutex
);
2550 static void blkif_release(struct gendisk
*disk
, fmode_t mode
)
2552 struct blkfront_info
*info
= disk
->private_data
;
2553 struct block_device
*bdev
;
2554 struct xenbus_device
*xbdev
;
2556 mutex_lock(&blkfront_mutex
);
2558 bdev
= bdget_disk(disk
, 0);
2561 WARN(1, "Block device %s yanked out from us!\n", disk
->disk_name
);
2564 if (bdev
->bd_openers
)
2568 * Check if we have been instructed to close. We will have
2569 * deferred this request, because the bdev was still open.
2572 mutex_lock(&info
->mutex
);
2573 xbdev
= info
->xbdev
;
2575 if (xbdev
&& xbdev
->state
== XenbusStateClosing
) {
2576 /* pending switch to state closed */
2577 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
2578 xlvbd_release_gendisk(info
);
2579 xenbus_frontend_closed(info
->xbdev
);
2582 mutex_unlock(&info
->mutex
);
2585 /* sudden device removal */
2586 dev_info(disk_to_dev(bdev
->bd_disk
), "releasing disk\n");
2587 xlvbd_release_gendisk(info
);
2588 disk
->private_data
= NULL
;
2595 mutex_unlock(&blkfront_mutex
);
2598 static const struct block_device_operations xlvbd_block_fops
=
2600 .owner
= THIS_MODULE
,
2602 .release
= blkif_release
,
2603 .getgeo
= blkif_getgeo
,
2604 .ioctl
= blkif_ioctl
,
2608 static const struct xenbus_device_id blkfront_ids
[] = {
2613 static struct xenbus_driver blkfront_driver
= {
2614 .ids
= blkfront_ids
,
2615 .probe
= blkfront_probe
,
2616 .remove
= blkfront_remove
,
2617 .resume
= blkfront_resume
,
2618 .otherend_changed
= blkback_changed
,
2619 .is_ready
= blkfront_is_ready
,
2622 static int __init
xlblk_init(void)
2625 int nr_cpus
= num_online_cpus();
2630 if (xen_blkif_max_segments
< BLKIF_MAX_SEGMENTS_PER_REQUEST
)
2631 xen_blkif_max_segments
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
2633 if (xen_blkif_max_ring_order
> XENBUS_MAX_RING_GRANT_ORDER
) {
2634 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2635 xen_blkif_max_ring_order
, XENBUS_MAX_RING_GRANT_ORDER
);
2636 xen_blkif_max_ring_order
= XENBUS_MAX_RING_GRANT_ORDER
;
2639 if (xen_blkif_max_queues
> nr_cpus
) {
2640 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2641 xen_blkif_max_queues
, nr_cpus
);
2642 xen_blkif_max_queues
= nr_cpus
;
2645 if (!xen_has_pv_disk_devices())
2648 if (register_blkdev(XENVBD_MAJOR
, DEV_NAME
)) {
2649 printk(KERN_WARNING
"xen_blk: can't get major %d with name %s\n",
2650 XENVBD_MAJOR
, DEV_NAME
);
2654 ret
= xenbus_register_frontend(&blkfront_driver
);
2656 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
2662 module_init(xlblk_init
);
2665 static void __exit
xlblk_exit(void)
2667 xenbus_unregister_driver(&blkfront_driver
);
2668 unregister_blkdev(XENVBD_MAJOR
, DEV_NAME
);
2671 module_exit(xlblk_exit
);
2673 MODULE_DESCRIPTION("Xen virtual block device frontend");
2674 MODULE_LICENSE("GPL");
2675 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR
);
2676 MODULE_ALIAS("xen:vbd");
2677 MODULE_ALIAS("xenblk");