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>
49 #include <linux/workqueue.h>
50 #include <linux/sched/mm.h>
53 #include <xen/xenbus.h>
54 #include <xen/grant_table.h>
55 #include <xen/events.h>
57 #include <xen/platform_pci.h>
59 #include <xen/interface/grant_table.h>
60 #include <xen/interface/io/blkif.h>
61 #include <xen/interface/io/protocols.h>
63 #include <asm/xen/hypervisor.h>
66 * The minimal size of segment supported by the block framework is PAGE_SIZE.
67 * When Linux is using a different page size than Xen, it may not be possible
68 * to put all the data in a single segment.
69 * This can happen when the backend doesn't support indirect descriptor and
70 * therefore the maximum amount of data that a request can carry is
71 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
73 * Note that we only support one extra request. So the Linux page size
74 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
77 #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
80 BLKIF_STATE_DISCONNECTED,
81 BLKIF_STATE_CONNECTED,
82 BLKIF_STATE_SUSPENDED,
89 struct list_head node;
101 struct blkif_request req;
102 struct request *request;
103 struct grant **grants_used;
104 struct grant **indirect_grants;
105 struct scatterlist *sg;
107 enum blk_req_status status;
109 #define NO_ASSOCIATED_ID ~0UL
111 * Id of the sibling if we ever need 2 requests when handling a
114 unsigned long associated_id;
121 static inline struct blkif_req *blkif_req(struct request *rq)
123 return blk_mq_rq_to_pdu(rq);
126 static DEFINE_MUTEX(blkfront_mutex);
127 static const struct block_device_operations xlvbd_block_fops;
128 static struct delayed_work blkfront_work;
129 static LIST_HEAD(info_list);
132 * Maximum number of segments in indirect requests, the actual value used by
133 * the frontend driver is the minimum of this value and the value provided
134 * by the backend driver.
137 static unsigned int xen_blkif_max_segments = 32;
138 module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
139 MODULE_PARM_DESC(max_indirect_segments,
140 "Maximum amount of segments in indirect requests (default is 32)");
142 static unsigned int xen_blkif_max_queues = 4;
143 module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
144 MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
147 * Maximum order of pages to be used for the shared ring between front and
148 * backend, 4KB page granularity is used.
150 static unsigned int xen_blkif_max_ring_order;
151 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
152 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
154 #define BLK_RING_SIZE(info) \
155 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
157 #define BLK_MAX_RING_SIZE \
158 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
161 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
162 * characters are enough. Define to 20 to keep consistent with backend.
164 #define RINGREF_NAME_LEN (20)
166 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
168 #define QUEUE_NAME_LEN (17)
172 * Every blkfront device can associate with one or more blkfront_ring_info,
173 * depending on how many hardware queues/rings to be used.
175 struct blkfront_ring_info {
176 /* Lock to protect data in every ring buffer. */
177 spinlock_t ring_lock;
178 struct blkif_front_ring ring;
179 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
180 unsigned int evtchn, irq;
181 struct work_struct work;
182 struct gnttab_free_callback callback;
183 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
184 struct list_head indirect_pages;
185 struct list_head grants;
186 unsigned int persistent_gnts_c;
187 unsigned long shadow_free;
188 struct blkfront_info *dev_info;
192 * We have one of these per vbd, whether ide, scsi or 'other'. They
193 * hang in private_data off the gendisk structure. We may end up
194 * putting all kinds of interesting stuff here :-)
199 struct xenbus_device *xbdev;
202 unsigned int physical_sector_size;
205 enum blkif_state connected;
206 /* Number of pages per ring buffer. */
207 unsigned int nr_ring_pages;
208 struct request_queue *rq;
209 unsigned int feature_flush:1;
210 unsigned int feature_fua:1;
211 unsigned int feature_discard:1;
212 unsigned int feature_secdiscard:1;
213 unsigned int feature_persistent:1;
214 unsigned int discard_granularity;
215 unsigned int discard_alignment;
216 /* Number of 4KB segments handled */
217 unsigned int max_indirect_segments;
219 struct blk_mq_tag_set tag_set;
220 struct blkfront_ring_info *rinfo;
221 unsigned int nr_rings;
222 /* Save uncomplete reqs and bios for migration. */
223 struct list_head requests;
224 struct bio_list bio_list;
225 struct list_head info_list;
228 static unsigned int nr_minors;
229 static unsigned long *minors;
230 static DEFINE_SPINLOCK(minor_lock);
232 #define GRANT_INVALID_REF 0
234 #define PARTS_PER_DISK 16
235 #define PARTS_PER_EXT_DISK 256
237 #define BLKIF_MAJOR(dev) ((dev)>>8)
238 #define BLKIF_MINOR(dev) ((dev) & 0xff)
241 #define EXTENDED (1<<EXT_SHIFT)
242 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
243 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
244 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
245 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
246 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
247 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
249 #define DEV_NAME "xvd" /* name in /dev */
252 * Grants are always the same size as a Xen page (i.e 4KB).
253 * A physical segment is always the same size as a Linux page.
254 * Number of grants per physical segment
256 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
258 #define GRANTS_PER_INDIRECT_FRAME \
259 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
261 #define INDIRECT_GREFS(_grants) \
262 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
264 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
265 static void blkfront_gather_backend_features(struct blkfront_info *info);
266 static int negotiate_mq(struct blkfront_info *info);
268 static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
270 unsigned long free = rinfo->shadow_free;
272 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
273 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
274 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
278 static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
281 if (rinfo->shadow[id].req.u.rw.id != id)
283 if (rinfo->shadow[id].request == NULL)
285 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
286 rinfo->shadow[id].request = NULL;
287 rinfo->shadow_free = id;
291 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
293 struct blkfront_info *info = rinfo->dev_info;
294 struct page *granted_page;
295 struct grant *gnt_list_entry, *n;
299 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
303 if (info->feature_persistent) {
304 granted_page = alloc_page(GFP_NOIO);
306 kfree(gnt_list_entry);
309 gnt_list_entry->page = granted_page;
312 gnt_list_entry->gref = GRANT_INVALID_REF;
313 list_add(&gnt_list_entry->node, &rinfo->grants);
320 list_for_each_entry_safe(gnt_list_entry, n,
321 &rinfo->grants, node) {
322 list_del(&gnt_list_entry->node);
323 if (info->feature_persistent)
324 __free_page(gnt_list_entry->page);
325 kfree(gnt_list_entry);
332 static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
334 struct grant *gnt_list_entry;
336 BUG_ON(list_empty(&rinfo->grants));
337 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
339 list_del(&gnt_list_entry->node);
341 if (gnt_list_entry->gref != GRANT_INVALID_REF)
342 rinfo->persistent_gnts_c--;
344 return gnt_list_entry;
347 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
348 const struct blkfront_info *info)
350 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
351 info->xbdev->otherend_id,
352 gnt_list_entry->page,
356 static struct grant *get_grant(grant_ref_t *gref_head,
358 struct blkfront_ring_info *rinfo)
360 struct grant *gnt_list_entry = get_free_grant(rinfo);
361 struct blkfront_info *info = rinfo->dev_info;
363 if (gnt_list_entry->gref != GRANT_INVALID_REF)
364 return gnt_list_entry;
366 /* Assign a gref to this page */
367 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
368 BUG_ON(gnt_list_entry->gref == -ENOSPC);
369 if (info->feature_persistent)
370 grant_foreign_access(gnt_list_entry, info);
372 /* Grant access to the GFN passed by the caller */
373 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
374 info->xbdev->otherend_id,
378 return gnt_list_entry;
381 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
382 struct blkfront_ring_info *rinfo)
384 struct grant *gnt_list_entry = get_free_grant(rinfo);
385 struct blkfront_info *info = rinfo->dev_info;
387 if (gnt_list_entry->gref != GRANT_INVALID_REF)
388 return gnt_list_entry;
390 /* Assign a gref to this page */
391 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
392 BUG_ON(gnt_list_entry->gref == -ENOSPC);
393 if (!info->feature_persistent) {
394 struct page *indirect_page;
396 /* Fetch a pre-allocated page to use for indirect grefs */
397 BUG_ON(list_empty(&rinfo->indirect_pages));
398 indirect_page = list_first_entry(&rinfo->indirect_pages,
400 list_del(&indirect_page->lru);
401 gnt_list_entry->page = indirect_page;
403 grant_foreign_access(gnt_list_entry, info);
405 return gnt_list_entry;
408 static const char *op_name(int op)
410 static const char *const names[] = {
411 [BLKIF_OP_READ] = "read",
412 [BLKIF_OP_WRITE] = "write",
413 [BLKIF_OP_WRITE_BARRIER] = "barrier",
414 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
415 [BLKIF_OP_DISCARD] = "discard" };
417 if (op < 0 || op >= ARRAY_SIZE(names))
425 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
427 unsigned int end = minor + nr;
430 if (end > nr_minors) {
431 unsigned long *bitmap, *old;
433 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
438 spin_lock(&minor_lock);
439 if (end > nr_minors) {
441 memcpy(bitmap, minors,
442 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
444 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
447 spin_unlock(&minor_lock);
451 spin_lock(&minor_lock);
452 if (find_next_bit(minors, end, minor) >= end) {
453 bitmap_set(minors, minor, nr);
457 spin_unlock(&minor_lock);
462 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
464 unsigned int end = minor + nr;
466 BUG_ON(end > nr_minors);
467 spin_lock(&minor_lock);
468 bitmap_clear(minors, minor, nr);
469 spin_unlock(&minor_lock);
472 static void blkif_restart_queue_callback(void *arg)
474 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
475 schedule_work(&rinfo->work);
478 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
480 /* We don't have real geometry info, but let's at least return
481 values consistent with the size of the device */
482 sector_t nsect = get_capacity(bd->bd_disk);
483 sector_t cylinders = nsect;
487 sector_div(cylinders, hg->heads * hg->sectors);
488 hg->cylinders = cylinders;
489 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
490 hg->cylinders = 0xffff;
494 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
495 unsigned command, unsigned long argument)
497 struct blkfront_info *info = bdev->bd_disk->private_data;
500 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
501 command, (long)argument);
504 case CDROMMULTISESSION:
505 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
506 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
507 if (put_user(0, (char __user *)(argument + i)))
511 case CDROM_GET_CAPABILITY: {
512 struct gendisk *gd = info->gd;
513 if (gd->flags & GENHD_FL_CD)
519 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
521 return -EINVAL; /* same return as native Linux */
527 static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
529 struct blkif_request **ring_req)
533 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
534 rinfo->ring.req_prod_pvt++;
536 id = get_id_from_freelist(rinfo);
537 rinfo->shadow[id].request = req;
538 rinfo->shadow[id].status = REQ_PROCESSING;
539 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
541 rinfo->shadow[id].req.u.rw.id = id;
546 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
548 struct blkfront_info *info = rinfo->dev_info;
549 struct blkif_request *ring_req, *final_ring_req;
552 /* Fill out a communications ring structure. */
553 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
554 ring_req = &rinfo->shadow[id].req;
556 ring_req->operation = BLKIF_OP_DISCARD;
557 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
558 ring_req->u.discard.id = id;
559 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
560 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
561 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
563 ring_req->u.discard.flag = 0;
565 /* Copy the request to the ring page. */
566 *final_ring_req = *ring_req;
567 rinfo->shadow[id].status = REQ_WAITING;
572 struct setup_rw_req {
573 unsigned int grant_idx;
574 struct blkif_request_segment *segments;
575 struct blkfront_ring_info *rinfo;
576 struct blkif_request *ring_req;
577 grant_ref_t gref_head;
579 /* Only used when persistent grant is used and it's a read request */
581 unsigned int bvec_off;
584 bool require_extra_req;
585 struct blkif_request *extra_ring_req;
588 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
589 unsigned int len, void *data)
591 struct setup_rw_req *setup = data;
593 struct grant *gnt_list_entry;
594 unsigned int fsect, lsect;
595 /* Convenient aliases */
596 unsigned int grant_idx = setup->grant_idx;
597 struct blkif_request *ring_req = setup->ring_req;
598 struct blkfront_ring_info *rinfo = setup->rinfo;
600 * We always use the shadow of the first request to store the list
601 * of grant associated to the block I/O request. This made the
602 * completion more easy to handle even if the block I/O request is
605 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
607 if (unlikely(setup->require_extra_req &&
608 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
610 * We are using the second request, setup grant_idx
611 * to be the index of the segment array.
613 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
614 ring_req = setup->extra_ring_req;
617 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
618 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
620 kunmap_atomic(setup->segments);
622 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
623 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
624 shadow->indirect_grants[n] = gnt_list_entry;
625 setup->segments = kmap_atomic(gnt_list_entry->page);
626 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
629 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
630 ref = gnt_list_entry->gref;
632 * All the grants are stored in the shadow of the first
633 * request. Therefore we have to use the global index.
635 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
637 if (setup->need_copy) {
640 shared_data = kmap_atomic(gnt_list_entry->page);
642 * this does not wipe data stored outside the
643 * range sg->offset..sg->offset+sg->length.
644 * Therefore, blkback *could* see data from
645 * previous requests. This is OK as long as
646 * persistent grants are shared with just one
647 * domain. It may need refactoring if this
650 memcpy(shared_data + offset,
651 setup->bvec_data + setup->bvec_off,
654 kunmap_atomic(shared_data);
655 setup->bvec_off += len;
659 lsect = fsect + (len >> 9) - 1;
660 if (ring_req->operation != BLKIF_OP_INDIRECT) {
661 ring_req->u.rw.seg[grant_idx] =
662 (struct blkif_request_segment) {
665 .last_sect = lsect };
667 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
668 (struct blkif_request_segment) {
671 .last_sect = lsect };
674 (setup->grant_idx)++;
677 static void blkif_setup_extra_req(struct blkif_request *first,
678 struct blkif_request *second)
680 uint16_t nr_segments = first->u.rw.nr_segments;
683 * The second request is only present when the first request uses
684 * all its segments. It's always the continuity of the first one.
686 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
688 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
689 second->u.rw.sector_number = first->u.rw.sector_number +
690 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
692 second->u.rw.handle = first->u.rw.handle;
693 second->operation = first->operation;
696 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
698 struct blkfront_info *info = rinfo->dev_info;
699 struct blkif_request *ring_req, *extra_ring_req = NULL;
700 struct blkif_request *final_ring_req, *final_extra_ring_req = NULL;
701 unsigned long id, extra_id = NO_ASSOCIATED_ID;
702 bool require_extra_req = false;
704 struct setup_rw_req setup = {
708 .need_copy = rq_data_dir(req) && info->feature_persistent,
712 * Used to store if we are able to queue the request by just using
713 * existing persistent grants, or if we have to get new grants,
714 * as there are not sufficiently many free.
716 bool new_persistent_gnts = false;
717 struct scatterlist *sg;
718 int num_sg, max_grefs, num_grant;
720 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
721 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
723 * If we are using indirect segments we need to account
724 * for the indirect grefs used in the request.
726 max_grefs += INDIRECT_GREFS(max_grefs);
728 /* Check if we have enough persistent grants to allocate a requests */
729 if (rinfo->persistent_gnts_c < max_grefs) {
730 new_persistent_gnts = true;
732 if (gnttab_alloc_grant_references(
733 max_grefs - rinfo->persistent_gnts_c,
734 &setup.gref_head) < 0) {
735 gnttab_request_free_callback(
737 blkif_restart_queue_callback,
739 max_grefs - rinfo->persistent_gnts_c);
744 /* Fill out a communications ring structure. */
745 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
746 ring_req = &rinfo->shadow[id].req;
748 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
750 /* Calculate the number of grant used */
751 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
752 num_grant += gnttab_count_grant(sg->offset, sg->length);
754 require_extra_req = info->max_indirect_segments == 0 &&
755 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
756 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
758 rinfo->shadow[id].num_sg = num_sg;
759 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
760 likely(!require_extra_req)) {
762 * The indirect operation can only be a BLKIF_OP_READ or
765 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
766 ring_req->operation = BLKIF_OP_INDIRECT;
767 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
768 BLKIF_OP_WRITE : BLKIF_OP_READ;
769 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
770 ring_req->u.indirect.handle = info->handle;
771 ring_req->u.indirect.nr_segments = num_grant;
773 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
774 ring_req->u.rw.handle = info->handle;
775 ring_req->operation = rq_data_dir(req) ?
776 BLKIF_OP_WRITE : BLKIF_OP_READ;
777 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
779 * Ideally we can do an unordered flush-to-disk.
780 * In case the backend onlysupports barriers, use that.
781 * A barrier request a superset of FUA, so we can
782 * implement it the same way. (It's also a FLUSH+FUA,
783 * since it is guaranteed ordered WRT previous writes.)
785 if (info->feature_flush && info->feature_fua)
786 ring_req->operation =
787 BLKIF_OP_WRITE_BARRIER;
788 else if (info->feature_flush)
789 ring_req->operation =
790 BLKIF_OP_FLUSH_DISKCACHE;
792 ring_req->operation = 0;
794 ring_req->u.rw.nr_segments = num_grant;
795 if (unlikely(require_extra_req)) {
796 extra_id = blkif_ring_get_request(rinfo, req,
797 &final_extra_ring_req);
798 extra_ring_req = &rinfo->shadow[extra_id].req;
801 * Only the first request contains the scatter-gather
804 rinfo->shadow[extra_id].num_sg = 0;
806 blkif_setup_extra_req(ring_req, extra_ring_req);
808 /* Link the 2 requests together */
809 rinfo->shadow[extra_id].associated_id = id;
810 rinfo->shadow[id].associated_id = extra_id;
814 setup.ring_req = ring_req;
817 setup.require_extra_req = require_extra_req;
818 if (unlikely(require_extra_req))
819 setup.extra_ring_req = extra_ring_req;
821 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
822 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
824 if (setup.need_copy) {
825 setup.bvec_off = sg->offset;
826 setup.bvec_data = kmap_atomic(sg_page(sg));
829 gnttab_foreach_grant_in_range(sg_page(sg),
832 blkif_setup_rw_req_grant,
836 kunmap_atomic(setup.bvec_data);
839 kunmap_atomic(setup.segments);
841 /* Copy request(s) to the ring page. */
842 *final_ring_req = *ring_req;
843 rinfo->shadow[id].status = REQ_WAITING;
844 if (unlikely(require_extra_req)) {
845 *final_extra_ring_req = *extra_ring_req;
846 rinfo->shadow[extra_id].status = REQ_WAITING;
849 if (new_persistent_gnts)
850 gnttab_free_grant_references(setup.gref_head);
856 * Generate a Xen blkfront IO request from a blk layer request. Reads
857 * and writes are handled as expected.
859 * @req: a request struct
861 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
863 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
866 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
867 req_op(req) == REQ_OP_SECURE_ERASE))
868 return blkif_queue_discard_req(req, rinfo);
870 return blkif_queue_rw_req(req, rinfo);
873 static inline void flush_requests(struct blkfront_ring_info *rinfo)
877 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
880 notify_remote_via_irq(rinfo->irq);
883 static inline bool blkif_request_flush_invalid(struct request *req,
884 struct blkfront_info *info)
886 return (blk_rq_is_passthrough(req) ||
887 ((req_op(req) == REQ_OP_FLUSH) &&
888 !info->feature_flush) ||
889 ((req->cmd_flags & REQ_FUA) &&
890 !info->feature_fua));
893 static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
894 const struct blk_mq_queue_data *qd)
897 int qid = hctx->queue_num;
898 struct blkfront_info *info = hctx->queue->queuedata;
899 struct blkfront_ring_info *rinfo = NULL;
901 BUG_ON(info->nr_rings <= qid);
902 rinfo = &info->rinfo[qid];
903 blk_mq_start_request(qd->rq);
904 spin_lock_irqsave(&rinfo->ring_lock, flags);
905 if (RING_FULL(&rinfo->ring))
908 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
911 if (blkif_queue_request(qd->rq, rinfo))
914 flush_requests(rinfo);
915 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
919 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
920 return BLK_STS_IOERR;
923 blk_mq_stop_hw_queue(hctx);
924 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
925 return BLK_STS_DEV_RESOURCE;
928 static void blkif_complete_rq(struct request *rq)
930 blk_mq_end_request(rq, blkif_req(rq)->error);
933 static const struct blk_mq_ops blkfront_mq_ops = {
934 .queue_rq = blkif_queue_rq,
935 .complete = blkif_complete_rq,
938 static void blkif_set_queue_limits(struct blkfront_info *info)
940 struct request_queue *rq = info->rq;
941 struct gendisk *gd = info->gd;
942 unsigned int segments = info->max_indirect_segments ? :
943 BLKIF_MAX_SEGMENTS_PER_REQUEST;
945 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
947 if (info->feature_discard) {
948 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
949 blk_queue_max_discard_sectors(rq, get_capacity(gd));
950 rq->limits.discard_granularity = info->discard_granularity ?:
951 info->physical_sector_size;
952 rq->limits.discard_alignment = info->discard_alignment;
953 if (info->feature_secdiscard)
954 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
957 /* Hard sector size and max sectors impersonate the equiv. hardware. */
958 blk_queue_logical_block_size(rq, info->sector_size);
959 blk_queue_physical_block_size(rq, info->physical_sector_size);
960 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
962 /* Each segment in a request is up to an aligned page in size. */
963 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
964 blk_queue_max_segment_size(rq, PAGE_SIZE);
966 /* Ensure a merged request will fit in a single I/O ring slot. */
967 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
969 /* Make sure buffer addresses are sector-aligned. */
970 blk_queue_dma_alignment(rq, 511);
973 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
974 unsigned int physical_sector_size)
976 struct request_queue *rq;
977 struct blkfront_info *info = gd->private_data;
979 memset(&info->tag_set, 0, sizeof(info->tag_set));
980 info->tag_set.ops = &blkfront_mq_ops;
981 info->tag_set.nr_hw_queues = info->nr_rings;
982 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
984 * When indirect descriptior is not supported, the I/O request
985 * will be split between multiple request in the ring.
986 * To avoid problems when sending the request, divide by
987 * 2 the depth of the queue.
989 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
991 info->tag_set.queue_depth = BLK_RING_SIZE(info);
992 info->tag_set.numa_node = NUMA_NO_NODE;
993 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
994 info->tag_set.cmd_size = sizeof(struct blkif_req);
995 info->tag_set.driver_data = info;
997 if (blk_mq_alloc_tag_set(&info->tag_set))
999 rq = blk_mq_init_queue(&info->tag_set);
1001 blk_mq_free_tag_set(&info->tag_set);
1005 rq->queuedata = info;
1006 info->rq = gd->queue = rq;
1008 info->sector_size = sector_size;
1009 info->physical_sector_size = physical_sector_size;
1010 blkif_set_queue_limits(info);
1015 static const char *flush_info(struct blkfront_info *info)
1017 if (info->feature_flush && info->feature_fua)
1018 return "barrier: enabled;";
1019 else if (info->feature_flush)
1020 return "flush diskcache: enabled;";
1022 return "barrier or flush: disabled;";
1025 static void xlvbd_flush(struct blkfront_info *info)
1027 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1028 info->feature_fua ? true : false);
1029 pr_info("blkfront: %s: %s %s %s %s %s\n",
1030 info->gd->disk_name, flush_info(info),
1031 "persistent grants:", info->feature_persistent ?
1032 "enabled;" : "disabled;", "indirect descriptors:",
1033 info->max_indirect_segments ? "enabled;" : "disabled;");
1036 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1039 major = BLKIF_MAJOR(vdevice);
1040 *minor = BLKIF_MINOR(vdevice);
1042 case XEN_IDE0_MAJOR:
1043 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1044 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1045 EMULATED_HD_DISK_MINOR_OFFSET;
1047 case XEN_IDE1_MAJOR:
1048 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1049 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1050 EMULATED_HD_DISK_MINOR_OFFSET;
1052 case XEN_SCSI_DISK0_MAJOR:
1053 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1054 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1056 case XEN_SCSI_DISK1_MAJOR:
1057 case XEN_SCSI_DISK2_MAJOR:
1058 case XEN_SCSI_DISK3_MAJOR:
1059 case XEN_SCSI_DISK4_MAJOR:
1060 case XEN_SCSI_DISK5_MAJOR:
1061 case XEN_SCSI_DISK6_MAJOR:
1062 case XEN_SCSI_DISK7_MAJOR:
1063 *offset = (*minor / PARTS_PER_DISK) +
1064 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1065 EMULATED_SD_DISK_NAME_OFFSET;
1067 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1068 EMULATED_SD_DISK_MINOR_OFFSET;
1070 case XEN_SCSI_DISK8_MAJOR:
1071 case XEN_SCSI_DISK9_MAJOR:
1072 case XEN_SCSI_DISK10_MAJOR:
1073 case XEN_SCSI_DISK11_MAJOR:
1074 case XEN_SCSI_DISK12_MAJOR:
1075 case XEN_SCSI_DISK13_MAJOR:
1076 case XEN_SCSI_DISK14_MAJOR:
1077 case XEN_SCSI_DISK15_MAJOR:
1078 *offset = (*minor / PARTS_PER_DISK) +
1079 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1080 EMULATED_SD_DISK_NAME_OFFSET;
1082 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1083 EMULATED_SD_DISK_MINOR_OFFSET;
1086 *offset = *minor / PARTS_PER_DISK;
1089 printk(KERN_WARNING "blkfront: your disk configuration is "
1090 "incorrect, please use an xvd device instead\n");
1096 static char *encode_disk_name(char *ptr, unsigned int n)
1099 ptr = encode_disk_name(ptr, n / 26 - 1);
1100 *ptr = 'a' + n % 26;
1104 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1105 struct blkfront_info *info,
1106 u16 vdisk_info, u16 sector_size,
1107 unsigned int physical_sector_size)
1112 unsigned int offset;
1117 BUG_ON(info->gd != NULL);
1118 BUG_ON(info->rq != NULL);
1120 if ((info->vdevice>>EXT_SHIFT) > 1) {
1121 /* this is above the extended range; something is wrong */
1122 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1126 if (!VDEV_IS_EXTENDED(info->vdevice)) {
1127 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1130 nr_parts = PARTS_PER_DISK;
1132 minor = BLKIF_MINOR_EXT(info->vdevice);
1133 nr_parts = PARTS_PER_EXT_DISK;
1134 offset = minor / nr_parts;
1135 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
1136 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1137 "emulated IDE disks,\n\t choose an xvd device name"
1138 "from xvde on\n", info->vdevice);
1140 if (minor >> MINORBITS) {
1141 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1142 info->vdevice, minor);
1146 if ((minor % nr_parts) == 0)
1147 nr_minors = nr_parts;
1149 err = xlbd_reserve_minors(minor, nr_minors);
1154 gd = alloc_disk(nr_minors);
1158 strcpy(gd->disk_name, DEV_NAME);
1159 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1160 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1164 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1165 "%d", minor & (nr_parts - 1));
1167 gd->major = XENVBD_MAJOR;
1168 gd->first_minor = minor;
1169 gd->fops = &xlvbd_block_fops;
1170 gd->private_data = info;
1171 set_capacity(gd, capacity);
1173 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
1180 if (vdisk_info & VDISK_READONLY)
1183 if (vdisk_info & VDISK_REMOVABLE)
1184 gd->flags |= GENHD_FL_REMOVABLE;
1186 if (vdisk_info & VDISK_CDROM)
1187 gd->flags |= GENHD_FL_CD;
1192 xlbd_release_minors(minor, nr_minors);
1197 static void xlvbd_release_gendisk(struct blkfront_info *info)
1199 unsigned int minor, nr_minors, i;
1201 if (info->rq == NULL)
1204 /* No more blkif_request(). */
1205 blk_mq_stop_hw_queues(info->rq);
1207 for (i = 0; i < info->nr_rings; i++) {
1208 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1210 /* No more gnttab callback work. */
1211 gnttab_cancel_free_callback(&rinfo->callback);
1213 /* Flush gnttab callback work. Must be done with no locks held. */
1214 flush_work(&rinfo->work);
1217 del_gendisk(info->gd);
1219 minor = info->gd->first_minor;
1220 nr_minors = info->gd->minors;
1221 xlbd_release_minors(minor, nr_minors);
1223 blk_cleanup_queue(info->rq);
1224 blk_mq_free_tag_set(&info->tag_set);
1231 /* Already hold rinfo->ring_lock. */
1232 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
1234 if (!RING_FULL(&rinfo->ring))
1235 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
1238 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1240 unsigned long flags;
1242 spin_lock_irqsave(&rinfo->ring_lock, flags);
1243 kick_pending_request_queues_locked(rinfo);
1244 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1247 static void blkif_restart_queue(struct work_struct *work)
1249 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
1251 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1252 kick_pending_request_queues(rinfo);
1255 static void blkif_free_ring(struct blkfront_ring_info *rinfo)
1257 struct grant *persistent_gnt, *n;
1258 struct blkfront_info *info = rinfo->dev_info;
1262 * Remove indirect pages, this only happens when using indirect
1263 * descriptors but not persistent grants
1265 if (!list_empty(&rinfo->indirect_pages)) {
1266 struct page *indirect_page, *n;
1268 BUG_ON(info->feature_persistent);
1269 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
1270 list_del(&indirect_page->lru);
1271 __free_page(indirect_page);
1275 /* Remove all persistent grants. */
1276 if (!list_empty(&rinfo->grants)) {
1277 list_for_each_entry_safe(persistent_gnt, n,
1278 &rinfo->grants, node) {
1279 list_del(&persistent_gnt->node);
1280 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1281 gnttab_end_foreign_access(persistent_gnt->gref,
1283 rinfo->persistent_gnts_c--;
1285 if (info->feature_persistent)
1286 __free_page(persistent_gnt->page);
1287 kfree(persistent_gnt);
1290 BUG_ON(rinfo->persistent_gnts_c != 0);
1292 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1294 * Clear persistent grants present in requests already
1295 * on the shared ring
1297 if (!rinfo->shadow[i].request)
1300 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1301 rinfo->shadow[i].req.u.indirect.nr_segments :
1302 rinfo->shadow[i].req.u.rw.nr_segments;
1303 for (j = 0; j < segs; j++) {
1304 persistent_gnt = rinfo->shadow[i].grants_used[j];
1305 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1306 if (info->feature_persistent)
1307 __free_page(persistent_gnt->page);
1308 kfree(persistent_gnt);
1311 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1313 * If this is not an indirect operation don't try to
1314 * free indirect segments
1318 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1319 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
1320 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1321 __free_page(persistent_gnt->page);
1322 kfree(persistent_gnt);
1326 kvfree(rinfo->shadow[i].grants_used);
1327 rinfo->shadow[i].grants_used = NULL;
1328 kvfree(rinfo->shadow[i].indirect_grants);
1329 rinfo->shadow[i].indirect_grants = NULL;
1330 kvfree(rinfo->shadow[i].sg);
1331 rinfo->shadow[i].sg = NULL;
1334 /* No more gnttab callback work. */
1335 gnttab_cancel_free_callback(&rinfo->callback);
1337 /* Flush gnttab callback work. Must be done with no locks held. */
1338 flush_work(&rinfo->work);
1340 /* Free resources associated with old device channel. */
1341 for (i = 0; i < info->nr_ring_pages; i++) {
1342 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1343 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1344 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1347 free_pages_exact(rinfo->ring.sring,
1348 info->nr_ring_pages * XEN_PAGE_SIZE);
1349 rinfo->ring.sring = NULL;
1352 unbind_from_irqhandler(rinfo->irq, rinfo);
1353 rinfo->evtchn = rinfo->irq = 0;
1356 static void blkif_free(struct blkfront_info *info, int suspend)
1360 /* Prevent new requests being issued until we fix things up. */
1361 info->connected = suspend ?
1362 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1363 /* No more blkif_request(). */
1365 blk_mq_stop_hw_queues(info->rq);
1367 for (i = 0; i < info->nr_rings; i++)
1368 blkif_free_ring(&info->rinfo[i]);
1370 kvfree(info->rinfo);
1375 struct copy_from_grant {
1376 const struct blk_shadow *s;
1377 unsigned int grant_idx;
1378 unsigned int bvec_offset;
1382 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1383 unsigned int len, void *data)
1385 struct copy_from_grant *info = data;
1387 /* Convenient aliases */
1388 const struct blk_shadow *s = info->s;
1390 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1392 memcpy(info->bvec_data + info->bvec_offset,
1393 shared_data + offset, len);
1395 info->bvec_offset += len;
1398 kunmap_atomic(shared_data);
1401 static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1405 case BLKIF_RSP_OKAY:
1407 case BLKIF_RSP_EOPNOTSUPP:
1408 return REQ_EOPNOTSUPP;
1409 case BLKIF_RSP_ERROR:
1417 * Get the final status of the block request based on two ring response
1419 static int blkif_get_final_status(enum blk_req_status s1,
1420 enum blk_req_status s2)
1422 BUG_ON(s1 < REQ_DONE);
1423 BUG_ON(s2 < REQ_DONE);
1425 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1426 return BLKIF_RSP_ERROR;
1427 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1428 return BLKIF_RSP_EOPNOTSUPP;
1429 return BLKIF_RSP_OKAY;
1434 * 1 response processed.
1435 * 0 missing further responses.
1436 * -1 error while processing.
1438 static int blkif_completion(unsigned long *id,
1439 struct blkfront_ring_info *rinfo,
1440 struct blkif_response *bret)
1443 struct scatterlist *sg;
1444 int num_sg, num_grant;
1445 struct blkfront_info *info = rinfo->dev_info;
1446 struct blk_shadow *s = &rinfo->shadow[*id];
1447 struct copy_from_grant data = {
1451 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1452 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1454 /* The I/O request may be split in two. */
1455 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1456 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1458 /* Keep the status of the current response in shadow. */
1459 s->status = blkif_rsp_to_req_status(bret->status);
1461 /* Wait the second response if not yet here. */
1462 if (s2->status < REQ_DONE)
1465 bret->status = blkif_get_final_status(s->status,
1469 * All the grants is stored in the first shadow in order
1470 * to make the completion code simpler.
1472 num_grant += s2->req.u.rw.nr_segments;
1475 * The two responses may not come in order. Only the
1476 * first request will store the scatter-gather list.
1478 if (s2->num_sg != 0) {
1479 /* Update "id" with the ID of the first response. */
1480 *id = s->associated_id;
1485 * We don't need anymore the second request, so recycling
1488 if (add_id_to_freelist(rinfo, s->associated_id))
1489 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1490 info->gd->disk_name, s->associated_id);
1496 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
1497 for_each_sg(s->sg, sg, num_sg, i) {
1498 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1500 data.bvec_offset = sg->offset;
1501 data.bvec_data = kmap_atomic(sg_page(sg));
1503 gnttab_foreach_grant_in_range(sg_page(sg),
1506 blkif_copy_from_grant,
1509 kunmap_atomic(data.bvec_data);
1512 /* Add the persistent grant into the list of free grants */
1513 for (i = 0; i < num_grant; i++) {
1514 if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) {
1516 * If the grant is still mapped by the backend (the
1517 * backend has chosen to make this grant persistent)
1518 * we add it at the head of the list, so it will be
1521 if (!info->feature_persistent) {
1522 pr_alert("backed has not unmapped grant: %u\n",
1523 s->grants_used[i]->gref);
1526 list_add(&s->grants_used[i]->node, &rinfo->grants);
1527 rinfo->persistent_gnts_c++;
1530 * If the grant is not mapped by the backend we add it
1531 * to the tail of the list, so it will not be picked
1532 * again unless we run out of persistent grants.
1534 s->grants_used[i]->gref = GRANT_INVALID_REF;
1535 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
1538 if (s->req.operation == BLKIF_OP_INDIRECT) {
1539 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1540 if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) {
1541 if (!info->feature_persistent) {
1542 pr_alert("backed has not unmapped grant: %u\n",
1543 s->indirect_grants[i]->gref);
1546 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1547 rinfo->persistent_gnts_c++;
1549 struct page *indirect_page;
1552 * Add the used indirect page back to the list of
1553 * available pages for indirect grefs.
1555 if (!info->feature_persistent) {
1556 indirect_page = s->indirect_grants[i]->page;
1557 list_add(&indirect_page->lru, &rinfo->indirect_pages);
1559 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1560 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
1568 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1570 struct request *req;
1571 struct blkif_response bret;
1573 unsigned long flags;
1574 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1575 struct blkfront_info *info = rinfo->dev_info;
1576 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1578 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
1579 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
1583 spin_lock_irqsave(&rinfo->ring_lock, flags);
1585 rp = READ_ONCE(rinfo->ring.sring->rsp_prod);
1586 virt_rmb(); /* Ensure we see queued responses up to 'rp'. */
1587 if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) {
1588 pr_alert("%s: illegal number of responses %u\n",
1589 info->gd->disk_name, rp - rinfo->ring.rsp_cons);
1593 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
1599 RING_COPY_RESPONSE(&rinfo->ring, i, &bret);
1603 * The backend has messed up and given us an id that we would
1604 * never have given to it (we stamp it up to BLK_RING_SIZE -
1605 * look in get_id_from_freelist.
1607 if (id >= BLK_RING_SIZE(info)) {
1608 pr_alert("%s: response has incorrect id (%ld)\n",
1609 info->gd->disk_name, id);
1612 if (rinfo->shadow[id].status != REQ_WAITING) {
1613 pr_alert("%s: response references no pending request\n",
1614 info->gd->disk_name);
1618 rinfo->shadow[id].status = REQ_PROCESSING;
1619 req = rinfo->shadow[id].request;
1621 op = rinfo->shadow[id].req.operation;
1622 if (op == BLKIF_OP_INDIRECT)
1623 op = rinfo->shadow[id].req.u.indirect.indirect_op;
1624 if (bret.operation != op) {
1625 pr_alert("%s: response has wrong operation (%u instead of %u)\n",
1626 info->gd->disk_name, bret.operation, op);
1630 if (bret.operation != BLKIF_OP_DISCARD) {
1634 * We may need to wait for an extra response if the
1635 * I/O request is split in 2
1637 ret = blkif_completion(&id, rinfo, &bret);
1640 if (unlikely(ret < 0))
1644 if (add_id_to_freelist(rinfo, id)) {
1645 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1646 info->gd->disk_name, op_name(bret.operation), id);
1650 if (bret.status == BLKIF_RSP_OKAY)
1651 blkif_req(req)->error = BLK_STS_OK;
1653 blkif_req(req)->error = BLK_STS_IOERR;
1655 switch (bret.operation) {
1656 case BLKIF_OP_DISCARD:
1657 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1658 struct request_queue *rq = info->rq;
1660 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1661 info->gd->disk_name, op_name(bret.operation));
1662 blkif_req(req)->error = BLK_STS_NOTSUPP;
1663 info->feature_discard = 0;
1664 info->feature_secdiscard = 0;
1665 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1666 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
1669 case BLKIF_OP_FLUSH_DISKCACHE:
1670 case BLKIF_OP_WRITE_BARRIER:
1671 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1672 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1673 info->gd->disk_name, op_name(bret.operation));
1674 blkif_req(req)->error = BLK_STS_NOTSUPP;
1676 if (unlikely(bret.status == BLKIF_RSP_ERROR &&
1677 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
1678 pr_warn_ratelimited("blkfront: %s: empty %s op failed\n",
1679 info->gd->disk_name, op_name(bret.operation));
1680 blkif_req(req)->error = BLK_STS_NOTSUPP;
1682 if (unlikely(blkif_req(req)->error)) {
1683 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1684 blkif_req(req)->error = BLK_STS_OK;
1685 info->feature_fua = 0;
1686 info->feature_flush = 0;
1691 case BLKIF_OP_WRITE:
1692 if (unlikely(bret.status != BLKIF_RSP_OKAY))
1693 dev_dbg_ratelimited(&info->xbdev->dev,
1694 "Bad return from blkdev data request: %#x\n",
1702 blk_mq_complete_request(req);
1705 rinfo->ring.rsp_cons = i;
1707 if (i != rinfo->ring.req_prod_pvt) {
1709 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
1713 rinfo->ring.sring->rsp_event = i + 1;
1715 kick_pending_request_queues_locked(rinfo);
1717 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1719 xen_irq_lateeoi(irq, eoiflag);
1724 info->connected = BLKIF_STATE_ERROR;
1726 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1728 /* No EOI in order to avoid further interrupts. */
1730 pr_alert("%s disabled for further use\n", info->gd->disk_name);
1735 static int setup_blkring(struct xenbus_device *dev,
1736 struct blkfront_ring_info *rinfo)
1738 struct blkif_sring *sring;
1740 struct blkfront_info *info = rinfo->dev_info;
1741 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1742 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
1744 for (i = 0; i < info->nr_ring_pages; i++)
1745 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1747 sring = alloc_pages_exact(ring_size, GFP_NOIO);
1749 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1752 SHARED_RING_INIT(sring);
1753 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
1755 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
1757 free_pages_exact(sring, ring_size);
1758 rinfo->ring.sring = NULL;
1761 for (i = 0; i < info->nr_ring_pages; i++)
1762 rinfo->ring_ref[i] = gref[i];
1764 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
1768 err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt,
1771 xenbus_dev_fatal(dev, err,
1772 "bind_evtchn_to_irqhandler failed");
1779 blkif_free(info, 0);
1784 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1785 * ring buffer may have multi pages depending on ->nr_ring_pages.
1787 static int write_per_ring_nodes(struct xenbus_transaction xbt,
1788 struct blkfront_ring_info *rinfo, const char *dir)
1792 const char *message = NULL;
1793 struct blkfront_info *info = rinfo->dev_info;
1795 if (info->nr_ring_pages == 1) {
1796 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1798 message = "writing ring-ref";
1799 goto abort_transaction;
1802 for (i = 0; i < info->nr_ring_pages; i++) {
1803 char ring_ref_name[RINGREF_NAME_LEN];
1805 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1806 err = xenbus_printf(xbt, dir, ring_ref_name,
1807 "%u", rinfo->ring_ref[i]);
1809 message = "writing ring-ref";
1810 goto abort_transaction;
1815 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1817 message = "writing event-channel";
1818 goto abort_transaction;
1824 xenbus_transaction_end(xbt, 1);
1826 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1831 static void free_info(struct blkfront_info *info)
1833 list_del(&info->info_list);
1837 /* Common code used when first setting up, and when resuming. */
1838 static int talk_to_blkback(struct xenbus_device *dev,
1839 struct blkfront_info *info)
1841 const char *message = NULL;
1842 struct xenbus_transaction xbt;
1844 unsigned int i, max_page_order;
1845 unsigned int ring_page_order;
1850 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1851 "max-ring-page-order", 0);
1852 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1853 info->nr_ring_pages = 1 << ring_page_order;
1855 err = negotiate_mq(info);
1857 goto destroy_blkring;
1859 for (i = 0; i < info->nr_rings; i++) {
1860 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1862 /* Create shared ring, alloc event channel. */
1863 err = setup_blkring(dev, rinfo);
1865 goto destroy_blkring;
1869 err = xenbus_transaction_start(&xbt);
1871 xenbus_dev_fatal(dev, err, "starting transaction");
1872 goto destroy_blkring;
1875 if (info->nr_ring_pages > 1) {
1876 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1879 message = "writing ring-page-order";
1880 goto abort_transaction;
1884 /* We already got the number of queues/rings in _probe */
1885 if (info->nr_rings == 1) {
1886 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1888 goto destroy_blkring;
1893 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1896 message = "writing multi-queue-num-queues";
1897 goto abort_transaction;
1900 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1901 path = kmalloc(pathsize, GFP_KERNEL);
1904 message = "ENOMEM while writing ring references";
1905 goto abort_transaction;
1908 for (i = 0; i < info->nr_rings; i++) {
1909 memset(path, 0, pathsize);
1910 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1911 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1914 goto destroy_blkring;
1919 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1920 XEN_IO_PROTO_ABI_NATIVE);
1922 message = "writing protocol";
1923 goto abort_transaction;
1925 err = xenbus_printf(xbt, dev->nodename,
1926 "feature-persistent", "%u", 1);
1929 "writing persistent grants feature to xenbus");
1931 err = xenbus_transaction_end(xbt, 0);
1935 xenbus_dev_fatal(dev, err, "completing transaction");
1936 goto destroy_blkring;
1939 for (i = 0; i < info->nr_rings; i++) {
1941 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1943 for (j = 0; j < BLK_RING_SIZE(info); j++)
1944 rinfo->shadow[j].req.u.rw.id = j + 1;
1945 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1947 xenbus_switch_state(dev, XenbusStateInitialised);
1952 xenbus_transaction_end(xbt, 1);
1954 xenbus_dev_fatal(dev, err, "%s", message);
1956 blkif_free(info, 0);
1958 mutex_lock(&blkfront_mutex);
1960 mutex_unlock(&blkfront_mutex);
1962 dev_set_drvdata(&dev->dev, NULL);
1967 static int negotiate_mq(struct blkfront_info *info)
1969 unsigned int backend_max_queues;
1972 BUG_ON(info->nr_rings);
1974 /* Check if backend supports multiple queues. */
1975 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1976 "multi-queue-max-queues", 1);
1977 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1978 /* We need at least one ring. */
1979 if (!info->nr_rings)
1982 info->rinfo = kvcalloc(info->nr_rings,
1983 sizeof(struct blkfront_ring_info),
1986 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1991 for (i = 0; i < info->nr_rings; i++) {
1992 struct blkfront_ring_info *rinfo;
1994 rinfo = &info->rinfo[i];
1995 INIT_LIST_HEAD(&rinfo->indirect_pages);
1996 INIT_LIST_HEAD(&rinfo->grants);
1997 rinfo->dev_info = info;
1998 INIT_WORK(&rinfo->work, blkif_restart_queue);
1999 spin_lock_init(&rinfo->ring_lock);
2004 * Entry point to this code when a new device is created. Allocate the basic
2005 * structures and the ring buffer for communication with the backend, and
2006 * inform the backend of the appropriate details for those. Switch to
2007 * Initialised state.
2009 static int blkfront_probe(struct xenbus_device *dev,
2010 const struct xenbus_device_id *id)
2013 struct blkfront_info *info;
2015 /* FIXME: Use dynamic device id if this is not set. */
2016 err = xenbus_scanf(XBT_NIL, dev->nodename,
2017 "virtual-device", "%i", &vdevice);
2019 /* go looking in the extended area instead */
2020 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
2023 xenbus_dev_fatal(dev, err, "reading virtual-device");
2028 if (xen_hvm_domain()) {
2031 /* no unplug has been done: do not hook devices != xen vbds */
2032 if (xen_has_pv_and_legacy_disk_devices()) {
2035 if (!VDEV_IS_EXTENDED(vdevice))
2036 major = BLKIF_MAJOR(vdevice);
2038 major = XENVBD_MAJOR;
2040 if (major != XENVBD_MAJOR) {
2042 "%s: HVM does not support vbd %d as xen block device\n",
2047 /* do not create a PV cdrom device if we are an HVM guest */
2048 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
2051 if (strncmp(type, "cdrom", 5) == 0) {
2057 info = kzalloc(sizeof(*info), GFP_KERNEL);
2059 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
2065 mutex_init(&info->mutex);
2066 info->vdevice = vdevice;
2067 info->connected = BLKIF_STATE_DISCONNECTED;
2069 /* Front end dir is a number, which is used as the id. */
2070 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
2071 dev_set_drvdata(&dev->dev, info);
2073 mutex_lock(&blkfront_mutex);
2074 list_add(&info->info_list, &info_list);
2075 mutex_unlock(&blkfront_mutex);
2080 static int blkif_recover(struct blkfront_info *info)
2082 unsigned int r_index;
2083 struct request *req, *n;
2088 blkfront_gather_backend_features(info);
2089 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2090 blkif_set_queue_limits(info);
2091 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
2092 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
2094 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2095 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
2097 rc = blkfront_setup_indirect(rinfo);
2101 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2103 /* Now safe for us to use the shared ring */
2104 info->connected = BLKIF_STATE_CONNECTED;
2106 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2107 struct blkfront_ring_info *rinfo;
2109 rinfo = &info->rinfo[r_index];
2110 /* Kick any other new requests queued since we resumed */
2111 kick_pending_request_queues(rinfo);
2114 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
2115 /* Requeue pending requests (flush or discard) */
2116 list_del_init(&req->queuelist);
2117 BUG_ON(req->nr_phys_segments > segs);
2118 blk_mq_requeue_request(req, false);
2120 blk_mq_start_stopped_hw_queues(info->rq, true);
2121 blk_mq_kick_requeue_list(info->rq);
2123 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
2124 /* Traverse the list of pending bios and re-queue them */
2132 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2133 * driver restart. We tear down our blkif structure and recreate it, but
2134 * leave the device-layer structures intact so that this is transparent to the
2135 * rest of the kernel.
2137 static int blkfront_resume(struct xenbus_device *dev)
2139 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2143 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2145 bio_list_init(&info->bio_list);
2146 INIT_LIST_HEAD(&info->requests);
2147 for (i = 0; i < info->nr_rings; i++) {
2148 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2149 struct bio_list merge_bio;
2150 struct blk_shadow *shadow = rinfo->shadow;
2152 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2154 if (!shadow[j].request)
2158 * Get the bios in the request so we can re-queue them.
2160 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2161 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2162 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
2163 shadow[j].request->cmd_flags & REQ_FUA) {
2165 * Flush operations don't contain bios, so
2166 * we need to requeue the whole request
2168 * XXX: but this doesn't make any sense for a
2169 * write with the FUA flag set..
2171 list_add(&shadow[j].request->queuelist, &info->requests);
2174 merge_bio.head = shadow[j].request->bio;
2175 merge_bio.tail = shadow[j].request->biotail;
2176 bio_list_merge(&info->bio_list, &merge_bio);
2177 shadow[j].request->bio = NULL;
2178 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
2182 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2184 err = talk_to_blkback(dev, info);
2186 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
2189 * We have to wait for the backend to switch to
2190 * connected state, since we want to read which
2191 * features it supports.
2197 static void blkfront_closing(struct blkfront_info *info)
2199 struct xenbus_device *xbdev = info->xbdev;
2200 struct block_device *bdev = NULL;
2202 mutex_lock(&info->mutex);
2204 if (xbdev->state == XenbusStateClosing) {
2205 mutex_unlock(&info->mutex);
2210 bdev = bdget_disk(info->gd, 0);
2212 mutex_unlock(&info->mutex);
2215 xenbus_frontend_closed(xbdev);
2219 mutex_lock(&bdev->bd_mutex);
2221 if (bdev->bd_openers) {
2222 xenbus_dev_error(xbdev, -EBUSY,
2223 "Device in use; refusing to close");
2224 xenbus_switch_state(xbdev, XenbusStateClosing);
2226 xlvbd_release_gendisk(info);
2227 xenbus_frontend_closed(xbdev);
2230 mutex_unlock(&bdev->bd_mutex);
2234 static void blkfront_setup_discard(struct blkfront_info *info)
2236 info->feature_discard = 1;
2237 info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend,
2238 "discard-granularity",
2240 info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend,
2241 "discard-alignment", 0);
2242 info->feature_secdiscard =
2243 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2247 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
2249 unsigned int psegs, grants, memflags;
2251 struct blkfront_info *info = rinfo->dev_info;
2253 memflags = memalloc_noio_save();
2255 if (info->max_indirect_segments == 0) {
2257 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2260 * When an extra req is required, the maximum
2261 * grants supported is related to the size of the
2262 * Linux block segment.
2264 grants = GRANTS_PER_PSEG;
2268 grants = info->max_indirect_segments;
2269 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
2271 err = fill_grant_buffer(rinfo,
2272 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
2276 if (!info->feature_persistent && info->max_indirect_segments) {
2278 * We are using indirect descriptors but not persistent
2279 * grants, we need to allocate a set of pages that can be
2280 * used for mapping indirect grefs
2282 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
2284 BUG_ON(!list_empty(&rinfo->indirect_pages));
2285 for (i = 0; i < num; i++) {
2286 struct page *indirect_page = alloc_page(GFP_KERNEL);
2289 list_add(&indirect_page->lru, &rinfo->indirect_pages);
2293 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2294 rinfo->shadow[i].grants_used =
2296 sizeof(rinfo->shadow[i].grants_used[0]),
2298 rinfo->shadow[i].sg = kvcalloc(psegs,
2299 sizeof(rinfo->shadow[i].sg[0]),
2301 if (info->max_indirect_segments)
2302 rinfo->shadow[i].indirect_grants =
2303 kvcalloc(INDIRECT_GREFS(grants),
2304 sizeof(rinfo->shadow[i].indirect_grants[0]),
2306 if ((rinfo->shadow[i].grants_used == NULL) ||
2307 (rinfo->shadow[i].sg == NULL) ||
2308 (info->max_indirect_segments &&
2309 (rinfo->shadow[i].indirect_grants == NULL)))
2311 sg_init_table(rinfo->shadow[i].sg, psegs);
2314 memalloc_noio_restore(memflags);
2319 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2320 kvfree(rinfo->shadow[i].grants_used);
2321 rinfo->shadow[i].grants_used = NULL;
2322 kvfree(rinfo->shadow[i].sg);
2323 rinfo->shadow[i].sg = NULL;
2324 kvfree(rinfo->shadow[i].indirect_grants);
2325 rinfo->shadow[i].indirect_grants = NULL;
2327 if (!list_empty(&rinfo->indirect_pages)) {
2328 struct page *indirect_page, *n;
2329 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
2330 list_del(&indirect_page->lru);
2331 __free_page(indirect_page);
2335 memalloc_noio_restore(memflags);
2341 * Gather all backend feature-*
2343 static void blkfront_gather_backend_features(struct blkfront_info *info)
2345 unsigned int indirect_segments;
2347 info->feature_flush = 0;
2348 info->feature_fua = 0;
2351 * If there's no "feature-barrier" defined, then it means
2352 * we're dealing with a very old backend which writes
2353 * synchronously; nothing to do.
2355 * If there are barriers, then we use flush.
2357 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
2358 info->feature_flush = 1;
2359 info->feature_fua = 1;
2363 * And if there is "feature-flush-cache" use that above
2366 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2368 info->feature_flush = 1;
2369 info->feature_fua = 0;
2372 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
2373 blkfront_setup_discard(info);
2375 info->feature_persistent =
2376 !!xenbus_read_unsigned(info->xbdev->otherend,
2377 "feature-persistent", 0);
2379 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2380 "feature-max-indirect-segments", 0);
2381 if (indirect_segments > xen_blkif_max_segments)
2382 indirect_segments = xen_blkif_max_segments;
2383 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2384 indirect_segments = 0;
2385 info->max_indirect_segments = indirect_segments;
2387 if (info->feature_persistent) {
2388 mutex_lock(&blkfront_mutex);
2389 schedule_delayed_work(&blkfront_work, HZ * 10);
2390 mutex_unlock(&blkfront_mutex);
2395 * Invoked when the backend is finally 'ready' (and has told produced
2396 * the details about the physical device - #sectors, size, etc).
2398 static void blkfront_connect(struct blkfront_info *info)
2400 unsigned long long sectors;
2401 unsigned long sector_size;
2402 unsigned int physical_sector_size;
2404 char *envp[] = { "RESIZE=1", NULL };
2407 switch (info->connected) {
2408 case BLKIF_STATE_CONNECTED:
2410 * Potentially, the back-end may be signalling
2411 * a capacity change; update the capacity.
2413 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2414 "sectors", "%Lu", §ors);
2415 if (XENBUS_EXIST_ERR(err))
2417 printk(KERN_INFO "Setting capacity to %Lu\n",
2419 set_capacity(info->gd, sectors);
2420 revalidate_disk(info->gd);
2421 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2425 case BLKIF_STATE_SUSPENDED:
2427 * If we are recovering from suspension, we need to wait
2428 * for the backend to announce it's features before
2429 * reconnecting, at least we need to know if the backend
2430 * supports indirect descriptors, and how many.
2432 blkif_recover(info);
2439 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2440 __func__, info->xbdev->otherend);
2442 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2443 "sectors", "%llu", §ors,
2444 "info", "%u", &binfo,
2445 "sector-size", "%lu", §or_size,
2448 xenbus_dev_fatal(info->xbdev, err,
2449 "reading backend fields at %s",
2450 info->xbdev->otherend);
2455 * physcial-sector-size is a newer field, so old backends may not
2456 * provide this. Assume physical sector size to be the same as
2457 * sector_size in that case.
2459 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2460 "physical-sector-size",
2462 blkfront_gather_backend_features(info);
2463 for (i = 0; i < info->nr_rings; i++) {
2464 err = blkfront_setup_indirect(&info->rinfo[i]);
2466 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2467 info->xbdev->otherend);
2468 blkif_free(info, 0);
2473 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2474 physical_sector_size);
2476 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2477 info->xbdev->otherend);
2481 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2483 /* Kick pending requests. */
2484 info->connected = BLKIF_STATE_CONNECTED;
2485 for (i = 0; i < info->nr_rings; i++)
2486 kick_pending_request_queues(&info->rinfo[i]);
2488 device_add_disk(&info->xbdev->dev, info->gd, NULL);
2494 blkif_free(info, 0);
2499 * Callback received when the backend's state changes.
2501 static void blkback_changed(struct xenbus_device *dev,
2502 enum xenbus_state backend_state)
2504 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2506 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2508 switch (backend_state) {
2509 case XenbusStateInitWait:
2510 if (dev->state != XenbusStateInitialising)
2512 if (talk_to_blkback(dev, info))
2514 case XenbusStateInitialising:
2515 case XenbusStateInitialised:
2516 case XenbusStateReconfiguring:
2517 case XenbusStateReconfigured:
2518 case XenbusStateUnknown:
2521 case XenbusStateConnected:
2523 * talk_to_blkback sets state to XenbusStateInitialised
2524 * and blkfront_connect sets it to XenbusStateConnected
2525 * (if connection went OK).
2527 * If the backend (or toolstack) decides to poke at backend
2528 * state (and re-trigger the watch by setting the state repeatedly
2529 * to XenbusStateConnected (4)) we need to deal with this.
2530 * This is allowed as this is used to communicate to the guest
2531 * that the size of disk has changed!
2533 if ((dev->state != XenbusStateInitialised) &&
2534 (dev->state != XenbusStateConnected)) {
2535 if (talk_to_blkback(dev, info))
2539 blkfront_connect(info);
2542 case XenbusStateClosed:
2543 if (dev->state == XenbusStateClosed)
2546 case XenbusStateClosing:
2548 blkfront_closing(info);
2553 static int blkfront_remove(struct xenbus_device *xbdev)
2555 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2556 struct block_device *bdev = NULL;
2557 struct gendisk *disk;
2559 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2564 blkif_free(info, 0);
2566 mutex_lock(&info->mutex);
2570 bdev = bdget_disk(disk, 0);
2573 mutex_unlock(&info->mutex);
2576 mutex_lock(&blkfront_mutex);
2578 mutex_unlock(&blkfront_mutex);
2583 * The xbdev was removed before we reached the Closed
2584 * state. See if it's safe to remove the disk. If the bdev
2585 * isn't closed yet, we let release take care of it.
2588 mutex_lock(&bdev->bd_mutex);
2589 info = disk->private_data;
2591 dev_warn(disk_to_dev(disk),
2592 "%s was hot-unplugged, %d stale handles\n",
2593 xbdev->nodename, bdev->bd_openers);
2595 if (info && !bdev->bd_openers) {
2596 xlvbd_release_gendisk(info);
2597 disk->private_data = NULL;
2598 mutex_lock(&blkfront_mutex);
2600 mutex_unlock(&blkfront_mutex);
2603 mutex_unlock(&bdev->bd_mutex);
2609 static int blkfront_is_ready(struct xenbus_device *dev)
2611 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2613 return info->is_ready && info->xbdev;
2616 static int blkif_open(struct block_device *bdev, fmode_t mode)
2618 struct gendisk *disk = bdev->bd_disk;
2619 struct blkfront_info *info;
2622 mutex_lock(&blkfront_mutex);
2624 info = disk->private_data;
2631 mutex_lock(&info->mutex);
2634 /* xbdev is closed */
2637 mutex_unlock(&info->mutex);
2640 mutex_unlock(&blkfront_mutex);
2644 static void blkif_release(struct gendisk *disk, fmode_t mode)
2646 struct blkfront_info *info = disk->private_data;
2647 struct block_device *bdev;
2648 struct xenbus_device *xbdev;
2650 mutex_lock(&blkfront_mutex);
2652 bdev = bdget_disk(disk, 0);
2655 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2658 if (bdev->bd_openers)
2662 * Check if we have been instructed to close. We will have
2663 * deferred this request, because the bdev was still open.
2666 mutex_lock(&info->mutex);
2667 xbdev = info->xbdev;
2669 if (xbdev && xbdev->state == XenbusStateClosing) {
2670 /* pending switch to state closed */
2671 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2672 xlvbd_release_gendisk(info);
2673 xenbus_frontend_closed(info->xbdev);
2676 mutex_unlock(&info->mutex);
2679 /* sudden device removal */
2680 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2681 xlvbd_release_gendisk(info);
2682 disk->private_data = NULL;
2689 mutex_unlock(&blkfront_mutex);
2692 static const struct block_device_operations xlvbd_block_fops =
2694 .owner = THIS_MODULE,
2696 .release = blkif_release,
2697 .getgeo = blkif_getgeo,
2698 .ioctl = blkif_ioctl,
2702 static const struct xenbus_device_id blkfront_ids[] = {
2707 static struct xenbus_driver blkfront_driver = {
2708 .ids = blkfront_ids,
2709 .probe = blkfront_probe,
2710 .remove = blkfront_remove,
2711 .resume = blkfront_resume,
2712 .otherend_changed = blkback_changed,
2713 .is_ready = blkfront_is_ready,
2716 static void purge_persistent_grants(struct blkfront_info *info)
2719 unsigned long flags;
2721 for (i = 0; i < info->nr_rings; i++) {
2722 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2723 struct grant *gnt_list_entry, *tmp;
2725 spin_lock_irqsave(&rinfo->ring_lock, flags);
2727 if (rinfo->persistent_gnts_c == 0) {
2728 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2732 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
2734 if (gnt_list_entry->gref == GRANT_INVALID_REF ||
2735 !gnttab_try_end_foreign_access(gnt_list_entry->gref))
2738 list_del(&gnt_list_entry->node);
2739 rinfo->persistent_gnts_c--;
2740 gnt_list_entry->gref = GRANT_INVALID_REF;
2741 list_add_tail(&gnt_list_entry->node, &rinfo->grants);
2744 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2748 static void blkfront_delay_work(struct work_struct *work)
2750 struct blkfront_info *info;
2751 bool need_schedule_work = false;
2753 mutex_lock(&blkfront_mutex);
2755 list_for_each_entry(info, &info_list, info_list) {
2756 if (info->feature_persistent) {
2757 need_schedule_work = true;
2758 mutex_lock(&info->mutex);
2759 purge_persistent_grants(info);
2760 mutex_unlock(&info->mutex);
2764 if (need_schedule_work)
2765 schedule_delayed_work(&blkfront_work, HZ * 10);
2767 mutex_unlock(&blkfront_mutex);
2770 static int __init xlblk_init(void)
2773 int nr_cpus = num_online_cpus();
2778 if (!xen_has_pv_disk_devices())
2781 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2782 pr_warn("xen_blk: can't get major %d with name %s\n",
2783 XENVBD_MAJOR, DEV_NAME);
2787 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2788 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2790 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2791 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2792 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2793 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
2796 if (xen_blkif_max_queues > nr_cpus) {
2797 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2798 xen_blkif_max_queues, nr_cpus);
2799 xen_blkif_max_queues = nr_cpus;
2802 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
2804 ret = xenbus_register_frontend(&blkfront_driver);
2806 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2812 module_init(xlblk_init);
2815 static void __exit xlblk_exit(void)
2817 cancel_delayed_work_sync(&blkfront_work);
2819 xenbus_unregister_driver(&blkfront_driver);
2820 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2823 module_exit(xlblk_exit);
2825 MODULE_DESCRIPTION("Xen virtual block device frontend");
2826 MODULE_LICENSE("GPL");
2827 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2828 MODULE_ALIAS("xen:vbd");
2829 MODULE_ALIAS("xenblk");