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 static bool __read_mostly xen_blkif_trusted = true;
155 module_param_named(trusted, xen_blkif_trusted, bool, 0644);
156 MODULE_PARM_DESC(trusted, "Is the backend trusted");
158 #define BLK_RING_SIZE(info) \
159 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
161 #define BLK_MAX_RING_SIZE \
162 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
165 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
166 * characters are enough. Define to 20 to keep consistent with backend.
168 #define RINGREF_NAME_LEN (20)
170 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
172 #define QUEUE_NAME_LEN (17)
176 * Every blkfront device can associate with one or more blkfront_ring_info,
177 * depending on how many hardware queues/rings to be used.
179 struct blkfront_ring_info {
180 /* Lock to protect data in every ring buffer. */
181 spinlock_t ring_lock;
182 struct blkif_front_ring ring;
183 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
184 unsigned int evtchn, irq;
185 struct work_struct work;
186 struct gnttab_free_callback callback;
187 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
188 struct list_head indirect_pages;
189 struct list_head grants;
190 unsigned int persistent_gnts_c;
191 unsigned long shadow_free;
192 struct blkfront_info *dev_info;
196 * We have one of these per vbd, whether ide, scsi or 'other'. They
197 * hang in private_data off the gendisk structure. We may end up
198 * putting all kinds of interesting stuff here :-)
203 struct xenbus_device *xbdev;
206 unsigned int physical_sector_size;
209 enum blkif_state connected;
210 /* Number of pages per ring buffer. */
211 unsigned int nr_ring_pages;
212 struct request_queue *rq;
213 unsigned int feature_flush:1;
214 unsigned int feature_fua:1;
215 unsigned int feature_discard:1;
216 unsigned int feature_secdiscard:1;
217 unsigned int feature_persistent:1;
218 unsigned int bounce:1;
219 unsigned int discard_granularity;
220 unsigned int discard_alignment;
221 /* Number of 4KB segments handled */
222 unsigned int max_indirect_segments;
224 struct blk_mq_tag_set tag_set;
225 struct blkfront_ring_info *rinfo;
226 unsigned int nr_rings;
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 GRANT_INVALID_REF 0
239 #define PARTS_PER_DISK 16
240 #define PARTS_PER_EXT_DISK 256
242 #define BLKIF_MAJOR(dev) ((dev)>>8)
243 #define BLKIF_MINOR(dev) ((dev) & 0xff)
246 #define EXTENDED (1<<EXT_SHIFT)
247 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
248 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
249 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
250 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
251 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
252 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
254 #define DEV_NAME "xvd" /* name in /dev */
257 * Grants are always the same size as a Xen page (i.e 4KB).
258 * A physical segment is always the same size as a Linux page.
259 * Number of grants per physical segment
261 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
263 #define GRANTS_PER_INDIRECT_FRAME \
264 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
266 #define INDIRECT_GREFS(_grants) \
267 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
269 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
270 static void blkfront_gather_backend_features(struct blkfront_info *info);
271 static int negotiate_mq(struct blkfront_info *info);
273 static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
275 unsigned long free = rinfo->shadow_free;
277 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
278 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
279 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
283 static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
286 if (rinfo->shadow[id].req.u.rw.id != id)
288 if (rinfo->shadow[id].request == NULL)
290 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
291 rinfo->shadow[id].request = NULL;
292 rinfo->shadow_free = id;
296 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
298 struct blkfront_info *info = rinfo->dev_info;
299 struct page *granted_page;
300 struct grant *gnt_list_entry, *n;
304 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
309 granted_page = alloc_page(GFP_NOIO | __GFP_ZERO);
311 kfree(gnt_list_entry);
314 gnt_list_entry->page = granted_page;
317 gnt_list_entry->gref = GRANT_INVALID_REF;
318 list_add(&gnt_list_entry->node, &rinfo->grants);
325 list_for_each_entry_safe(gnt_list_entry, n,
326 &rinfo->grants, node) {
327 list_del(&gnt_list_entry->node);
329 __free_page(gnt_list_entry->page);
330 kfree(gnt_list_entry);
337 static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
339 struct grant *gnt_list_entry;
341 BUG_ON(list_empty(&rinfo->grants));
342 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
344 list_del(&gnt_list_entry->node);
346 if (gnt_list_entry->gref != GRANT_INVALID_REF)
347 rinfo->persistent_gnts_c--;
349 return gnt_list_entry;
352 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
353 const struct blkfront_info *info)
355 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
356 info->xbdev->otherend_id,
357 gnt_list_entry->page,
361 static struct grant *get_grant(grant_ref_t *gref_head,
363 struct blkfront_ring_info *rinfo)
365 struct grant *gnt_list_entry = get_free_grant(rinfo);
366 struct blkfront_info *info = rinfo->dev_info;
368 if (gnt_list_entry->gref != GRANT_INVALID_REF)
369 return gnt_list_entry;
371 /* Assign a gref to this page */
372 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
373 BUG_ON(gnt_list_entry->gref == -ENOSPC);
375 grant_foreign_access(gnt_list_entry, info);
377 /* Grant access to the GFN passed by the caller */
378 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
379 info->xbdev->otherend_id,
383 return gnt_list_entry;
386 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
387 struct blkfront_ring_info *rinfo)
389 struct grant *gnt_list_entry = get_free_grant(rinfo);
390 struct blkfront_info *info = rinfo->dev_info;
392 if (gnt_list_entry->gref != GRANT_INVALID_REF)
393 return gnt_list_entry;
395 /* Assign a gref to this page */
396 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
397 BUG_ON(gnt_list_entry->gref == -ENOSPC);
399 struct page *indirect_page;
401 /* Fetch a pre-allocated page to use for indirect grefs */
402 BUG_ON(list_empty(&rinfo->indirect_pages));
403 indirect_page = list_first_entry(&rinfo->indirect_pages,
405 list_del(&indirect_page->lru);
406 gnt_list_entry->page = indirect_page;
408 grant_foreign_access(gnt_list_entry, info);
410 return gnt_list_entry;
413 static const char *op_name(int op)
415 static const char *const names[] = {
416 [BLKIF_OP_READ] = "read",
417 [BLKIF_OP_WRITE] = "write",
418 [BLKIF_OP_WRITE_BARRIER] = "barrier",
419 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
420 [BLKIF_OP_DISCARD] = "discard" };
422 if (op < 0 || op >= ARRAY_SIZE(names))
430 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
432 unsigned int end = minor + nr;
435 if (end > nr_minors) {
436 unsigned long *bitmap, *old;
438 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
443 spin_lock(&minor_lock);
444 if (end > nr_minors) {
446 memcpy(bitmap, minors,
447 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
449 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
452 spin_unlock(&minor_lock);
456 spin_lock(&minor_lock);
457 if (find_next_bit(minors, end, minor) >= end) {
458 bitmap_set(minors, minor, nr);
462 spin_unlock(&minor_lock);
467 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
469 unsigned int end = minor + nr;
471 BUG_ON(end > nr_minors);
472 spin_lock(&minor_lock);
473 bitmap_clear(minors, minor, nr);
474 spin_unlock(&minor_lock);
477 static void blkif_restart_queue_callback(void *arg)
479 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
480 schedule_work(&rinfo->work);
483 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
485 /* We don't have real geometry info, but let's at least return
486 values consistent with the size of the device */
487 sector_t nsect = get_capacity(bd->bd_disk);
488 sector_t cylinders = nsect;
492 sector_div(cylinders, hg->heads * hg->sectors);
493 hg->cylinders = cylinders;
494 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
495 hg->cylinders = 0xffff;
499 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
500 unsigned command, unsigned long argument)
502 struct blkfront_info *info = bdev->bd_disk->private_data;
505 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
506 command, (long)argument);
509 case CDROMMULTISESSION:
510 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
511 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
512 if (put_user(0, (char __user *)(argument + i)))
516 case CDROM_GET_CAPABILITY: {
517 struct gendisk *gd = info->gd;
518 if (gd->flags & GENHD_FL_CD)
524 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
526 return -EINVAL; /* same return as native Linux */
532 static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
534 struct blkif_request **ring_req)
538 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
539 rinfo->ring.req_prod_pvt++;
541 id = get_id_from_freelist(rinfo);
542 rinfo->shadow[id].request = req;
543 rinfo->shadow[id].status = REQ_PROCESSING;
544 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
546 rinfo->shadow[id].req.u.rw.id = id;
551 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
553 struct blkfront_info *info = rinfo->dev_info;
554 struct blkif_request *ring_req, *final_ring_req;
557 /* Fill out a communications ring structure. */
558 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
559 ring_req = &rinfo->shadow[id].req;
561 ring_req->operation = BLKIF_OP_DISCARD;
562 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
563 ring_req->u.discard.id = id;
564 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
565 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
566 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
568 ring_req->u.discard.flag = 0;
570 /* Copy the request to the ring page. */
571 *final_ring_req = *ring_req;
572 rinfo->shadow[id].status = REQ_WAITING;
577 struct setup_rw_req {
578 unsigned int grant_idx;
579 struct blkif_request_segment *segments;
580 struct blkfront_ring_info *rinfo;
581 struct blkif_request *ring_req;
582 grant_ref_t gref_head;
584 /* Only used when persistent grant is used and it's a read request */
586 unsigned int bvec_off;
589 bool require_extra_req;
590 struct blkif_request *extra_ring_req;
593 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
594 unsigned int len, void *data)
596 struct setup_rw_req *setup = data;
598 struct grant *gnt_list_entry;
599 unsigned int fsect, lsect;
600 /* Convenient aliases */
601 unsigned int grant_idx = setup->grant_idx;
602 struct blkif_request *ring_req = setup->ring_req;
603 struct blkfront_ring_info *rinfo = setup->rinfo;
605 * We always use the shadow of the first request to store the list
606 * of grant associated to the block I/O request. This made the
607 * completion more easy to handle even if the block I/O request is
610 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
612 if (unlikely(setup->require_extra_req &&
613 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
615 * We are using the second request, setup grant_idx
616 * to be the index of the segment array.
618 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
619 ring_req = setup->extra_ring_req;
622 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
623 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
625 kunmap_atomic(setup->segments);
627 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
628 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
629 shadow->indirect_grants[n] = gnt_list_entry;
630 setup->segments = kmap_atomic(gnt_list_entry->page);
631 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
634 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
635 ref = gnt_list_entry->gref;
637 * All the grants are stored in the shadow of the first
638 * request. Therefore we have to use the global index.
640 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
642 if (setup->need_copy) {
645 shared_data = kmap_atomic(gnt_list_entry->page);
647 * this does not wipe data stored outside the
648 * range sg->offset..sg->offset+sg->length.
649 * Therefore, blkback *could* see data from
650 * previous requests. This is OK as long as
651 * persistent grants are shared with just one
652 * domain. It may need refactoring if this
655 memcpy(shared_data + offset,
656 setup->bvec_data + setup->bvec_off,
659 kunmap_atomic(shared_data);
660 setup->bvec_off += len;
664 lsect = fsect + (len >> 9) - 1;
665 if (ring_req->operation != BLKIF_OP_INDIRECT) {
666 ring_req->u.rw.seg[grant_idx] =
667 (struct blkif_request_segment) {
670 .last_sect = lsect };
672 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
673 (struct blkif_request_segment) {
676 .last_sect = lsect };
679 (setup->grant_idx)++;
682 static void blkif_setup_extra_req(struct blkif_request *first,
683 struct blkif_request *second)
685 uint16_t nr_segments = first->u.rw.nr_segments;
688 * The second request is only present when the first request uses
689 * all its segments. It's always the continuity of the first one.
691 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
693 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
694 second->u.rw.sector_number = first->u.rw.sector_number +
695 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
697 second->u.rw.handle = first->u.rw.handle;
698 second->operation = first->operation;
701 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
703 struct blkfront_info *info = rinfo->dev_info;
704 struct blkif_request *ring_req, *extra_ring_req = NULL;
705 struct blkif_request *final_ring_req, *final_extra_ring_req = NULL;
706 unsigned long id, extra_id = NO_ASSOCIATED_ID;
707 bool require_extra_req = false;
709 struct setup_rw_req setup = {
713 .need_copy = rq_data_dir(req) && info->bounce,
717 * Used to store if we are able to queue the request by just using
718 * existing persistent grants, or if we have to get new grants,
719 * as there are not sufficiently many free.
721 bool new_persistent_gnts = false;
722 struct scatterlist *sg;
723 int num_sg, max_grefs, num_grant;
725 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
726 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
728 * If we are using indirect segments we need to account
729 * for the indirect grefs used in the request.
731 max_grefs += INDIRECT_GREFS(max_grefs);
733 /* Check if we have enough persistent grants to allocate a requests */
734 if (rinfo->persistent_gnts_c < max_grefs) {
735 new_persistent_gnts = true;
737 if (gnttab_alloc_grant_references(
738 max_grefs - rinfo->persistent_gnts_c,
739 &setup.gref_head) < 0) {
740 gnttab_request_free_callback(
742 blkif_restart_queue_callback,
744 max_grefs - rinfo->persistent_gnts_c);
749 /* Fill out a communications ring structure. */
750 id = blkif_ring_get_request(rinfo, req, &final_ring_req);
751 ring_req = &rinfo->shadow[id].req;
753 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
755 /* Calculate the number of grant used */
756 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
757 num_grant += gnttab_count_grant(sg->offset, sg->length);
759 require_extra_req = info->max_indirect_segments == 0 &&
760 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
761 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
763 rinfo->shadow[id].num_sg = num_sg;
764 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
765 likely(!require_extra_req)) {
767 * The indirect operation can only be a BLKIF_OP_READ or
770 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
771 ring_req->operation = BLKIF_OP_INDIRECT;
772 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
773 BLKIF_OP_WRITE : BLKIF_OP_READ;
774 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
775 ring_req->u.indirect.handle = info->handle;
776 ring_req->u.indirect.nr_segments = num_grant;
778 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
779 ring_req->u.rw.handle = info->handle;
780 ring_req->operation = rq_data_dir(req) ?
781 BLKIF_OP_WRITE : BLKIF_OP_READ;
782 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
784 * Ideally we can do an unordered flush-to-disk.
785 * In case the backend onlysupports barriers, use that.
786 * A barrier request a superset of FUA, so we can
787 * implement it the same way. (It's also a FLUSH+FUA,
788 * since it is guaranteed ordered WRT previous writes.)
790 if (info->feature_flush && info->feature_fua)
791 ring_req->operation =
792 BLKIF_OP_WRITE_BARRIER;
793 else if (info->feature_flush)
794 ring_req->operation =
795 BLKIF_OP_FLUSH_DISKCACHE;
797 ring_req->operation = 0;
799 ring_req->u.rw.nr_segments = num_grant;
800 if (unlikely(require_extra_req)) {
801 extra_id = blkif_ring_get_request(rinfo, req,
802 &final_extra_ring_req);
803 extra_ring_req = &rinfo->shadow[extra_id].req;
806 * Only the first request contains the scatter-gather
809 rinfo->shadow[extra_id].num_sg = 0;
811 blkif_setup_extra_req(ring_req, extra_ring_req);
813 /* Link the 2 requests together */
814 rinfo->shadow[extra_id].associated_id = id;
815 rinfo->shadow[id].associated_id = extra_id;
819 setup.ring_req = ring_req;
822 setup.require_extra_req = require_extra_req;
823 if (unlikely(require_extra_req))
824 setup.extra_ring_req = extra_ring_req;
826 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
827 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
829 if (setup.need_copy) {
830 setup.bvec_off = sg->offset;
831 setup.bvec_data = kmap_atomic(sg_page(sg));
834 gnttab_foreach_grant_in_range(sg_page(sg),
837 blkif_setup_rw_req_grant,
841 kunmap_atomic(setup.bvec_data);
844 kunmap_atomic(setup.segments);
846 /* Copy request(s) to the ring page. */
847 *final_ring_req = *ring_req;
848 rinfo->shadow[id].status = REQ_WAITING;
849 if (unlikely(require_extra_req)) {
850 *final_extra_ring_req = *extra_ring_req;
851 rinfo->shadow[extra_id].status = REQ_WAITING;
854 if (new_persistent_gnts)
855 gnttab_free_grant_references(setup.gref_head);
861 * Generate a Xen blkfront IO request from a blk layer request. Reads
862 * and writes are handled as expected.
864 * @req: a request struct
866 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
868 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
871 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
872 req_op(req) == REQ_OP_SECURE_ERASE))
873 return blkif_queue_discard_req(req, rinfo);
875 return blkif_queue_rw_req(req, rinfo);
878 static inline void flush_requests(struct blkfront_ring_info *rinfo)
882 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
885 notify_remote_via_irq(rinfo->irq);
888 static inline bool blkif_request_flush_invalid(struct request *req,
889 struct blkfront_info *info)
891 return (blk_rq_is_passthrough(req) ||
892 ((req_op(req) == REQ_OP_FLUSH) &&
893 !info->feature_flush) ||
894 ((req->cmd_flags & REQ_FUA) &&
895 !info->feature_fua));
898 static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
899 const struct blk_mq_queue_data *qd)
902 int qid = hctx->queue_num;
903 struct blkfront_info *info = hctx->queue->queuedata;
904 struct blkfront_ring_info *rinfo = NULL;
906 BUG_ON(info->nr_rings <= qid);
907 rinfo = &info->rinfo[qid];
908 blk_mq_start_request(qd->rq);
909 spin_lock_irqsave(&rinfo->ring_lock, flags);
910 if (RING_FULL(&rinfo->ring))
913 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
916 if (blkif_queue_request(qd->rq, rinfo))
919 flush_requests(rinfo);
920 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
924 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
925 return BLK_STS_IOERR;
928 blk_mq_stop_hw_queue(hctx);
929 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
930 return BLK_STS_DEV_RESOURCE;
933 static void blkif_complete_rq(struct request *rq)
935 blk_mq_end_request(rq, blkif_req(rq)->error);
938 static const struct blk_mq_ops blkfront_mq_ops = {
939 .queue_rq = blkif_queue_rq,
940 .complete = blkif_complete_rq,
943 static void blkif_set_queue_limits(struct blkfront_info *info)
945 struct request_queue *rq = info->rq;
946 struct gendisk *gd = info->gd;
947 unsigned int segments = info->max_indirect_segments ? :
948 BLKIF_MAX_SEGMENTS_PER_REQUEST;
950 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
952 if (info->feature_discard) {
953 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
954 blk_queue_max_discard_sectors(rq, get_capacity(gd));
955 rq->limits.discard_granularity = info->discard_granularity ?:
956 info->physical_sector_size;
957 rq->limits.discard_alignment = info->discard_alignment;
958 if (info->feature_secdiscard)
959 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
962 /* Hard sector size and max sectors impersonate the equiv. hardware. */
963 blk_queue_logical_block_size(rq, info->sector_size);
964 blk_queue_physical_block_size(rq, info->physical_sector_size);
965 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
967 /* Each segment in a request is up to an aligned page in size. */
968 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
969 blk_queue_max_segment_size(rq, PAGE_SIZE);
971 /* Ensure a merged request will fit in a single I/O ring slot. */
972 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
974 /* Make sure buffer addresses are sector-aligned. */
975 blk_queue_dma_alignment(rq, 511);
978 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
979 unsigned int physical_sector_size)
981 struct request_queue *rq;
982 struct blkfront_info *info = gd->private_data;
984 memset(&info->tag_set, 0, sizeof(info->tag_set));
985 info->tag_set.ops = &blkfront_mq_ops;
986 info->tag_set.nr_hw_queues = info->nr_rings;
987 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
989 * When indirect descriptior is not supported, the I/O request
990 * will be split between multiple request in the ring.
991 * To avoid problems when sending the request, divide by
992 * 2 the depth of the queue.
994 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
996 info->tag_set.queue_depth = BLK_RING_SIZE(info);
997 info->tag_set.numa_node = NUMA_NO_NODE;
998 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
999 info->tag_set.cmd_size = sizeof(struct blkif_req);
1000 info->tag_set.driver_data = info;
1002 if (blk_mq_alloc_tag_set(&info->tag_set))
1004 rq = blk_mq_init_queue(&info->tag_set);
1006 blk_mq_free_tag_set(&info->tag_set);
1010 rq->queuedata = info;
1011 info->rq = gd->queue = rq;
1013 info->sector_size = sector_size;
1014 info->physical_sector_size = physical_sector_size;
1015 blkif_set_queue_limits(info);
1020 static const char *flush_info(struct blkfront_info *info)
1022 if (info->feature_flush && info->feature_fua)
1023 return "barrier: enabled;";
1024 else if (info->feature_flush)
1025 return "flush diskcache: enabled;";
1027 return "barrier or flush: disabled;";
1030 static void xlvbd_flush(struct blkfront_info *info)
1032 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1033 info->feature_fua ? true : false);
1034 pr_info("blkfront: %s: %s %s %s %s %s %s %s\n",
1035 info->gd->disk_name, flush_info(info),
1036 "persistent grants:", info->feature_persistent ?
1037 "enabled;" : "disabled;", "indirect descriptors:",
1038 info->max_indirect_segments ? "enabled;" : "disabled;",
1039 "bounce buffer:", info->bounce ? "enabled" : "disabled;");
1042 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1045 major = BLKIF_MAJOR(vdevice);
1046 *minor = BLKIF_MINOR(vdevice);
1048 case XEN_IDE0_MAJOR:
1049 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1050 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1051 EMULATED_HD_DISK_MINOR_OFFSET;
1053 case XEN_IDE1_MAJOR:
1054 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1055 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1056 EMULATED_HD_DISK_MINOR_OFFSET;
1058 case XEN_SCSI_DISK0_MAJOR:
1059 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1060 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1062 case XEN_SCSI_DISK1_MAJOR:
1063 case XEN_SCSI_DISK2_MAJOR:
1064 case XEN_SCSI_DISK3_MAJOR:
1065 case XEN_SCSI_DISK4_MAJOR:
1066 case XEN_SCSI_DISK5_MAJOR:
1067 case XEN_SCSI_DISK6_MAJOR:
1068 case XEN_SCSI_DISK7_MAJOR:
1069 *offset = (*minor / PARTS_PER_DISK) +
1070 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1071 EMULATED_SD_DISK_NAME_OFFSET;
1073 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1074 EMULATED_SD_DISK_MINOR_OFFSET;
1076 case XEN_SCSI_DISK8_MAJOR:
1077 case XEN_SCSI_DISK9_MAJOR:
1078 case XEN_SCSI_DISK10_MAJOR:
1079 case XEN_SCSI_DISK11_MAJOR:
1080 case XEN_SCSI_DISK12_MAJOR:
1081 case XEN_SCSI_DISK13_MAJOR:
1082 case XEN_SCSI_DISK14_MAJOR:
1083 case XEN_SCSI_DISK15_MAJOR:
1084 *offset = (*minor / PARTS_PER_DISK) +
1085 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1086 EMULATED_SD_DISK_NAME_OFFSET;
1088 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1089 EMULATED_SD_DISK_MINOR_OFFSET;
1092 *offset = *minor / PARTS_PER_DISK;
1095 printk(KERN_WARNING "blkfront: your disk configuration is "
1096 "incorrect, please use an xvd device instead\n");
1102 static char *encode_disk_name(char *ptr, unsigned int n)
1105 ptr = encode_disk_name(ptr, n / 26 - 1);
1106 *ptr = 'a' + n % 26;
1110 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1111 struct blkfront_info *info,
1112 u16 vdisk_info, u16 sector_size,
1113 unsigned int physical_sector_size)
1118 unsigned int offset;
1123 BUG_ON(info->gd != NULL);
1124 BUG_ON(info->rq != NULL);
1126 if ((info->vdevice>>EXT_SHIFT) > 1) {
1127 /* this is above the extended range; something is wrong */
1128 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1132 if (!VDEV_IS_EXTENDED(info->vdevice)) {
1133 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1136 nr_parts = PARTS_PER_DISK;
1138 minor = BLKIF_MINOR_EXT(info->vdevice);
1139 nr_parts = PARTS_PER_EXT_DISK;
1140 offset = minor / nr_parts;
1141 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
1142 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1143 "emulated IDE disks,\n\t choose an xvd device name"
1144 "from xvde on\n", info->vdevice);
1146 if (minor >> MINORBITS) {
1147 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1148 info->vdevice, minor);
1152 if ((minor % nr_parts) == 0)
1153 nr_minors = nr_parts;
1155 err = xlbd_reserve_minors(minor, nr_minors);
1160 gd = alloc_disk(nr_minors);
1164 strcpy(gd->disk_name, DEV_NAME);
1165 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1166 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1170 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1171 "%d", minor & (nr_parts - 1));
1173 gd->major = XENVBD_MAJOR;
1174 gd->first_minor = minor;
1175 gd->fops = &xlvbd_block_fops;
1176 gd->private_data = info;
1177 set_capacity(gd, capacity);
1179 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
1186 if (vdisk_info & VDISK_READONLY)
1189 if (vdisk_info & VDISK_REMOVABLE)
1190 gd->flags |= GENHD_FL_REMOVABLE;
1192 if (vdisk_info & VDISK_CDROM)
1193 gd->flags |= GENHD_FL_CD;
1198 xlbd_release_minors(minor, nr_minors);
1203 static void xlvbd_release_gendisk(struct blkfront_info *info)
1205 unsigned int minor, nr_minors, i;
1207 if (info->rq == NULL)
1210 /* No more blkif_request(). */
1211 blk_mq_stop_hw_queues(info->rq);
1213 for (i = 0; i < info->nr_rings; i++) {
1214 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1216 /* No more gnttab callback work. */
1217 gnttab_cancel_free_callback(&rinfo->callback);
1219 /* Flush gnttab callback work. Must be done with no locks held. */
1220 flush_work(&rinfo->work);
1223 del_gendisk(info->gd);
1225 minor = info->gd->first_minor;
1226 nr_minors = info->gd->minors;
1227 xlbd_release_minors(minor, nr_minors);
1229 blk_cleanup_queue(info->rq);
1230 blk_mq_free_tag_set(&info->tag_set);
1237 /* Already hold rinfo->ring_lock. */
1238 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
1240 if (!RING_FULL(&rinfo->ring))
1241 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
1244 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1246 unsigned long flags;
1248 spin_lock_irqsave(&rinfo->ring_lock, flags);
1249 kick_pending_request_queues_locked(rinfo);
1250 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1253 static void blkif_restart_queue(struct work_struct *work)
1255 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
1257 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1258 kick_pending_request_queues(rinfo);
1261 static void blkif_free_ring(struct blkfront_ring_info *rinfo)
1263 struct grant *persistent_gnt, *n;
1264 struct blkfront_info *info = rinfo->dev_info;
1268 * Remove indirect pages, this only happens when using indirect
1269 * descriptors but not persistent grants
1271 if (!list_empty(&rinfo->indirect_pages)) {
1272 struct page *indirect_page, *n;
1274 BUG_ON(info->bounce);
1275 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
1276 list_del(&indirect_page->lru);
1277 __free_page(indirect_page);
1281 /* Remove all persistent grants. */
1282 if (!list_empty(&rinfo->grants)) {
1283 list_for_each_entry_safe(persistent_gnt, n,
1284 &rinfo->grants, node) {
1285 list_del(&persistent_gnt->node);
1286 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1287 gnttab_end_foreign_access(persistent_gnt->gref,
1289 rinfo->persistent_gnts_c--;
1292 __free_page(persistent_gnt->page);
1293 kfree(persistent_gnt);
1296 BUG_ON(rinfo->persistent_gnts_c != 0);
1298 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1300 * Clear persistent grants present in requests already
1301 * on the shared ring
1303 if (!rinfo->shadow[i].request)
1306 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1307 rinfo->shadow[i].req.u.indirect.nr_segments :
1308 rinfo->shadow[i].req.u.rw.nr_segments;
1309 for (j = 0; j < segs; j++) {
1310 persistent_gnt = rinfo->shadow[i].grants_used[j];
1311 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1313 __free_page(persistent_gnt->page);
1314 kfree(persistent_gnt);
1317 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1319 * If this is not an indirect operation don't try to
1320 * free indirect segments
1324 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1325 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
1326 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1327 __free_page(persistent_gnt->page);
1328 kfree(persistent_gnt);
1332 kvfree(rinfo->shadow[i].grants_used);
1333 rinfo->shadow[i].grants_used = NULL;
1334 kvfree(rinfo->shadow[i].indirect_grants);
1335 rinfo->shadow[i].indirect_grants = NULL;
1336 kvfree(rinfo->shadow[i].sg);
1337 rinfo->shadow[i].sg = NULL;
1340 /* No more gnttab callback work. */
1341 gnttab_cancel_free_callback(&rinfo->callback);
1343 /* Flush gnttab callback work. Must be done with no locks held. */
1344 flush_work(&rinfo->work);
1346 /* Free resources associated with old device channel. */
1347 for (i = 0; i < info->nr_ring_pages; i++) {
1348 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1349 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1350 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1353 free_pages_exact(rinfo->ring.sring,
1354 info->nr_ring_pages * XEN_PAGE_SIZE);
1355 rinfo->ring.sring = NULL;
1358 unbind_from_irqhandler(rinfo->irq, rinfo);
1359 rinfo->evtchn = rinfo->irq = 0;
1362 static void blkif_free(struct blkfront_info *info, int suspend)
1366 /* Prevent new requests being issued until we fix things up. */
1367 info->connected = suspend ?
1368 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1369 /* No more blkif_request(). */
1371 blk_mq_stop_hw_queues(info->rq);
1373 for (i = 0; i < info->nr_rings; i++)
1374 blkif_free_ring(&info->rinfo[i]);
1376 kvfree(info->rinfo);
1381 struct copy_from_grant {
1382 const struct blk_shadow *s;
1383 unsigned int grant_idx;
1384 unsigned int bvec_offset;
1388 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1389 unsigned int len, void *data)
1391 struct copy_from_grant *info = data;
1393 /* Convenient aliases */
1394 const struct blk_shadow *s = info->s;
1396 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1398 memcpy(info->bvec_data + info->bvec_offset,
1399 shared_data + offset, len);
1401 info->bvec_offset += len;
1404 kunmap_atomic(shared_data);
1407 static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1411 case BLKIF_RSP_OKAY:
1413 case BLKIF_RSP_EOPNOTSUPP:
1414 return REQ_EOPNOTSUPP;
1415 case BLKIF_RSP_ERROR:
1423 * Get the final status of the block request based on two ring response
1425 static int blkif_get_final_status(enum blk_req_status s1,
1426 enum blk_req_status s2)
1428 BUG_ON(s1 < REQ_DONE);
1429 BUG_ON(s2 < REQ_DONE);
1431 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1432 return BLKIF_RSP_ERROR;
1433 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1434 return BLKIF_RSP_EOPNOTSUPP;
1435 return BLKIF_RSP_OKAY;
1440 * 1 response processed.
1441 * 0 missing further responses.
1442 * -1 error while processing.
1444 static int blkif_completion(unsigned long *id,
1445 struct blkfront_ring_info *rinfo,
1446 struct blkif_response *bret)
1449 struct scatterlist *sg;
1450 int num_sg, num_grant;
1451 struct blkfront_info *info = rinfo->dev_info;
1452 struct blk_shadow *s = &rinfo->shadow[*id];
1453 struct copy_from_grant data = {
1457 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1458 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1460 /* The I/O request may be split in two. */
1461 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1462 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1464 /* Keep the status of the current response in shadow. */
1465 s->status = blkif_rsp_to_req_status(bret->status);
1467 /* Wait the second response if not yet here. */
1468 if (s2->status < REQ_DONE)
1471 bret->status = blkif_get_final_status(s->status,
1475 * All the grants is stored in the first shadow in order
1476 * to make the completion code simpler.
1478 num_grant += s2->req.u.rw.nr_segments;
1481 * The two responses may not come in order. Only the
1482 * first request will store the scatter-gather list.
1484 if (s2->num_sg != 0) {
1485 /* Update "id" with the ID of the first response. */
1486 *id = s->associated_id;
1491 * We don't need anymore the second request, so recycling
1494 if (add_id_to_freelist(rinfo, s->associated_id))
1495 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1496 info->gd->disk_name, s->associated_id);
1502 if (bret->operation == BLKIF_OP_READ && info->bounce) {
1503 for_each_sg(s->sg, sg, num_sg, i) {
1504 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1506 data.bvec_offset = sg->offset;
1507 data.bvec_data = kmap_atomic(sg_page(sg));
1509 gnttab_foreach_grant_in_range(sg_page(sg),
1512 blkif_copy_from_grant,
1515 kunmap_atomic(data.bvec_data);
1518 /* Add the persistent grant into the list of free grants */
1519 for (i = 0; i < num_grant; i++) {
1520 if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) {
1522 * If the grant is still mapped by the backend (the
1523 * backend has chosen to make this grant persistent)
1524 * we add it at the head of the list, so it will be
1527 if (!info->feature_persistent) {
1528 pr_alert("backed has not unmapped grant: %u\n",
1529 s->grants_used[i]->gref);
1532 list_add(&s->grants_used[i]->node, &rinfo->grants);
1533 rinfo->persistent_gnts_c++;
1536 * If the grant is not mapped by the backend we add it
1537 * to the tail of the list, so it will not be picked
1538 * again unless we run out of persistent grants.
1540 s->grants_used[i]->gref = GRANT_INVALID_REF;
1541 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
1544 if (s->req.operation == BLKIF_OP_INDIRECT) {
1545 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1546 if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) {
1547 if (!info->feature_persistent) {
1548 pr_alert("backed has not unmapped grant: %u\n",
1549 s->indirect_grants[i]->gref);
1552 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1553 rinfo->persistent_gnts_c++;
1555 struct page *indirect_page;
1558 * Add the used indirect page back to the list of
1559 * available pages for indirect grefs.
1561 if (!info->bounce) {
1562 indirect_page = s->indirect_grants[i]->page;
1563 list_add(&indirect_page->lru, &rinfo->indirect_pages);
1565 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1566 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
1574 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1576 struct request *req;
1577 struct blkif_response bret;
1579 unsigned long flags;
1580 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1581 struct blkfront_info *info = rinfo->dev_info;
1582 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
1584 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
1585 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
1589 spin_lock_irqsave(&rinfo->ring_lock, flags);
1591 rp = READ_ONCE(rinfo->ring.sring->rsp_prod);
1592 virt_rmb(); /* Ensure we see queued responses up to 'rp'. */
1593 if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) {
1594 pr_alert("%s: illegal number of responses %u\n",
1595 info->gd->disk_name, rp - rinfo->ring.rsp_cons);
1599 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
1605 RING_COPY_RESPONSE(&rinfo->ring, i, &bret);
1609 * The backend has messed up and given us an id that we would
1610 * never have given to it (we stamp it up to BLK_RING_SIZE -
1611 * look in get_id_from_freelist.
1613 if (id >= BLK_RING_SIZE(info)) {
1614 pr_alert("%s: response has incorrect id (%ld)\n",
1615 info->gd->disk_name, id);
1618 if (rinfo->shadow[id].status != REQ_WAITING) {
1619 pr_alert("%s: response references no pending request\n",
1620 info->gd->disk_name);
1624 rinfo->shadow[id].status = REQ_PROCESSING;
1625 req = rinfo->shadow[id].request;
1627 op = rinfo->shadow[id].req.operation;
1628 if (op == BLKIF_OP_INDIRECT)
1629 op = rinfo->shadow[id].req.u.indirect.indirect_op;
1630 if (bret.operation != op) {
1631 pr_alert("%s: response has wrong operation (%u instead of %u)\n",
1632 info->gd->disk_name, bret.operation, op);
1636 if (bret.operation != BLKIF_OP_DISCARD) {
1640 * We may need to wait for an extra response if the
1641 * I/O request is split in 2
1643 ret = blkif_completion(&id, rinfo, &bret);
1646 if (unlikely(ret < 0))
1650 if (add_id_to_freelist(rinfo, id)) {
1651 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1652 info->gd->disk_name, op_name(bret.operation), id);
1656 if (bret.status == BLKIF_RSP_OKAY)
1657 blkif_req(req)->error = BLK_STS_OK;
1659 blkif_req(req)->error = BLK_STS_IOERR;
1661 switch (bret.operation) {
1662 case BLKIF_OP_DISCARD:
1663 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1664 struct request_queue *rq = info->rq;
1666 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1667 info->gd->disk_name, op_name(bret.operation));
1668 blkif_req(req)->error = BLK_STS_NOTSUPP;
1669 info->feature_discard = 0;
1670 info->feature_secdiscard = 0;
1671 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1672 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
1675 case BLKIF_OP_FLUSH_DISKCACHE:
1676 case BLKIF_OP_WRITE_BARRIER:
1677 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1678 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1679 info->gd->disk_name, op_name(bret.operation));
1680 blkif_req(req)->error = BLK_STS_NOTSUPP;
1682 if (unlikely(bret.status == BLKIF_RSP_ERROR &&
1683 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
1684 pr_warn_ratelimited("blkfront: %s: empty %s op failed\n",
1685 info->gd->disk_name, op_name(bret.operation));
1686 blkif_req(req)->error = BLK_STS_NOTSUPP;
1688 if (unlikely(blkif_req(req)->error)) {
1689 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1690 blkif_req(req)->error = BLK_STS_OK;
1691 info->feature_fua = 0;
1692 info->feature_flush = 0;
1697 case BLKIF_OP_WRITE:
1698 if (unlikely(bret.status != BLKIF_RSP_OKAY))
1699 dev_dbg_ratelimited(&info->xbdev->dev,
1700 "Bad return from blkdev data request: %#x\n",
1708 blk_mq_complete_request(req);
1711 rinfo->ring.rsp_cons = i;
1713 if (i != rinfo->ring.req_prod_pvt) {
1715 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
1719 rinfo->ring.sring->rsp_event = i + 1;
1721 kick_pending_request_queues_locked(rinfo);
1723 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1725 xen_irq_lateeoi(irq, eoiflag);
1730 info->connected = BLKIF_STATE_ERROR;
1732 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1734 /* No EOI in order to avoid further interrupts. */
1736 pr_alert("%s disabled for further use\n", info->gd->disk_name);
1741 static int setup_blkring(struct xenbus_device *dev,
1742 struct blkfront_ring_info *rinfo)
1744 struct blkif_sring *sring;
1746 struct blkfront_info *info = rinfo->dev_info;
1747 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1748 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
1750 for (i = 0; i < info->nr_ring_pages; i++)
1751 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1753 sring = alloc_pages_exact(ring_size, GFP_NOIO | __GFP_ZERO);
1755 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1758 SHARED_RING_INIT(sring);
1759 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
1761 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
1763 free_pages_exact(sring, ring_size);
1764 rinfo->ring.sring = NULL;
1767 for (i = 0; i < info->nr_ring_pages; i++)
1768 rinfo->ring_ref[i] = gref[i];
1770 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
1774 err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt,
1777 xenbus_dev_fatal(dev, err,
1778 "bind_evtchn_to_irqhandler failed");
1785 blkif_free(info, 0);
1790 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1791 * ring buffer may have multi pages depending on ->nr_ring_pages.
1793 static int write_per_ring_nodes(struct xenbus_transaction xbt,
1794 struct blkfront_ring_info *rinfo, const char *dir)
1798 const char *message = NULL;
1799 struct blkfront_info *info = rinfo->dev_info;
1801 if (info->nr_ring_pages == 1) {
1802 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1804 message = "writing ring-ref";
1805 goto abort_transaction;
1808 for (i = 0; i < info->nr_ring_pages; i++) {
1809 char ring_ref_name[RINGREF_NAME_LEN];
1811 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1812 err = xenbus_printf(xbt, dir, ring_ref_name,
1813 "%u", rinfo->ring_ref[i]);
1815 message = "writing ring-ref";
1816 goto abort_transaction;
1821 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1823 message = "writing event-channel";
1824 goto abort_transaction;
1830 xenbus_transaction_end(xbt, 1);
1832 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1837 static void free_info(struct blkfront_info *info)
1839 list_del(&info->info_list);
1843 /* Common code used when first setting up, and when resuming. */
1844 static int talk_to_blkback(struct xenbus_device *dev,
1845 struct blkfront_info *info)
1847 const char *message = NULL;
1848 struct xenbus_transaction xbt;
1850 unsigned int i, max_page_order;
1851 unsigned int ring_page_order;
1856 /* Check if backend is trusted. */
1857 info->bounce = !xen_blkif_trusted ||
1858 !xenbus_read_unsigned(dev->nodename, "trusted", 1);
1860 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1861 "max-ring-page-order", 0);
1862 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1863 info->nr_ring_pages = 1 << ring_page_order;
1865 err = negotiate_mq(info);
1867 goto destroy_blkring;
1869 for (i = 0; i < info->nr_rings; i++) {
1870 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1872 /* Create shared ring, alloc event channel. */
1873 err = setup_blkring(dev, rinfo);
1875 goto destroy_blkring;
1879 err = xenbus_transaction_start(&xbt);
1881 xenbus_dev_fatal(dev, err, "starting transaction");
1882 goto destroy_blkring;
1885 if (info->nr_ring_pages > 1) {
1886 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1889 message = "writing ring-page-order";
1890 goto abort_transaction;
1894 /* We already got the number of queues/rings in _probe */
1895 if (info->nr_rings == 1) {
1896 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1898 goto destroy_blkring;
1903 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1906 message = "writing multi-queue-num-queues";
1907 goto abort_transaction;
1910 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1911 path = kmalloc(pathsize, GFP_KERNEL);
1914 message = "ENOMEM while writing ring references";
1915 goto abort_transaction;
1918 for (i = 0; i < info->nr_rings; i++) {
1919 memset(path, 0, pathsize);
1920 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1921 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1924 goto destroy_blkring;
1929 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1930 XEN_IO_PROTO_ABI_NATIVE);
1932 message = "writing protocol";
1933 goto abort_transaction;
1935 err = xenbus_printf(xbt, dev->nodename,
1936 "feature-persistent", "%u", 1);
1939 "writing persistent grants feature to xenbus");
1941 err = xenbus_transaction_end(xbt, 0);
1945 xenbus_dev_fatal(dev, err, "completing transaction");
1946 goto destroy_blkring;
1949 for (i = 0; i < info->nr_rings; i++) {
1951 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1953 for (j = 0; j < BLK_RING_SIZE(info); j++)
1954 rinfo->shadow[j].req.u.rw.id = j + 1;
1955 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1957 xenbus_switch_state(dev, XenbusStateInitialised);
1962 xenbus_transaction_end(xbt, 1);
1964 xenbus_dev_fatal(dev, err, "%s", message);
1966 blkif_free(info, 0);
1968 mutex_lock(&blkfront_mutex);
1970 mutex_unlock(&blkfront_mutex);
1972 dev_set_drvdata(&dev->dev, NULL);
1977 static int negotiate_mq(struct blkfront_info *info)
1979 unsigned int backend_max_queues;
1982 BUG_ON(info->nr_rings);
1984 /* Check if backend supports multiple queues. */
1985 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1986 "multi-queue-max-queues", 1);
1987 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1988 /* We need at least one ring. */
1989 if (!info->nr_rings)
1992 info->rinfo = kvcalloc(info->nr_rings,
1993 sizeof(struct blkfront_ring_info),
1996 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
2001 for (i = 0; i < info->nr_rings; i++) {
2002 struct blkfront_ring_info *rinfo;
2004 rinfo = &info->rinfo[i];
2005 INIT_LIST_HEAD(&rinfo->indirect_pages);
2006 INIT_LIST_HEAD(&rinfo->grants);
2007 rinfo->dev_info = info;
2008 INIT_WORK(&rinfo->work, blkif_restart_queue);
2009 spin_lock_init(&rinfo->ring_lock);
2014 * Entry point to this code when a new device is created. Allocate the basic
2015 * structures and the ring buffer for communication with the backend, and
2016 * inform the backend of the appropriate details for those. Switch to
2017 * Initialised state.
2019 static int blkfront_probe(struct xenbus_device *dev,
2020 const struct xenbus_device_id *id)
2023 struct blkfront_info *info;
2025 /* FIXME: Use dynamic device id if this is not set. */
2026 err = xenbus_scanf(XBT_NIL, dev->nodename,
2027 "virtual-device", "%i", &vdevice);
2029 /* go looking in the extended area instead */
2030 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
2033 xenbus_dev_fatal(dev, err, "reading virtual-device");
2038 if (xen_hvm_domain()) {
2041 /* no unplug has been done: do not hook devices != xen vbds */
2042 if (xen_has_pv_and_legacy_disk_devices()) {
2045 if (!VDEV_IS_EXTENDED(vdevice))
2046 major = BLKIF_MAJOR(vdevice);
2048 major = XENVBD_MAJOR;
2050 if (major != XENVBD_MAJOR) {
2052 "%s: HVM does not support vbd %d as xen block device\n",
2057 /* do not create a PV cdrom device if we are an HVM guest */
2058 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
2061 if (strncmp(type, "cdrom", 5) == 0) {
2067 info = kzalloc(sizeof(*info), GFP_KERNEL);
2069 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
2075 mutex_init(&info->mutex);
2076 info->vdevice = vdevice;
2077 info->connected = BLKIF_STATE_DISCONNECTED;
2079 /* Front end dir is a number, which is used as the id. */
2080 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
2081 dev_set_drvdata(&dev->dev, info);
2083 mutex_lock(&blkfront_mutex);
2084 list_add(&info->info_list, &info_list);
2085 mutex_unlock(&blkfront_mutex);
2090 static int blkif_recover(struct blkfront_info *info)
2092 unsigned int r_index;
2093 struct request *req, *n;
2098 blkfront_gather_backend_features(info);
2099 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2100 blkif_set_queue_limits(info);
2101 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
2102 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
2104 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2105 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
2107 rc = blkfront_setup_indirect(rinfo);
2111 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2113 /* Now safe for us to use the shared ring */
2114 info->connected = BLKIF_STATE_CONNECTED;
2116 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2117 struct blkfront_ring_info *rinfo;
2119 rinfo = &info->rinfo[r_index];
2120 /* Kick any other new requests queued since we resumed */
2121 kick_pending_request_queues(rinfo);
2124 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
2125 /* Requeue pending requests (flush or discard) */
2126 list_del_init(&req->queuelist);
2127 BUG_ON(req->nr_phys_segments > segs);
2128 blk_mq_requeue_request(req, false);
2130 blk_mq_start_stopped_hw_queues(info->rq, true);
2131 blk_mq_kick_requeue_list(info->rq);
2133 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
2134 /* Traverse the list of pending bios and re-queue them */
2142 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2143 * driver restart. We tear down our blkif structure and recreate it, but
2144 * leave the device-layer structures intact so that this is transparent to the
2145 * rest of the kernel.
2147 static int blkfront_resume(struct xenbus_device *dev)
2149 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2153 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2155 bio_list_init(&info->bio_list);
2156 INIT_LIST_HEAD(&info->requests);
2157 for (i = 0; i < info->nr_rings; i++) {
2158 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2159 struct bio_list merge_bio;
2160 struct blk_shadow *shadow = rinfo->shadow;
2162 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2164 if (!shadow[j].request)
2168 * Get the bios in the request so we can re-queue them.
2170 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2171 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2172 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
2173 shadow[j].request->cmd_flags & REQ_FUA) {
2175 * Flush operations don't contain bios, so
2176 * we need to requeue the whole request
2178 * XXX: but this doesn't make any sense for a
2179 * write with the FUA flag set..
2181 list_add(&shadow[j].request->queuelist, &info->requests);
2184 merge_bio.head = shadow[j].request->bio;
2185 merge_bio.tail = shadow[j].request->biotail;
2186 bio_list_merge(&info->bio_list, &merge_bio);
2187 shadow[j].request->bio = NULL;
2188 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
2192 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2194 err = talk_to_blkback(dev, info);
2196 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
2199 * We have to wait for the backend to switch to
2200 * connected state, since we want to read which
2201 * features it supports.
2207 static void blkfront_closing(struct blkfront_info *info)
2209 struct xenbus_device *xbdev = info->xbdev;
2210 struct block_device *bdev = NULL;
2212 mutex_lock(&info->mutex);
2214 if (xbdev->state == XenbusStateClosing) {
2215 mutex_unlock(&info->mutex);
2220 bdev = bdget_disk(info->gd, 0);
2222 mutex_unlock(&info->mutex);
2225 xenbus_frontend_closed(xbdev);
2229 mutex_lock(&bdev->bd_mutex);
2231 if (bdev->bd_openers) {
2232 xenbus_dev_error(xbdev, -EBUSY,
2233 "Device in use; refusing to close");
2234 xenbus_switch_state(xbdev, XenbusStateClosing);
2236 xlvbd_release_gendisk(info);
2237 xenbus_frontend_closed(xbdev);
2240 mutex_unlock(&bdev->bd_mutex);
2244 static void blkfront_setup_discard(struct blkfront_info *info)
2246 info->feature_discard = 1;
2247 info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend,
2248 "discard-granularity",
2250 info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend,
2251 "discard-alignment", 0);
2252 info->feature_secdiscard =
2253 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2257 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
2259 unsigned int psegs, grants, memflags;
2261 struct blkfront_info *info = rinfo->dev_info;
2263 memflags = memalloc_noio_save();
2265 if (info->max_indirect_segments == 0) {
2267 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2270 * When an extra req is required, the maximum
2271 * grants supported is related to the size of the
2272 * Linux block segment.
2274 grants = GRANTS_PER_PSEG;
2278 grants = info->max_indirect_segments;
2279 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
2281 err = fill_grant_buffer(rinfo,
2282 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
2286 if (!info->bounce && info->max_indirect_segments) {
2288 * We are using indirect descriptors but don't have a bounce
2289 * buffer, we need to allocate a set of pages that can be
2290 * used for mapping indirect grefs
2292 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
2294 BUG_ON(!list_empty(&rinfo->indirect_pages));
2295 for (i = 0; i < num; i++) {
2296 struct page *indirect_page = alloc_page(GFP_KERNEL |
2300 list_add(&indirect_page->lru, &rinfo->indirect_pages);
2304 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2305 rinfo->shadow[i].grants_used =
2307 sizeof(rinfo->shadow[i].grants_used[0]),
2309 rinfo->shadow[i].sg = kvcalloc(psegs,
2310 sizeof(rinfo->shadow[i].sg[0]),
2312 if (info->max_indirect_segments)
2313 rinfo->shadow[i].indirect_grants =
2314 kvcalloc(INDIRECT_GREFS(grants),
2315 sizeof(rinfo->shadow[i].indirect_grants[0]),
2317 if ((rinfo->shadow[i].grants_used == NULL) ||
2318 (rinfo->shadow[i].sg == NULL) ||
2319 (info->max_indirect_segments &&
2320 (rinfo->shadow[i].indirect_grants == NULL)))
2322 sg_init_table(rinfo->shadow[i].sg, psegs);
2325 memalloc_noio_restore(memflags);
2330 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2331 kvfree(rinfo->shadow[i].grants_used);
2332 rinfo->shadow[i].grants_used = NULL;
2333 kvfree(rinfo->shadow[i].sg);
2334 rinfo->shadow[i].sg = NULL;
2335 kvfree(rinfo->shadow[i].indirect_grants);
2336 rinfo->shadow[i].indirect_grants = NULL;
2338 if (!list_empty(&rinfo->indirect_pages)) {
2339 struct page *indirect_page, *n;
2340 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
2341 list_del(&indirect_page->lru);
2342 __free_page(indirect_page);
2346 memalloc_noio_restore(memflags);
2352 * Gather all backend feature-*
2354 static void blkfront_gather_backend_features(struct blkfront_info *info)
2356 unsigned int indirect_segments;
2358 info->feature_flush = 0;
2359 info->feature_fua = 0;
2362 * If there's no "feature-barrier" defined, then it means
2363 * we're dealing with a very old backend which writes
2364 * synchronously; nothing to do.
2366 * If there are barriers, then we use flush.
2368 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
2369 info->feature_flush = 1;
2370 info->feature_fua = 1;
2374 * And if there is "feature-flush-cache" use that above
2377 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2379 info->feature_flush = 1;
2380 info->feature_fua = 0;
2383 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
2384 blkfront_setup_discard(info);
2386 info->feature_persistent =
2387 !!xenbus_read_unsigned(info->xbdev->otherend,
2388 "feature-persistent", 0);
2389 if (info->feature_persistent)
2390 info->bounce = true;
2392 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2393 "feature-max-indirect-segments", 0);
2394 if (indirect_segments > xen_blkif_max_segments)
2395 indirect_segments = xen_blkif_max_segments;
2396 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2397 indirect_segments = 0;
2398 info->max_indirect_segments = indirect_segments;
2400 if (info->feature_persistent) {
2401 mutex_lock(&blkfront_mutex);
2402 schedule_delayed_work(&blkfront_work, HZ * 10);
2403 mutex_unlock(&blkfront_mutex);
2408 * Invoked when the backend is finally 'ready' (and has told produced
2409 * the details about the physical device - #sectors, size, etc).
2411 static void blkfront_connect(struct blkfront_info *info)
2413 unsigned long long sectors;
2414 unsigned long sector_size;
2415 unsigned int physical_sector_size;
2417 char *envp[] = { "RESIZE=1", NULL };
2420 switch (info->connected) {
2421 case BLKIF_STATE_CONNECTED:
2423 * Potentially, the back-end may be signalling
2424 * a capacity change; update the capacity.
2426 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2427 "sectors", "%Lu", §ors);
2428 if (XENBUS_EXIST_ERR(err))
2430 printk(KERN_INFO "Setting capacity to %Lu\n",
2432 set_capacity(info->gd, sectors);
2433 revalidate_disk(info->gd);
2434 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2438 case BLKIF_STATE_SUSPENDED:
2440 * If we are recovering from suspension, we need to wait
2441 * for the backend to announce it's features before
2442 * reconnecting, at least we need to know if the backend
2443 * supports indirect descriptors, and how many.
2445 blkif_recover(info);
2452 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2453 __func__, info->xbdev->otherend);
2455 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2456 "sectors", "%llu", §ors,
2457 "info", "%u", &binfo,
2458 "sector-size", "%lu", §or_size,
2461 xenbus_dev_fatal(info->xbdev, err,
2462 "reading backend fields at %s",
2463 info->xbdev->otherend);
2468 * physcial-sector-size is a newer field, so old backends may not
2469 * provide this. Assume physical sector size to be the same as
2470 * sector_size in that case.
2472 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2473 "physical-sector-size",
2475 blkfront_gather_backend_features(info);
2476 for (i = 0; i < info->nr_rings; i++) {
2477 err = blkfront_setup_indirect(&info->rinfo[i]);
2479 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2480 info->xbdev->otherend);
2481 blkif_free(info, 0);
2486 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2487 physical_sector_size);
2489 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2490 info->xbdev->otherend);
2494 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2496 /* Kick pending requests. */
2497 info->connected = BLKIF_STATE_CONNECTED;
2498 for (i = 0; i < info->nr_rings; i++)
2499 kick_pending_request_queues(&info->rinfo[i]);
2501 device_add_disk(&info->xbdev->dev, info->gd, NULL);
2507 blkif_free(info, 0);
2512 * Callback received when the backend's state changes.
2514 static void blkback_changed(struct xenbus_device *dev,
2515 enum xenbus_state backend_state)
2517 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2519 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2521 switch (backend_state) {
2522 case XenbusStateInitWait:
2523 if (dev->state != XenbusStateInitialising)
2525 if (talk_to_blkback(dev, info))
2527 case XenbusStateInitialising:
2528 case XenbusStateInitialised:
2529 case XenbusStateReconfiguring:
2530 case XenbusStateReconfigured:
2531 case XenbusStateUnknown:
2534 case XenbusStateConnected:
2536 * talk_to_blkback sets state to XenbusStateInitialised
2537 * and blkfront_connect sets it to XenbusStateConnected
2538 * (if connection went OK).
2540 * If the backend (or toolstack) decides to poke at backend
2541 * state (and re-trigger the watch by setting the state repeatedly
2542 * to XenbusStateConnected (4)) we need to deal with this.
2543 * This is allowed as this is used to communicate to the guest
2544 * that the size of disk has changed!
2546 if ((dev->state != XenbusStateInitialised) &&
2547 (dev->state != XenbusStateConnected)) {
2548 if (talk_to_blkback(dev, info))
2552 blkfront_connect(info);
2555 case XenbusStateClosed:
2556 if (dev->state == XenbusStateClosed)
2559 case XenbusStateClosing:
2561 blkfront_closing(info);
2566 static int blkfront_remove(struct xenbus_device *xbdev)
2568 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2569 struct block_device *bdev = NULL;
2570 struct gendisk *disk;
2572 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2577 blkif_free(info, 0);
2579 mutex_lock(&info->mutex);
2583 bdev = bdget_disk(disk, 0);
2586 mutex_unlock(&info->mutex);
2589 mutex_lock(&blkfront_mutex);
2591 mutex_unlock(&blkfront_mutex);
2596 * The xbdev was removed before we reached the Closed
2597 * state. See if it's safe to remove the disk. If the bdev
2598 * isn't closed yet, we let release take care of it.
2601 mutex_lock(&bdev->bd_mutex);
2602 info = disk->private_data;
2604 dev_warn(disk_to_dev(disk),
2605 "%s was hot-unplugged, %d stale handles\n",
2606 xbdev->nodename, bdev->bd_openers);
2608 if (info && !bdev->bd_openers) {
2609 xlvbd_release_gendisk(info);
2610 disk->private_data = NULL;
2611 mutex_lock(&blkfront_mutex);
2613 mutex_unlock(&blkfront_mutex);
2616 mutex_unlock(&bdev->bd_mutex);
2622 static int blkfront_is_ready(struct xenbus_device *dev)
2624 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2626 return info->is_ready && info->xbdev;
2629 static int blkif_open(struct block_device *bdev, fmode_t mode)
2631 struct gendisk *disk = bdev->bd_disk;
2632 struct blkfront_info *info;
2635 mutex_lock(&blkfront_mutex);
2637 info = disk->private_data;
2644 mutex_lock(&info->mutex);
2647 /* xbdev is closed */
2650 mutex_unlock(&info->mutex);
2653 mutex_unlock(&blkfront_mutex);
2657 static void blkif_release(struct gendisk *disk, fmode_t mode)
2659 struct blkfront_info *info = disk->private_data;
2660 struct block_device *bdev;
2661 struct xenbus_device *xbdev;
2663 mutex_lock(&blkfront_mutex);
2665 bdev = bdget_disk(disk, 0);
2668 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2671 if (bdev->bd_openers)
2675 * Check if we have been instructed to close. We will have
2676 * deferred this request, because the bdev was still open.
2679 mutex_lock(&info->mutex);
2680 xbdev = info->xbdev;
2682 if (xbdev && xbdev->state == XenbusStateClosing) {
2683 /* pending switch to state closed */
2684 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2685 xlvbd_release_gendisk(info);
2686 xenbus_frontend_closed(info->xbdev);
2689 mutex_unlock(&info->mutex);
2692 /* sudden device removal */
2693 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2694 xlvbd_release_gendisk(info);
2695 disk->private_data = NULL;
2702 mutex_unlock(&blkfront_mutex);
2705 static const struct block_device_operations xlvbd_block_fops =
2707 .owner = THIS_MODULE,
2709 .release = blkif_release,
2710 .getgeo = blkif_getgeo,
2711 .ioctl = blkif_ioctl,
2715 static const struct xenbus_device_id blkfront_ids[] = {
2720 static struct xenbus_driver blkfront_driver = {
2721 .ids = blkfront_ids,
2722 .probe = blkfront_probe,
2723 .remove = blkfront_remove,
2724 .resume = blkfront_resume,
2725 .otherend_changed = blkback_changed,
2726 .is_ready = blkfront_is_ready,
2729 static void purge_persistent_grants(struct blkfront_info *info)
2732 unsigned long flags;
2734 for (i = 0; i < info->nr_rings; i++) {
2735 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2736 struct grant *gnt_list_entry, *tmp;
2738 spin_lock_irqsave(&rinfo->ring_lock, flags);
2740 if (rinfo->persistent_gnts_c == 0) {
2741 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2745 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
2747 if (gnt_list_entry->gref == GRANT_INVALID_REF ||
2748 !gnttab_try_end_foreign_access(gnt_list_entry->gref))
2751 list_del(&gnt_list_entry->node);
2752 rinfo->persistent_gnts_c--;
2753 gnt_list_entry->gref = GRANT_INVALID_REF;
2754 list_add_tail(&gnt_list_entry->node, &rinfo->grants);
2757 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2761 static void blkfront_delay_work(struct work_struct *work)
2763 struct blkfront_info *info;
2764 bool need_schedule_work = false;
2767 * Note that when using bounce buffers but not persistent grants
2768 * there's no need to run blkfront_delay_work because grants are
2769 * revoked in blkif_completion or else an error is reported and the
2770 * connection is closed.
2773 mutex_lock(&blkfront_mutex);
2775 list_for_each_entry(info, &info_list, info_list) {
2776 if (info->feature_persistent) {
2777 need_schedule_work = true;
2778 mutex_lock(&info->mutex);
2779 purge_persistent_grants(info);
2780 mutex_unlock(&info->mutex);
2784 if (need_schedule_work)
2785 schedule_delayed_work(&blkfront_work, HZ * 10);
2787 mutex_unlock(&blkfront_mutex);
2790 static int __init xlblk_init(void)
2793 int nr_cpus = num_online_cpus();
2798 if (!xen_has_pv_disk_devices())
2801 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2802 pr_warn("xen_blk: can't get major %d with name %s\n",
2803 XENVBD_MAJOR, DEV_NAME);
2807 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2808 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2810 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2811 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2812 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2813 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
2816 if (xen_blkif_max_queues > nr_cpus) {
2817 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2818 xen_blkif_max_queues, nr_cpus);
2819 xen_blkif_max_queues = nr_cpus;
2822 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
2824 ret = xenbus_register_frontend(&blkfront_driver);
2826 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2832 module_init(xlblk_init);
2835 static void __exit xlblk_exit(void)
2837 cancel_delayed_work_sync(&blkfront_work);
2839 xenbus_unregister_driver(&blkfront_driver);
2840 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2843 module_exit(xlblk_exit);
2845 MODULE_DESCRIPTION("Xen virtual block device frontend");
2846 MODULE_LICENSE("GPL");
2847 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2848 MODULE_ALIAS("xen:vbd");
2849 MODULE_ALIAS("xenblk");