4 * XenLinux virtual block device driver.
6 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
7 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
8 * Copyright (c) 2004, Christian Limpach
9 * Copyright (c) 2004, Andrew Warfield
10 * Copyright (c) 2005, Christopher Clark
11 * Copyright (c) 2005, XenSource Ltd
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation; or, when distributed
16 * separately from the Linux kernel or incorporated into other
17 * software packages, subject to the following license:
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this source file (the "Software"), to deal in the Software without
21 * restriction, including without limitation the rights to use, copy, modify,
22 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
23 * and to permit persons to whom the Software is furnished to do so, subject to
24 * the following conditions:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
38 #include <linux/interrupt.h>
39 #include <linux/blkdev.h>
40 #include <linux/blk-mq.h>
41 #include <linux/hdreg.h>
42 #include <linux/cdrom.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/mutex.h>
46 #include <linux/scatterlist.h>
47 #include <linux/bitmap.h>
48 #include <linux/list.h>
51 #include <xen/xenbus.h>
52 #include <xen/grant_table.h>
53 #include <xen/events.h>
55 #include <xen/platform_pci.h>
57 #include <xen/interface/grant_table.h>
58 #include <xen/interface/io/blkif.h>
59 #include <xen/interface/io/protocols.h>
61 #include <asm/xen/hypervisor.h>
64 BLKIF_STATE_DISCONNECTED,
65 BLKIF_STATE_CONNECTED,
66 BLKIF_STATE_SUSPENDED,
73 struct list_head node;
77 struct blkif_request req;
78 struct request *request;
79 struct grant **grants_used;
80 struct grant **indirect_grants;
81 struct scatterlist *sg;
91 static DEFINE_MUTEX(blkfront_mutex);
92 static const struct block_device_operations xlvbd_block_fops;
95 * Maximum number of segments in indirect requests, the actual value used by
96 * the frontend driver is the minimum of this value and the value provided
97 * by the backend driver.
100 static unsigned int xen_blkif_max_segments = 32;
101 module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
102 MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
105 * Maximum order of pages to be used for the shared ring between front and
106 * backend, 4KB page granularity is used.
108 static unsigned int xen_blkif_max_ring_order;
109 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
110 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
112 #define BLK_RING_SIZE(info) \
113 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
115 #define BLK_MAX_RING_SIZE \
116 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
119 * ring-ref%i i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
120 * characters are enough. Define to 20 to keep consist with backend.
122 #define RINGREF_NAME_LEN (20)
125 * We have one of these per vbd, whether ide, scsi or 'other'. They
126 * hang in private_data off the gendisk structure. We may end up
127 * putting all kinds of interesting stuff here :-)
133 struct xenbus_device *xbdev;
137 enum blkif_state connected;
138 int ring_ref[XENBUS_MAX_RING_GRANTS];
139 unsigned int nr_ring_pages;
140 struct blkif_front_ring ring;
141 unsigned int evtchn, irq;
142 struct request_queue *rq;
143 struct work_struct work;
144 struct gnttab_free_callback callback;
145 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
146 struct list_head grants;
147 struct list_head indirect_pages;
148 unsigned int persistent_gnts_c;
149 unsigned long shadow_free;
150 unsigned int feature_flush;
151 unsigned int feature_discard:1;
152 unsigned int feature_secdiscard:1;
153 unsigned int discard_granularity;
154 unsigned int discard_alignment;
155 unsigned int feature_persistent:1;
156 /* Number of 4KB segments handled */
157 unsigned int max_indirect_segments;
159 struct blk_mq_tag_set tag_set;
162 static unsigned int nr_minors;
163 static unsigned long *minors;
164 static DEFINE_SPINLOCK(minor_lock);
166 #define GRANT_INVALID_REF 0
168 #define PARTS_PER_DISK 16
169 #define PARTS_PER_EXT_DISK 256
171 #define BLKIF_MAJOR(dev) ((dev)>>8)
172 #define BLKIF_MINOR(dev) ((dev) & 0xff)
175 #define EXTENDED (1<<EXT_SHIFT)
176 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
177 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
178 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
179 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
180 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
181 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
183 #define DEV_NAME "xvd" /* name in /dev */
186 * Grants are always the same size as a Xen page (i.e 4KB).
187 * A physical segment is always the same size as a Linux page.
188 * Number of grants per physical segment
190 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
192 #define GRANTS_PER_INDIRECT_FRAME \
193 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
195 #define PSEGS_PER_INDIRECT_FRAME \
196 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
198 #define INDIRECT_GREFS(_grants) \
199 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
201 #define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
203 static int blkfront_setup_indirect(struct blkfront_info *info);
204 static int blkfront_gather_backend_features(struct blkfront_info *info);
206 static int get_id_from_freelist(struct blkfront_info *info)
208 unsigned long free = info->shadow_free;
209 BUG_ON(free >= BLK_RING_SIZE(info));
210 info->shadow_free = info->shadow[free].req.u.rw.id;
211 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
215 static int add_id_to_freelist(struct blkfront_info *info,
218 if (info->shadow[id].req.u.rw.id != id)
220 if (info->shadow[id].request == NULL)
222 info->shadow[id].req.u.rw.id = info->shadow_free;
223 info->shadow[id].request = NULL;
224 info->shadow_free = id;
228 static int fill_grant_buffer(struct blkfront_info *info, int num)
230 struct page *granted_page;
231 struct grant *gnt_list_entry, *n;
235 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
239 if (info->feature_persistent) {
240 granted_page = alloc_page(GFP_NOIO);
242 kfree(gnt_list_entry);
245 gnt_list_entry->page = granted_page;
248 gnt_list_entry->gref = GRANT_INVALID_REF;
249 list_add(&gnt_list_entry->node, &info->grants);
256 list_for_each_entry_safe(gnt_list_entry, n,
257 &info->grants, node) {
258 list_del(&gnt_list_entry->node);
259 if (info->feature_persistent)
260 __free_page(gnt_list_entry->page);
261 kfree(gnt_list_entry);
268 static struct grant *get_free_grant(struct blkfront_info *info)
270 struct grant *gnt_list_entry;
272 BUG_ON(list_empty(&info->grants));
273 gnt_list_entry = list_first_entry(&info->grants, struct grant,
275 list_del(&gnt_list_entry->node);
277 if (gnt_list_entry->gref != GRANT_INVALID_REF)
278 info->persistent_gnts_c--;
280 return gnt_list_entry;
283 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
284 const struct blkfront_info *info)
286 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
287 info->xbdev->otherend_id,
288 gnt_list_entry->page,
292 static struct grant *get_grant(grant_ref_t *gref_head,
294 struct blkfront_info *info)
296 struct grant *gnt_list_entry = get_free_grant(info);
298 if (gnt_list_entry->gref != GRANT_INVALID_REF)
299 return gnt_list_entry;
301 /* Assign a gref to this page */
302 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
303 BUG_ON(gnt_list_entry->gref == -ENOSPC);
304 if (info->feature_persistent)
305 grant_foreign_access(gnt_list_entry, info);
307 /* Grant access to the GFN passed by the caller */
308 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
309 info->xbdev->otherend_id,
313 return gnt_list_entry;
316 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
317 struct blkfront_info *info)
319 struct grant *gnt_list_entry = get_free_grant(info);
321 if (gnt_list_entry->gref != GRANT_INVALID_REF)
322 return gnt_list_entry;
324 /* Assign a gref to this page */
325 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
326 BUG_ON(gnt_list_entry->gref == -ENOSPC);
327 if (!info->feature_persistent) {
328 struct page *indirect_page;
330 /* Fetch a pre-allocated page to use for indirect grefs */
331 BUG_ON(list_empty(&info->indirect_pages));
332 indirect_page = list_first_entry(&info->indirect_pages,
334 list_del(&indirect_page->lru);
335 gnt_list_entry->page = indirect_page;
337 grant_foreign_access(gnt_list_entry, info);
339 return gnt_list_entry;
342 static const char *op_name(int op)
344 static const char *const names[] = {
345 [BLKIF_OP_READ] = "read",
346 [BLKIF_OP_WRITE] = "write",
347 [BLKIF_OP_WRITE_BARRIER] = "barrier",
348 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
349 [BLKIF_OP_DISCARD] = "discard" };
351 if (op < 0 || op >= ARRAY_SIZE(names))
359 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
361 unsigned int end = minor + nr;
364 if (end > nr_minors) {
365 unsigned long *bitmap, *old;
367 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
372 spin_lock(&minor_lock);
373 if (end > nr_minors) {
375 memcpy(bitmap, minors,
376 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
378 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
381 spin_unlock(&minor_lock);
385 spin_lock(&minor_lock);
386 if (find_next_bit(minors, end, minor) >= end) {
387 bitmap_set(minors, minor, nr);
391 spin_unlock(&minor_lock);
396 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
398 unsigned int end = minor + nr;
400 BUG_ON(end > nr_minors);
401 spin_lock(&minor_lock);
402 bitmap_clear(minors, minor, nr);
403 spin_unlock(&minor_lock);
406 static void blkif_restart_queue_callback(void *arg)
408 struct blkfront_info *info = (struct blkfront_info *)arg;
409 schedule_work(&info->work);
412 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
414 /* We don't have real geometry info, but let's at least return
415 values consistent with the size of the device */
416 sector_t nsect = get_capacity(bd->bd_disk);
417 sector_t cylinders = nsect;
421 sector_div(cylinders, hg->heads * hg->sectors);
422 hg->cylinders = cylinders;
423 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
424 hg->cylinders = 0xffff;
428 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
429 unsigned command, unsigned long argument)
431 struct blkfront_info *info = bdev->bd_disk->private_data;
434 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
435 command, (long)argument);
438 case CDROMMULTISESSION:
439 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
440 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
441 if (put_user(0, (char __user *)(argument + i)))
445 case CDROM_GET_CAPABILITY: {
446 struct gendisk *gd = info->gd;
447 if (gd->flags & GENHD_FL_CD)
453 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
455 return -EINVAL; /* same return as native Linux */
461 static unsigned long blkif_ring_get_request(struct blkfront_info *info,
463 struct blkif_request **ring_req)
467 *ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
468 info->ring.req_prod_pvt++;
470 id = get_id_from_freelist(info);
471 info->shadow[id].request = req;
472 info->shadow[id].req.u.rw.id = id;
477 static int blkif_queue_discard_req(struct request *req)
479 struct blkfront_info *info = req->rq_disk->private_data;
480 struct blkif_request *ring_req, *final_ring_req;
483 /* Fill out a communications ring structure. */
484 id = blkif_ring_get_request(info, req, &final_ring_req);
485 ring_req = &info->shadow[id].req;
487 ring_req->operation = BLKIF_OP_DISCARD;
488 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
489 ring_req->u.discard.id = id;
490 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
491 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
492 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
494 ring_req->u.discard.flag = 0;
496 info->ring.req_prod_pvt++;
498 /* Copy the request to the ring page. */
499 *final_ring_req = *ring_req;
500 info->shadow[id].inflight = true;
505 struct setup_rw_req {
506 unsigned int grant_idx;
507 struct blkif_request_segment *segments;
508 struct blkfront_info *info;
509 struct blkif_request *ring_req;
510 grant_ref_t gref_head;
512 /* Only used when persistent grant is used and it's a read request */
514 unsigned int bvec_off;
518 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
519 unsigned int len, void *data)
521 struct setup_rw_req *setup = data;
523 struct grant *gnt_list_entry;
524 unsigned int fsect, lsect;
525 /* Convenient aliases */
526 unsigned int grant_idx = setup->grant_idx;
527 struct blkif_request *ring_req = setup->ring_req;
528 struct blkfront_info *info = setup->info;
529 struct blk_shadow *shadow = &info->shadow[setup->id];
531 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
532 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
534 kunmap_atomic(setup->segments);
536 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
537 gnt_list_entry = get_indirect_grant(&setup->gref_head, info);
538 shadow->indirect_grants[n] = gnt_list_entry;
539 setup->segments = kmap_atomic(gnt_list_entry->page);
540 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
543 gnt_list_entry = get_grant(&setup->gref_head, gfn, info);
544 ref = gnt_list_entry->gref;
545 shadow->grants_used[grant_idx] = gnt_list_entry;
547 if (setup->need_copy) {
550 shared_data = kmap_atomic(gnt_list_entry->page);
552 * this does not wipe data stored outside the
553 * range sg->offset..sg->offset+sg->length.
554 * Therefore, blkback *could* see data from
555 * previous requests. This is OK as long as
556 * persistent grants are shared with just one
557 * domain. It may need refactoring if this
560 memcpy(shared_data + offset,
561 setup->bvec_data + setup->bvec_off,
564 kunmap_atomic(shared_data);
565 setup->bvec_off += len;
569 lsect = fsect + (len >> 9) - 1;
570 if (ring_req->operation != BLKIF_OP_INDIRECT) {
571 ring_req->u.rw.seg[grant_idx] =
572 (struct blkif_request_segment) {
575 .last_sect = lsect };
577 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
578 (struct blkif_request_segment) {
581 .last_sect = lsect };
584 (setup->grant_idx)++;
587 static int blkif_queue_rw_req(struct request *req)
589 struct blkfront_info *info = req->rq_disk->private_data;
590 struct blkif_request *ring_req, *final_ring_req;
593 struct setup_rw_req setup = {
597 .need_copy = rq_data_dir(req) && info->feature_persistent,
601 * Used to store if we are able to queue the request by just using
602 * existing persistent grants, or if we have to get new grants,
603 * as there are not sufficiently many free.
605 bool new_persistent_gnts;
606 struct scatterlist *sg;
607 int num_sg, max_grefs, num_grant;
609 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
610 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
612 * If we are using indirect segments we need to account
613 * for the indirect grefs used in the request.
615 max_grefs += INDIRECT_GREFS(max_grefs);
617 /* Check if we have enough grants to allocate a requests */
618 if (info->persistent_gnts_c < max_grefs) {
619 new_persistent_gnts = 1;
620 if (gnttab_alloc_grant_references(
621 max_grefs - info->persistent_gnts_c,
622 &setup.gref_head) < 0) {
623 gnttab_request_free_callback(
625 blkif_restart_queue_callback,
631 new_persistent_gnts = 0;
633 /* Fill out a communications ring structure. */
634 id = blkif_ring_get_request(info, req, &final_ring_req);
635 ring_req = &info->shadow[id].req;
637 BUG_ON(info->max_indirect_segments == 0 &&
638 GREFS(req->nr_phys_segments) > BLKIF_MAX_SEGMENTS_PER_REQUEST);
639 BUG_ON(info->max_indirect_segments &&
640 GREFS(req->nr_phys_segments) > info->max_indirect_segments);
642 num_sg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
644 /* Calculate the number of grant used */
645 for_each_sg(info->shadow[id].sg, sg, num_sg, i)
646 num_grant += gnttab_count_grant(sg->offset, sg->length);
648 ring_req->u.rw.id = id;
649 info->shadow[id].num_sg = num_sg;
650 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
652 * The indirect operation can only be a BLKIF_OP_READ or
655 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
656 ring_req->operation = BLKIF_OP_INDIRECT;
657 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
658 BLKIF_OP_WRITE : BLKIF_OP_READ;
659 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
660 ring_req->u.indirect.handle = info->handle;
661 ring_req->u.indirect.nr_segments = num_grant;
663 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
664 ring_req->u.rw.handle = info->handle;
665 ring_req->operation = rq_data_dir(req) ?
666 BLKIF_OP_WRITE : BLKIF_OP_READ;
667 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
669 * Ideally we can do an unordered flush-to-disk.
670 * In case the backend onlysupports barriers, use that.
671 * A barrier request a superset of FUA, so we can
672 * implement it the same way. (It's also a FLUSH+FUA,
673 * since it is guaranteed ordered WRT previous writes.)
675 switch (info->feature_flush &
676 ((REQ_FLUSH|REQ_FUA))) {
677 case REQ_FLUSH|REQ_FUA:
678 ring_req->operation =
679 BLKIF_OP_WRITE_BARRIER;
682 ring_req->operation =
683 BLKIF_OP_FLUSH_DISKCACHE;
686 ring_req->operation = 0;
689 ring_req->u.rw.nr_segments = num_grant;
692 setup.ring_req = ring_req;
694 for_each_sg(info->shadow[id].sg, sg, num_sg, i) {
695 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
697 if (setup.need_copy) {
698 setup.bvec_off = sg->offset;
699 setup.bvec_data = kmap_atomic(sg_page(sg));
702 gnttab_foreach_grant_in_range(sg_page(sg),
705 blkif_setup_rw_req_grant,
709 kunmap_atomic(setup.bvec_data);
712 kunmap_atomic(setup.segments);
714 info->ring.req_prod_pvt++;
716 /* Copy request(s) to the ring page. */
717 *final_ring_req = *ring_req;
718 info->shadow[id].inflight = true;
720 if (new_persistent_gnts)
721 gnttab_free_grant_references(setup.gref_head);
727 * Generate a Xen blkfront IO request from a blk layer request. Reads
728 * and writes are handled as expected.
730 * @req: a request struct
732 static int blkif_queue_request(struct request *req)
734 struct blkfront_info *info = req->rq_disk->private_data;
736 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
739 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE)))
740 return blkif_queue_discard_req(req);
742 return blkif_queue_rw_req(req);
745 static inline void flush_requests(struct blkfront_info *info)
749 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
752 notify_remote_via_irq(info->irq);
755 static inline bool blkif_request_flush_invalid(struct request *req,
756 struct blkfront_info *info)
758 return ((req->cmd_type != REQ_TYPE_FS) ||
759 ((req->cmd_flags & REQ_FLUSH) &&
760 !(info->feature_flush & REQ_FLUSH)) ||
761 ((req->cmd_flags & REQ_FUA) &&
762 !(info->feature_flush & REQ_FUA)));
765 static int blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
766 const struct blk_mq_queue_data *qd)
768 struct blkfront_info *info = qd->rq->rq_disk->private_data;
770 blk_mq_start_request(qd->rq);
771 spin_lock_irq(&info->io_lock);
772 if (RING_FULL(&info->ring))
775 if (blkif_request_flush_invalid(qd->rq, info))
778 if (blkif_queue_request(qd->rq))
781 flush_requests(info);
782 spin_unlock_irq(&info->io_lock);
783 return BLK_MQ_RQ_QUEUE_OK;
786 spin_unlock_irq(&info->io_lock);
787 return BLK_MQ_RQ_QUEUE_ERROR;
790 spin_unlock_irq(&info->io_lock);
791 blk_mq_stop_hw_queue(hctx);
792 return BLK_MQ_RQ_QUEUE_BUSY;
795 static struct blk_mq_ops blkfront_mq_ops = {
796 .queue_rq = blkif_queue_rq,
797 .map_queue = blk_mq_map_queue,
800 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
801 unsigned int physical_sector_size,
802 unsigned int segments)
804 struct request_queue *rq;
805 struct blkfront_info *info = gd->private_data;
807 memset(&info->tag_set, 0, sizeof(info->tag_set));
808 info->tag_set.ops = &blkfront_mq_ops;
809 info->tag_set.nr_hw_queues = 1;
810 info->tag_set.queue_depth = BLK_RING_SIZE(info);
811 info->tag_set.numa_node = NUMA_NO_NODE;
812 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
813 info->tag_set.cmd_size = 0;
814 info->tag_set.driver_data = info;
816 if (blk_mq_alloc_tag_set(&info->tag_set))
818 rq = blk_mq_init_queue(&info->tag_set);
820 blk_mq_free_tag_set(&info->tag_set);
824 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
826 if (info->feature_discard) {
827 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
828 blk_queue_max_discard_sectors(rq, get_capacity(gd));
829 rq->limits.discard_granularity = info->discard_granularity;
830 rq->limits.discard_alignment = info->discard_alignment;
831 if (info->feature_secdiscard)
832 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
835 /* Hard sector size and max sectors impersonate the equiv. hardware. */
836 blk_queue_logical_block_size(rq, sector_size);
837 blk_queue_physical_block_size(rq, physical_sector_size);
838 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
840 /* Each segment in a request is up to an aligned page in size. */
841 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
842 blk_queue_max_segment_size(rq, PAGE_SIZE);
844 /* Ensure a merged request will fit in a single I/O ring slot. */
845 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
847 /* Make sure buffer addresses are sector-aligned. */
848 blk_queue_dma_alignment(rq, 511);
850 /* Make sure we don't use bounce buffers. */
851 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
858 static const char *flush_info(unsigned int feature_flush)
860 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
861 case REQ_FLUSH|REQ_FUA:
862 return "barrier: enabled;";
864 return "flush diskcache: enabled;";
866 return "barrier or flush: disabled;";
870 static void xlvbd_flush(struct blkfront_info *info)
872 blk_queue_flush(info->rq, info->feature_flush);
873 pr_info("blkfront: %s: %s %s %s %s %s\n",
874 info->gd->disk_name, flush_info(info->feature_flush),
875 "persistent grants:", info->feature_persistent ?
876 "enabled;" : "disabled;", "indirect descriptors:",
877 info->max_indirect_segments ? "enabled;" : "disabled;");
880 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
883 major = BLKIF_MAJOR(vdevice);
884 *minor = BLKIF_MINOR(vdevice);
887 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
888 *minor = ((*minor / 64) * PARTS_PER_DISK) +
889 EMULATED_HD_DISK_MINOR_OFFSET;
892 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
893 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
894 EMULATED_HD_DISK_MINOR_OFFSET;
896 case XEN_SCSI_DISK0_MAJOR:
897 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
898 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
900 case XEN_SCSI_DISK1_MAJOR:
901 case XEN_SCSI_DISK2_MAJOR:
902 case XEN_SCSI_DISK3_MAJOR:
903 case XEN_SCSI_DISK4_MAJOR:
904 case XEN_SCSI_DISK5_MAJOR:
905 case XEN_SCSI_DISK6_MAJOR:
906 case XEN_SCSI_DISK7_MAJOR:
907 *offset = (*minor / PARTS_PER_DISK) +
908 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
909 EMULATED_SD_DISK_NAME_OFFSET;
911 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
912 EMULATED_SD_DISK_MINOR_OFFSET;
914 case XEN_SCSI_DISK8_MAJOR:
915 case XEN_SCSI_DISK9_MAJOR:
916 case XEN_SCSI_DISK10_MAJOR:
917 case XEN_SCSI_DISK11_MAJOR:
918 case XEN_SCSI_DISK12_MAJOR:
919 case XEN_SCSI_DISK13_MAJOR:
920 case XEN_SCSI_DISK14_MAJOR:
921 case XEN_SCSI_DISK15_MAJOR:
922 *offset = (*minor / PARTS_PER_DISK) +
923 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
924 EMULATED_SD_DISK_NAME_OFFSET;
926 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
927 EMULATED_SD_DISK_MINOR_OFFSET;
930 *offset = *minor / PARTS_PER_DISK;
933 printk(KERN_WARNING "blkfront: your disk configuration is "
934 "incorrect, please use an xvd device instead\n");
940 static char *encode_disk_name(char *ptr, unsigned int n)
943 ptr = encode_disk_name(ptr, n / 26 - 1);
948 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
949 struct blkfront_info *info,
950 u16 vdisk_info, u16 sector_size,
951 unsigned int physical_sector_size)
961 BUG_ON(info->gd != NULL);
962 BUG_ON(info->rq != NULL);
964 if ((info->vdevice>>EXT_SHIFT) > 1) {
965 /* this is above the extended range; something is wrong */
966 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
970 if (!VDEV_IS_EXTENDED(info->vdevice)) {
971 err = xen_translate_vdev(info->vdevice, &minor, &offset);
974 nr_parts = PARTS_PER_DISK;
976 minor = BLKIF_MINOR_EXT(info->vdevice);
977 nr_parts = PARTS_PER_EXT_DISK;
978 offset = minor / nr_parts;
979 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
980 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
981 "emulated IDE disks,\n\t choose an xvd device name"
982 "from xvde on\n", info->vdevice);
984 if (minor >> MINORBITS) {
985 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
986 info->vdevice, minor);
990 if ((minor % nr_parts) == 0)
991 nr_minors = nr_parts;
993 err = xlbd_reserve_minors(minor, nr_minors);
998 gd = alloc_disk(nr_minors);
1002 strcpy(gd->disk_name, DEV_NAME);
1003 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1004 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1008 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1009 "%d", minor & (nr_parts - 1));
1011 gd->major = XENVBD_MAJOR;
1012 gd->first_minor = minor;
1013 gd->fops = &xlvbd_block_fops;
1014 gd->private_data = info;
1015 gd->driverfs_dev = &(info->xbdev->dev);
1016 set_capacity(gd, capacity);
1018 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
1019 info->max_indirect_segments ? :
1020 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
1025 info->rq = gd->queue;
1030 if (vdisk_info & VDISK_READONLY)
1033 if (vdisk_info & VDISK_REMOVABLE)
1034 gd->flags |= GENHD_FL_REMOVABLE;
1036 if (vdisk_info & VDISK_CDROM)
1037 gd->flags |= GENHD_FL_CD;
1042 xlbd_release_minors(minor, nr_minors);
1047 static void xlvbd_release_gendisk(struct blkfront_info *info)
1049 unsigned int minor, nr_minors;
1051 if (info->rq == NULL)
1054 /* No more blkif_request(). */
1055 blk_mq_stop_hw_queues(info->rq);
1057 /* No more gnttab callback work. */
1058 gnttab_cancel_free_callback(&info->callback);
1060 /* Flush gnttab callback work. Must be done with no locks held. */
1061 flush_work(&info->work);
1063 del_gendisk(info->gd);
1065 minor = info->gd->first_minor;
1066 nr_minors = info->gd->minors;
1067 xlbd_release_minors(minor, nr_minors);
1069 blk_cleanup_queue(info->rq);
1070 blk_mq_free_tag_set(&info->tag_set);
1077 /* Must be called with io_lock holded */
1078 static void kick_pending_request_queues(struct blkfront_info *info)
1080 if (!RING_FULL(&info->ring))
1081 blk_mq_start_stopped_hw_queues(info->rq, true);
1084 static void blkif_restart_queue(struct work_struct *work)
1086 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
1088 spin_lock_irq(&info->io_lock);
1089 if (info->connected == BLKIF_STATE_CONNECTED)
1090 kick_pending_request_queues(info);
1091 spin_unlock_irq(&info->io_lock);
1094 static void blkif_free(struct blkfront_info *info, int suspend)
1096 struct grant *persistent_gnt;
1100 /* Prevent new requests being issued until we fix things up. */
1101 spin_lock_irq(&info->io_lock);
1102 info->connected = suspend ?
1103 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1104 /* No more blkif_request(). */
1106 blk_mq_stop_hw_queues(info->rq);
1108 /* Remove all persistent grants */
1109 if (!list_empty(&info->grants)) {
1110 list_for_each_entry_safe(persistent_gnt, n,
1111 &info->grants, node) {
1112 list_del(&persistent_gnt->node);
1113 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1114 gnttab_end_foreign_access(persistent_gnt->gref,
1116 info->persistent_gnts_c--;
1118 if (info->feature_persistent)
1119 __free_page(persistent_gnt->page);
1120 kfree(persistent_gnt);
1123 BUG_ON(info->persistent_gnts_c != 0);
1126 * Remove indirect pages, this only happens when using indirect
1127 * descriptors but not persistent grants
1129 if (!list_empty(&info->indirect_pages)) {
1130 struct page *indirect_page, *n;
1132 BUG_ON(info->feature_persistent);
1133 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1134 list_del(&indirect_page->lru);
1135 __free_page(indirect_page);
1139 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1141 * Clear persistent grants present in requests already
1142 * on the shared ring
1144 if (!info->shadow[i].request)
1147 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1148 info->shadow[i].req.u.indirect.nr_segments :
1149 info->shadow[i].req.u.rw.nr_segments;
1150 for (j = 0; j < segs; j++) {
1151 persistent_gnt = info->shadow[i].grants_used[j];
1152 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1153 if (info->feature_persistent)
1154 __free_page(persistent_gnt->page);
1155 kfree(persistent_gnt);
1158 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1160 * If this is not an indirect operation don't try to
1161 * free indirect segments
1165 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1166 persistent_gnt = info->shadow[i].indirect_grants[j];
1167 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1168 __free_page(persistent_gnt->page);
1169 kfree(persistent_gnt);
1173 kfree(info->shadow[i].grants_used);
1174 info->shadow[i].grants_used = NULL;
1175 kfree(info->shadow[i].indirect_grants);
1176 info->shadow[i].indirect_grants = NULL;
1177 kfree(info->shadow[i].sg);
1178 info->shadow[i].sg = NULL;
1181 /* No more gnttab callback work. */
1182 gnttab_cancel_free_callback(&info->callback);
1183 spin_unlock_irq(&info->io_lock);
1185 /* Flush gnttab callback work. Must be done with no locks held. */
1186 flush_work(&info->work);
1188 /* Free resources associated with old device channel. */
1189 for (i = 0; i < info->nr_ring_pages; i++) {
1190 if (info->ring_ref[i] != GRANT_INVALID_REF) {
1191 gnttab_end_foreign_access(info->ring_ref[i], 0, 0);
1192 info->ring_ref[i] = GRANT_INVALID_REF;
1195 free_pages((unsigned long)info->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1196 info->ring.sring = NULL;
1199 unbind_from_irqhandler(info->irq, info);
1200 info->evtchn = info->irq = 0;
1204 struct copy_from_grant {
1205 const struct blk_shadow *s;
1206 unsigned int grant_idx;
1207 unsigned int bvec_offset;
1211 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1212 unsigned int len, void *data)
1214 struct copy_from_grant *info = data;
1216 /* Convenient aliases */
1217 const struct blk_shadow *s = info->s;
1219 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1221 memcpy(info->bvec_data + info->bvec_offset,
1222 shared_data + offset, len);
1224 info->bvec_offset += len;
1227 kunmap_atomic(shared_data);
1230 static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1231 struct blkif_response *bret)
1234 struct scatterlist *sg;
1235 int num_sg, num_grant;
1236 struct copy_from_grant data = {
1241 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1242 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1245 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
1246 for_each_sg(s->sg, sg, num_sg, i) {
1247 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1249 data.bvec_offset = sg->offset;
1250 data.bvec_data = kmap_atomic(sg_page(sg));
1252 gnttab_foreach_grant_in_range(sg_page(sg),
1255 blkif_copy_from_grant,
1258 kunmap_atomic(data.bvec_data);
1261 /* Add the persistent grant into the list of free grants */
1262 for (i = 0; i < num_grant; i++) {
1263 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1265 * If the grant is still mapped by the backend (the
1266 * backend has chosen to make this grant persistent)
1267 * we add it at the head of the list, so it will be
1270 if (!info->feature_persistent)
1271 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1272 s->grants_used[i]->gref);
1273 list_add(&s->grants_used[i]->node, &info->grants);
1274 info->persistent_gnts_c++;
1277 * If the grant is not mapped by the backend we end the
1278 * foreign access and add it to the tail of the list,
1279 * so it will not be picked again unless we run out of
1280 * persistent grants.
1282 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1283 s->grants_used[i]->gref = GRANT_INVALID_REF;
1284 list_add_tail(&s->grants_used[i]->node, &info->grants);
1287 if (s->req.operation == BLKIF_OP_INDIRECT) {
1288 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1289 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
1290 if (!info->feature_persistent)
1291 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1292 s->indirect_grants[i]->gref);
1293 list_add(&s->indirect_grants[i]->node, &info->grants);
1294 info->persistent_gnts_c++;
1296 struct page *indirect_page;
1298 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
1300 * Add the used indirect page back to the list of
1301 * available pages for indirect grefs.
1303 if (!info->feature_persistent) {
1304 indirect_page = s->indirect_grants[i]->page;
1305 list_add(&indirect_page->lru, &info->indirect_pages);
1307 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1308 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
1314 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1316 struct request *req;
1317 struct blkif_response bret;
1319 unsigned long flags;
1320 struct blkfront_info *info = (struct blkfront_info *)dev_id;
1323 spin_lock_irqsave(&info->io_lock, flags);
1325 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
1326 spin_unlock_irqrestore(&info->io_lock, flags);
1331 rp = READ_ONCE(info->ring.sring->rsp_prod);
1332 rmb(); /* Ensure we see queued responses up to 'rp'. */
1333 if (RING_RESPONSE_PROD_OVERFLOW(&info->ring, rp)) {
1334 pr_alert("%s: illegal number of responses %u\n",
1335 info->gd->disk_name, rp - info->ring.rsp_cons);
1339 for (i = info->ring.rsp_cons; i != rp; i++) {
1343 RING_COPY_RESPONSE(&info->ring, i, &bret);
1347 * The backend has messed up and given us an id that we would
1348 * never have given to it (we stamp it up to BLK_RING_SIZE -
1349 * look in get_id_from_freelist.
1351 if (id >= BLK_RING_SIZE(info)) {
1352 pr_alert("%s: response has incorrect id (%ld)\n",
1353 info->gd->disk_name, id);
1356 if (!info->shadow[id].inflight) {
1357 pr_alert("%s: response references no pending request\n",
1358 info->gd->disk_name);
1362 info->shadow[id].inflight = false;
1363 req = info->shadow[id].request;
1365 op = info->shadow[id].req.operation;
1366 if (op == BLKIF_OP_INDIRECT)
1367 op = info->shadow[id].req.u.indirect.indirect_op;
1368 if (bret.operation != op) {
1369 pr_alert("%s: response has wrong operation (%u instead of %u)\n",
1370 info->gd->disk_name, bret.operation, op);
1374 if (bret.operation != BLKIF_OP_DISCARD)
1375 blkif_completion(&info->shadow[id], info, &bret);
1377 if (add_id_to_freelist(info, id)) {
1378 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1379 info->gd->disk_name, op_name(bret.operation), id);
1383 error = (bret.status == BLKIF_RSP_OKAY) ? 0 : -EIO;
1384 switch (bret.operation) {
1385 case BLKIF_OP_DISCARD:
1386 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1387 struct request_queue *rq = info->rq;
1389 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1390 info->gd->disk_name, op_name(bret.operation));
1391 error = -EOPNOTSUPP;
1392 info->feature_discard = 0;
1393 info->feature_secdiscard = 0;
1394 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1395 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
1397 blk_mq_complete_request(req, error);
1399 case BLKIF_OP_FLUSH_DISKCACHE:
1400 case BLKIF_OP_WRITE_BARRIER:
1401 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) {
1402 pr_warn_ratelimited("blkfront: %s: %s op failed\n",
1403 info->gd->disk_name, op_name(bret.operation));
1404 error = -EOPNOTSUPP;
1406 if (unlikely(bret.status == BLKIF_RSP_ERROR &&
1407 info->shadow[id].req.u.rw.nr_segments == 0)) {
1408 pr_warn_ratelimited("blkfront: %s: empty %s op failed\n",
1409 info->gd->disk_name, op_name(bret.operation));
1410 error = -EOPNOTSUPP;
1412 if (unlikely(error)) {
1413 if (error == -EOPNOTSUPP)
1415 info->feature_flush = 0;
1420 case BLKIF_OP_WRITE:
1421 if (unlikely(bret.status != BLKIF_RSP_OKAY))
1422 dev_dbg_ratelimited(&info->xbdev->dev,
1423 "Bad return from blkdev data request: %x\n",
1426 blk_mq_complete_request(req, error);
1433 info->ring.rsp_cons = i;
1435 if (i != info->ring.req_prod_pvt) {
1437 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1441 info->ring.sring->rsp_event = i + 1;
1443 kick_pending_request_queues(info);
1445 spin_unlock_irqrestore(&info->io_lock, flags);
1450 info->connected = BLKIF_STATE_ERROR;
1452 spin_unlock_irqrestore(&info->io_lock, flags);
1454 pr_alert("%s disabled for further use\n", info->gd->disk_name);
1459 static int setup_blkring(struct xenbus_device *dev,
1460 struct blkfront_info *info)
1462 struct blkif_sring *sring;
1464 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1465 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
1467 for (i = 0; i < info->nr_ring_pages; i++)
1468 info->ring_ref[i] = GRANT_INVALID_REF;
1470 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1471 get_order(ring_size));
1473 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1476 SHARED_RING_INIT(sring);
1477 FRONT_RING_INIT(&info->ring, sring, ring_size);
1479 err = xenbus_grant_ring(dev, info->ring.sring, info->nr_ring_pages, gref);
1481 free_pages((unsigned long)sring, get_order(ring_size));
1482 info->ring.sring = NULL;
1485 for (i = 0; i < info->nr_ring_pages; i++)
1486 info->ring_ref[i] = gref[i];
1488 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1492 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1495 xenbus_dev_fatal(dev, err,
1496 "bind_evtchn_to_irqhandler failed");
1503 blkif_free(info, 0);
1508 /* Common code used when first setting up, and when resuming. */
1509 static int talk_to_blkback(struct xenbus_device *dev,
1510 struct blkfront_info *info)
1512 const char *message = NULL;
1513 struct xenbus_transaction xbt;
1515 unsigned int max_page_order = 0;
1516 unsigned int ring_page_order = 0;
1518 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1519 "max-ring-page-order", "%u", &max_page_order);
1521 info->nr_ring_pages = 1;
1523 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1524 info->nr_ring_pages = 1 << ring_page_order;
1527 /* Create shared ring, alloc event channel. */
1528 err = setup_blkring(dev, info);
1533 err = xenbus_transaction_start(&xbt);
1535 xenbus_dev_fatal(dev, err, "starting transaction");
1536 goto destroy_blkring;
1539 if (info->nr_ring_pages == 1) {
1540 err = xenbus_printf(xbt, dev->nodename,
1541 "ring-ref", "%u", info->ring_ref[0]);
1543 message = "writing ring-ref";
1544 goto abort_transaction;
1547 err = xenbus_printf(xbt, dev->nodename,
1548 "ring-page-order", "%u", ring_page_order);
1550 message = "writing ring-page-order";
1551 goto abort_transaction;
1554 for (i = 0; i < info->nr_ring_pages; i++) {
1555 char ring_ref_name[RINGREF_NAME_LEN];
1557 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1558 err = xenbus_printf(xbt, dev->nodename, ring_ref_name,
1559 "%u", info->ring_ref[i]);
1561 message = "writing ring-ref";
1562 goto abort_transaction;
1566 err = xenbus_printf(xbt, dev->nodename,
1567 "event-channel", "%u", info->evtchn);
1569 message = "writing event-channel";
1570 goto abort_transaction;
1572 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1573 XEN_IO_PROTO_ABI_NATIVE);
1575 message = "writing protocol";
1576 goto abort_transaction;
1578 err = xenbus_printf(xbt, dev->nodename,
1579 "feature-persistent", "%u", 1);
1582 "writing persistent grants feature to xenbus");
1584 err = xenbus_transaction_end(xbt, 0);
1588 xenbus_dev_fatal(dev, err, "completing transaction");
1589 goto destroy_blkring;
1592 for (i = 0; i < BLK_RING_SIZE(info); i++)
1593 info->shadow[i].req.u.rw.id = i+1;
1594 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1595 xenbus_switch_state(dev, XenbusStateInitialised);
1600 xenbus_transaction_end(xbt, 1);
1602 xenbus_dev_fatal(dev, err, "%s", message);
1604 blkif_free(info, 0);
1610 * Entry point to this code when a new device is created. Allocate the basic
1611 * structures and the ring buffer for communication with the backend, and
1612 * inform the backend of the appropriate details for those. Switch to
1613 * Initialised state.
1615 static int blkfront_probe(struct xenbus_device *dev,
1616 const struct xenbus_device_id *id)
1619 struct blkfront_info *info;
1621 /* FIXME: Use dynamic device id if this is not set. */
1622 err = xenbus_scanf(XBT_NIL, dev->nodename,
1623 "virtual-device", "%i", &vdevice);
1625 /* go looking in the extended area instead */
1626 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1629 xenbus_dev_fatal(dev, err, "reading virtual-device");
1634 if (xen_hvm_domain()) {
1637 /* no unplug has been done: do not hook devices != xen vbds */
1638 if (xen_has_pv_and_legacy_disk_devices()) {
1641 if (!VDEV_IS_EXTENDED(vdevice))
1642 major = BLKIF_MAJOR(vdevice);
1644 major = XENVBD_MAJOR;
1646 if (major != XENVBD_MAJOR) {
1648 "%s: HVM does not support vbd %d as xen block device\n",
1653 /* do not create a PV cdrom device if we are an HVM guest */
1654 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1657 if (strncmp(type, "cdrom", 5) == 0) {
1663 info = kzalloc(sizeof(*info), GFP_KERNEL);
1665 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1669 mutex_init(&info->mutex);
1670 spin_lock_init(&info->io_lock);
1672 info->vdevice = vdevice;
1673 INIT_LIST_HEAD(&info->grants);
1674 INIT_LIST_HEAD(&info->indirect_pages);
1675 info->persistent_gnts_c = 0;
1676 info->connected = BLKIF_STATE_DISCONNECTED;
1677 INIT_WORK(&info->work, blkif_restart_queue);
1679 /* Front end dir is a number, which is used as the id. */
1680 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
1681 dev_set_drvdata(&dev->dev, info);
1686 static void split_bio_end(struct bio *bio)
1688 struct split_bio *split_bio = bio->bi_private;
1690 if (atomic_dec_and_test(&split_bio->pending)) {
1691 split_bio->bio->bi_phys_segments = 0;
1692 split_bio->bio->bi_error = bio->bi_error;
1693 bio_endio(split_bio->bio);
1699 static int blkif_recover(struct blkfront_info *info)
1702 struct request *req, *n;
1703 struct blk_shadow *copy;
1705 struct bio *bio, *cloned_bio;
1706 struct bio_list bio_list, merge_bio;
1707 unsigned int segs, offset;
1709 struct split_bio *split_bio;
1710 struct list_head requests;
1712 /* Stage 1: Make a safe copy of the shadow state. */
1713 copy = kmemdup(info->shadow, sizeof(info->shadow),
1714 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
1718 /* Stage 2: Set up free list. */
1719 memset(&info->shadow, 0, sizeof(info->shadow));
1720 for (i = 0; i < BLK_RING_SIZE(info); i++)
1721 info->shadow[i].req.u.rw.id = i+1;
1722 info->shadow_free = info->ring.req_prod_pvt;
1723 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1725 rc = blkfront_gather_backend_features(info);
1731 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1732 blk_queue_max_segments(info->rq, segs);
1733 bio_list_init(&bio_list);
1734 INIT_LIST_HEAD(&requests);
1735 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1737 if (!copy[i].request)
1741 * Get the bios in the request so we can re-queue them.
1743 if (copy[i].request->cmd_flags &
1744 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1746 * Flush operations don't contain bios, so
1747 * we need to requeue the whole request
1749 list_add(©[i].request->queuelist, &requests);
1752 merge_bio.head = copy[i].request->bio;
1753 merge_bio.tail = copy[i].request->biotail;
1754 bio_list_merge(&bio_list, &merge_bio);
1755 copy[i].request->bio = NULL;
1756 blk_end_request_all(copy[i].request, 0);
1761 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1763 spin_lock_irq(&info->io_lock);
1765 /* Now safe for us to use the shared ring */
1766 info->connected = BLKIF_STATE_CONNECTED;
1768 /* Kick any other new requests queued since we resumed */
1769 kick_pending_request_queues(info);
1771 list_for_each_entry_safe(req, n, &requests, queuelist) {
1772 /* Requeue pending requests (flush or discard) */
1773 list_del_init(&req->queuelist);
1774 BUG_ON(req->nr_phys_segments > segs);
1775 blk_mq_requeue_request(req);
1777 spin_unlock_irq(&info->io_lock);
1778 blk_mq_kick_requeue_list(info->rq);
1780 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1781 /* Traverse the list of pending bios and re-queue them */
1782 if (bio_segments(bio) > segs) {
1784 * This bio has more segments than what we can
1785 * handle, we have to split it.
1787 pending = (bio_segments(bio) + segs - 1) / segs;
1788 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1789 BUG_ON(split_bio == NULL);
1790 atomic_set(&split_bio->pending, pending);
1791 split_bio->bio = bio;
1792 for (i = 0; i < pending; i++) {
1793 offset = (i * segs * XEN_PAGE_SIZE) >> 9;
1794 size = min((unsigned int)(segs * XEN_PAGE_SIZE) >> 9,
1795 (unsigned int)bio_sectors(bio) - offset);
1796 cloned_bio = bio_clone(bio, GFP_NOIO);
1797 BUG_ON(cloned_bio == NULL);
1798 bio_trim(cloned_bio, offset, size);
1799 cloned_bio->bi_private = split_bio;
1800 cloned_bio->bi_end_io = split_bio_end;
1801 submit_bio(cloned_bio->bi_rw, cloned_bio);
1804 * Now we have to wait for all those smaller bios to
1805 * end, so we can also end the "parent" bio.
1809 /* We don't need to split this bio */
1810 submit_bio(bio->bi_rw, bio);
1817 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1818 * driver restart. We tear down our blkif structure and recreate it, but
1819 * leave the device-layer structures intact so that this is transparent to the
1820 * rest of the kernel.
1822 static int blkfront_resume(struct xenbus_device *dev)
1824 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1827 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1829 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1831 err = talk_to_blkback(dev, info);
1834 * We have to wait for the backend to switch to
1835 * connected state, since we want to read which
1836 * features it supports.
1843 blkfront_closing(struct blkfront_info *info)
1845 struct xenbus_device *xbdev = info->xbdev;
1846 struct block_device *bdev = NULL;
1848 mutex_lock(&info->mutex);
1850 if (xbdev->state == XenbusStateClosing) {
1851 mutex_unlock(&info->mutex);
1856 bdev = bdget_disk(info->gd, 0);
1858 mutex_unlock(&info->mutex);
1861 xenbus_frontend_closed(xbdev);
1865 mutex_lock(&bdev->bd_mutex);
1867 if (bdev->bd_openers) {
1868 xenbus_dev_error(xbdev, -EBUSY,
1869 "Device in use; refusing to close");
1870 xenbus_switch_state(xbdev, XenbusStateClosing);
1872 xlvbd_release_gendisk(info);
1873 xenbus_frontend_closed(xbdev);
1876 mutex_unlock(&bdev->bd_mutex);
1880 static void blkfront_setup_discard(struct blkfront_info *info)
1883 unsigned int discard_granularity;
1884 unsigned int discard_alignment;
1885 unsigned int discard_secure;
1887 info->feature_discard = 1;
1888 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1889 "discard-granularity", "%u", &discard_granularity,
1890 "discard-alignment", "%u", &discard_alignment,
1893 info->discard_granularity = discard_granularity;
1894 info->discard_alignment = discard_alignment;
1896 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1897 "discard-secure", "%d", &discard_secure,
1900 info->feature_secdiscard = !!discard_secure;
1903 static int blkfront_setup_indirect(struct blkfront_info *info)
1905 unsigned int psegs, grants;
1908 if (info->max_indirect_segments == 0)
1909 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1911 grants = info->max_indirect_segments;
1912 psegs = grants / GRANTS_PER_PSEG;
1914 err = fill_grant_buffer(info,
1915 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
1919 if (!info->feature_persistent && info->max_indirect_segments) {
1921 * We are using indirect descriptors but not persistent
1922 * grants, we need to allocate a set of pages that can be
1923 * used for mapping indirect grefs
1925 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
1927 BUG_ON(!list_empty(&info->indirect_pages));
1928 for (i = 0; i < num; i++) {
1929 struct page *indirect_page = alloc_page(GFP_NOIO);
1932 list_add(&indirect_page->lru, &info->indirect_pages);
1936 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1937 info->shadow[i].grants_used = kzalloc(
1938 sizeof(info->shadow[i].grants_used[0]) * grants,
1940 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * psegs, GFP_NOIO);
1941 if (info->max_indirect_segments)
1942 info->shadow[i].indirect_grants = kzalloc(
1943 sizeof(info->shadow[i].indirect_grants[0]) *
1944 INDIRECT_GREFS(grants),
1946 if ((info->shadow[i].grants_used == NULL) ||
1947 (info->shadow[i].sg == NULL) ||
1948 (info->max_indirect_segments &&
1949 (info->shadow[i].indirect_grants == NULL)))
1951 sg_init_table(info->shadow[i].sg, psegs);
1958 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1959 kfree(info->shadow[i].grants_used);
1960 info->shadow[i].grants_used = NULL;
1961 kfree(info->shadow[i].sg);
1962 info->shadow[i].sg = NULL;
1963 kfree(info->shadow[i].indirect_grants);
1964 info->shadow[i].indirect_grants = NULL;
1965 info->shadow[i].inflight = false;
1967 if (!list_empty(&info->indirect_pages)) {
1968 struct page *indirect_page, *n;
1969 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1970 list_del(&indirect_page->lru);
1971 __free_page(indirect_page);
1978 * Gather all backend feature-*
1980 static int blkfront_gather_backend_features(struct blkfront_info *info)
1983 int barrier, flush, discard, persistent;
1984 unsigned int indirect_segments;
1986 info->feature_flush = 0;
1988 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1989 "feature-barrier", "%d", &barrier,
1993 * If there's no "feature-barrier" defined, then it means
1994 * we're dealing with a very old backend which writes
1995 * synchronously; nothing to do.
1997 * If there are barriers, then we use flush.
1999 if (!err && barrier)
2000 info->feature_flush = REQ_FLUSH | REQ_FUA;
2002 * And if there is "feature-flush-cache" use that above
2005 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2006 "feature-flush-cache", "%d", &flush,
2010 info->feature_flush = REQ_FLUSH;
2012 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2013 "feature-discard", "%d", &discard,
2016 if (!err && discard)
2017 blkfront_setup_discard(info);
2019 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2020 "feature-persistent", "%u", &persistent,
2023 info->feature_persistent = 0;
2025 info->feature_persistent = persistent;
2027 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2028 "feature-max-indirect-segments", "%u", &indirect_segments,
2031 info->max_indirect_segments = 0;
2033 info->max_indirect_segments = min(indirect_segments,
2034 xen_blkif_max_segments);
2036 return blkfront_setup_indirect(info);
2040 * Invoked when the backend is finally 'ready' (and has told produced
2041 * the details about the physical device - #sectors, size, etc).
2043 static void blkfront_connect(struct blkfront_info *info)
2045 unsigned long long sectors;
2046 unsigned long sector_size;
2047 unsigned int physical_sector_size;
2051 switch (info->connected) {
2052 case BLKIF_STATE_CONNECTED:
2054 * Potentially, the back-end may be signalling
2055 * a capacity change; update the capacity.
2057 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2058 "sectors", "%Lu", §ors);
2059 if (XENBUS_EXIST_ERR(err))
2061 printk(KERN_INFO "Setting capacity to %Lu\n",
2063 set_capacity(info->gd, sectors);
2064 revalidate_disk(info->gd);
2067 case BLKIF_STATE_SUSPENDED:
2069 * If we are recovering from suspension, we need to wait
2070 * for the backend to announce it's features before
2071 * reconnecting, at least we need to know if the backend
2072 * supports indirect descriptors, and how many.
2074 blkif_recover(info);
2081 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2082 __func__, info->xbdev->otherend);
2084 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2085 "sectors", "%llu", §ors,
2086 "info", "%u", &binfo,
2087 "sector-size", "%lu", §or_size,
2090 xenbus_dev_fatal(info->xbdev, err,
2091 "reading backend fields at %s",
2092 info->xbdev->otherend);
2097 * physcial-sector-size is a newer field, so old backends may not
2098 * provide this. Assume physical sector size to be the same as
2099 * sector_size in that case.
2101 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2102 "physical-sector-size", "%u", &physical_sector_size);
2104 physical_sector_size = sector_size;
2106 err = blkfront_gather_backend_features(info);
2108 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2109 info->xbdev->otherend);
2113 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2114 physical_sector_size);
2116 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2117 info->xbdev->otherend);
2121 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2123 /* Kick pending requests. */
2124 spin_lock_irq(&info->io_lock);
2125 info->connected = BLKIF_STATE_CONNECTED;
2126 kick_pending_request_queues(info);
2127 spin_unlock_irq(&info->io_lock);
2135 * Callback received when the backend's state changes.
2137 static void blkback_changed(struct xenbus_device *dev,
2138 enum xenbus_state backend_state)
2140 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2142 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2144 switch (backend_state) {
2145 case XenbusStateInitWait:
2146 if (dev->state != XenbusStateInitialising)
2148 if (talk_to_blkback(dev, info)) {
2150 dev_set_drvdata(&dev->dev, NULL);
2153 case XenbusStateInitialising:
2154 case XenbusStateInitialised:
2155 case XenbusStateReconfiguring:
2156 case XenbusStateReconfigured:
2157 case XenbusStateUnknown:
2160 case XenbusStateConnected:
2161 blkfront_connect(info);
2164 case XenbusStateClosed:
2165 if (dev->state == XenbusStateClosed)
2167 /* Missed the backend's Closing state -- fallthrough */
2168 case XenbusStateClosing:
2170 blkfront_closing(info);
2175 static int blkfront_remove(struct xenbus_device *xbdev)
2177 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2178 struct block_device *bdev = NULL;
2179 struct gendisk *disk;
2181 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2183 blkif_free(info, 0);
2185 mutex_lock(&info->mutex);
2189 bdev = bdget_disk(disk, 0);
2192 mutex_unlock(&info->mutex);
2200 * The xbdev was removed before we reached the Closed
2201 * state. See if it's safe to remove the disk. If the bdev
2202 * isn't closed yet, we let release take care of it.
2205 mutex_lock(&bdev->bd_mutex);
2206 info = disk->private_data;
2208 dev_warn(disk_to_dev(disk),
2209 "%s was hot-unplugged, %d stale handles\n",
2210 xbdev->nodename, bdev->bd_openers);
2212 if (info && !bdev->bd_openers) {
2213 xlvbd_release_gendisk(info);
2214 disk->private_data = NULL;
2218 mutex_unlock(&bdev->bd_mutex);
2224 static int blkfront_is_ready(struct xenbus_device *dev)
2226 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2228 return info->is_ready && info->xbdev;
2231 static int blkif_open(struct block_device *bdev, fmode_t mode)
2233 struct gendisk *disk = bdev->bd_disk;
2234 struct blkfront_info *info;
2237 mutex_lock(&blkfront_mutex);
2239 info = disk->private_data;
2246 mutex_lock(&info->mutex);
2249 /* xbdev is closed */
2252 mutex_unlock(&info->mutex);
2255 mutex_unlock(&blkfront_mutex);
2259 static void blkif_release(struct gendisk *disk, fmode_t mode)
2261 struct blkfront_info *info = disk->private_data;
2262 struct block_device *bdev;
2263 struct xenbus_device *xbdev;
2265 mutex_lock(&blkfront_mutex);
2267 bdev = bdget_disk(disk, 0);
2270 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2273 if (bdev->bd_openers)
2277 * Check if we have been instructed to close. We will have
2278 * deferred this request, because the bdev was still open.
2281 mutex_lock(&info->mutex);
2282 xbdev = info->xbdev;
2284 if (xbdev && xbdev->state == XenbusStateClosing) {
2285 /* pending switch to state closed */
2286 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2287 xlvbd_release_gendisk(info);
2288 xenbus_frontend_closed(info->xbdev);
2291 mutex_unlock(&info->mutex);
2294 /* sudden device removal */
2295 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2296 xlvbd_release_gendisk(info);
2297 disk->private_data = NULL;
2304 mutex_unlock(&blkfront_mutex);
2307 static const struct block_device_operations xlvbd_block_fops =
2309 .owner = THIS_MODULE,
2311 .release = blkif_release,
2312 .getgeo = blkif_getgeo,
2313 .ioctl = blkif_ioctl,
2317 static const struct xenbus_device_id blkfront_ids[] = {
2322 static struct xenbus_driver blkfront_driver = {
2323 .ids = blkfront_ids,
2324 .probe = blkfront_probe,
2325 .remove = blkfront_remove,
2326 .resume = blkfront_resume,
2327 .otherend_changed = blkback_changed,
2328 .is_ready = blkfront_is_ready,
2331 static int __init xlblk_init(void)
2338 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2339 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2340 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2341 xen_blkif_max_ring_order = 0;
2344 if (!xen_has_pv_disk_devices())
2347 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2348 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2349 XENVBD_MAJOR, DEV_NAME);
2353 ret = xenbus_register_frontend(&blkfront_driver);
2355 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2361 module_init(xlblk_init);
2364 static void __exit xlblk_exit(void)
2366 xenbus_unregister_driver(&blkfront_driver);
2367 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2370 module_exit(xlblk_exit);
2372 MODULE_DESCRIPTION("Xen virtual block device frontend");
2373 MODULE_LICENSE("GPL");
2374 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2375 MODULE_ALIAS("xen:vbd");
2376 MODULE_ALIAS("xenblk");