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/major.h>
46 #include <linux/mutex.h>
47 #include <linux/scatterlist.h>
48 #include <linux/bitmap.h>
49 #include <linux/list.h>
50 #include <linux/workqueue.h>
51 #include <linux/sched/mm.h>
54 #include <xen/xenbus.h>
55 #include <xen/grant_table.h>
56 #include <xen/events.h>
58 #include <xen/platform_pci.h>
60 #include <xen/interface/grant_table.h>
61 #include <xen/interface/io/blkif.h>
62 #include <xen/interface/io/protocols.h>
64 #include <asm/xen/hypervisor.h>
67 * The minimal size of segment supported by the block framework is PAGE_SIZE.
68 * When Linux is using a different page size than Xen, it may not be possible
69 * to put all the data in a single segment.
70 * This can happen when the backend doesn't support indirect descriptor and
71 * therefore the maximum amount of data that a request can carry is
72 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
74 * Note that we only support one extra request. So the Linux page size
75 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
78 #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
81 BLKIF_STATE_DISCONNECTED,
82 BLKIF_STATE_CONNECTED,
83 BLKIF_STATE_SUSPENDED,
90 struct list_head node;
102 struct blkif_request req;
103 struct request *request;
104 struct grant **grants_used;
105 struct grant **indirect_grants;
106 struct scatterlist *sg;
108 enum blk_req_status status;
110 #define NO_ASSOCIATED_ID ~0UL
112 * Id of the sibling if we ever need 2 requests when handling a
115 unsigned long associated_id;
122 static inline struct blkif_req *blkif_req(struct request *rq)
124 return blk_mq_rq_to_pdu(rq);
127 static DEFINE_MUTEX(blkfront_mutex);
128 static const struct block_device_operations xlvbd_block_fops;
129 static struct delayed_work blkfront_work;
130 static LIST_HEAD(info_list);
133 * Maximum number of segments in indirect requests, the actual value used by
134 * the frontend driver is the minimum of this value and the value provided
135 * by the backend driver.
138 static unsigned int xen_blkif_max_segments = 32;
139 module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
140 MODULE_PARM_DESC(max_indirect_segments,
141 "Maximum amount of segments in indirect requests (default is 32)");
143 static unsigned int xen_blkif_max_queues = 4;
144 module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
145 MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
148 * Maximum order of pages to be used for the shared ring between front and
149 * backend, 4KB page granularity is used.
151 static unsigned int xen_blkif_max_ring_order;
152 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
153 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
155 static bool __read_mostly xen_blkif_trusted = true;
156 module_param_named(trusted, xen_blkif_trusted, bool, 0644);
157 MODULE_PARM_DESC(trusted, "Is the backend trusted");
159 #define BLK_RING_SIZE(info) \
160 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
163 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
164 * characters are enough. Define to 20 to keep consistent with backend.
166 #define RINGREF_NAME_LEN (20)
168 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
170 #define QUEUE_NAME_LEN (17)
174 * Every blkfront device can associate with one or more blkfront_ring_info,
175 * depending on how many hardware queues/rings to be used.
177 struct blkfront_ring_info {
178 /* Lock to protect data in every ring buffer. */
179 spinlock_t ring_lock;
180 struct blkif_front_ring ring;
181 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
182 unsigned int evtchn, irq;
183 struct work_struct work;
184 struct gnttab_free_callback callback;
185 struct list_head indirect_pages;
186 struct list_head grants;
187 unsigned int persistent_gnts_c;
188 unsigned long shadow_free;
189 struct blkfront_info *dev_info;
190 struct blk_shadow shadow[];
194 * We have one of these per vbd, whether ide, scsi or 'other'. They
195 * hang in private_data off the gendisk structure. We may end up
196 * putting all kinds of interesting stuff here :-)
201 struct xenbus_device *xbdev;
204 unsigned int physical_sector_size;
205 unsigned long vdisk_info;
208 enum blkif_state connected;
209 /* Number of pages per ring buffer. */
210 unsigned int nr_ring_pages;
211 struct request_queue *rq;
212 unsigned int feature_flush:1;
213 unsigned int feature_fua:1;
214 unsigned int feature_discard:1;
215 unsigned int feature_secdiscard:1;
216 unsigned int feature_persistent:1;
217 unsigned int bounce:1;
218 unsigned int discard_granularity;
219 unsigned int discard_alignment;
220 /* Number of 4KB segments handled */
221 unsigned int max_indirect_segments;
223 struct blk_mq_tag_set tag_set;
224 struct blkfront_ring_info *rinfo;
225 unsigned int nr_rings;
226 unsigned int rinfo_size;
227 /* Save uncomplete reqs and bios for migration. */
228 struct list_head requests;
229 struct bio_list bio_list;
230 struct list_head info_list;
233 static unsigned int nr_minors;
234 static unsigned long *minors;
235 static DEFINE_SPINLOCK(minor_lock);
237 #define PARTS_PER_DISK 16
238 #define PARTS_PER_EXT_DISK 256
240 #define BLKIF_MAJOR(dev) ((dev)>>8)
241 #define BLKIF_MINOR(dev) ((dev) & 0xff)
244 #define EXTENDED (1<<EXT_SHIFT)
245 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
246 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
247 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
248 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
249 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
250 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
252 #define DEV_NAME "xvd" /* name in /dev */
255 * Grants are always the same size as a Xen page (i.e 4KB).
256 * A physical segment is always the same size as a Linux page.
257 * Number of grants per physical segment
259 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
261 #define GRANTS_PER_INDIRECT_FRAME \
262 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
264 #define INDIRECT_GREFS(_grants) \
265 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
267 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
268 static void blkfront_gather_backend_features(struct blkfront_info *info);
269 static int negotiate_mq(struct blkfront_info *info);
271 #define for_each_rinfo(info, ptr, idx) \
272 for ((ptr) = (info)->rinfo, (idx) = 0; \
273 (idx) < (info)->nr_rings; \
274 (idx)++, (ptr) = (void *)(ptr) + (info)->rinfo_size)
276 static inline struct blkfront_ring_info *
277 get_rinfo(const struct blkfront_info *info, unsigned int i)
279 BUG_ON(i >= info->nr_rings);
280 return (void *)info->rinfo + i * info->rinfo_size;
283 static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
285 unsigned long free = rinfo->shadow_free;
287 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
288 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
289 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
293 static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
296 if (rinfo->shadow[id].req.u.rw.id != id)
298 if (rinfo->shadow[id].request == NULL)
300 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
301 rinfo->shadow[id].request = NULL;
302 rinfo->shadow_free = id;
306 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
308 struct blkfront_info *info = rinfo->dev_info;
309 struct page *granted_page;
310 struct grant *gnt_list_entry, *n;
314 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
319 granted_page = alloc_page(GFP_NOIO | __GFP_ZERO);
321 kfree(gnt_list_entry);
324 gnt_list_entry->page = granted_page;
327 gnt_list_entry->gref = INVALID_GRANT_REF;
328 list_add(&gnt_list_entry->node, &rinfo->grants);
335 list_for_each_entry_safe(gnt_list_entry, n,
336 &rinfo->grants, node) {
337 list_del(&gnt_list_entry->node);
339 __free_page(gnt_list_entry->page);
340 kfree(gnt_list_entry);
347 static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
349 struct grant *gnt_list_entry;
351 BUG_ON(list_empty(&rinfo->grants));
352 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
354 list_del(&gnt_list_entry->node);
356 if (gnt_list_entry->gref != INVALID_GRANT_REF)
357 rinfo->persistent_gnts_c--;
359 return gnt_list_entry;
362 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
363 const struct blkfront_info *info)
365 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
366 info->xbdev->otherend_id,
367 gnt_list_entry->page,
371 static struct grant *get_grant(grant_ref_t *gref_head,
373 struct blkfront_ring_info *rinfo)
375 struct grant *gnt_list_entry = get_free_grant(rinfo);
376 struct blkfront_info *info = rinfo->dev_info;
378 if (gnt_list_entry->gref != INVALID_GRANT_REF)
379 return gnt_list_entry;
381 /* Assign a gref to this page */
382 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
383 BUG_ON(gnt_list_entry->gref == -ENOSPC);
385 grant_foreign_access(gnt_list_entry, info);
387 /* Grant access to the GFN passed by the caller */
388 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
389 info->xbdev->otherend_id,
393 return gnt_list_entry;
396 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
397 struct blkfront_ring_info *rinfo)
399 struct grant *gnt_list_entry = get_free_grant(rinfo);
400 struct blkfront_info *info = rinfo->dev_info;
402 if (gnt_list_entry->gref != INVALID_GRANT_REF)
403 return gnt_list_entry;
405 /* Assign a gref to this page */
406 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
407 BUG_ON(gnt_list_entry->gref == -ENOSPC);
409 struct page *indirect_page;
411 /* Fetch a pre-allocated page to use for indirect grefs */
412 BUG_ON(list_empty(&rinfo->indirect_pages));
413 indirect_page = list_first_entry(&rinfo->indirect_pages,
415 list_del(&indirect_page->lru);
416 gnt_list_entry->page = indirect_page;
418 grant_foreign_access(gnt_list_entry, info);
420 return gnt_list_entry;
423 static const char *op_name(int op)
425 static const char *const names[] = {
426 [BLKIF_OP_READ] = "read",
427 [BLKIF_OP_WRITE] = "write",
428 [BLKIF_OP_WRITE_BARRIER] = "barrier",
429 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
430 [BLKIF_OP_DISCARD] = "discard" };
432 if (op < 0 || op >= ARRAY_SIZE(names))
440 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
442 unsigned int end = minor + nr;
445 if (end > nr_minors) {
446 unsigned long *bitmap, *old;
448 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
453 spin_lock(&minor_lock);
454 if (end > nr_minors) {
456 memcpy(bitmap, minors,
457 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
459 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
462 spin_unlock(&minor_lock);
466 spin_lock(&minor_lock);
467 if (find_next_bit(minors, end, minor) >= end) {
468 bitmap_set(minors, minor, nr);
472 spin_unlock(&minor_lock);
477 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
479 unsigned int end = minor + nr;
481 BUG_ON(end > nr_minors);
482 spin_lock(&minor_lock);
483 bitmap_clear(minors, minor, nr);
484 spin_unlock(&minor_lock);
487 static void blkif_restart_queue_callback(void *arg)
489 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
490 schedule_work(&rinfo->work);
493 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
495 /* We don't have real geometry info, but let's at least return
496 values consistent with the size of the device */
497 sector_t nsect = get_capacity(bd->bd_disk);
498 sector_t cylinders = nsect;
502 sector_div(cylinders, hg->heads * hg->sectors);
503 hg->cylinders = cylinders;
504 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
505 hg->cylinders = 0xffff;
509 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
510 unsigned command, unsigned long argument)
512 struct blkfront_info *info = bdev->bd_disk->private_data;
516 case CDROMMULTISESSION:
517 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
518 if (put_user(0, (char __user *)(argument + i)))
521 case CDROM_GET_CAPABILITY:
522 if (!(info->vdisk_info & VDISK_CDROM))
530 static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
532 struct blkif_request **ring_req)
536 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
537 rinfo->ring.req_prod_pvt++;
539 id = get_id_from_freelist(rinfo);
540 rinfo->shadow[id].request = req;
541 rinfo->shadow[id].status = REQ_PROCESSING;
542 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
544 rinfo->shadow[id].req.u.rw.id = id;
549 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
551 struct blkfront_info *info = rinfo->dev_info;
552 struct blkif_request *ring_req, *final_ring_req;
555 /* Fill out a communications ring structure. */
556 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
557 ring_req = &rinfo->shadow[id].req;
559 ring_req->operation = BLKIF_OP_DISCARD;
560 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
561 ring_req->u.discard.id = id;
562 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
563 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
564 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
566 ring_req->u.discard.flag = 0;
568 /* Copy the request to the ring page. */
569 *final_ring_req = *ring_req;
570 rinfo->shadow[id].status = REQ_WAITING;
575 struct setup_rw_req {
576 unsigned int grant_idx;
577 struct blkif_request_segment *segments;
578 struct blkfront_ring_info *rinfo;
579 struct blkif_request *ring_req;
580 grant_ref_t gref_head;
582 /* Only used when persistent grant is used and it's a write request */
584 unsigned int bvec_off;
587 bool require_extra_req;
588 struct blkif_request *extra_ring_req;
591 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
592 unsigned int len, void *data)
594 struct setup_rw_req *setup = data;
596 struct grant *gnt_list_entry;
597 unsigned int fsect, lsect;
598 /* Convenient aliases */
599 unsigned int grant_idx = setup->grant_idx;
600 struct blkif_request *ring_req = setup->ring_req;
601 struct blkfront_ring_info *rinfo = setup->rinfo;
603 * We always use the shadow of the first request to store the list
604 * of grant associated to the block I/O request. This made the
605 * completion more easy to handle even if the block I/O request is
608 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
610 if (unlikely(setup->require_extra_req &&
611 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
613 * We are using the second request, setup grant_idx
614 * to be the index of the segment array.
616 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
617 ring_req = setup->extra_ring_req;
620 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
621 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
623 kunmap_atomic(setup->segments);
625 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
626 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
627 shadow->indirect_grants[n] = gnt_list_entry;
628 setup->segments = kmap_atomic(gnt_list_entry->page);
629 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
632 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
633 ref = gnt_list_entry->gref;
635 * All the grants are stored in the shadow of the first
636 * request. Therefore we have to use the global index.
638 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
640 if (setup->need_copy) {
643 shared_data = kmap_atomic(gnt_list_entry->page);
645 * this does not wipe data stored outside the
646 * range sg->offset..sg->offset+sg->length.
647 * Therefore, blkback *could* see data from
648 * previous requests. This is OK as long as
649 * persistent grants are shared with just one
650 * domain. It may need refactoring if this
653 memcpy(shared_data + offset,
654 setup->bvec_data + setup->bvec_off,
657 kunmap_atomic(shared_data);
658 setup->bvec_off += len;
662 lsect = fsect + (len >> 9) - 1;
663 if (ring_req->operation != BLKIF_OP_INDIRECT) {
664 ring_req->u.rw.seg[grant_idx] =
665 (struct blkif_request_segment) {
668 .last_sect = lsect };
670 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
671 (struct blkif_request_segment) {
674 .last_sect = lsect };
677 (setup->grant_idx)++;
680 static void blkif_setup_extra_req(struct blkif_request *first,
681 struct blkif_request *second)
683 uint16_t nr_segments = first->u.rw.nr_segments;
686 * The second request is only present when the first request uses
687 * all its segments. It's always the continuity of the first one.
689 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
691 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
692 second->u.rw.sector_number = first->u.rw.sector_number +
693 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
695 second->u.rw.handle = first->u.rw.handle;
696 second->operation = first->operation;
699 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
701 struct blkfront_info *info = rinfo->dev_info;
702 struct blkif_request *ring_req, *extra_ring_req = NULL;
703 struct blkif_request *final_ring_req, *final_extra_ring_req = NULL;
704 unsigned long id, extra_id = NO_ASSOCIATED_ID;
705 bool require_extra_req = false;
707 struct setup_rw_req setup = {
711 .need_copy = rq_data_dir(req) && info->bounce,
715 * Used to store if we are able to queue the request by just using
716 * existing persistent grants, or if we have to get new grants,
717 * as there are not sufficiently many free.
719 bool new_persistent_gnts = false;
720 struct scatterlist *sg;
721 int num_sg, max_grefs, num_grant;
723 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
724 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
726 * If we are using indirect segments we need to account
727 * for the indirect grefs used in the request.
729 max_grefs += INDIRECT_GREFS(max_grefs);
731 /* Check if we have enough persistent grants to allocate a requests */
732 if (rinfo->persistent_gnts_c < max_grefs) {
733 new_persistent_gnts = true;
735 if (gnttab_alloc_grant_references(
736 max_grefs - rinfo->persistent_gnts_c,
737 &setup.gref_head) < 0) {
738 gnttab_request_free_callback(
740 blkif_restart_queue_callback,
742 max_grefs - rinfo->persistent_gnts_c);
747 /* Fill out a communications ring structure. */
748 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
749 ring_req = &rinfo->shadow[id].req;
751 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
753 /* Calculate the number of grant used */
754 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
755 num_grant += gnttab_count_grant(sg->offset, sg->length);
757 require_extra_req = info->max_indirect_segments == 0 &&
758 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
759 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
761 rinfo->shadow[id].num_sg = num_sg;
762 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
763 likely(!require_extra_req)) {
765 * The indirect operation can only be a BLKIF_OP_READ or
768 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
769 ring_req->operation = BLKIF_OP_INDIRECT;
770 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
771 BLKIF_OP_WRITE : BLKIF_OP_READ;
772 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
773 ring_req->u.indirect.handle = info->handle;
774 ring_req->u.indirect.nr_segments = num_grant;
776 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
777 ring_req->u.rw.handle = info->handle;
778 ring_req->operation = rq_data_dir(req) ?
779 BLKIF_OP_WRITE : BLKIF_OP_READ;
780 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
782 * Ideally we can do an unordered flush-to-disk.
783 * In case the backend onlysupports barriers, use that.
784 * A barrier request a superset of FUA, so we can
785 * implement it the same way. (It's also a FLUSH+FUA,
786 * since it is guaranteed ordered WRT previous writes.)
788 if (info->feature_flush && info->feature_fua)
789 ring_req->operation =
790 BLKIF_OP_WRITE_BARRIER;
791 else if (info->feature_flush)
792 ring_req->operation =
793 BLKIF_OP_FLUSH_DISKCACHE;
795 ring_req->operation = 0;
797 ring_req->u.rw.nr_segments = num_grant;
798 if (unlikely(require_extra_req)) {
799 extra_id = blkif_ring_get_request(rinfo, req,
800 &final_extra_ring_req);
801 extra_ring_req = &rinfo->shadow[extra_id].req;
804 * Only the first request contains the scatter-gather
807 rinfo->shadow[extra_id].num_sg = 0;
809 blkif_setup_extra_req(ring_req, extra_ring_req);
811 /* Link the 2 requests together */
812 rinfo->shadow[extra_id].associated_id = id;
813 rinfo->shadow[id].associated_id = extra_id;
817 setup.ring_req = ring_req;
820 setup.require_extra_req = require_extra_req;
821 if (unlikely(require_extra_req))
822 setup.extra_ring_req = extra_ring_req;
824 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
825 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
827 if (setup.need_copy) {
828 setup.bvec_off = sg->offset;
829 setup.bvec_data = kmap_atomic(sg_page(sg));
832 gnttab_foreach_grant_in_range(sg_page(sg),
835 blkif_setup_rw_req_grant,
839 kunmap_atomic(setup.bvec_data);
842 kunmap_atomic(setup.segments);
844 /* Copy request(s) to the ring page. */
845 *final_ring_req = *ring_req;
846 rinfo->shadow[id].status = REQ_WAITING;
847 if (unlikely(require_extra_req)) {
848 *final_extra_ring_req = *extra_ring_req;
849 rinfo->shadow[extra_id].status = REQ_WAITING;
852 if (new_persistent_gnts)
853 gnttab_free_grant_references(setup.gref_head);
859 * Generate a Xen blkfront IO request from a blk layer request. Reads
860 * and writes are handled as expected.
862 * @req: a request struct
864 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
866 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
869 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
870 req_op(req) == REQ_OP_SECURE_ERASE))
871 return blkif_queue_discard_req(req, rinfo);
873 return blkif_queue_rw_req(req, rinfo);
876 static inline void flush_requests(struct blkfront_ring_info *rinfo)
880 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
883 notify_remote_via_irq(rinfo->irq);
886 static inline bool blkif_request_flush_invalid(struct request *req,
887 struct blkfront_info *info)
889 return (blk_rq_is_passthrough(req) ||
890 ((req_op(req) == REQ_OP_FLUSH) &&
891 !info->feature_flush) ||
892 ((req->cmd_flags & REQ_FUA) &&
893 !info->feature_fua));
896 static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
897 const struct blk_mq_queue_data *qd)
900 int qid = hctx->queue_num;
901 struct blkfront_info *info = hctx->queue->queuedata;
902 struct blkfront_ring_info *rinfo = NULL;
904 rinfo = get_rinfo(info, qid);
905 blk_mq_start_request(qd->rq);
906 spin_lock_irqsave(&rinfo->ring_lock, flags);
907 if (RING_FULL(&rinfo->ring))
910 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
913 if (blkif_queue_request(qd->rq, rinfo))
916 flush_requests(rinfo);
917 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
921 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
922 return BLK_STS_IOERR;
925 blk_mq_stop_hw_queue(hctx);
926 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
927 return BLK_STS_DEV_RESOURCE;
930 static void blkif_complete_rq(struct request *rq)
932 blk_mq_end_request(rq, blkif_req(rq)->error);
935 static const struct blk_mq_ops blkfront_mq_ops = {
936 .queue_rq = blkif_queue_rq,
937 .complete = blkif_complete_rq,
940 static void blkif_set_queue_limits(struct blkfront_info *info)
942 struct request_queue *rq = info->rq;
943 struct gendisk *gd = info->gd;
944 unsigned int segments = info->max_indirect_segments ? :
945 BLKIF_MAX_SEGMENTS_PER_REQUEST;
947 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
949 if (info->feature_discard) {
950 blk_queue_max_discard_sectors(rq, get_capacity(gd));
951 rq->limits.discard_granularity = info->discard_granularity ?:
952 info->physical_sector_size;
953 rq->limits.discard_alignment = info->discard_alignment;
954 if (info->feature_secdiscard)
955 blk_queue_max_secure_erase_sectors(rq,
959 /* Hard sector size and max sectors impersonate the equiv. hardware. */
960 blk_queue_logical_block_size(rq, info->sector_size);
961 blk_queue_physical_block_size(rq, info->physical_sector_size);
962 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
964 /* Each segment in a request is up to an aligned page in size. */
965 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
966 blk_queue_max_segment_size(rq, PAGE_SIZE);
968 /* Ensure a merged request will fit in a single I/O ring slot. */
969 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
971 /* Make sure buffer addresses are sector-aligned. */
972 blk_queue_dma_alignment(rq, 511);
975 static const char *flush_info(struct blkfront_info *info)
977 if (info->feature_flush && info->feature_fua)
978 return "barrier: enabled;";
979 else if (info->feature_flush)
980 return "flush diskcache: enabled;";
982 return "barrier or flush: disabled;";
985 static void xlvbd_flush(struct blkfront_info *info)
987 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
988 info->feature_fua ? true : false);
989 pr_info("blkfront: %s: %s %s %s %s %s %s %s\n",
990 info->gd->disk_name, flush_info(info),
991 "persistent grants:", info->feature_persistent ?
992 "enabled;" : "disabled;", "indirect descriptors:",
993 info->max_indirect_segments ? "enabled;" : "disabled;",
994 "bounce buffer:", info->bounce ? "enabled" : "disabled;");
997 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1000 major = BLKIF_MAJOR(vdevice);
1001 *minor = BLKIF_MINOR(vdevice);
1003 case XEN_IDE0_MAJOR:
1004 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1005 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1006 EMULATED_HD_DISK_MINOR_OFFSET;
1008 case XEN_IDE1_MAJOR:
1009 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1010 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1011 EMULATED_HD_DISK_MINOR_OFFSET;
1013 case XEN_SCSI_DISK0_MAJOR:
1014 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1015 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1017 case XEN_SCSI_DISK1_MAJOR:
1018 case XEN_SCSI_DISK2_MAJOR:
1019 case XEN_SCSI_DISK3_MAJOR:
1020 case XEN_SCSI_DISK4_MAJOR:
1021 case XEN_SCSI_DISK5_MAJOR:
1022 case XEN_SCSI_DISK6_MAJOR:
1023 case XEN_SCSI_DISK7_MAJOR:
1024 *offset = (*minor / PARTS_PER_DISK) +
1025 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1026 EMULATED_SD_DISK_NAME_OFFSET;
1028 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1029 EMULATED_SD_DISK_MINOR_OFFSET;
1031 case XEN_SCSI_DISK8_MAJOR:
1032 case XEN_SCSI_DISK9_MAJOR:
1033 case XEN_SCSI_DISK10_MAJOR:
1034 case XEN_SCSI_DISK11_MAJOR:
1035 case XEN_SCSI_DISK12_MAJOR:
1036 case XEN_SCSI_DISK13_MAJOR:
1037 case XEN_SCSI_DISK14_MAJOR:
1038 case XEN_SCSI_DISK15_MAJOR:
1039 *offset = (*minor / PARTS_PER_DISK) +
1040 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1041 EMULATED_SD_DISK_NAME_OFFSET;
1043 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1044 EMULATED_SD_DISK_MINOR_OFFSET;
1047 *offset = *minor / PARTS_PER_DISK;
1050 printk(KERN_WARNING "blkfront: your disk configuration is "
1051 "incorrect, please use an xvd device instead\n");
1057 static char *encode_disk_name(char *ptr, unsigned int n)
1060 ptr = encode_disk_name(ptr, n / 26 - 1);
1061 *ptr = 'a' + n % 26;
1065 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1066 struct blkfront_info *info, u16 sector_size,
1067 unsigned int physical_sector_size)
1072 unsigned int offset;
1077 BUG_ON(info->gd != NULL);
1078 BUG_ON(info->rq != NULL);
1080 if ((info->vdevice>>EXT_SHIFT) > 1) {
1081 /* this is above the extended range; something is wrong */
1082 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1086 if (!VDEV_IS_EXTENDED(info->vdevice)) {
1087 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1090 nr_parts = PARTS_PER_DISK;
1092 minor = BLKIF_MINOR_EXT(info->vdevice);
1093 nr_parts = PARTS_PER_EXT_DISK;
1094 offset = minor / nr_parts;
1095 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
1096 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1097 "emulated IDE disks,\n\t choose an xvd device name"
1098 "from xvde on\n", info->vdevice);
1100 if (minor >> MINORBITS) {
1101 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1102 info->vdevice, minor);
1106 if ((minor % nr_parts) == 0)
1107 nr_minors = nr_parts;
1109 err = xlbd_reserve_minors(minor, nr_minors);
1113 memset(&info->tag_set, 0, sizeof(info->tag_set));
1114 info->tag_set.ops = &blkfront_mq_ops;
1115 info->tag_set.nr_hw_queues = info->nr_rings;
1116 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
1118 * When indirect descriptior is not supported, the I/O request
1119 * will be split between multiple request in the ring.
1120 * To avoid problems when sending the request, divide by
1121 * 2 the depth of the queue.
1123 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
1125 info->tag_set.queue_depth = BLK_RING_SIZE(info);
1126 info->tag_set.numa_node = NUMA_NO_NODE;
1127 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1128 info->tag_set.cmd_size = sizeof(struct blkif_req);
1129 info->tag_set.driver_data = info;
1131 err = blk_mq_alloc_tag_set(&info->tag_set);
1133 goto out_release_minors;
1135 gd = blk_mq_alloc_disk(&info->tag_set, info);
1138 goto out_free_tag_set;
1141 strcpy(gd->disk_name, DEV_NAME);
1142 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1143 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1147 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1148 "%d", minor & (nr_parts - 1));
1150 gd->major = XENVBD_MAJOR;
1151 gd->first_minor = minor;
1152 gd->minors = nr_minors;
1153 gd->fops = &xlvbd_block_fops;
1154 gd->private_data = info;
1155 set_capacity(gd, capacity);
1157 info->rq = gd->queue;
1159 info->sector_size = sector_size;
1160 info->physical_sector_size = physical_sector_size;
1161 blkif_set_queue_limits(info);
1165 if (info->vdisk_info & VDISK_READONLY)
1167 if (info->vdisk_info & VDISK_REMOVABLE)
1168 gd->flags |= GENHD_FL_REMOVABLE;
1173 blk_mq_free_tag_set(&info->tag_set);
1175 xlbd_release_minors(minor, nr_minors);
1179 /* Already hold rinfo->ring_lock. */
1180 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
1182 if (!RING_FULL(&rinfo->ring))
1183 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
1186 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1188 unsigned long flags;
1190 spin_lock_irqsave(&rinfo->ring_lock, flags);
1191 kick_pending_request_queues_locked(rinfo);
1192 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1195 static void blkif_restart_queue(struct work_struct *work)
1197 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
1199 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1200 kick_pending_request_queues(rinfo);
1203 static void blkif_free_ring(struct blkfront_ring_info *rinfo)
1205 struct grant *persistent_gnt, *n;
1206 struct blkfront_info *info = rinfo->dev_info;
1210 * Remove indirect pages, this only happens when using indirect
1211 * descriptors but not persistent grants
1213 if (!list_empty(&rinfo->indirect_pages)) {
1214 struct page *indirect_page, *n;
1216 BUG_ON(info->bounce);
1217 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
1218 list_del(&indirect_page->lru);
1219 __free_page(indirect_page);
1223 /* Remove all persistent grants. */
1224 if (!list_empty(&rinfo->grants)) {
1225 list_for_each_entry_safe(persistent_gnt, n,
1226 &rinfo->grants, node) {
1227 list_del(&persistent_gnt->node);
1228 if (persistent_gnt->gref != INVALID_GRANT_REF) {
1229 gnttab_end_foreign_access(persistent_gnt->gref,
1231 rinfo->persistent_gnts_c--;
1234 __free_page(persistent_gnt->page);
1235 kfree(persistent_gnt);
1238 BUG_ON(rinfo->persistent_gnts_c != 0);
1240 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1242 * Clear persistent grants present in requests already
1243 * on the shared ring
1245 if (!rinfo->shadow[i].request)
1248 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1249 rinfo->shadow[i].req.u.indirect.nr_segments :
1250 rinfo->shadow[i].req.u.rw.nr_segments;
1251 for (j = 0; j < segs; j++) {
1252 persistent_gnt = rinfo->shadow[i].grants_used[j];
1253 gnttab_end_foreign_access(persistent_gnt->gref, NULL);
1255 __free_page(persistent_gnt->page);
1256 kfree(persistent_gnt);
1259 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1261 * If this is not an indirect operation don't try to
1262 * free indirect segments
1266 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1267 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
1268 gnttab_end_foreign_access(persistent_gnt->gref, NULL);
1269 __free_page(persistent_gnt->page);
1270 kfree(persistent_gnt);
1274 kvfree(rinfo->shadow[i].grants_used);
1275 rinfo->shadow[i].grants_used = NULL;
1276 kvfree(rinfo->shadow[i].indirect_grants);
1277 rinfo->shadow[i].indirect_grants = NULL;
1278 kvfree(rinfo->shadow[i].sg);
1279 rinfo->shadow[i].sg = NULL;
1282 /* No more gnttab callback work. */
1283 gnttab_cancel_free_callback(&rinfo->callback);
1285 /* Flush gnttab callback work. Must be done with no locks held. */
1286 flush_work(&rinfo->work);
1288 /* Free resources associated with old device channel. */
1289 xenbus_teardown_ring((void **)&rinfo->ring.sring, info->nr_ring_pages,
1293 unbind_from_irqhandler(rinfo->irq, rinfo);
1294 rinfo->evtchn = rinfo->irq = 0;
1297 static void blkif_free(struct blkfront_info *info, int suspend)
1300 struct blkfront_ring_info *rinfo;
1302 /* Prevent new requests being issued until we fix things up. */
1303 info->connected = suspend ?
1304 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1305 /* No more blkif_request(). */
1307 blk_mq_stop_hw_queues(info->rq);
1309 for_each_rinfo(info, rinfo, i)
1310 blkif_free_ring(rinfo);
1312 kvfree(info->rinfo);
1317 struct copy_from_grant {
1318 const struct blk_shadow *s;
1319 unsigned int grant_idx;
1320 unsigned int bvec_offset;
1324 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1325 unsigned int len, void *data)
1327 struct copy_from_grant *info = data;
1329 /* Convenient aliases */
1330 const struct blk_shadow *s = info->s;
1332 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1334 memcpy(info->bvec_data + info->bvec_offset,
1335 shared_data + offset, len);
1337 info->bvec_offset += len;
1340 kunmap_atomic(shared_data);
1343 static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1347 case BLKIF_RSP_OKAY:
1349 case BLKIF_RSP_EOPNOTSUPP:
1350 return REQ_EOPNOTSUPP;
1351 case BLKIF_RSP_ERROR:
1358 * Get the final status of the block request based on two ring response
1360 static int blkif_get_final_status(enum blk_req_status s1,
1361 enum blk_req_status s2)
1363 BUG_ON(s1 < REQ_DONE);
1364 BUG_ON(s2 < REQ_DONE);
1366 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1367 return BLKIF_RSP_ERROR;
1368 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1369 return BLKIF_RSP_EOPNOTSUPP;
1370 return BLKIF_RSP_OKAY;
1375 * 1 response processed.
1376 * 0 missing further responses.
1377 * -1 error while processing.
1379 static int blkif_completion(unsigned long *id,
1380 struct blkfront_ring_info *rinfo,
1381 struct blkif_response *bret)
1384 struct scatterlist *sg;
1385 int num_sg, num_grant;
1386 struct blkfront_info *info = rinfo->dev_info;
1387 struct blk_shadow *s = &rinfo->shadow[*id];
1388 struct copy_from_grant data = {
1392 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1393 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1395 /* The I/O request may be split in two. */
1396 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1397 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1399 /* Keep the status of the current response in shadow. */
1400 s->status = blkif_rsp_to_req_status(bret->status);
1402 /* Wait the second response if not yet here. */
1403 if (s2->status < REQ_DONE)
1406 bret->status = blkif_get_final_status(s->status,
1410 * All the grants is stored in the first shadow in order
1411 * to make the completion code simpler.
1413 num_grant += s2->req.u.rw.nr_segments;
1416 * The two responses may not come in order. Only the
1417 * first request will store the scatter-gather list.
1419 if (s2->num_sg != 0) {
1420 /* Update "id" with the ID of the first response. */
1421 *id = s->associated_id;
1426 * We don't need anymore the second request, so recycling
1429 if (add_id_to_freelist(rinfo, s->associated_id))
1430 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1431 info->gd->disk_name, s->associated_id);
1437 if (bret->operation == BLKIF_OP_READ && info->bounce) {
1438 for_each_sg(s->sg, sg, num_sg, i) {
1439 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1441 data.bvec_offset = sg->offset;
1442 data.bvec_data = kmap_atomic(sg_page(sg));
1444 gnttab_foreach_grant_in_range(sg_page(sg),
1447 blkif_copy_from_grant,
1450 kunmap_atomic(data.bvec_data);
1453 /* Add the persistent grant into the list of free grants */
1454 for (i = 0; i < num_grant; i++) {
1455 if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) {
1457 * If the grant is still mapped by the backend (the
1458 * backend has chosen to make this grant persistent)
1459 * we add it at the head of the list, so it will be
1462 if (!info->feature_persistent) {
1463 pr_alert("backed has not unmapped grant: %u\n",
1464 s->grants_used[i]->gref);
1467 list_add(&s->grants_used[i]->node, &rinfo->grants);
1468 rinfo->persistent_gnts_c++;
1471 * If the grant is not mapped by the backend we add it
1472 * to the tail of the list, so it will not be picked
1473 * again unless we run out of persistent grants.
1475 s->grants_used[i]->gref = INVALID_GRANT_REF;
1476 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
1479 if (s->req.operation == BLKIF_OP_INDIRECT) {
1480 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1481 if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) {
1482 if (!info->feature_persistent) {
1483 pr_alert("backed has not unmapped grant: %u\n",
1484 s->indirect_grants[i]->gref);
1487 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1488 rinfo->persistent_gnts_c++;
1490 struct page *indirect_page;
1493 * Add the used indirect page back to the list of
1494 * available pages for indirect grefs.
1496 if (!info->bounce) {
1497 indirect_page = s->indirect_grants[i]->page;
1498 list_add(&indirect_page->lru, &rinfo->indirect_pages);
1500 s->indirect_grants[i]->gref = INVALID_GRANT_REF;
1501 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
1509 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1511 struct request *req;
1512 struct blkif_response bret;
1514 unsigned long flags;
1515 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1516 struct blkfront_info *info = rinfo->dev_info;
1517 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1519 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
1520 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
1524 spin_lock_irqsave(&rinfo->ring_lock, flags);
1526 rp = READ_ONCE(rinfo->ring.sring->rsp_prod);
1527 virt_rmb(); /* Ensure we see queued responses up to 'rp'. */
1528 if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) {
1529 pr_alert("%s: illegal number of responses %u\n",
1530 info->gd->disk_name, rp - rinfo->ring.rsp_cons);
1534 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
1540 RING_COPY_RESPONSE(&rinfo->ring, i, &bret);
1544 * The backend has messed up and given us an id that we would
1545 * never have given to it (we stamp it up to BLK_RING_SIZE -
1546 * look in get_id_from_freelist.
1548 if (id >= BLK_RING_SIZE(info)) {
1549 pr_alert("%s: response has incorrect id (%ld)\n",
1550 info->gd->disk_name, id);
1553 if (rinfo->shadow[id].status != REQ_WAITING) {
1554 pr_alert("%s: response references no pending request\n",
1555 info->gd->disk_name);
1559 rinfo->shadow[id].status = REQ_PROCESSING;
1560 req = rinfo->shadow[id].request;
1562 op = rinfo->shadow[id].req.operation;
1563 if (op == BLKIF_OP_INDIRECT)
1564 op = rinfo->shadow[id].req.u.indirect.indirect_op;
1565 if (bret.operation != op) {
1566 pr_alert("%s: response has wrong operation (%u instead of %u)\n",
1567 info->gd->disk_name, bret.operation, op);
1571 if (bret.operation != BLKIF_OP_DISCARD) {
1575 * We may need to wait for an extra response if the
1576 * I/O request is split in 2
1578 ret = blkif_completion(&id, rinfo, &bret);
1581 if (unlikely(ret < 0))
1585 if (add_id_to_freelist(rinfo, id)) {
1586 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1587 info->gd->disk_name, op_name(bret.operation), id);
1591 if (bret.status == BLKIF_RSP_OKAY)
1592 blkif_req(req)->error = BLK_STS_OK;
1594 blkif_req(req)->error = BLK_STS_IOERR;
1596 switch (bret.operation) {
1597 case BLKIF_OP_DISCARD:
1598 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1599 struct request_queue *rq = info->rq;
1601 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1602 info->gd->disk_name, op_name(bret.operation));
1603 blkif_req(req)->error = BLK_STS_NOTSUPP;
1604 info->feature_discard = 0;
1605 info->feature_secdiscard = 0;
1606 blk_queue_max_discard_sectors(rq, 0);
1607 blk_queue_max_secure_erase_sectors(rq, 0);
1610 case BLKIF_OP_FLUSH_DISKCACHE:
1611 case BLKIF_OP_WRITE_BARRIER:
1612 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1613 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1614 info->gd->disk_name, op_name(bret.operation));
1615 blkif_req(req)->error = BLK_STS_NOTSUPP;
1617 if (unlikely(bret.status == BLKIF_RSP_ERROR &&
1618 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
1619 pr_warn_ratelimited("blkfront: %s: empty %s op failed\n",
1620 info->gd->disk_name, op_name(bret.operation));
1621 blkif_req(req)->error = BLK_STS_NOTSUPP;
1623 if (unlikely(blkif_req(req)->error)) {
1624 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1625 blkif_req(req)->error = BLK_STS_OK;
1626 info->feature_fua = 0;
1627 info->feature_flush = 0;
1632 case BLKIF_OP_WRITE:
1633 if (unlikely(bret.status != BLKIF_RSP_OKAY))
1634 dev_dbg_ratelimited(&info->xbdev->dev,
1635 "Bad return from blkdev data request: %#x\n",
1643 if (likely(!blk_should_fake_timeout(req->q)))
1644 blk_mq_complete_request(req);
1647 rinfo->ring.rsp_cons = i;
1649 if (i != rinfo->ring.req_prod_pvt) {
1651 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
1655 rinfo->ring.sring->rsp_event = i + 1;
1657 kick_pending_request_queues_locked(rinfo);
1659 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1661 xen_irq_lateeoi(irq, eoiflag);
1666 info->connected = BLKIF_STATE_ERROR;
1668 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1670 /* No EOI in order to avoid further interrupts. */
1672 pr_alert("%s disabled for further use\n", info->gd->disk_name);
1677 static int setup_blkring(struct xenbus_device *dev,
1678 struct blkfront_ring_info *rinfo)
1680 struct blkif_sring *sring;
1682 struct blkfront_info *info = rinfo->dev_info;
1683 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1685 err = xenbus_setup_ring(dev, GFP_NOIO, (void **)&sring,
1686 info->nr_ring_pages, rinfo->ring_ref);
1690 XEN_FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
1692 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
1696 err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt,
1699 xenbus_dev_fatal(dev, err,
1700 "bind_evtchn_to_irqhandler failed");
1707 blkif_free(info, 0);
1712 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1713 * ring buffer may have multi pages depending on ->nr_ring_pages.
1715 static int write_per_ring_nodes(struct xenbus_transaction xbt,
1716 struct blkfront_ring_info *rinfo, const char *dir)
1720 const char *message = NULL;
1721 struct blkfront_info *info = rinfo->dev_info;
1723 if (info->nr_ring_pages == 1) {
1724 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1726 message = "writing ring-ref";
1727 goto abort_transaction;
1730 for (i = 0; i < info->nr_ring_pages; i++) {
1731 char ring_ref_name[RINGREF_NAME_LEN];
1733 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1734 err = xenbus_printf(xbt, dir, ring_ref_name,
1735 "%u", rinfo->ring_ref[i]);
1737 message = "writing ring-ref";
1738 goto abort_transaction;
1743 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1745 message = "writing event-channel";
1746 goto abort_transaction;
1752 xenbus_transaction_end(xbt, 1);
1754 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1759 /* Common code used when first setting up, and when resuming. */
1760 static int talk_to_blkback(struct xenbus_device *dev,
1761 struct blkfront_info *info)
1763 const char *message = NULL;
1764 struct xenbus_transaction xbt;
1766 unsigned int i, max_page_order;
1767 unsigned int ring_page_order;
1768 struct blkfront_ring_info *rinfo;
1773 /* Check if backend is trusted. */
1774 info->bounce = !xen_blkif_trusted ||
1775 !xenbus_read_unsigned(dev->nodename, "trusted", 1);
1777 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1778 "max-ring-page-order", 0);
1779 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1780 info->nr_ring_pages = 1 << ring_page_order;
1782 err = negotiate_mq(info);
1784 goto destroy_blkring;
1786 for_each_rinfo(info, rinfo, i) {
1787 /* Create shared ring, alloc event channel. */
1788 err = setup_blkring(dev, rinfo);
1790 goto destroy_blkring;
1794 err = xenbus_transaction_start(&xbt);
1796 xenbus_dev_fatal(dev, err, "starting transaction");
1797 goto destroy_blkring;
1800 if (info->nr_ring_pages > 1) {
1801 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1804 message = "writing ring-page-order";
1805 goto abort_transaction;
1809 /* We already got the number of queues/rings in _probe */
1810 if (info->nr_rings == 1) {
1811 err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename);
1813 goto destroy_blkring;
1818 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1821 message = "writing multi-queue-num-queues";
1822 goto abort_transaction;
1825 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1826 path = kmalloc(pathsize, GFP_KERNEL);
1829 message = "ENOMEM while writing ring references";
1830 goto abort_transaction;
1833 for_each_rinfo(info, rinfo, i) {
1834 memset(path, 0, pathsize);
1835 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1836 err = write_per_ring_nodes(xbt, rinfo, path);
1839 goto destroy_blkring;
1844 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1845 XEN_IO_PROTO_ABI_NATIVE);
1847 message = "writing protocol";
1848 goto abort_transaction;
1850 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u",
1851 info->feature_persistent);
1854 "writing persistent grants feature to xenbus");
1856 err = xenbus_transaction_end(xbt, 0);
1860 xenbus_dev_fatal(dev, err, "completing transaction");
1861 goto destroy_blkring;
1864 for_each_rinfo(info, rinfo, i) {
1867 for (j = 0; j < BLK_RING_SIZE(info); j++)
1868 rinfo->shadow[j].req.u.rw.id = j + 1;
1869 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1871 xenbus_switch_state(dev, XenbusStateInitialised);
1876 xenbus_transaction_end(xbt, 1);
1878 xenbus_dev_fatal(dev, err, "%s", message);
1880 blkif_free(info, 0);
1884 static int negotiate_mq(struct blkfront_info *info)
1886 unsigned int backend_max_queues;
1888 struct blkfront_ring_info *rinfo;
1890 BUG_ON(info->nr_rings);
1892 /* Check if backend supports multiple queues. */
1893 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1894 "multi-queue-max-queues", 1);
1895 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1896 /* We need at least one ring. */
1897 if (!info->nr_rings)
1900 info->rinfo_size = struct_size(info->rinfo, shadow,
1901 BLK_RING_SIZE(info));
1902 info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL);
1904 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1909 for_each_rinfo(info, rinfo, i) {
1910 INIT_LIST_HEAD(&rinfo->indirect_pages);
1911 INIT_LIST_HEAD(&rinfo->grants);
1912 rinfo->dev_info = info;
1913 INIT_WORK(&rinfo->work, blkif_restart_queue);
1914 spin_lock_init(&rinfo->ring_lock);
1919 /* Enable the persistent grants feature. */
1920 static bool feature_persistent = true;
1921 module_param(feature_persistent, bool, 0644);
1922 MODULE_PARM_DESC(feature_persistent,
1923 "Enables the persistent grants feature");
1926 * Entry point to this code when a new device is created. Allocate the basic
1927 * structures and the ring buffer for communication with the backend, and
1928 * inform the backend of the appropriate details for those. Switch to
1929 * Initialised state.
1931 static int blkfront_probe(struct xenbus_device *dev,
1932 const struct xenbus_device_id *id)
1935 struct blkfront_info *info;
1937 /* FIXME: Use dynamic device id if this is not set. */
1938 err = xenbus_scanf(XBT_NIL, dev->nodename,
1939 "virtual-device", "%i", &vdevice);
1941 /* go looking in the extended area instead */
1942 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1945 xenbus_dev_fatal(dev, err, "reading virtual-device");
1950 if (xen_hvm_domain()) {
1953 /* no unplug has been done: do not hook devices != xen vbds */
1954 if (xen_has_pv_and_legacy_disk_devices()) {
1957 if (!VDEV_IS_EXTENDED(vdevice))
1958 major = BLKIF_MAJOR(vdevice);
1960 major = XENVBD_MAJOR;
1962 if (major != XENVBD_MAJOR) {
1964 "%s: HVM does not support vbd %d as xen block device\n",
1969 /* do not create a PV cdrom device if we are an HVM guest */
1970 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1973 if (strncmp(type, "cdrom", 5) == 0) {
1979 info = kzalloc(sizeof(*info), GFP_KERNEL);
1981 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1987 mutex_init(&info->mutex);
1988 info->vdevice = vdevice;
1989 info->connected = BLKIF_STATE_DISCONNECTED;
1991 /* Front end dir is a number, which is used as the id. */
1992 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
1993 dev_set_drvdata(&dev->dev, info);
1995 mutex_lock(&blkfront_mutex);
1996 list_add(&info->info_list, &info_list);
1997 mutex_unlock(&blkfront_mutex);
2002 static int blkif_recover(struct blkfront_info *info)
2004 unsigned int r_index;
2005 struct request *req, *n;
2009 struct blkfront_ring_info *rinfo;
2011 blkfront_gather_backend_features(info);
2012 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2013 blkif_set_queue_limits(info);
2014 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
2015 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
2017 for_each_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_each_rinfo(info, rinfo, r_index) {
2028 /* Kick any other new requests queued since we resumed */
2029 kick_pending_request_queues(rinfo);
2032 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
2033 /* Requeue pending requests (flush or discard) */
2034 list_del_init(&req->queuelist);
2035 BUG_ON(req->nr_phys_segments > segs);
2036 blk_mq_requeue_request(req, false);
2038 blk_mq_start_stopped_hw_queues(info->rq, true);
2039 blk_mq_kick_requeue_list(info->rq);
2041 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
2042 /* Traverse the list of pending bios and re-queue them */
2050 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2051 * driver restart. We tear down our blkif structure and recreate it, but
2052 * leave the device-layer structures intact so that this is transparent to the
2053 * rest of the kernel.
2055 static int blkfront_resume(struct xenbus_device *dev)
2057 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2060 struct blkfront_ring_info *rinfo;
2062 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2064 bio_list_init(&info->bio_list);
2065 INIT_LIST_HEAD(&info->requests);
2066 for_each_rinfo(info, rinfo, i) {
2067 struct bio_list merge_bio;
2068 struct blk_shadow *shadow = rinfo->shadow;
2070 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2072 if (!shadow[j].request)
2076 * Get the bios in the request so we can re-queue them.
2078 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2079 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2080 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
2081 shadow[j].request->cmd_flags & REQ_FUA) {
2083 * Flush operations don't contain bios, so
2084 * we need to requeue the whole request
2086 * XXX: but this doesn't make any sense for a
2087 * write with the FUA flag set..
2089 list_add(&shadow[j].request->queuelist, &info->requests);
2092 merge_bio.head = shadow[j].request->bio;
2093 merge_bio.tail = shadow[j].request->biotail;
2094 bio_list_merge(&info->bio_list, &merge_bio);
2095 shadow[j].request->bio = NULL;
2096 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
2100 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2102 err = talk_to_blkback(dev, info);
2104 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
2107 * We have to wait for the backend to switch to
2108 * connected state, since we want to read which
2109 * features it supports.
2115 static void blkfront_closing(struct blkfront_info *info)
2117 struct xenbus_device *xbdev = info->xbdev;
2118 struct blkfront_ring_info *rinfo;
2121 if (xbdev->state == XenbusStateClosing)
2124 /* No more blkif_request(). */
2125 if (info->rq && info->gd) {
2126 blk_mq_stop_hw_queues(info->rq);
2127 blk_mark_disk_dead(info->gd);
2128 set_capacity(info->gd, 0);
2131 for_each_rinfo(info, rinfo, i) {
2132 /* No more gnttab callback work. */
2133 gnttab_cancel_free_callback(&rinfo->callback);
2135 /* Flush gnttab callback work. Must be done with no locks held. */
2136 flush_work(&rinfo->work);
2139 xenbus_frontend_closed(xbdev);
2142 static void blkfront_setup_discard(struct blkfront_info *info)
2144 info->feature_discard = 1;
2145 info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend,
2146 "discard-granularity",
2148 info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend,
2149 "discard-alignment", 0);
2150 info->feature_secdiscard =
2151 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2155 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
2157 unsigned int psegs, grants, memflags;
2159 struct blkfront_info *info = rinfo->dev_info;
2161 memflags = memalloc_noio_save();
2163 if (info->max_indirect_segments == 0) {
2165 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2168 * When an extra req is required, the maximum
2169 * grants supported is related to the size of the
2170 * Linux block segment.
2172 grants = GRANTS_PER_PSEG;
2176 grants = info->max_indirect_segments;
2177 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
2179 err = fill_grant_buffer(rinfo,
2180 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
2184 if (!info->bounce && info->max_indirect_segments) {
2186 * We are using indirect descriptors but don't have a bounce
2187 * buffer, we need to allocate a set of pages that can be
2188 * used for mapping indirect grefs
2190 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
2192 BUG_ON(!list_empty(&rinfo->indirect_pages));
2193 for (i = 0; i < num; i++) {
2194 struct page *indirect_page = alloc_page(GFP_KERNEL |
2198 list_add(&indirect_page->lru, &rinfo->indirect_pages);
2202 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2203 rinfo->shadow[i].grants_used =
2205 sizeof(rinfo->shadow[i].grants_used[0]),
2207 rinfo->shadow[i].sg = kvcalloc(psegs,
2208 sizeof(rinfo->shadow[i].sg[0]),
2210 if (info->max_indirect_segments)
2211 rinfo->shadow[i].indirect_grants =
2212 kvcalloc(INDIRECT_GREFS(grants),
2213 sizeof(rinfo->shadow[i].indirect_grants[0]),
2215 if ((rinfo->shadow[i].grants_used == NULL) ||
2216 (rinfo->shadow[i].sg == NULL) ||
2217 (info->max_indirect_segments &&
2218 (rinfo->shadow[i].indirect_grants == NULL)))
2220 sg_init_table(rinfo->shadow[i].sg, psegs);
2223 memalloc_noio_restore(memflags);
2228 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2229 kvfree(rinfo->shadow[i].grants_used);
2230 rinfo->shadow[i].grants_used = NULL;
2231 kvfree(rinfo->shadow[i].sg);
2232 rinfo->shadow[i].sg = NULL;
2233 kvfree(rinfo->shadow[i].indirect_grants);
2234 rinfo->shadow[i].indirect_grants = NULL;
2236 if (!list_empty(&rinfo->indirect_pages)) {
2237 struct page *indirect_page, *n;
2238 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
2239 list_del(&indirect_page->lru);
2240 __free_page(indirect_page);
2244 memalloc_noio_restore(memflags);
2250 * Gather all backend feature-*
2252 static void blkfront_gather_backend_features(struct blkfront_info *info)
2254 unsigned int indirect_segments;
2256 info->feature_flush = 0;
2257 info->feature_fua = 0;
2260 * If there's no "feature-barrier" defined, then it means
2261 * we're dealing with a very old backend which writes
2262 * synchronously; nothing to do.
2264 * If there are barriers, then we use flush.
2266 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
2267 info->feature_flush = 1;
2268 info->feature_fua = 1;
2272 * And if there is "feature-flush-cache" use that above
2275 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2277 info->feature_flush = 1;
2278 info->feature_fua = 0;
2281 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
2282 blkfront_setup_discard(info);
2284 if (feature_persistent)
2285 info->feature_persistent =
2286 !!xenbus_read_unsigned(info->xbdev->otherend,
2287 "feature-persistent", 0);
2288 if (info->feature_persistent)
2289 info->bounce = true;
2291 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2292 "feature-max-indirect-segments", 0);
2293 if (indirect_segments > xen_blkif_max_segments)
2294 indirect_segments = xen_blkif_max_segments;
2295 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2296 indirect_segments = 0;
2297 info->max_indirect_segments = indirect_segments;
2299 if (info->feature_persistent) {
2300 mutex_lock(&blkfront_mutex);
2301 schedule_delayed_work(&blkfront_work, HZ * 10);
2302 mutex_unlock(&blkfront_mutex);
2307 * Invoked when the backend is finally 'ready' (and has told produced
2308 * the details about the physical device - #sectors, size, etc).
2310 static void blkfront_connect(struct blkfront_info *info)
2312 unsigned long long sectors;
2313 unsigned long sector_size;
2314 unsigned int physical_sector_size;
2316 struct blkfront_ring_info *rinfo;
2318 switch (info->connected) {
2319 case BLKIF_STATE_CONNECTED:
2321 * Potentially, the back-end may be signalling
2322 * a capacity change; update the capacity.
2324 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2325 "sectors", "%Lu", §ors);
2326 if (XENBUS_EXIST_ERR(err))
2328 printk(KERN_INFO "Setting capacity to %Lu\n",
2330 set_capacity_and_notify(info->gd, sectors);
2333 case BLKIF_STATE_SUSPENDED:
2335 * If we are recovering from suspension, we need to wait
2336 * for the backend to announce it's features before
2337 * reconnecting, at least we need to know if the backend
2338 * supports indirect descriptors, and how many.
2340 blkif_recover(info);
2347 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2348 __func__, info->xbdev->otherend);
2350 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2351 "sectors", "%llu", §ors,
2352 "info", "%u", &info->vdisk_info,
2353 "sector-size", "%lu", §or_size,
2356 xenbus_dev_fatal(info->xbdev, err,
2357 "reading backend fields at %s",
2358 info->xbdev->otherend);
2363 * physical-sector-size is a newer field, so old backends may not
2364 * provide this. Assume physical sector size to be the same as
2365 * sector_size in that case.
2367 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2368 "physical-sector-size",
2370 blkfront_gather_backend_features(info);
2371 for_each_rinfo(info, rinfo, i) {
2372 err = blkfront_setup_indirect(rinfo);
2374 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2375 info->xbdev->otherend);
2376 blkif_free(info, 0);
2381 err = xlvbd_alloc_gendisk(sectors, info, sector_size,
2382 physical_sector_size);
2384 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2385 info->xbdev->otherend);
2389 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2391 /* Kick pending requests. */
2392 info->connected = BLKIF_STATE_CONNECTED;
2393 for_each_rinfo(info, rinfo, i)
2394 kick_pending_request_queues(rinfo);
2396 err = device_add_disk(&info->xbdev->dev, info->gd, NULL);
2399 blk_mq_free_tag_set(&info->tag_set);
2408 blkif_free(info, 0);
2413 * Callback received when the backend's state changes.
2415 static void blkback_changed(struct xenbus_device *dev,
2416 enum xenbus_state backend_state)
2418 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2420 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2422 switch (backend_state) {
2423 case XenbusStateInitWait:
2424 if (dev->state != XenbusStateInitialising)
2426 if (talk_to_blkback(dev, info))
2429 case XenbusStateInitialising:
2430 case XenbusStateInitialised:
2431 case XenbusStateReconfiguring:
2432 case XenbusStateReconfigured:
2433 case XenbusStateUnknown:
2436 case XenbusStateConnected:
2438 * talk_to_blkback sets state to XenbusStateInitialised
2439 * and blkfront_connect sets it to XenbusStateConnected
2440 * (if connection went OK).
2442 * If the backend (or toolstack) decides to poke at backend
2443 * state (and re-trigger the watch by setting the state repeatedly
2444 * to XenbusStateConnected (4)) we need to deal with this.
2445 * This is allowed as this is used to communicate to the guest
2446 * that the size of disk has changed!
2448 if ((dev->state != XenbusStateInitialised) &&
2449 (dev->state != XenbusStateConnected)) {
2450 if (talk_to_blkback(dev, info))
2454 blkfront_connect(info);
2457 case XenbusStateClosed:
2458 if (dev->state == XenbusStateClosed)
2461 case XenbusStateClosing:
2462 blkfront_closing(info);
2467 static int blkfront_remove(struct xenbus_device *xbdev)
2469 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2471 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2474 del_gendisk(info->gd);
2476 mutex_lock(&blkfront_mutex);
2477 list_del(&info->info_list);
2478 mutex_unlock(&blkfront_mutex);
2480 blkif_free(info, 0);
2482 xlbd_release_minors(info->gd->first_minor, info->gd->minors);
2484 blk_mq_free_tag_set(&info->tag_set);
2491 static int blkfront_is_ready(struct xenbus_device *dev)
2493 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2495 return info->is_ready && info->xbdev;
2498 static const struct block_device_operations xlvbd_block_fops =
2500 .owner = THIS_MODULE,
2501 .getgeo = blkif_getgeo,
2502 .ioctl = blkif_ioctl,
2503 .compat_ioctl = blkdev_compat_ptr_ioctl,
2507 static const struct xenbus_device_id blkfront_ids[] = {
2512 static struct xenbus_driver blkfront_driver = {
2513 .ids = blkfront_ids,
2514 .probe = blkfront_probe,
2515 .remove = blkfront_remove,
2516 .resume = blkfront_resume,
2517 .otherend_changed = blkback_changed,
2518 .is_ready = blkfront_is_ready,
2521 static void purge_persistent_grants(struct blkfront_info *info)
2524 unsigned long flags;
2525 struct blkfront_ring_info *rinfo;
2527 for_each_rinfo(info, rinfo, i) {
2528 struct grant *gnt_list_entry, *tmp;
2531 spin_lock_irqsave(&rinfo->ring_lock, flags);
2533 if (rinfo->persistent_gnts_c == 0) {
2534 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2538 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
2540 if (gnt_list_entry->gref == INVALID_GRANT_REF ||
2541 !gnttab_try_end_foreign_access(gnt_list_entry->gref))
2544 list_del(&gnt_list_entry->node);
2545 rinfo->persistent_gnts_c--;
2546 gnt_list_entry->gref = INVALID_GRANT_REF;
2547 list_add_tail(&gnt_list_entry->node, &grants);
2550 list_splice_tail(&grants, &rinfo->grants);
2552 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2556 static void blkfront_delay_work(struct work_struct *work)
2558 struct blkfront_info *info;
2559 bool need_schedule_work = false;
2562 * Note that when using bounce buffers but not persistent grants
2563 * there's no need to run blkfront_delay_work because grants are
2564 * revoked in blkif_completion or else an error is reported and the
2565 * connection is closed.
2568 mutex_lock(&blkfront_mutex);
2570 list_for_each_entry(info, &info_list, info_list) {
2571 if (info->feature_persistent) {
2572 need_schedule_work = true;
2573 mutex_lock(&info->mutex);
2574 purge_persistent_grants(info);
2575 mutex_unlock(&info->mutex);
2579 if (need_schedule_work)
2580 schedule_delayed_work(&blkfront_work, HZ * 10);
2582 mutex_unlock(&blkfront_mutex);
2585 static int __init xlblk_init(void)
2588 int nr_cpus = num_online_cpus();
2593 if (!xen_has_pv_disk_devices())
2596 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2597 pr_warn("xen_blk: can't get major %d with name %s\n",
2598 XENVBD_MAJOR, DEV_NAME);
2602 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2603 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2605 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2606 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2607 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2608 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
2611 if (xen_blkif_max_queues > nr_cpus) {
2612 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2613 xen_blkif_max_queues, nr_cpus);
2614 xen_blkif_max_queues = nr_cpus;
2617 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
2619 ret = xenbus_register_frontend(&blkfront_driver);
2621 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2627 module_init(xlblk_init);
2630 static void __exit xlblk_exit(void)
2632 cancel_delayed_work_sync(&blkfront_work);
2634 xenbus_unregister_driver(&blkfront_driver);
2635 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2638 module_exit(xlblk_exit);
2640 MODULE_DESCRIPTION("Xen virtual block device frontend");
2641 MODULE_LICENSE("GPL");
2642 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2643 MODULE_ALIAS("xen:vbd");
2644 MODULE_ALIAS("xenblk");