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>
52 #include <xen/xenbus.h>
53 #include <xen/grant_table.h>
54 #include <xen/events.h>
56 #include <xen/platform_pci.h>
58 #include <xen/interface/grant_table.h>
59 #include <xen/interface/io/blkif.h>
60 #include <xen/interface/io/protocols.h>
62 #include <asm/xen/hypervisor.h>
65 * The minimal size of segment supported by the block framework is PAGE_SIZE.
66 * When Linux is using a different page size than Xen, it may not be possible
67 * to put all the data in a single segment.
68 * This can happen when the backend doesn't support indirect descriptor and
69 * therefore the maximum amount of data that a request can carry is
70 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
72 * Note that we only support one extra request. So the Linux page size
73 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
76 #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
79 BLKIF_STATE_DISCONNECTED,
80 BLKIF_STATE_CONNECTED,
81 BLKIF_STATE_SUSPENDED,
87 struct list_head node;
98 struct blkif_request req;
99 struct request *request;
100 struct grant **grants_used;
101 struct grant **indirect_grants;
102 struct scatterlist *sg;
104 enum blk_req_status status;
106 #define NO_ASSOCIATED_ID ~0UL
108 * Id of the sibling if we ever need 2 requests when handling a
111 unsigned long associated_id;
118 static inline struct blkif_req *blkif_req(struct request *rq)
120 return blk_mq_rq_to_pdu(rq);
123 static DEFINE_MUTEX(blkfront_mutex);
124 static const struct block_device_operations xlvbd_block_fops;
125 static struct delayed_work blkfront_work;
126 static LIST_HEAD(info_list);
129 * Maximum number of segments in indirect requests, the actual value used by
130 * the frontend driver is the minimum of this value and the value provided
131 * by the backend driver.
134 static unsigned int xen_blkif_max_segments = 32;
135 module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
136 MODULE_PARM_DESC(max_indirect_segments,
137 "Maximum amount of segments in indirect requests (default is 32)");
139 static unsigned int xen_blkif_max_queues = 4;
140 module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
141 MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
144 * Maximum order of pages to be used for the shared ring between front and
145 * backend, 4KB page granularity is used.
147 static unsigned int xen_blkif_max_ring_order;
148 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
149 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
151 #define BLK_RING_SIZE(info) \
152 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
154 #define BLK_MAX_RING_SIZE \
155 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
158 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
159 * characters are enough. Define to 20 to keep consistent with backend.
161 #define RINGREF_NAME_LEN (20)
163 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
165 #define QUEUE_NAME_LEN (17)
169 * Every blkfront device can associate with one or more blkfront_ring_info,
170 * depending on how many hardware queues/rings to be used.
172 struct blkfront_ring_info {
173 /* Lock to protect data in every ring buffer. */
174 spinlock_t ring_lock;
175 struct blkif_front_ring ring;
176 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
177 unsigned int evtchn, irq;
178 struct work_struct work;
179 struct gnttab_free_callback callback;
180 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
181 struct list_head indirect_pages;
182 struct list_head grants;
183 unsigned int persistent_gnts_c;
184 unsigned long shadow_free;
185 struct blkfront_info *dev_info;
189 * We have one of these per vbd, whether ide, scsi or 'other'. They
190 * hang in private_data off the gendisk structure. We may end up
191 * putting all kinds of interesting stuff here :-)
196 struct xenbus_device *xbdev;
199 unsigned int physical_sector_size;
202 enum blkif_state connected;
203 /* Number of pages per ring buffer. */
204 unsigned int nr_ring_pages;
205 struct request_queue *rq;
206 unsigned int feature_flush:1;
207 unsigned int feature_fua:1;
208 unsigned int feature_discard:1;
209 unsigned int feature_secdiscard:1;
210 unsigned int feature_persistent:1;
211 unsigned int discard_granularity;
212 unsigned int discard_alignment;
213 /* Number of 4KB segments handled */
214 unsigned int max_indirect_segments;
216 struct blk_mq_tag_set tag_set;
217 struct blkfront_ring_info *rinfo;
218 unsigned int nr_rings;
219 /* Save uncomplete reqs and bios for migration. */
220 struct list_head requests;
221 struct bio_list bio_list;
222 struct list_head info_list;
225 static unsigned int nr_minors;
226 static unsigned long *minors;
227 static DEFINE_SPINLOCK(minor_lock);
229 #define GRANT_INVALID_REF 0
231 #define PARTS_PER_DISK 16
232 #define PARTS_PER_EXT_DISK 256
234 #define BLKIF_MAJOR(dev) ((dev)>>8)
235 #define BLKIF_MINOR(dev) ((dev) & 0xff)
238 #define EXTENDED (1<<EXT_SHIFT)
239 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
240 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
241 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
242 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
243 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
244 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
246 #define DEV_NAME "xvd" /* name in /dev */
249 * Grants are always the same size as a Xen page (i.e 4KB).
250 * A physical segment is always the same size as a Linux page.
251 * Number of grants per physical segment
253 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
255 #define GRANTS_PER_INDIRECT_FRAME \
256 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
258 #define INDIRECT_GREFS(_grants) \
259 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
261 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
262 static void blkfront_gather_backend_features(struct blkfront_info *info);
263 static int negotiate_mq(struct blkfront_info *info);
265 static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
267 unsigned long free = rinfo->shadow_free;
269 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
270 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
271 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
275 static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
278 if (rinfo->shadow[id].req.u.rw.id != id)
280 if (rinfo->shadow[id].request == NULL)
282 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
283 rinfo->shadow[id].request = NULL;
284 rinfo->shadow_free = id;
288 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
290 struct blkfront_info *info = rinfo->dev_info;
291 struct page *granted_page;
292 struct grant *gnt_list_entry, *n;
296 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
300 if (info->feature_persistent) {
301 granted_page = alloc_page(GFP_NOIO);
303 kfree(gnt_list_entry);
306 gnt_list_entry->page = granted_page;
309 gnt_list_entry->gref = GRANT_INVALID_REF;
310 list_add(&gnt_list_entry->node, &rinfo->grants);
317 list_for_each_entry_safe(gnt_list_entry, n,
318 &rinfo->grants, node) {
319 list_del(&gnt_list_entry->node);
320 if (info->feature_persistent)
321 __free_page(gnt_list_entry->page);
322 kfree(gnt_list_entry);
329 static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
331 struct grant *gnt_list_entry;
333 BUG_ON(list_empty(&rinfo->grants));
334 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
336 list_del(&gnt_list_entry->node);
338 if (gnt_list_entry->gref != GRANT_INVALID_REF)
339 rinfo->persistent_gnts_c--;
341 return gnt_list_entry;
344 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
345 const struct blkfront_info *info)
347 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
348 info->xbdev->otherend_id,
349 gnt_list_entry->page,
353 static struct grant *get_grant(grant_ref_t *gref_head,
355 struct blkfront_ring_info *rinfo)
357 struct grant *gnt_list_entry = get_free_grant(rinfo);
358 struct blkfront_info *info = rinfo->dev_info;
360 if (gnt_list_entry->gref != GRANT_INVALID_REF)
361 return gnt_list_entry;
363 /* Assign a gref to this page */
364 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
365 BUG_ON(gnt_list_entry->gref == -ENOSPC);
366 if (info->feature_persistent)
367 grant_foreign_access(gnt_list_entry, info);
369 /* Grant access to the GFN passed by the caller */
370 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
371 info->xbdev->otherend_id,
375 return gnt_list_entry;
378 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
379 struct blkfront_ring_info *rinfo)
381 struct grant *gnt_list_entry = get_free_grant(rinfo);
382 struct blkfront_info *info = rinfo->dev_info;
384 if (gnt_list_entry->gref != GRANT_INVALID_REF)
385 return gnt_list_entry;
387 /* Assign a gref to this page */
388 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
389 BUG_ON(gnt_list_entry->gref == -ENOSPC);
390 if (!info->feature_persistent) {
391 struct page *indirect_page;
393 /* Fetch a pre-allocated page to use for indirect grefs */
394 BUG_ON(list_empty(&rinfo->indirect_pages));
395 indirect_page = list_first_entry(&rinfo->indirect_pages,
397 list_del(&indirect_page->lru);
398 gnt_list_entry->page = indirect_page;
400 grant_foreign_access(gnt_list_entry, info);
402 return gnt_list_entry;
405 static const char *op_name(int op)
407 static const char *const names[] = {
408 [BLKIF_OP_READ] = "read",
409 [BLKIF_OP_WRITE] = "write",
410 [BLKIF_OP_WRITE_BARRIER] = "barrier",
411 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
412 [BLKIF_OP_DISCARD] = "discard" };
414 if (op < 0 || op >= ARRAY_SIZE(names))
422 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
424 unsigned int end = minor + nr;
427 if (end > nr_minors) {
428 unsigned long *bitmap, *old;
430 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
435 spin_lock(&minor_lock);
436 if (end > nr_minors) {
438 memcpy(bitmap, minors,
439 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
441 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
444 spin_unlock(&minor_lock);
448 spin_lock(&minor_lock);
449 if (find_next_bit(minors, end, minor) >= end) {
450 bitmap_set(minors, minor, nr);
454 spin_unlock(&minor_lock);
459 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
461 unsigned int end = minor + nr;
463 BUG_ON(end > nr_minors);
464 spin_lock(&minor_lock);
465 bitmap_clear(minors, minor, nr);
466 spin_unlock(&minor_lock);
469 static void blkif_restart_queue_callback(void *arg)
471 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
472 schedule_work(&rinfo->work);
475 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
477 /* We don't have real geometry info, but let's at least return
478 values consistent with the size of the device */
479 sector_t nsect = get_capacity(bd->bd_disk);
480 sector_t cylinders = nsect;
484 sector_div(cylinders, hg->heads * hg->sectors);
485 hg->cylinders = cylinders;
486 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
487 hg->cylinders = 0xffff;
491 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
492 unsigned command, unsigned long argument)
494 struct blkfront_info *info = bdev->bd_disk->private_data;
497 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
498 command, (long)argument);
501 case CDROMMULTISESSION:
502 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
503 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
504 if (put_user(0, (char __user *)(argument + i)))
508 case CDROM_GET_CAPABILITY: {
509 struct gendisk *gd = info->gd;
510 if (gd->flags & GENHD_FL_CD)
516 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
518 return -EINVAL; /* same return as native Linux */
524 static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
526 struct blkif_request **ring_req)
530 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
531 rinfo->ring.req_prod_pvt++;
533 id = get_id_from_freelist(rinfo);
534 rinfo->shadow[id].request = req;
535 rinfo->shadow[id].status = REQ_WAITING;
536 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
538 (*ring_req)->u.rw.id = id;
543 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
545 struct blkfront_info *info = rinfo->dev_info;
546 struct blkif_request *ring_req;
549 /* Fill out a communications ring structure. */
550 id = blkif_ring_get_request(rinfo, req, &ring_req);
552 ring_req->operation = BLKIF_OP_DISCARD;
553 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
554 ring_req->u.discard.id = id;
555 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
556 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
557 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
559 ring_req->u.discard.flag = 0;
561 /* Keep a private copy so we can reissue requests when recovering. */
562 rinfo->shadow[id].req = *ring_req;
567 struct setup_rw_req {
568 unsigned int grant_idx;
569 struct blkif_request_segment *segments;
570 struct blkfront_ring_info *rinfo;
571 struct blkif_request *ring_req;
572 grant_ref_t gref_head;
574 /* Only used when persistent grant is used and it's a read request */
576 unsigned int bvec_off;
579 bool require_extra_req;
580 struct blkif_request *extra_ring_req;
583 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
584 unsigned int len, void *data)
586 struct setup_rw_req *setup = data;
588 struct grant *gnt_list_entry;
589 unsigned int fsect, lsect;
590 /* Convenient aliases */
591 unsigned int grant_idx = setup->grant_idx;
592 struct blkif_request *ring_req = setup->ring_req;
593 struct blkfront_ring_info *rinfo = setup->rinfo;
595 * We always use the shadow of the first request to store the list
596 * of grant associated to the block I/O request. This made the
597 * completion more easy to handle even if the block I/O request is
600 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
602 if (unlikely(setup->require_extra_req &&
603 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
605 * We are using the second request, setup grant_idx
606 * to be the index of the segment array.
608 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
609 ring_req = setup->extra_ring_req;
612 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
613 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
615 kunmap_atomic(setup->segments);
617 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
618 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
619 shadow->indirect_grants[n] = gnt_list_entry;
620 setup->segments = kmap_atomic(gnt_list_entry->page);
621 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
624 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
625 ref = gnt_list_entry->gref;
627 * All the grants are stored in the shadow of the first
628 * request. Therefore we have to use the global index.
630 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
632 if (setup->need_copy) {
635 shared_data = kmap_atomic(gnt_list_entry->page);
637 * this does not wipe data stored outside the
638 * range sg->offset..sg->offset+sg->length.
639 * Therefore, blkback *could* see data from
640 * previous requests. This is OK as long as
641 * persistent grants are shared with just one
642 * domain. It may need refactoring if this
645 memcpy(shared_data + offset,
646 setup->bvec_data + setup->bvec_off,
649 kunmap_atomic(shared_data);
650 setup->bvec_off += len;
654 lsect = fsect + (len >> 9) - 1;
655 if (ring_req->operation != BLKIF_OP_INDIRECT) {
656 ring_req->u.rw.seg[grant_idx] =
657 (struct blkif_request_segment) {
660 .last_sect = lsect };
662 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
663 (struct blkif_request_segment) {
666 .last_sect = lsect };
669 (setup->grant_idx)++;
672 static void blkif_setup_extra_req(struct blkif_request *first,
673 struct blkif_request *second)
675 uint16_t nr_segments = first->u.rw.nr_segments;
678 * The second request is only present when the first request uses
679 * all its segments. It's always the continuity of the first one.
681 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
683 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
684 second->u.rw.sector_number = first->u.rw.sector_number +
685 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
687 second->u.rw.handle = first->u.rw.handle;
688 second->operation = first->operation;
691 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
693 struct blkfront_info *info = rinfo->dev_info;
694 struct blkif_request *ring_req, *extra_ring_req = NULL;
695 unsigned long id, extra_id = NO_ASSOCIATED_ID;
696 bool require_extra_req = false;
698 struct setup_rw_req setup = {
702 .need_copy = rq_data_dir(req) && info->feature_persistent,
706 * Used to store if we are able to queue the request by just using
707 * existing persistent grants, or if we have to get new grants,
708 * as there are not sufficiently many free.
710 bool new_persistent_gnts = false;
711 struct scatterlist *sg;
712 int num_sg, max_grefs, num_grant;
714 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
715 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
717 * If we are using indirect segments we need to account
718 * for the indirect grefs used in the request.
720 max_grefs += INDIRECT_GREFS(max_grefs);
722 /* Check if we have enough persistent grants to allocate a requests */
723 if (rinfo->persistent_gnts_c < max_grefs) {
724 new_persistent_gnts = true;
726 if (gnttab_alloc_grant_references(
727 max_grefs - rinfo->persistent_gnts_c,
728 &setup.gref_head) < 0) {
729 gnttab_request_free_callback(
731 blkif_restart_queue_callback,
733 max_grefs - rinfo->persistent_gnts_c);
738 /* Fill out a communications ring structure. */
739 id = blkif_ring_get_request(rinfo, req, &ring_req);
741 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
743 /* Calculate the number of grant used */
744 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
745 num_grant += gnttab_count_grant(sg->offset, sg->length);
747 require_extra_req = info->max_indirect_segments == 0 &&
748 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
749 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
751 rinfo->shadow[id].num_sg = num_sg;
752 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
753 likely(!require_extra_req)) {
755 * The indirect operation can only be a BLKIF_OP_READ or
758 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
759 ring_req->operation = BLKIF_OP_INDIRECT;
760 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
761 BLKIF_OP_WRITE : BLKIF_OP_READ;
762 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
763 ring_req->u.indirect.handle = info->handle;
764 ring_req->u.indirect.nr_segments = num_grant;
766 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
767 ring_req->u.rw.handle = info->handle;
768 ring_req->operation = rq_data_dir(req) ?
769 BLKIF_OP_WRITE : BLKIF_OP_READ;
770 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
772 * Ideally we can do an unordered flush-to-disk.
773 * In case the backend onlysupports barriers, use that.
774 * A barrier request a superset of FUA, so we can
775 * implement it the same way. (It's also a FLUSH+FUA,
776 * since it is guaranteed ordered WRT previous writes.)
778 if (info->feature_flush && info->feature_fua)
779 ring_req->operation =
780 BLKIF_OP_WRITE_BARRIER;
781 else if (info->feature_flush)
782 ring_req->operation =
783 BLKIF_OP_FLUSH_DISKCACHE;
785 ring_req->operation = 0;
787 ring_req->u.rw.nr_segments = num_grant;
788 if (unlikely(require_extra_req)) {
789 extra_id = blkif_ring_get_request(rinfo, req,
792 * Only the first request contains the scatter-gather
795 rinfo->shadow[extra_id].num_sg = 0;
797 blkif_setup_extra_req(ring_req, extra_ring_req);
799 /* Link the 2 requests together */
800 rinfo->shadow[extra_id].associated_id = id;
801 rinfo->shadow[id].associated_id = extra_id;
805 setup.ring_req = ring_req;
808 setup.require_extra_req = require_extra_req;
809 if (unlikely(require_extra_req))
810 setup.extra_ring_req = extra_ring_req;
812 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
813 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
815 if (setup.need_copy) {
816 setup.bvec_off = sg->offset;
817 setup.bvec_data = kmap_atomic(sg_page(sg));
820 gnttab_foreach_grant_in_range(sg_page(sg),
823 blkif_setup_rw_req_grant,
827 kunmap_atomic(setup.bvec_data);
830 kunmap_atomic(setup.segments);
832 /* Keep a private copy so we can reissue requests when recovering. */
833 rinfo->shadow[id].req = *ring_req;
834 if (unlikely(require_extra_req))
835 rinfo->shadow[extra_id].req = *extra_ring_req;
837 if (new_persistent_gnts)
838 gnttab_free_grant_references(setup.gref_head);
844 * Generate a Xen blkfront IO request from a blk layer request. Reads
845 * and writes are handled as expected.
847 * @req: a request struct
849 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
851 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
854 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
855 req_op(req) == REQ_OP_SECURE_ERASE))
856 return blkif_queue_discard_req(req, rinfo);
858 return blkif_queue_rw_req(req, rinfo);
861 static inline void flush_requests(struct blkfront_ring_info *rinfo)
865 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
868 notify_remote_via_irq(rinfo->irq);
871 static inline bool blkif_request_flush_invalid(struct request *req,
872 struct blkfront_info *info)
874 return (blk_rq_is_passthrough(req) ||
875 ((req_op(req) == REQ_OP_FLUSH) &&
876 !info->feature_flush) ||
877 ((req->cmd_flags & REQ_FUA) &&
878 !info->feature_fua));
881 static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
882 const struct blk_mq_queue_data *qd)
885 int qid = hctx->queue_num;
886 struct blkfront_info *info = hctx->queue->queuedata;
887 struct blkfront_ring_info *rinfo = NULL;
889 BUG_ON(info->nr_rings <= qid);
890 rinfo = &info->rinfo[qid];
891 blk_mq_start_request(qd->rq);
892 spin_lock_irqsave(&rinfo->ring_lock, flags);
893 if (RING_FULL(&rinfo->ring))
896 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
899 if (blkif_queue_request(qd->rq, rinfo))
902 flush_requests(rinfo);
903 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
907 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
908 return BLK_STS_IOERR;
911 blk_mq_stop_hw_queue(hctx);
912 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
913 return BLK_STS_DEV_RESOURCE;
916 static void blkif_complete_rq(struct request *rq)
918 blk_mq_end_request(rq, blkif_req(rq)->error);
921 static const struct blk_mq_ops blkfront_mq_ops = {
922 .queue_rq = blkif_queue_rq,
923 .complete = blkif_complete_rq,
926 static void blkif_set_queue_limits(struct blkfront_info *info)
928 struct request_queue *rq = info->rq;
929 struct gendisk *gd = info->gd;
930 unsigned int segments = info->max_indirect_segments ? :
931 BLKIF_MAX_SEGMENTS_PER_REQUEST;
933 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
935 if (info->feature_discard) {
936 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
937 blk_queue_max_discard_sectors(rq, get_capacity(gd));
938 rq->limits.discard_granularity = info->discard_granularity;
939 rq->limits.discard_alignment = info->discard_alignment;
940 if (info->feature_secdiscard)
941 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
944 /* Hard sector size and max sectors impersonate the equiv. hardware. */
945 blk_queue_logical_block_size(rq, info->sector_size);
946 blk_queue_physical_block_size(rq, info->physical_sector_size);
947 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
949 /* Each segment in a request is up to an aligned page in size. */
950 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
951 blk_queue_max_segment_size(rq, PAGE_SIZE);
953 /* Ensure a merged request will fit in a single I/O ring slot. */
954 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
956 /* Make sure buffer addresses are sector-aligned. */
957 blk_queue_dma_alignment(rq, 511);
960 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
961 unsigned int physical_sector_size)
963 struct request_queue *rq;
964 struct blkfront_info *info = gd->private_data;
966 memset(&info->tag_set, 0, sizeof(info->tag_set));
967 info->tag_set.ops = &blkfront_mq_ops;
968 info->tag_set.nr_hw_queues = info->nr_rings;
969 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
971 * When indirect descriptior is not supported, the I/O request
972 * will be split between multiple request in the ring.
973 * To avoid problems when sending the request, divide by
974 * 2 the depth of the queue.
976 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
978 info->tag_set.queue_depth = BLK_RING_SIZE(info);
979 info->tag_set.numa_node = NUMA_NO_NODE;
980 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
981 info->tag_set.cmd_size = sizeof(struct blkif_req);
982 info->tag_set.driver_data = info;
984 if (blk_mq_alloc_tag_set(&info->tag_set))
986 rq = blk_mq_init_queue(&info->tag_set);
988 blk_mq_free_tag_set(&info->tag_set);
992 rq->queuedata = info;
993 info->rq = gd->queue = rq;
995 info->sector_size = sector_size;
996 info->physical_sector_size = physical_sector_size;
997 blkif_set_queue_limits(info);
1002 static const char *flush_info(struct blkfront_info *info)
1004 if (info->feature_flush && info->feature_fua)
1005 return "barrier: enabled;";
1006 else if (info->feature_flush)
1007 return "flush diskcache: enabled;";
1009 return "barrier or flush: disabled;";
1012 static void xlvbd_flush(struct blkfront_info *info)
1014 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1015 info->feature_fua ? true : false);
1016 pr_info("blkfront: %s: %s %s %s %s %s\n",
1017 info->gd->disk_name, flush_info(info),
1018 "persistent grants:", info->feature_persistent ?
1019 "enabled;" : "disabled;", "indirect descriptors:",
1020 info->max_indirect_segments ? "enabled;" : "disabled;");
1023 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1026 major = BLKIF_MAJOR(vdevice);
1027 *minor = BLKIF_MINOR(vdevice);
1029 case XEN_IDE0_MAJOR:
1030 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1031 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1032 EMULATED_HD_DISK_MINOR_OFFSET;
1034 case XEN_IDE1_MAJOR:
1035 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1036 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1037 EMULATED_HD_DISK_MINOR_OFFSET;
1039 case XEN_SCSI_DISK0_MAJOR:
1040 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1041 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1043 case XEN_SCSI_DISK1_MAJOR:
1044 case XEN_SCSI_DISK2_MAJOR:
1045 case XEN_SCSI_DISK3_MAJOR:
1046 case XEN_SCSI_DISK4_MAJOR:
1047 case XEN_SCSI_DISK5_MAJOR:
1048 case XEN_SCSI_DISK6_MAJOR:
1049 case XEN_SCSI_DISK7_MAJOR:
1050 *offset = (*minor / PARTS_PER_DISK) +
1051 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1052 EMULATED_SD_DISK_NAME_OFFSET;
1054 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1055 EMULATED_SD_DISK_MINOR_OFFSET;
1057 case XEN_SCSI_DISK8_MAJOR:
1058 case XEN_SCSI_DISK9_MAJOR:
1059 case XEN_SCSI_DISK10_MAJOR:
1060 case XEN_SCSI_DISK11_MAJOR:
1061 case XEN_SCSI_DISK12_MAJOR:
1062 case XEN_SCSI_DISK13_MAJOR:
1063 case XEN_SCSI_DISK14_MAJOR:
1064 case XEN_SCSI_DISK15_MAJOR:
1065 *offset = (*minor / PARTS_PER_DISK) +
1066 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1067 EMULATED_SD_DISK_NAME_OFFSET;
1069 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1070 EMULATED_SD_DISK_MINOR_OFFSET;
1073 *offset = *minor / PARTS_PER_DISK;
1076 printk(KERN_WARNING "blkfront: your disk configuration is "
1077 "incorrect, please use an xvd device instead\n");
1083 static char *encode_disk_name(char *ptr, unsigned int n)
1086 ptr = encode_disk_name(ptr, n / 26 - 1);
1087 *ptr = 'a' + n % 26;
1091 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1092 struct blkfront_info *info,
1093 u16 vdisk_info, u16 sector_size,
1094 unsigned int physical_sector_size)
1099 unsigned int offset;
1104 BUG_ON(info->gd != NULL);
1105 BUG_ON(info->rq != NULL);
1107 if ((info->vdevice>>EXT_SHIFT) > 1) {
1108 /* this is above the extended range; something is wrong */
1109 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1113 if (!VDEV_IS_EXTENDED(info->vdevice)) {
1114 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1117 nr_parts = PARTS_PER_DISK;
1119 minor = BLKIF_MINOR_EXT(info->vdevice);
1120 nr_parts = PARTS_PER_EXT_DISK;
1121 offset = minor / nr_parts;
1122 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
1123 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1124 "emulated IDE disks,\n\t choose an xvd device name"
1125 "from xvde on\n", info->vdevice);
1127 if (minor >> MINORBITS) {
1128 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1129 info->vdevice, minor);
1133 if ((minor % nr_parts) == 0)
1134 nr_minors = nr_parts;
1136 err = xlbd_reserve_minors(minor, nr_minors);
1141 gd = alloc_disk(nr_minors);
1145 strcpy(gd->disk_name, DEV_NAME);
1146 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1147 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1151 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1152 "%d", minor & (nr_parts - 1));
1154 gd->major = XENVBD_MAJOR;
1155 gd->first_minor = minor;
1156 gd->fops = &xlvbd_block_fops;
1157 gd->private_data = info;
1158 set_capacity(gd, capacity);
1160 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
1167 if (vdisk_info & VDISK_READONLY)
1170 if (vdisk_info & VDISK_REMOVABLE)
1171 gd->flags |= GENHD_FL_REMOVABLE;
1173 if (vdisk_info & VDISK_CDROM)
1174 gd->flags |= GENHD_FL_CD;
1179 xlbd_release_minors(minor, nr_minors);
1184 static void xlvbd_release_gendisk(struct blkfront_info *info)
1186 unsigned int minor, nr_minors, i;
1188 if (info->rq == NULL)
1191 /* No more blkif_request(). */
1192 blk_mq_stop_hw_queues(info->rq);
1194 for (i = 0; i < info->nr_rings; i++) {
1195 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1197 /* No more gnttab callback work. */
1198 gnttab_cancel_free_callback(&rinfo->callback);
1200 /* Flush gnttab callback work. Must be done with no locks held. */
1201 flush_work(&rinfo->work);
1204 del_gendisk(info->gd);
1206 minor = info->gd->first_minor;
1207 nr_minors = info->gd->minors;
1208 xlbd_release_minors(minor, nr_minors);
1210 blk_cleanup_queue(info->rq);
1211 blk_mq_free_tag_set(&info->tag_set);
1218 /* Already hold rinfo->ring_lock. */
1219 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
1221 if (!RING_FULL(&rinfo->ring))
1222 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
1225 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1227 unsigned long flags;
1229 spin_lock_irqsave(&rinfo->ring_lock, flags);
1230 kick_pending_request_queues_locked(rinfo);
1231 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1234 static void blkif_restart_queue(struct work_struct *work)
1236 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
1238 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1239 kick_pending_request_queues(rinfo);
1242 static void blkif_free_ring(struct blkfront_ring_info *rinfo)
1244 struct grant *persistent_gnt, *n;
1245 struct blkfront_info *info = rinfo->dev_info;
1249 * Remove indirect pages, this only happens when using indirect
1250 * descriptors but not persistent grants
1252 if (!list_empty(&rinfo->indirect_pages)) {
1253 struct page *indirect_page, *n;
1255 BUG_ON(info->feature_persistent);
1256 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
1257 list_del(&indirect_page->lru);
1258 __free_page(indirect_page);
1262 /* Remove all persistent grants. */
1263 if (!list_empty(&rinfo->grants)) {
1264 list_for_each_entry_safe(persistent_gnt, n,
1265 &rinfo->grants, node) {
1266 list_del(&persistent_gnt->node);
1267 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1268 gnttab_end_foreign_access(persistent_gnt->gref,
1270 rinfo->persistent_gnts_c--;
1272 if (info->feature_persistent)
1273 __free_page(persistent_gnt->page);
1274 kfree(persistent_gnt);
1277 BUG_ON(rinfo->persistent_gnts_c != 0);
1279 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1281 * Clear persistent grants present in requests already
1282 * on the shared ring
1284 if (!rinfo->shadow[i].request)
1287 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1288 rinfo->shadow[i].req.u.indirect.nr_segments :
1289 rinfo->shadow[i].req.u.rw.nr_segments;
1290 for (j = 0; j < segs; j++) {
1291 persistent_gnt = rinfo->shadow[i].grants_used[j];
1292 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1293 if (info->feature_persistent)
1294 __free_page(persistent_gnt->page);
1295 kfree(persistent_gnt);
1298 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1300 * If this is not an indirect operation don't try to
1301 * free indirect segments
1305 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1306 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
1307 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1308 __free_page(persistent_gnt->page);
1309 kfree(persistent_gnt);
1313 kfree(rinfo->shadow[i].grants_used);
1314 rinfo->shadow[i].grants_used = NULL;
1315 kfree(rinfo->shadow[i].indirect_grants);
1316 rinfo->shadow[i].indirect_grants = NULL;
1317 kfree(rinfo->shadow[i].sg);
1318 rinfo->shadow[i].sg = NULL;
1321 /* No more gnttab callback work. */
1322 gnttab_cancel_free_callback(&rinfo->callback);
1324 /* Flush gnttab callback work. Must be done with no locks held. */
1325 flush_work(&rinfo->work);
1327 /* Free resources associated with old device channel. */
1328 for (i = 0; i < info->nr_ring_pages; i++) {
1329 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1330 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1331 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1334 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE));
1335 rinfo->ring.sring = NULL;
1338 unbind_from_irqhandler(rinfo->irq, rinfo);
1339 rinfo->evtchn = rinfo->irq = 0;
1342 static void blkif_free(struct blkfront_info *info, int suspend)
1346 /* Prevent new requests being issued until we fix things up. */
1347 info->connected = suspend ?
1348 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1349 /* No more blkif_request(). */
1351 blk_mq_stop_hw_queues(info->rq);
1353 for (i = 0; i < info->nr_rings; i++)
1354 blkif_free_ring(&info->rinfo[i]);
1361 struct copy_from_grant {
1362 const struct blk_shadow *s;
1363 unsigned int grant_idx;
1364 unsigned int bvec_offset;
1368 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1369 unsigned int len, void *data)
1371 struct copy_from_grant *info = data;
1373 /* Convenient aliases */
1374 const struct blk_shadow *s = info->s;
1376 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1378 memcpy(info->bvec_data + info->bvec_offset,
1379 shared_data + offset, len);
1381 info->bvec_offset += len;
1384 kunmap_atomic(shared_data);
1387 static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1391 case BLKIF_RSP_OKAY:
1393 case BLKIF_RSP_EOPNOTSUPP:
1394 return REQ_EOPNOTSUPP;
1395 case BLKIF_RSP_ERROR:
1403 * Get the final status of the block request based on two ring response
1405 static int blkif_get_final_status(enum blk_req_status s1,
1406 enum blk_req_status s2)
1408 BUG_ON(s1 == REQ_WAITING);
1409 BUG_ON(s2 == REQ_WAITING);
1411 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1412 return BLKIF_RSP_ERROR;
1413 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1414 return BLKIF_RSP_EOPNOTSUPP;
1415 return BLKIF_RSP_OKAY;
1418 static bool blkif_completion(unsigned long *id,
1419 struct blkfront_ring_info *rinfo,
1420 struct blkif_response *bret)
1423 struct scatterlist *sg;
1424 int num_sg, num_grant;
1425 struct blkfront_info *info = rinfo->dev_info;
1426 struct blk_shadow *s = &rinfo->shadow[*id];
1427 struct copy_from_grant data = {
1431 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1432 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1434 /* The I/O request may be split in two. */
1435 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1436 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1438 /* Keep the status of the current response in shadow. */
1439 s->status = blkif_rsp_to_req_status(bret->status);
1441 /* Wait the second response if not yet here. */
1442 if (s2->status == REQ_WAITING)
1445 bret->status = blkif_get_final_status(s->status,
1449 * All the grants is stored in the first shadow in order
1450 * to make the completion code simpler.
1452 num_grant += s2->req.u.rw.nr_segments;
1455 * The two responses may not come in order. Only the
1456 * first request will store the scatter-gather list.
1458 if (s2->num_sg != 0) {
1459 /* Update "id" with the ID of the first response. */
1460 *id = s->associated_id;
1465 * We don't need anymore the second request, so recycling
1468 if (add_id_to_freelist(rinfo, s->associated_id))
1469 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1470 info->gd->disk_name, s->associated_id);
1476 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
1477 for_each_sg(s->sg, sg, num_sg, i) {
1478 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1480 data.bvec_offset = sg->offset;
1481 data.bvec_data = kmap_atomic(sg_page(sg));
1483 gnttab_foreach_grant_in_range(sg_page(sg),
1486 blkif_copy_from_grant,
1489 kunmap_atomic(data.bvec_data);
1492 /* Add the persistent grant into the list of free grants */
1493 for (i = 0; i < num_grant; i++) {
1494 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1496 * If the grant is still mapped by the backend (the
1497 * backend has chosen to make this grant persistent)
1498 * we add it at the head of the list, so it will be
1501 if (!info->feature_persistent)
1502 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1503 s->grants_used[i]->gref);
1504 list_add(&s->grants_used[i]->node, &rinfo->grants);
1505 rinfo->persistent_gnts_c++;
1508 * If the grant is not mapped by the backend we end the
1509 * foreign access and add it to the tail of the list,
1510 * so it will not be picked again unless we run out of
1511 * persistent grants.
1513 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1514 s->grants_used[i]->gref = GRANT_INVALID_REF;
1515 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
1518 if (s->req.operation == BLKIF_OP_INDIRECT) {
1519 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1520 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
1521 if (!info->feature_persistent)
1522 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1523 s->indirect_grants[i]->gref);
1524 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1525 rinfo->persistent_gnts_c++;
1527 struct page *indirect_page;
1529 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
1531 * Add the used indirect page back to the list of
1532 * available pages for indirect grefs.
1534 if (!info->feature_persistent) {
1535 indirect_page = s->indirect_grants[i]->page;
1536 list_add(&indirect_page->lru, &rinfo->indirect_pages);
1538 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1539 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
1547 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1549 struct request *req;
1550 struct blkif_response *bret;
1552 unsigned long flags;
1553 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1554 struct blkfront_info *info = rinfo->dev_info;
1556 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
1559 spin_lock_irqsave(&rinfo->ring_lock, flags);
1561 rp = rinfo->ring.sring->rsp_prod;
1562 rmb(); /* Ensure we see queued responses up to 'rp'. */
1564 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
1567 bret = RING_GET_RESPONSE(&rinfo->ring, i);
1570 * The backend has messed up and given us an id that we would
1571 * never have given to it (we stamp it up to BLK_RING_SIZE -
1572 * look in get_id_from_freelist.
1574 if (id >= BLK_RING_SIZE(info)) {
1575 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1576 info->gd->disk_name, op_name(bret->operation), id);
1577 /* We can't safely get the 'struct request' as
1578 * the id is busted. */
1581 req = rinfo->shadow[id].request;
1583 if (bret->operation != BLKIF_OP_DISCARD) {
1585 * We may need to wait for an extra response if the
1586 * I/O request is split in 2
1588 if (!blkif_completion(&id, rinfo, bret))
1592 if (add_id_to_freelist(rinfo, id)) {
1593 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1594 info->gd->disk_name, op_name(bret->operation), id);
1598 if (bret->status == BLKIF_RSP_OKAY)
1599 blkif_req(req)->error = BLK_STS_OK;
1601 blkif_req(req)->error = BLK_STS_IOERR;
1603 switch (bret->operation) {
1604 case BLKIF_OP_DISCARD:
1605 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1606 struct request_queue *rq = info->rq;
1607 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1608 info->gd->disk_name, op_name(bret->operation));
1609 blkif_req(req)->error = BLK_STS_NOTSUPP;
1610 info->feature_discard = 0;
1611 info->feature_secdiscard = 0;
1612 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1613 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
1616 case BLKIF_OP_FLUSH_DISKCACHE:
1617 case BLKIF_OP_WRITE_BARRIER:
1618 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1619 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1620 info->gd->disk_name, op_name(bret->operation));
1621 blkif_req(req)->error = BLK_STS_NOTSUPP;
1623 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
1624 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
1625 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1626 info->gd->disk_name, op_name(bret->operation));
1627 blkif_req(req)->error = BLK_STS_NOTSUPP;
1629 if (unlikely(blkif_req(req)->error)) {
1630 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1631 blkif_req(req)->error = BLK_STS_OK;
1632 info->feature_fua = 0;
1633 info->feature_flush = 0;
1638 case BLKIF_OP_WRITE:
1639 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1640 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1641 "request: %x\n", bret->status);
1648 blk_mq_complete_request(req);
1651 rinfo->ring.rsp_cons = i;
1653 if (i != rinfo->ring.req_prod_pvt) {
1655 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
1659 rinfo->ring.sring->rsp_event = i + 1;
1661 kick_pending_request_queues_locked(rinfo);
1663 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1669 static int setup_blkring(struct xenbus_device *dev,
1670 struct blkfront_ring_info *rinfo)
1672 struct blkif_sring *sring;
1674 struct blkfront_info *info = rinfo->dev_info;
1675 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1676 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
1678 for (i = 0; i < info->nr_ring_pages; i++)
1679 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1681 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1682 get_order(ring_size));
1684 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1687 SHARED_RING_INIT(sring);
1688 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
1690 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
1692 free_pages((unsigned long)sring, get_order(ring_size));
1693 rinfo->ring.sring = NULL;
1696 for (i = 0; i < info->nr_ring_pages; i++)
1697 rinfo->ring_ref[i] = gref[i];
1699 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
1703 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1706 xenbus_dev_fatal(dev, err,
1707 "bind_evtchn_to_irqhandler failed");
1714 blkif_free(info, 0);
1719 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1720 * ring buffer may have multi pages depending on ->nr_ring_pages.
1722 static int write_per_ring_nodes(struct xenbus_transaction xbt,
1723 struct blkfront_ring_info *rinfo, const char *dir)
1727 const char *message = NULL;
1728 struct blkfront_info *info = rinfo->dev_info;
1730 if (info->nr_ring_pages == 1) {
1731 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1733 message = "writing ring-ref";
1734 goto abort_transaction;
1737 for (i = 0; i < info->nr_ring_pages; i++) {
1738 char ring_ref_name[RINGREF_NAME_LEN];
1740 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1741 err = xenbus_printf(xbt, dir, ring_ref_name,
1742 "%u", rinfo->ring_ref[i]);
1744 message = "writing ring-ref";
1745 goto abort_transaction;
1750 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1752 message = "writing event-channel";
1753 goto abort_transaction;
1759 xenbus_transaction_end(xbt, 1);
1761 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1766 static void free_info(struct blkfront_info *info)
1768 list_del(&info->info_list);
1772 /* Common code used when first setting up, and when resuming. */
1773 static int talk_to_blkback(struct xenbus_device *dev,
1774 struct blkfront_info *info)
1776 const char *message = NULL;
1777 struct xenbus_transaction xbt;
1779 unsigned int i, max_page_order;
1780 unsigned int ring_page_order;
1785 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1786 "max-ring-page-order", 0);
1787 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1788 info->nr_ring_pages = 1 << ring_page_order;
1790 err = negotiate_mq(info);
1792 goto destroy_blkring;
1794 for (i = 0; i < info->nr_rings; i++) {
1795 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1797 /* Create shared ring, alloc event channel. */
1798 err = setup_blkring(dev, rinfo);
1800 goto destroy_blkring;
1804 err = xenbus_transaction_start(&xbt);
1806 xenbus_dev_fatal(dev, err, "starting transaction");
1807 goto destroy_blkring;
1810 if (info->nr_ring_pages > 1) {
1811 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1814 message = "writing ring-page-order";
1815 goto abort_transaction;
1819 /* We already got the number of queues/rings in _probe */
1820 if (info->nr_rings == 1) {
1821 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1823 goto destroy_blkring;
1828 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1831 message = "writing multi-queue-num-queues";
1832 goto abort_transaction;
1835 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1836 path = kmalloc(pathsize, GFP_KERNEL);
1839 message = "ENOMEM while writing ring references";
1840 goto abort_transaction;
1843 for (i = 0; i < info->nr_rings; i++) {
1844 memset(path, 0, pathsize);
1845 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1846 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1849 goto destroy_blkring;
1854 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1855 XEN_IO_PROTO_ABI_NATIVE);
1857 message = "writing protocol";
1858 goto abort_transaction;
1860 err = xenbus_printf(xbt, dev->nodename,
1861 "feature-persistent", "%u", 1);
1864 "writing persistent grants feature to xenbus");
1866 err = xenbus_transaction_end(xbt, 0);
1870 xenbus_dev_fatal(dev, err, "completing transaction");
1871 goto destroy_blkring;
1874 for (i = 0; i < info->nr_rings; i++) {
1876 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1878 for (j = 0; j < BLK_RING_SIZE(info); j++)
1879 rinfo->shadow[j].req.u.rw.id = j + 1;
1880 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1882 xenbus_switch_state(dev, XenbusStateInitialised);
1887 xenbus_transaction_end(xbt, 1);
1889 xenbus_dev_fatal(dev, err, "%s", message);
1891 blkif_free(info, 0);
1893 mutex_lock(&blkfront_mutex);
1895 mutex_unlock(&blkfront_mutex);
1897 dev_set_drvdata(&dev->dev, NULL);
1902 static int negotiate_mq(struct blkfront_info *info)
1904 unsigned int backend_max_queues;
1907 BUG_ON(info->nr_rings);
1909 /* Check if backend supports multiple queues. */
1910 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1911 "multi-queue-max-queues", 1);
1912 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1913 /* We need at least one ring. */
1914 if (!info->nr_rings)
1917 info->rinfo = kcalloc(info->nr_rings,
1918 sizeof(struct blkfront_ring_info),
1921 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1925 for (i = 0; i < info->nr_rings; i++) {
1926 struct blkfront_ring_info *rinfo;
1928 rinfo = &info->rinfo[i];
1929 INIT_LIST_HEAD(&rinfo->indirect_pages);
1930 INIT_LIST_HEAD(&rinfo->grants);
1931 rinfo->dev_info = info;
1932 INIT_WORK(&rinfo->work, blkif_restart_queue);
1933 spin_lock_init(&rinfo->ring_lock);
1938 * Entry point to this code when a new device is created. Allocate the basic
1939 * structures and the ring buffer for communication with the backend, and
1940 * inform the backend of the appropriate details for those. Switch to
1941 * Initialised state.
1943 static int blkfront_probe(struct xenbus_device *dev,
1944 const struct xenbus_device_id *id)
1947 struct blkfront_info *info;
1949 /* FIXME: Use dynamic device id if this is not set. */
1950 err = xenbus_scanf(XBT_NIL, dev->nodename,
1951 "virtual-device", "%i", &vdevice);
1953 /* go looking in the extended area instead */
1954 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1957 xenbus_dev_fatal(dev, err, "reading virtual-device");
1962 if (xen_hvm_domain()) {
1965 /* no unplug has been done: do not hook devices != xen vbds */
1966 if (xen_has_pv_and_legacy_disk_devices()) {
1969 if (!VDEV_IS_EXTENDED(vdevice))
1970 major = BLKIF_MAJOR(vdevice);
1972 major = XENVBD_MAJOR;
1974 if (major != XENVBD_MAJOR) {
1976 "%s: HVM does not support vbd %d as xen block device\n",
1981 /* do not create a PV cdrom device if we are an HVM guest */
1982 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1985 if (strncmp(type, "cdrom", 5) == 0) {
1991 info = kzalloc(sizeof(*info), GFP_KERNEL);
1993 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1999 mutex_init(&info->mutex);
2000 info->vdevice = vdevice;
2001 info->connected = BLKIF_STATE_DISCONNECTED;
2003 /* Front end dir is a number, which is used as the id. */
2004 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
2005 dev_set_drvdata(&dev->dev, info);
2007 mutex_lock(&blkfront_mutex);
2008 list_add(&info->info_list, &info_list);
2009 mutex_unlock(&blkfront_mutex);
2014 static int blkif_recover(struct blkfront_info *info)
2016 unsigned int r_index;
2017 struct request *req, *n;
2022 blkfront_gather_backend_features(info);
2023 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2024 blkif_set_queue_limits(info);
2025 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
2026 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
2028 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2029 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
2031 rc = blkfront_setup_indirect(rinfo);
2035 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2037 /* Now safe for us to use the shared ring */
2038 info->connected = BLKIF_STATE_CONNECTED;
2040 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2041 struct blkfront_ring_info *rinfo;
2043 rinfo = &info->rinfo[r_index];
2044 /* Kick any other new requests queued since we resumed */
2045 kick_pending_request_queues(rinfo);
2048 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
2049 /* Requeue pending requests (flush or discard) */
2050 list_del_init(&req->queuelist);
2051 BUG_ON(req->nr_phys_segments > segs);
2052 blk_mq_requeue_request(req, false);
2054 blk_mq_start_stopped_hw_queues(info->rq, true);
2055 blk_mq_kick_requeue_list(info->rq);
2057 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
2058 /* Traverse the list of pending bios and re-queue them */
2066 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2067 * driver restart. We tear down our blkif structure and recreate it, but
2068 * leave the device-layer structures intact so that this is transparent to the
2069 * rest of the kernel.
2071 static int blkfront_resume(struct xenbus_device *dev)
2073 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2077 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2079 bio_list_init(&info->bio_list);
2080 INIT_LIST_HEAD(&info->requests);
2081 for (i = 0; i < info->nr_rings; i++) {
2082 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2083 struct bio_list merge_bio;
2084 struct blk_shadow *shadow = rinfo->shadow;
2086 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2088 if (!shadow[j].request)
2092 * Get the bios in the request so we can re-queue them.
2094 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2095 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2096 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
2097 shadow[j].request->cmd_flags & REQ_FUA) {
2099 * Flush operations don't contain bios, so
2100 * we need to requeue the whole request
2102 * XXX: but this doesn't make any sense for a
2103 * write with the FUA flag set..
2105 list_add(&shadow[j].request->queuelist, &info->requests);
2108 merge_bio.head = shadow[j].request->bio;
2109 merge_bio.tail = shadow[j].request->biotail;
2110 bio_list_merge(&info->bio_list, &merge_bio);
2111 shadow[j].request->bio = NULL;
2112 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
2116 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2118 err = talk_to_blkback(dev, info);
2120 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
2123 * We have to wait for the backend to switch to
2124 * connected state, since we want to read which
2125 * features it supports.
2131 static void blkfront_closing(struct blkfront_info *info)
2133 struct xenbus_device *xbdev = info->xbdev;
2134 struct block_device *bdev = NULL;
2136 mutex_lock(&info->mutex);
2138 if (xbdev->state == XenbusStateClosing) {
2139 mutex_unlock(&info->mutex);
2144 bdev = bdget_disk(info->gd, 0);
2146 mutex_unlock(&info->mutex);
2149 xenbus_frontend_closed(xbdev);
2153 mutex_lock(&bdev->bd_mutex);
2155 if (bdev->bd_openers) {
2156 xenbus_dev_error(xbdev, -EBUSY,
2157 "Device in use; refusing to close");
2158 xenbus_switch_state(xbdev, XenbusStateClosing);
2160 xlvbd_release_gendisk(info);
2161 xenbus_frontend_closed(xbdev);
2164 mutex_unlock(&bdev->bd_mutex);
2168 static void blkfront_setup_discard(struct blkfront_info *info)
2171 unsigned int discard_granularity;
2172 unsigned int discard_alignment;
2174 info->feature_discard = 1;
2175 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2176 "discard-granularity", "%u", &discard_granularity,
2177 "discard-alignment", "%u", &discard_alignment,
2180 info->discard_granularity = discard_granularity;
2181 info->discard_alignment = discard_alignment;
2183 info->feature_secdiscard =
2184 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2188 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
2190 unsigned int psegs, grants;
2192 struct blkfront_info *info = rinfo->dev_info;
2194 if (info->max_indirect_segments == 0) {
2196 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2199 * When an extra req is required, the maximum
2200 * grants supported is related to the size of the
2201 * Linux block segment.
2203 grants = GRANTS_PER_PSEG;
2207 grants = info->max_indirect_segments;
2208 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
2210 err = fill_grant_buffer(rinfo,
2211 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
2215 if (!info->feature_persistent && info->max_indirect_segments) {
2217 * We are using indirect descriptors but not persistent
2218 * grants, we need to allocate a set of pages that can be
2219 * used for mapping indirect grefs
2221 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
2223 BUG_ON(!list_empty(&rinfo->indirect_pages));
2224 for (i = 0; i < num; i++) {
2225 struct page *indirect_page = alloc_page(GFP_NOIO);
2228 list_add(&indirect_page->lru, &rinfo->indirect_pages);
2232 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2233 rinfo->shadow[i].grants_used =
2235 sizeof(rinfo->shadow[i].grants_used[0]),
2237 rinfo->shadow[i].sg = kcalloc(psegs,
2238 sizeof(rinfo->shadow[i].sg[0]),
2240 if (info->max_indirect_segments)
2241 rinfo->shadow[i].indirect_grants =
2242 kcalloc(INDIRECT_GREFS(grants),
2243 sizeof(rinfo->shadow[i].indirect_grants[0]),
2245 if ((rinfo->shadow[i].grants_used == NULL) ||
2246 (rinfo->shadow[i].sg == NULL) ||
2247 (info->max_indirect_segments &&
2248 (rinfo->shadow[i].indirect_grants == NULL)))
2250 sg_init_table(rinfo->shadow[i].sg, psegs);
2257 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2258 kfree(rinfo->shadow[i].grants_used);
2259 rinfo->shadow[i].grants_used = NULL;
2260 kfree(rinfo->shadow[i].sg);
2261 rinfo->shadow[i].sg = NULL;
2262 kfree(rinfo->shadow[i].indirect_grants);
2263 rinfo->shadow[i].indirect_grants = NULL;
2265 if (!list_empty(&rinfo->indirect_pages)) {
2266 struct page *indirect_page, *n;
2267 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
2268 list_del(&indirect_page->lru);
2269 __free_page(indirect_page);
2276 * Gather all backend feature-*
2278 static void blkfront_gather_backend_features(struct blkfront_info *info)
2280 unsigned int indirect_segments;
2282 info->feature_flush = 0;
2283 info->feature_fua = 0;
2286 * If there's no "feature-barrier" defined, then it means
2287 * we're dealing with a very old backend which writes
2288 * synchronously; nothing to do.
2290 * If there are barriers, then we use flush.
2292 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
2293 info->feature_flush = 1;
2294 info->feature_fua = 1;
2298 * And if there is "feature-flush-cache" use that above
2301 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2303 info->feature_flush = 1;
2304 info->feature_fua = 0;
2307 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
2308 blkfront_setup_discard(info);
2310 info->feature_persistent =
2311 !!xenbus_read_unsigned(info->xbdev->otherend,
2312 "feature-persistent", 0);
2314 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2315 "feature-max-indirect-segments", 0);
2316 if (indirect_segments > xen_blkif_max_segments)
2317 indirect_segments = xen_blkif_max_segments;
2318 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2319 indirect_segments = 0;
2320 info->max_indirect_segments = indirect_segments;
2322 if (info->feature_persistent) {
2323 mutex_lock(&blkfront_mutex);
2324 schedule_delayed_work(&blkfront_work, HZ * 10);
2325 mutex_unlock(&blkfront_mutex);
2330 * Invoked when the backend is finally 'ready' (and has told produced
2331 * the details about the physical device - #sectors, size, etc).
2333 static void blkfront_connect(struct blkfront_info *info)
2335 unsigned long long sectors;
2336 unsigned long sector_size;
2337 unsigned int physical_sector_size;
2339 char *envp[] = { "RESIZE=1", NULL };
2342 switch (info->connected) {
2343 case BLKIF_STATE_CONNECTED:
2345 * Potentially, the back-end may be signalling
2346 * a capacity change; update the capacity.
2348 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2349 "sectors", "%Lu", §ors);
2350 if (XENBUS_EXIST_ERR(err))
2352 printk(KERN_INFO "Setting capacity to %Lu\n",
2354 set_capacity(info->gd, sectors);
2355 revalidate_disk(info->gd);
2356 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2360 case BLKIF_STATE_SUSPENDED:
2362 * If we are recovering from suspension, we need to wait
2363 * for the backend to announce it's features before
2364 * reconnecting, at least we need to know if the backend
2365 * supports indirect descriptors, and how many.
2367 blkif_recover(info);
2374 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2375 __func__, info->xbdev->otherend);
2377 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2378 "sectors", "%llu", §ors,
2379 "info", "%u", &binfo,
2380 "sector-size", "%lu", §or_size,
2383 xenbus_dev_fatal(info->xbdev, err,
2384 "reading backend fields at %s",
2385 info->xbdev->otherend);
2390 * physcial-sector-size is a newer field, so old backends may not
2391 * provide this. Assume physical sector size to be the same as
2392 * sector_size in that case.
2394 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2395 "physical-sector-size",
2397 blkfront_gather_backend_features(info);
2398 for (i = 0; i < info->nr_rings; i++) {
2399 err = blkfront_setup_indirect(&info->rinfo[i]);
2401 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2402 info->xbdev->otherend);
2403 blkif_free(info, 0);
2408 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2409 physical_sector_size);
2411 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2412 info->xbdev->otherend);
2416 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2418 /* Kick pending requests. */
2419 info->connected = BLKIF_STATE_CONNECTED;
2420 for (i = 0; i < info->nr_rings; i++)
2421 kick_pending_request_queues(&info->rinfo[i]);
2423 device_add_disk(&info->xbdev->dev, info->gd);
2429 blkif_free(info, 0);
2434 * Callback received when the backend's state changes.
2436 static void blkback_changed(struct xenbus_device *dev,
2437 enum xenbus_state backend_state)
2439 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2441 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2443 switch (backend_state) {
2444 case XenbusStateInitWait:
2445 if (dev->state != XenbusStateInitialising)
2447 if (talk_to_blkback(dev, info))
2449 case XenbusStateInitialising:
2450 case XenbusStateInitialised:
2451 case XenbusStateReconfiguring:
2452 case XenbusStateReconfigured:
2453 case XenbusStateUnknown:
2456 case XenbusStateConnected:
2458 * talk_to_blkback sets state to XenbusStateInitialised
2459 * and blkfront_connect sets it to XenbusStateConnected
2460 * (if connection went OK).
2462 * If the backend (or toolstack) decides to poke at backend
2463 * state (and re-trigger the watch by setting the state repeatedly
2464 * to XenbusStateConnected (4)) we need to deal with this.
2465 * This is allowed as this is used to communicate to the guest
2466 * that the size of disk has changed!
2468 if ((dev->state != XenbusStateInitialised) &&
2469 (dev->state != XenbusStateConnected)) {
2470 if (talk_to_blkback(dev, info))
2474 blkfront_connect(info);
2477 case XenbusStateClosed:
2478 if (dev->state == XenbusStateClosed)
2481 case XenbusStateClosing:
2483 blkfront_closing(info);
2488 static int blkfront_remove(struct xenbus_device *xbdev)
2490 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2491 struct block_device *bdev = NULL;
2492 struct gendisk *disk;
2494 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2496 blkif_free(info, 0);
2498 mutex_lock(&info->mutex);
2502 bdev = bdget_disk(disk, 0);
2505 mutex_unlock(&info->mutex);
2508 mutex_lock(&blkfront_mutex);
2510 mutex_unlock(&blkfront_mutex);
2515 * The xbdev was removed before we reached the Closed
2516 * state. See if it's safe to remove the disk. If the bdev
2517 * isn't closed yet, we let release take care of it.
2520 mutex_lock(&bdev->bd_mutex);
2521 info = disk->private_data;
2523 dev_warn(disk_to_dev(disk),
2524 "%s was hot-unplugged, %d stale handles\n",
2525 xbdev->nodename, bdev->bd_openers);
2527 if (info && !bdev->bd_openers) {
2528 xlvbd_release_gendisk(info);
2529 disk->private_data = NULL;
2530 mutex_lock(&blkfront_mutex);
2532 mutex_unlock(&blkfront_mutex);
2535 mutex_unlock(&bdev->bd_mutex);
2541 static int blkfront_is_ready(struct xenbus_device *dev)
2543 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2545 return info->is_ready && info->xbdev;
2548 static int blkif_open(struct block_device *bdev, fmode_t mode)
2550 struct gendisk *disk = bdev->bd_disk;
2551 struct blkfront_info *info;
2554 mutex_lock(&blkfront_mutex);
2556 info = disk->private_data;
2563 mutex_lock(&info->mutex);
2566 /* xbdev is closed */
2569 mutex_unlock(&info->mutex);
2572 mutex_unlock(&blkfront_mutex);
2576 static void blkif_release(struct gendisk *disk, fmode_t mode)
2578 struct blkfront_info *info = disk->private_data;
2579 struct block_device *bdev;
2580 struct xenbus_device *xbdev;
2582 mutex_lock(&blkfront_mutex);
2584 bdev = bdget_disk(disk, 0);
2587 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2590 if (bdev->bd_openers)
2594 * Check if we have been instructed to close. We will have
2595 * deferred this request, because the bdev was still open.
2598 mutex_lock(&info->mutex);
2599 xbdev = info->xbdev;
2601 if (xbdev && xbdev->state == XenbusStateClosing) {
2602 /* pending switch to state closed */
2603 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2604 xlvbd_release_gendisk(info);
2605 xenbus_frontend_closed(info->xbdev);
2608 mutex_unlock(&info->mutex);
2611 /* sudden device removal */
2612 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2613 xlvbd_release_gendisk(info);
2614 disk->private_data = NULL;
2621 mutex_unlock(&blkfront_mutex);
2624 static const struct block_device_operations xlvbd_block_fops =
2626 .owner = THIS_MODULE,
2628 .release = blkif_release,
2629 .getgeo = blkif_getgeo,
2630 .ioctl = blkif_ioctl,
2634 static const struct xenbus_device_id blkfront_ids[] = {
2639 static struct xenbus_driver blkfront_driver = {
2640 .ids = blkfront_ids,
2641 .probe = blkfront_probe,
2642 .remove = blkfront_remove,
2643 .resume = blkfront_resume,
2644 .otherend_changed = blkback_changed,
2645 .is_ready = blkfront_is_ready,
2648 static void purge_persistent_grants(struct blkfront_info *info)
2651 unsigned long flags;
2653 for (i = 0; i < info->nr_rings; i++) {
2654 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2655 struct grant *gnt_list_entry, *tmp;
2657 spin_lock_irqsave(&rinfo->ring_lock, flags);
2659 if (rinfo->persistent_gnts_c == 0) {
2660 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2664 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
2666 if (gnt_list_entry->gref == GRANT_INVALID_REF ||
2667 gnttab_query_foreign_access(gnt_list_entry->gref))
2670 list_del(&gnt_list_entry->node);
2671 gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL);
2672 rinfo->persistent_gnts_c--;
2673 __free_page(gnt_list_entry->page);
2674 kfree(gnt_list_entry);
2677 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2681 static void blkfront_delay_work(struct work_struct *work)
2683 struct blkfront_info *info;
2684 bool need_schedule_work = false;
2686 mutex_lock(&blkfront_mutex);
2688 list_for_each_entry(info, &info_list, info_list) {
2689 if (info->feature_persistent) {
2690 need_schedule_work = true;
2691 mutex_lock(&info->mutex);
2692 purge_persistent_grants(info);
2693 mutex_unlock(&info->mutex);
2697 if (need_schedule_work)
2698 schedule_delayed_work(&blkfront_work, HZ * 10);
2700 mutex_unlock(&blkfront_mutex);
2703 static int __init xlblk_init(void)
2706 int nr_cpus = num_online_cpus();
2711 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2712 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2714 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2715 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2716 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2717 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
2720 if (xen_blkif_max_queues > nr_cpus) {
2721 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2722 xen_blkif_max_queues, nr_cpus);
2723 xen_blkif_max_queues = nr_cpus;
2726 if (!xen_has_pv_disk_devices())
2729 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2730 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2731 XENVBD_MAJOR, DEV_NAME);
2735 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
2737 ret = xenbus_register_frontend(&blkfront_driver);
2739 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2745 module_init(xlblk_init);
2748 static void __exit xlblk_exit(void)
2750 cancel_delayed_work_sync(&blkfront_work);
2752 xenbus_unregister_driver(&blkfront_driver);
2753 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2756 module_exit(xlblk_exit);
2758 MODULE_DESCRIPTION("Xen virtual block device frontend");
2759 MODULE_LICENSE("GPL");
2760 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2761 MODULE_ALIAS("xen:vbd");
2762 MODULE_ALIAS("xenblk");