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,
72 struct list_head node;
76 struct blkif_request req;
77 struct request *request;
78 struct grant **grants_used;
79 struct grant **indirect_grants;
80 struct scatterlist *sg;
89 static DEFINE_MUTEX(blkfront_mutex);
90 static const struct block_device_operations xlvbd_block_fops;
93 * Maximum number of segments in indirect requests, the actual value used by
94 * the frontend driver is the minimum of this value and the value provided
95 * by the backend driver.
98 static unsigned int xen_blkif_max_segments = 32;
99 module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
100 MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
102 static unsigned int xen_blkif_max_queues = 4;
103 module_param_named(max_queues, xen_blkif_max_queues, uint, S_IRUGO);
104 MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
107 * Maximum order of pages to be used for the shared ring between front and
108 * backend, 4KB page granularity is used.
110 static unsigned int xen_blkif_max_ring_order;
111 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
112 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
114 #define BLK_RING_SIZE(info) \
115 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
117 #define BLK_MAX_RING_SIZE \
118 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
121 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
122 * characters are enough. Define to 20 to keep consistent with backend.
124 #define RINGREF_NAME_LEN (20)
126 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
128 #define QUEUE_NAME_LEN (17)
132 * Every blkfront device can associate with one or more blkfront_ring_info,
133 * depending on how many hardware queues/rings to be used.
135 struct blkfront_ring_info {
136 /* Lock to protect data in every ring buffer. */
137 spinlock_t ring_lock;
138 struct blkif_front_ring ring;
139 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
140 unsigned int evtchn, irq;
141 struct work_struct work;
142 struct gnttab_free_callback callback;
143 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
144 struct list_head indirect_pages;
145 unsigned long shadow_free;
146 struct blkfront_info *dev_info;
150 * We have one of these per vbd, whether ide, scsi or 'other'. They
151 * hang in private_data off the gendisk structure. We may end up
152 * putting all kinds of interesting stuff here :-)
157 struct xenbus_device *xbdev;
161 enum blkif_state connected;
162 /* Number of pages per ring buffer. */
163 unsigned int nr_ring_pages;
164 struct request_queue *rq;
166 * Lock to protect info->grants list and persistent_gnts_c shared by all
170 struct list_head grants;
171 unsigned int persistent_gnts_c;
172 unsigned int feature_flush;
173 unsigned int feature_discard:1;
174 unsigned int feature_secdiscard:1;
175 unsigned int discard_granularity;
176 unsigned int discard_alignment;
177 unsigned int feature_persistent:1;
178 /* Number of 4KB segments handled */
179 unsigned int max_indirect_segments;
181 struct blk_mq_tag_set tag_set;
182 struct blkfront_ring_info *rinfo;
183 unsigned int nr_rings;
186 static unsigned int nr_minors;
187 static unsigned long *minors;
188 static DEFINE_SPINLOCK(minor_lock);
190 #define GRANT_INVALID_REF 0
192 #define PARTS_PER_DISK 16
193 #define PARTS_PER_EXT_DISK 256
195 #define BLKIF_MAJOR(dev) ((dev)>>8)
196 #define BLKIF_MINOR(dev) ((dev) & 0xff)
199 #define EXTENDED (1<<EXT_SHIFT)
200 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
201 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
202 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
203 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
204 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
205 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
207 #define DEV_NAME "xvd" /* name in /dev */
210 * Grants are always the same size as a Xen page (i.e 4KB).
211 * A physical segment is always the same size as a Linux page.
212 * Number of grants per physical segment
214 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
216 #define GRANTS_PER_INDIRECT_FRAME \
217 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
219 #define PSEGS_PER_INDIRECT_FRAME \
220 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
222 #define INDIRECT_GREFS(_grants) \
223 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
225 #define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
227 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
228 static void blkfront_gather_backend_features(struct blkfront_info *info);
230 static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
232 unsigned long free = rinfo->shadow_free;
234 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
235 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
236 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
240 static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
243 if (rinfo->shadow[id].req.u.rw.id != id)
245 if (rinfo->shadow[id].request == NULL)
247 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
248 rinfo->shadow[id].request = NULL;
249 rinfo->shadow_free = id;
253 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
255 struct blkfront_info *info = rinfo->dev_info;
256 struct page *granted_page;
257 struct grant *gnt_list_entry, *n;
261 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
265 if (info->feature_persistent) {
266 granted_page = alloc_page(GFP_NOIO);
268 kfree(gnt_list_entry);
271 gnt_list_entry->page = granted_page;
274 gnt_list_entry->gref = GRANT_INVALID_REF;
275 spin_lock_irq(&info->dev_lock);
276 list_add(&gnt_list_entry->node, &info->grants);
277 spin_unlock_irq(&info->dev_lock);
284 list_for_each_entry_safe(gnt_list_entry, n,
285 &info->grants, node) {
286 spin_lock_irq(&info->dev_lock);
287 list_del(&gnt_list_entry->node);
288 spin_unlock_irq(&info->dev_lock);
289 if (info->feature_persistent)
290 __free_page(gnt_list_entry->page);
291 kfree(gnt_list_entry);
298 static struct grant *get_free_grant(struct blkfront_info *info)
300 struct grant *gnt_list_entry;
303 spin_lock_irqsave(&info->dev_lock, flags);
304 BUG_ON(list_empty(&info->grants));
305 gnt_list_entry = list_first_entry(&info->grants, struct grant,
307 list_del(&gnt_list_entry->node);
309 if (gnt_list_entry->gref != GRANT_INVALID_REF)
310 info->persistent_gnts_c--;
311 spin_unlock_irqrestore(&info->dev_lock, flags);
313 return gnt_list_entry;
316 static inline void grant_foreign_access(const struct grant *gnt_list_entry,
317 const struct blkfront_info *info)
319 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
320 info->xbdev->otherend_id,
321 gnt_list_entry->page,
325 static struct grant *get_grant(grant_ref_t *gref_head,
327 struct blkfront_info *info)
329 struct grant *gnt_list_entry = get_free_grant(info);
331 if (gnt_list_entry->gref != GRANT_INVALID_REF)
332 return gnt_list_entry;
334 /* Assign a gref to this page */
335 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
336 BUG_ON(gnt_list_entry->gref == -ENOSPC);
337 if (info->feature_persistent)
338 grant_foreign_access(gnt_list_entry, info);
340 /* Grant access to the GFN passed by the caller */
341 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
342 info->xbdev->otherend_id,
346 return gnt_list_entry;
349 static struct grant *get_indirect_grant(grant_ref_t *gref_head,
350 struct blkfront_info *info)
352 struct grant *gnt_list_entry = get_free_grant(info);
354 if (gnt_list_entry->gref != GRANT_INVALID_REF)
355 return gnt_list_entry;
357 /* Assign a gref to this page */
358 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
359 BUG_ON(gnt_list_entry->gref == -ENOSPC);
360 if (!info->feature_persistent) {
361 struct page *indirect_page;
363 /* Fetch a pre-allocated page to use for indirect grefs */
364 BUG_ON(list_empty(&info->rinfo->indirect_pages));
365 indirect_page = list_first_entry(&info->rinfo->indirect_pages,
367 list_del(&indirect_page->lru);
368 gnt_list_entry->page = indirect_page;
370 grant_foreign_access(gnt_list_entry, info);
372 return gnt_list_entry;
375 static const char *op_name(int op)
377 static const char *const names[] = {
378 [BLKIF_OP_READ] = "read",
379 [BLKIF_OP_WRITE] = "write",
380 [BLKIF_OP_WRITE_BARRIER] = "barrier",
381 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
382 [BLKIF_OP_DISCARD] = "discard" };
384 if (op < 0 || op >= ARRAY_SIZE(names))
392 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
394 unsigned int end = minor + nr;
397 if (end > nr_minors) {
398 unsigned long *bitmap, *old;
400 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
405 spin_lock(&minor_lock);
406 if (end > nr_minors) {
408 memcpy(bitmap, minors,
409 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
411 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
414 spin_unlock(&minor_lock);
418 spin_lock(&minor_lock);
419 if (find_next_bit(minors, end, minor) >= end) {
420 bitmap_set(minors, minor, nr);
424 spin_unlock(&minor_lock);
429 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
431 unsigned int end = minor + nr;
433 BUG_ON(end > nr_minors);
434 spin_lock(&minor_lock);
435 bitmap_clear(minors, minor, nr);
436 spin_unlock(&minor_lock);
439 static void blkif_restart_queue_callback(void *arg)
441 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
442 schedule_work(&rinfo->work);
445 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
447 /* We don't have real geometry info, but let's at least return
448 values consistent with the size of the device */
449 sector_t nsect = get_capacity(bd->bd_disk);
450 sector_t cylinders = nsect;
454 sector_div(cylinders, hg->heads * hg->sectors);
455 hg->cylinders = cylinders;
456 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
457 hg->cylinders = 0xffff;
461 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
462 unsigned command, unsigned long argument)
464 struct blkfront_info *info = bdev->bd_disk->private_data;
467 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
468 command, (long)argument);
471 case CDROMMULTISESSION:
472 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
473 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
474 if (put_user(0, (char __user *)(argument + i)))
478 case CDROM_GET_CAPABILITY: {
479 struct gendisk *gd = info->gd;
480 if (gd->flags & GENHD_FL_CD)
486 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
488 return -EINVAL; /* same return as native Linux */
494 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
496 struct blkfront_info *info = rinfo->dev_info;
497 struct blkif_request *ring_req;
500 /* Fill out a communications ring structure. */
501 ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
502 id = get_id_from_freelist(rinfo);
503 rinfo->shadow[id].request = req;
505 ring_req->operation = BLKIF_OP_DISCARD;
506 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
507 ring_req->u.discard.id = id;
508 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
509 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
510 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
512 ring_req->u.discard.flag = 0;
514 rinfo->ring.req_prod_pvt++;
516 /* Keep a private copy so we can reissue requests when recovering. */
517 rinfo->shadow[id].req = *ring_req;
522 struct setup_rw_req {
523 unsigned int grant_idx;
524 struct blkif_request_segment *segments;
525 struct blkfront_ring_info *rinfo;
526 struct blkif_request *ring_req;
527 grant_ref_t gref_head;
529 /* Only used when persistent grant is used and it's a read request */
531 unsigned int bvec_off;
535 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
536 unsigned int len, void *data)
538 struct setup_rw_req *setup = data;
540 struct grant *gnt_list_entry;
541 unsigned int fsect, lsect;
542 /* Convenient aliases */
543 unsigned int grant_idx = setup->grant_idx;
544 struct blkif_request *ring_req = setup->ring_req;
545 struct blkfront_ring_info *rinfo = setup->rinfo;
546 struct blkfront_info *info = rinfo->dev_info;
547 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
549 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
550 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
552 kunmap_atomic(setup->segments);
554 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
555 gnt_list_entry = get_indirect_grant(&setup->gref_head, info);
556 shadow->indirect_grants[n] = gnt_list_entry;
557 setup->segments = kmap_atomic(gnt_list_entry->page);
558 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
561 gnt_list_entry = get_grant(&setup->gref_head, gfn, info);
562 ref = gnt_list_entry->gref;
563 shadow->grants_used[grant_idx] = gnt_list_entry;
565 if (setup->need_copy) {
568 shared_data = kmap_atomic(gnt_list_entry->page);
570 * this does not wipe data stored outside the
571 * range sg->offset..sg->offset+sg->length.
572 * Therefore, blkback *could* see data from
573 * previous requests. This is OK as long as
574 * persistent grants are shared with just one
575 * domain. It may need refactoring if this
578 memcpy(shared_data + offset,
579 setup->bvec_data + setup->bvec_off,
582 kunmap_atomic(shared_data);
583 setup->bvec_off += len;
587 lsect = fsect + (len >> 9) - 1;
588 if (ring_req->operation != BLKIF_OP_INDIRECT) {
589 ring_req->u.rw.seg[grant_idx] =
590 (struct blkif_request_segment) {
593 .last_sect = lsect };
595 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
596 (struct blkif_request_segment) {
599 .last_sect = lsect };
602 (setup->grant_idx)++;
605 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
607 struct blkfront_info *info = rinfo->dev_info;
608 struct blkif_request *ring_req;
611 struct setup_rw_req setup = {
615 .need_copy = rq_data_dir(req) && info->feature_persistent,
619 * Used to store if we are able to queue the request by just using
620 * existing persistent grants, or if we have to get new grants,
621 * as there are not sufficiently many free.
623 struct scatterlist *sg;
624 int num_sg, max_grefs, num_grant;
626 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
627 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
629 * If we are using indirect segments we need to account
630 * for the indirect grefs used in the request.
632 max_grefs += INDIRECT_GREFS(max_grefs);
635 * We have to reserve 'max_grefs' grants because persistent
636 * grants are shared by all rings.
639 if (gnttab_alloc_grant_references(max_grefs, &setup.gref_head) < 0) {
640 gnttab_request_free_callback(
642 blkif_restart_queue_callback,
648 /* Fill out a communications ring structure. */
649 ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
650 id = get_id_from_freelist(rinfo);
651 rinfo->shadow[id].request = req;
653 BUG_ON(info->max_indirect_segments == 0 &&
654 GREFS(req->nr_phys_segments) > BLKIF_MAX_SEGMENTS_PER_REQUEST);
655 BUG_ON(info->max_indirect_segments &&
656 GREFS(req->nr_phys_segments) > info->max_indirect_segments);
658 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
660 /* Calculate the number of grant used */
661 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
662 num_grant += gnttab_count_grant(sg->offset, sg->length);
664 ring_req->u.rw.id = id;
665 rinfo->shadow[id].num_sg = num_sg;
666 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
668 * The indirect operation can only be a BLKIF_OP_READ or
671 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
672 ring_req->operation = BLKIF_OP_INDIRECT;
673 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
674 BLKIF_OP_WRITE : BLKIF_OP_READ;
675 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
676 ring_req->u.indirect.handle = info->handle;
677 ring_req->u.indirect.nr_segments = num_grant;
679 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
680 ring_req->u.rw.handle = info->handle;
681 ring_req->operation = rq_data_dir(req) ?
682 BLKIF_OP_WRITE : BLKIF_OP_READ;
683 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
685 * Ideally we can do an unordered flush-to-disk.
686 * In case the backend onlysupports barriers, use that.
687 * A barrier request a superset of FUA, so we can
688 * implement it the same way. (It's also a FLUSH+FUA,
689 * since it is guaranteed ordered WRT previous writes.)
691 switch (info->feature_flush &
692 ((REQ_FLUSH|REQ_FUA))) {
693 case REQ_FLUSH|REQ_FUA:
694 ring_req->operation =
695 BLKIF_OP_WRITE_BARRIER;
698 ring_req->operation =
699 BLKIF_OP_FLUSH_DISKCACHE;
702 ring_req->operation = 0;
705 ring_req->u.rw.nr_segments = num_grant;
708 setup.ring_req = ring_req;
710 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
711 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
713 if (setup.need_copy) {
714 setup.bvec_off = sg->offset;
715 setup.bvec_data = kmap_atomic(sg_page(sg));
718 gnttab_foreach_grant_in_range(sg_page(sg),
721 blkif_setup_rw_req_grant,
725 kunmap_atomic(setup.bvec_data);
728 kunmap_atomic(setup.segments);
730 rinfo->ring.req_prod_pvt++;
732 /* Keep a private copy so we can reissue requests when recovering. */
733 rinfo->shadow[id].req = *ring_req;
736 gnttab_free_grant_references(setup.gref_head);
742 * Generate a Xen blkfront IO request from a blk layer request. Reads
743 * and writes are handled as expected.
745 * @req: a request struct
747 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
749 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
752 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE)))
753 return blkif_queue_discard_req(req, rinfo);
755 return blkif_queue_rw_req(req, rinfo);
758 static inline void flush_requests(struct blkfront_ring_info *rinfo)
762 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
765 notify_remote_via_irq(rinfo->irq);
768 static inline bool blkif_request_flush_invalid(struct request *req,
769 struct blkfront_info *info)
771 return ((req->cmd_type != REQ_TYPE_FS) ||
772 ((req->cmd_flags & REQ_FLUSH) &&
773 !(info->feature_flush & REQ_FLUSH)) ||
774 ((req->cmd_flags & REQ_FUA) &&
775 !(info->feature_flush & REQ_FUA)));
778 static int blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
779 const struct blk_mq_queue_data *qd)
782 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)hctx->driver_data;
784 blk_mq_start_request(qd->rq);
785 spin_lock_irqsave(&rinfo->ring_lock, flags);
786 if (RING_FULL(&rinfo->ring))
789 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
792 if (blkif_queue_request(qd->rq, rinfo))
795 flush_requests(rinfo);
796 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
797 return BLK_MQ_RQ_QUEUE_OK;
800 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
801 return BLK_MQ_RQ_QUEUE_ERROR;
804 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
805 blk_mq_stop_hw_queue(hctx);
806 return BLK_MQ_RQ_QUEUE_BUSY;
809 static int blk_mq_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
812 struct blkfront_info *info = (struct blkfront_info *)data;
814 BUG_ON(info->nr_rings <= index);
815 hctx->driver_data = &info->rinfo[index];
819 static struct blk_mq_ops blkfront_mq_ops = {
820 .queue_rq = blkif_queue_rq,
821 .map_queue = blk_mq_map_queue,
822 .init_hctx = blk_mq_init_hctx,
825 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
826 unsigned int physical_sector_size,
827 unsigned int segments)
829 struct request_queue *rq;
830 struct blkfront_info *info = gd->private_data;
832 memset(&info->tag_set, 0, sizeof(info->tag_set));
833 info->tag_set.ops = &blkfront_mq_ops;
834 info->tag_set.nr_hw_queues = info->nr_rings;
835 info->tag_set.queue_depth = BLK_RING_SIZE(info);
836 info->tag_set.numa_node = NUMA_NO_NODE;
837 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
838 info->tag_set.cmd_size = 0;
839 info->tag_set.driver_data = info;
841 if (blk_mq_alloc_tag_set(&info->tag_set))
843 rq = blk_mq_init_queue(&info->tag_set);
845 blk_mq_free_tag_set(&info->tag_set);
849 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
851 if (info->feature_discard) {
852 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
853 blk_queue_max_discard_sectors(rq, get_capacity(gd));
854 rq->limits.discard_granularity = info->discard_granularity;
855 rq->limits.discard_alignment = info->discard_alignment;
856 if (info->feature_secdiscard)
857 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
860 /* Hard sector size and max sectors impersonate the equiv. hardware. */
861 blk_queue_logical_block_size(rq, sector_size);
862 blk_queue_physical_block_size(rq, physical_sector_size);
863 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
865 /* Each segment in a request is up to an aligned page in size. */
866 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
867 blk_queue_max_segment_size(rq, PAGE_SIZE);
869 /* Ensure a merged request will fit in a single I/O ring slot. */
870 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
872 /* Make sure buffer addresses are sector-aligned. */
873 blk_queue_dma_alignment(rq, 511);
875 /* Make sure we don't use bounce buffers. */
876 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
883 static const char *flush_info(unsigned int feature_flush)
885 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
886 case REQ_FLUSH|REQ_FUA:
887 return "barrier: enabled;";
889 return "flush diskcache: enabled;";
891 return "barrier or flush: disabled;";
895 static void xlvbd_flush(struct blkfront_info *info)
897 blk_queue_flush(info->rq, info->feature_flush);
898 pr_info("blkfront: %s: %s %s %s %s %s\n",
899 info->gd->disk_name, flush_info(info->feature_flush),
900 "persistent grants:", info->feature_persistent ?
901 "enabled;" : "disabled;", "indirect descriptors:",
902 info->max_indirect_segments ? "enabled;" : "disabled;");
905 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
908 major = BLKIF_MAJOR(vdevice);
909 *minor = BLKIF_MINOR(vdevice);
912 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
913 *minor = ((*minor / 64) * PARTS_PER_DISK) +
914 EMULATED_HD_DISK_MINOR_OFFSET;
917 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
918 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
919 EMULATED_HD_DISK_MINOR_OFFSET;
921 case XEN_SCSI_DISK0_MAJOR:
922 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
923 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
925 case XEN_SCSI_DISK1_MAJOR:
926 case XEN_SCSI_DISK2_MAJOR:
927 case XEN_SCSI_DISK3_MAJOR:
928 case XEN_SCSI_DISK4_MAJOR:
929 case XEN_SCSI_DISK5_MAJOR:
930 case XEN_SCSI_DISK6_MAJOR:
931 case XEN_SCSI_DISK7_MAJOR:
932 *offset = (*minor / PARTS_PER_DISK) +
933 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
934 EMULATED_SD_DISK_NAME_OFFSET;
936 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
937 EMULATED_SD_DISK_MINOR_OFFSET;
939 case XEN_SCSI_DISK8_MAJOR:
940 case XEN_SCSI_DISK9_MAJOR:
941 case XEN_SCSI_DISK10_MAJOR:
942 case XEN_SCSI_DISK11_MAJOR:
943 case XEN_SCSI_DISK12_MAJOR:
944 case XEN_SCSI_DISK13_MAJOR:
945 case XEN_SCSI_DISK14_MAJOR:
946 case XEN_SCSI_DISK15_MAJOR:
947 *offset = (*minor / PARTS_PER_DISK) +
948 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
949 EMULATED_SD_DISK_NAME_OFFSET;
951 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
952 EMULATED_SD_DISK_MINOR_OFFSET;
955 *offset = *minor / PARTS_PER_DISK;
958 printk(KERN_WARNING "blkfront: your disk configuration is "
959 "incorrect, please use an xvd device instead\n");
965 static char *encode_disk_name(char *ptr, unsigned int n)
968 ptr = encode_disk_name(ptr, n / 26 - 1);
973 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
974 struct blkfront_info *info,
975 u16 vdisk_info, u16 sector_size,
976 unsigned int physical_sector_size)
986 BUG_ON(info->gd != NULL);
987 BUG_ON(info->rq != NULL);
989 if ((info->vdevice>>EXT_SHIFT) > 1) {
990 /* this is above the extended range; something is wrong */
991 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
995 if (!VDEV_IS_EXTENDED(info->vdevice)) {
996 err = xen_translate_vdev(info->vdevice, &minor, &offset);
999 nr_parts = PARTS_PER_DISK;
1001 minor = BLKIF_MINOR_EXT(info->vdevice);
1002 nr_parts = PARTS_PER_EXT_DISK;
1003 offset = minor / nr_parts;
1004 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
1005 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1006 "emulated IDE disks,\n\t choose an xvd device name"
1007 "from xvde on\n", info->vdevice);
1009 if (minor >> MINORBITS) {
1010 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1011 info->vdevice, minor);
1015 if ((minor % nr_parts) == 0)
1016 nr_minors = nr_parts;
1018 err = xlbd_reserve_minors(minor, nr_minors);
1023 gd = alloc_disk(nr_minors);
1027 strcpy(gd->disk_name, DEV_NAME);
1028 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1029 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1033 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1034 "%d", minor & (nr_parts - 1));
1036 gd->major = XENVBD_MAJOR;
1037 gd->first_minor = minor;
1038 gd->fops = &xlvbd_block_fops;
1039 gd->private_data = info;
1040 gd->driverfs_dev = &(info->xbdev->dev);
1041 set_capacity(gd, capacity);
1043 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
1044 info->max_indirect_segments ? :
1045 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
1050 info->rq = gd->queue;
1055 if (vdisk_info & VDISK_READONLY)
1058 if (vdisk_info & VDISK_REMOVABLE)
1059 gd->flags |= GENHD_FL_REMOVABLE;
1061 if (vdisk_info & VDISK_CDROM)
1062 gd->flags |= GENHD_FL_CD;
1067 xlbd_release_minors(minor, nr_minors);
1072 static void xlvbd_release_gendisk(struct blkfront_info *info)
1074 unsigned int minor, nr_minors, i;
1076 if (info->rq == NULL)
1079 /* No more blkif_request(). */
1080 blk_mq_stop_hw_queues(info->rq);
1082 for (i = 0; i < info->nr_rings; i++) {
1083 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1085 /* No more gnttab callback work. */
1086 gnttab_cancel_free_callback(&rinfo->callback);
1088 /* Flush gnttab callback work. Must be done with no locks held. */
1089 flush_work(&rinfo->work);
1092 del_gendisk(info->gd);
1094 minor = info->gd->first_minor;
1095 nr_minors = info->gd->minors;
1096 xlbd_release_minors(minor, nr_minors);
1098 blk_cleanup_queue(info->rq);
1099 blk_mq_free_tag_set(&info->tag_set);
1106 /* Already hold rinfo->ring_lock. */
1107 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
1109 if (!RING_FULL(&rinfo->ring))
1110 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
1113 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1115 unsigned long flags;
1117 spin_lock_irqsave(&rinfo->ring_lock, flags);
1118 kick_pending_request_queues_locked(rinfo);
1119 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1122 static void blkif_restart_queue(struct work_struct *work)
1124 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
1126 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1127 kick_pending_request_queues(rinfo);
1130 static void blkif_free_ring(struct blkfront_ring_info *rinfo)
1132 struct grant *persistent_gnt;
1133 struct blkfront_info *info = rinfo->dev_info;
1137 * Remove indirect pages, this only happens when using indirect
1138 * descriptors but not persistent grants
1140 if (!list_empty(&rinfo->indirect_pages)) {
1141 struct page *indirect_page, *n;
1143 BUG_ON(info->feature_persistent);
1144 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
1145 list_del(&indirect_page->lru);
1146 __free_page(indirect_page);
1150 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1152 * Clear persistent grants present in requests already
1153 * on the shared ring
1155 if (!rinfo->shadow[i].request)
1158 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1159 rinfo->shadow[i].req.u.indirect.nr_segments :
1160 rinfo->shadow[i].req.u.rw.nr_segments;
1161 for (j = 0; j < segs; j++) {
1162 persistent_gnt = rinfo->shadow[i].grants_used[j];
1163 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1164 if (info->feature_persistent)
1165 __free_page(persistent_gnt->page);
1166 kfree(persistent_gnt);
1169 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1171 * If this is not an indirect operation don't try to
1172 * free indirect segments
1176 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1177 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
1178 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1179 __free_page(persistent_gnt->page);
1180 kfree(persistent_gnt);
1184 kfree(rinfo->shadow[i].grants_used);
1185 rinfo->shadow[i].grants_used = NULL;
1186 kfree(rinfo->shadow[i].indirect_grants);
1187 rinfo->shadow[i].indirect_grants = NULL;
1188 kfree(rinfo->shadow[i].sg);
1189 rinfo->shadow[i].sg = NULL;
1192 /* No more gnttab callback work. */
1193 gnttab_cancel_free_callback(&rinfo->callback);
1195 /* Flush gnttab callback work. Must be done with no locks held. */
1196 flush_work(&rinfo->work);
1198 /* Free resources associated with old device channel. */
1199 for (i = 0; i < info->nr_ring_pages; i++) {
1200 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1201 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1202 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1205 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1206 rinfo->ring.sring = NULL;
1209 unbind_from_irqhandler(rinfo->irq, rinfo);
1210 rinfo->evtchn = rinfo->irq = 0;
1213 static void blkif_free(struct blkfront_info *info, int suspend)
1215 struct grant *persistent_gnt, *n;
1218 /* Prevent new requests being issued until we fix things up. */
1219 info->connected = suspend ?
1220 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1221 /* No more blkif_request(). */
1223 blk_mq_stop_hw_queues(info->rq);
1225 /* Remove all persistent grants */
1226 spin_lock_irq(&info->dev_lock);
1227 if (!list_empty(&info->grants)) {
1228 list_for_each_entry_safe(persistent_gnt, n,
1229 &info->grants, node) {
1230 list_del(&persistent_gnt->node);
1231 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1232 gnttab_end_foreign_access(persistent_gnt->gref,
1234 info->persistent_gnts_c--;
1236 if (info->feature_persistent)
1237 __free_page(persistent_gnt->page);
1238 kfree(persistent_gnt);
1241 BUG_ON(info->persistent_gnts_c != 0);
1242 spin_unlock_irq(&info->dev_lock);
1244 for (i = 0; i < info->nr_rings; i++)
1245 blkif_free_ring(&info->rinfo[i]);
1252 struct copy_from_grant {
1253 const struct blk_shadow *s;
1254 unsigned int grant_idx;
1255 unsigned int bvec_offset;
1259 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1260 unsigned int len, void *data)
1262 struct copy_from_grant *info = data;
1264 /* Convenient aliases */
1265 const struct blk_shadow *s = info->s;
1267 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1269 memcpy(info->bvec_data + info->bvec_offset,
1270 shared_data + offset, len);
1272 info->bvec_offset += len;
1275 kunmap_atomic(shared_data);
1278 static void blkif_completion(struct blk_shadow *s, struct blkfront_ring_info *rinfo,
1279 struct blkif_response *bret)
1282 struct scatterlist *sg;
1283 int num_sg, num_grant;
1284 unsigned long flags;
1285 struct blkfront_info *info = rinfo->dev_info;
1286 struct copy_from_grant data = {
1291 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
1292 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1295 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
1296 for_each_sg(s->sg, sg, num_sg, i) {
1297 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1299 data.bvec_offset = sg->offset;
1300 data.bvec_data = kmap_atomic(sg_page(sg));
1302 gnttab_foreach_grant_in_range(sg_page(sg),
1305 blkif_copy_from_grant,
1308 kunmap_atomic(data.bvec_data);
1311 /* Add the persistent grant into the list of free grants */
1312 for (i = 0; i < num_grant; i++) {
1313 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1315 * If the grant is still mapped by the backend (the
1316 * backend has chosen to make this grant persistent)
1317 * we add it at the head of the list, so it will be
1320 if (!info->feature_persistent)
1321 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1322 s->grants_used[i]->gref);
1323 spin_lock_irqsave(&info->dev_lock, flags);
1324 list_add(&s->grants_used[i]->node, &info->grants);
1325 info->persistent_gnts_c++;
1326 spin_unlock_irqrestore(&info->dev_lock, flags);
1329 * If the grant is not mapped by the backend we end the
1330 * foreign access and add it to the tail of the list,
1331 * so it will not be picked again unless we run out of
1332 * persistent grants.
1334 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1335 s->grants_used[i]->gref = GRANT_INVALID_REF;
1336 spin_lock_irqsave(&info->dev_lock, flags);
1337 list_add_tail(&s->grants_used[i]->node, &info->grants);
1338 spin_unlock_irqrestore(&info->dev_lock, flags);
1341 if (s->req.operation == BLKIF_OP_INDIRECT) {
1342 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
1343 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
1344 if (!info->feature_persistent)
1345 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1346 s->indirect_grants[i]->gref);
1347 spin_lock_irqsave(&info->dev_lock, flags);
1348 list_add(&s->indirect_grants[i]->node, &info->grants);
1349 info->persistent_gnts_c++;
1350 spin_unlock_irqrestore(&info->dev_lock, flags);
1352 struct page *indirect_page;
1354 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
1356 * Add the used indirect page back to the list of
1357 * available pages for indirect grefs.
1359 if (!info->feature_persistent) {
1360 indirect_page = s->indirect_grants[i]->page;
1361 list_add(&indirect_page->lru, &rinfo->indirect_pages);
1363 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1364 spin_lock_irqsave(&info->dev_lock, flags);
1365 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
1366 spin_unlock_irqrestore(&info->dev_lock, flags);
1372 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1374 struct request *req;
1375 struct blkif_response *bret;
1377 unsigned long flags;
1378 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1379 struct blkfront_info *info = rinfo->dev_info;
1382 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
1385 spin_lock_irqsave(&rinfo->ring_lock, flags);
1387 rp = rinfo->ring.sring->rsp_prod;
1388 rmb(); /* Ensure we see queued responses up to 'rp'. */
1390 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
1393 bret = RING_GET_RESPONSE(&rinfo->ring, i);
1396 * The backend has messed up and given us an id that we would
1397 * never have given to it (we stamp it up to BLK_RING_SIZE -
1398 * look in get_id_from_freelist.
1400 if (id >= BLK_RING_SIZE(info)) {
1401 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1402 info->gd->disk_name, op_name(bret->operation), id);
1403 /* We can't safely get the 'struct request' as
1404 * the id is busted. */
1407 req = rinfo->shadow[id].request;
1409 if (bret->operation != BLKIF_OP_DISCARD)
1410 blkif_completion(&rinfo->shadow[id], rinfo, bret);
1412 if (add_id_to_freelist(rinfo, id)) {
1413 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1414 info->gd->disk_name, op_name(bret->operation), id);
1418 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
1419 switch (bret->operation) {
1420 case BLKIF_OP_DISCARD:
1421 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1422 struct request_queue *rq = info->rq;
1423 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1424 info->gd->disk_name, op_name(bret->operation));
1425 error = -EOPNOTSUPP;
1426 info->feature_discard = 0;
1427 info->feature_secdiscard = 0;
1428 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1429 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
1431 blk_mq_complete_request(req, error);
1433 case BLKIF_OP_FLUSH_DISKCACHE:
1434 case BLKIF_OP_WRITE_BARRIER:
1435 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1436 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1437 info->gd->disk_name, op_name(bret->operation));
1438 error = -EOPNOTSUPP;
1440 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
1441 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
1442 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1443 info->gd->disk_name, op_name(bret->operation));
1444 error = -EOPNOTSUPP;
1446 if (unlikely(error)) {
1447 if (error == -EOPNOTSUPP)
1449 info->feature_flush = 0;
1454 case BLKIF_OP_WRITE:
1455 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1456 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1457 "request: %x\n", bret->status);
1459 blk_mq_complete_request(req, error);
1466 rinfo->ring.rsp_cons = i;
1468 if (i != rinfo->ring.req_prod_pvt) {
1470 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
1474 rinfo->ring.sring->rsp_event = i + 1;
1476 kick_pending_request_queues_locked(rinfo);
1478 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1484 static int setup_blkring(struct xenbus_device *dev,
1485 struct blkfront_ring_info *rinfo)
1487 struct blkif_sring *sring;
1489 struct blkfront_info *info = rinfo->dev_info;
1490 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
1491 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
1493 for (i = 0; i < info->nr_ring_pages; i++)
1494 rinfo->ring_ref[i] = GRANT_INVALID_REF;
1496 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1497 get_order(ring_size));
1499 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1502 SHARED_RING_INIT(sring);
1503 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
1505 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
1507 free_pages((unsigned long)sring, get_order(ring_size));
1508 rinfo->ring.sring = NULL;
1511 for (i = 0; i < info->nr_ring_pages; i++)
1512 rinfo->ring_ref[i] = gref[i];
1514 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
1518 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1521 xenbus_dev_fatal(dev, err,
1522 "bind_evtchn_to_irqhandler failed");
1529 blkif_free(info, 0);
1534 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1535 * ring buffer may have multi pages depending on ->nr_ring_pages.
1537 static int write_per_ring_nodes(struct xenbus_transaction xbt,
1538 struct blkfront_ring_info *rinfo, const char *dir)
1542 const char *message = NULL;
1543 struct blkfront_info *info = rinfo->dev_info;
1545 if (info->nr_ring_pages == 1) {
1546 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1548 message = "writing ring-ref";
1549 goto abort_transaction;
1552 for (i = 0; i < info->nr_ring_pages; i++) {
1553 char ring_ref_name[RINGREF_NAME_LEN];
1555 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1556 err = xenbus_printf(xbt, dir, ring_ref_name,
1557 "%u", rinfo->ring_ref[i]);
1559 message = "writing ring-ref";
1560 goto abort_transaction;
1565 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1567 message = "writing event-channel";
1568 goto abort_transaction;
1574 xenbus_transaction_end(xbt, 1);
1576 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1581 /* Common code used when first setting up, and when resuming. */
1582 static int talk_to_blkback(struct xenbus_device *dev,
1583 struct blkfront_info *info)
1585 const char *message = NULL;
1586 struct xenbus_transaction xbt;
1588 unsigned int i, max_page_order = 0;
1589 unsigned int ring_page_order = 0;
1591 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1592 "max-ring-page-order", "%u", &max_page_order);
1594 info->nr_ring_pages = 1;
1596 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1597 info->nr_ring_pages = 1 << ring_page_order;
1600 for (i = 0; i < info->nr_rings; i++) {
1601 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1603 /* Create shared ring, alloc event channel. */
1604 err = setup_blkring(dev, rinfo);
1606 goto destroy_blkring;
1610 err = xenbus_transaction_start(&xbt);
1612 xenbus_dev_fatal(dev, err, "starting transaction");
1613 goto destroy_blkring;
1616 if (info->nr_ring_pages > 1) {
1617 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1620 message = "writing ring-page-order";
1621 goto abort_transaction;
1625 /* We already got the number of queues/rings in _probe */
1626 if (info->nr_rings == 1) {
1627 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1629 goto destroy_blkring;
1634 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1637 message = "writing multi-queue-num-queues";
1638 goto abort_transaction;
1641 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1642 path = kmalloc(pathsize, GFP_KERNEL);
1645 message = "ENOMEM while writing ring references";
1646 goto abort_transaction;
1649 for (i = 0; i < info->nr_rings; i++) {
1650 memset(path, 0, pathsize);
1651 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1652 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1655 goto destroy_blkring;
1660 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1661 XEN_IO_PROTO_ABI_NATIVE);
1663 message = "writing protocol";
1664 goto abort_transaction;
1666 err = xenbus_printf(xbt, dev->nodename,
1667 "feature-persistent", "%u", 1);
1670 "writing persistent grants feature to xenbus");
1672 err = xenbus_transaction_end(xbt, 0);
1676 xenbus_dev_fatal(dev, err, "completing transaction");
1677 goto destroy_blkring;
1680 for (i = 0; i < info->nr_rings; i++) {
1682 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1684 for (j = 0; j < BLK_RING_SIZE(info); j++)
1685 rinfo->shadow[j].req.u.rw.id = j + 1;
1686 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1688 xenbus_switch_state(dev, XenbusStateInitialised);
1693 xenbus_transaction_end(xbt, 1);
1695 xenbus_dev_fatal(dev, err, "%s", message);
1697 blkif_free(info, 0);
1703 * Entry point to this code when a new device is created. Allocate the basic
1704 * structures and the ring buffer for communication with the backend, and
1705 * inform the backend of the appropriate details for those. Switch to
1706 * Initialised state.
1708 static int blkfront_probe(struct xenbus_device *dev,
1709 const struct xenbus_device_id *id)
1712 unsigned int r_index;
1713 struct blkfront_info *info;
1714 unsigned int backend_max_queues = 0;
1716 /* FIXME: Use dynamic device id if this is not set. */
1717 err = xenbus_scanf(XBT_NIL, dev->nodename,
1718 "virtual-device", "%i", &vdevice);
1720 /* go looking in the extended area instead */
1721 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1724 xenbus_dev_fatal(dev, err, "reading virtual-device");
1729 if (xen_hvm_domain()) {
1732 /* no unplug has been done: do not hook devices != xen vbds */
1733 if (xen_has_pv_and_legacy_disk_devices()) {
1736 if (!VDEV_IS_EXTENDED(vdevice))
1737 major = BLKIF_MAJOR(vdevice);
1739 major = XENVBD_MAJOR;
1741 if (major != XENVBD_MAJOR) {
1743 "%s: HVM does not support vbd %d as xen block device\n",
1748 /* do not create a PV cdrom device if we are an HVM guest */
1749 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1752 if (strncmp(type, "cdrom", 5) == 0) {
1758 info = kzalloc(sizeof(*info), GFP_KERNEL);
1760 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1765 /* Check if backend supports multiple queues. */
1766 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1767 "multi-queue-max-queues", "%u", &backend_max_queues);
1769 backend_max_queues = 1;
1771 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1772 /* We need at least one ring. */
1773 if (!info->nr_rings)
1776 info->rinfo = kzalloc(sizeof(struct blkfront_ring_info) * info->nr_rings, GFP_KERNEL);
1778 xenbus_dev_fatal(dev, -ENOMEM, "allocating ring_info structure");
1783 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1784 struct blkfront_ring_info *rinfo;
1786 rinfo = &info->rinfo[r_index];
1787 INIT_LIST_HEAD(&rinfo->indirect_pages);
1788 rinfo->dev_info = info;
1789 INIT_WORK(&rinfo->work, blkif_restart_queue);
1790 spin_lock_init(&rinfo->ring_lock);
1793 mutex_init(&info->mutex);
1794 spin_lock_init(&info->dev_lock);
1796 info->vdevice = vdevice;
1797 INIT_LIST_HEAD(&info->grants);
1798 info->connected = BLKIF_STATE_DISCONNECTED;
1800 /* Front end dir is a number, which is used as the id. */
1801 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
1802 dev_set_drvdata(&dev->dev, info);
1807 static void split_bio_end(struct bio *bio)
1809 struct split_bio *split_bio = bio->bi_private;
1811 if (atomic_dec_and_test(&split_bio->pending)) {
1812 split_bio->bio->bi_phys_segments = 0;
1813 split_bio->bio->bi_error = bio->bi_error;
1814 bio_endio(split_bio->bio);
1820 static int blkif_recover(struct blkfront_info *info)
1822 unsigned int i, r_index;
1823 struct request *req, *n;
1824 struct blk_shadow *copy;
1826 struct bio *bio, *cloned_bio;
1827 struct bio_list bio_list, merge_bio;
1828 unsigned int segs, offset;
1830 struct split_bio *split_bio;
1831 struct list_head requests;
1833 blkfront_gather_backend_features(info);
1834 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1835 blk_queue_max_segments(info->rq, segs);
1836 bio_list_init(&bio_list);
1837 INIT_LIST_HEAD(&requests);
1839 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1840 struct blkfront_ring_info *rinfo;
1842 rinfo = &info->rinfo[r_index];
1843 /* Stage 1: Make a safe copy of the shadow state. */
1844 copy = kmemdup(rinfo->shadow, sizeof(rinfo->shadow),
1845 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
1849 /* Stage 2: Set up free list. */
1850 memset(&rinfo->shadow, 0, sizeof(rinfo->shadow));
1851 for (i = 0; i < BLK_RING_SIZE(info); i++)
1852 rinfo->shadow[i].req.u.rw.id = i+1;
1853 rinfo->shadow_free = rinfo->ring.req_prod_pvt;
1854 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1856 rc = blkfront_setup_indirect(rinfo);
1862 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1864 if (!copy[i].request)
1868 * Get the bios in the request so we can re-queue them.
1870 if (copy[i].request->cmd_flags &
1871 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1873 * Flush operations don't contain bios, so
1874 * we need to requeue the whole request
1876 list_add(©[i].request->queuelist, &requests);
1879 merge_bio.head = copy[i].request->bio;
1880 merge_bio.tail = copy[i].request->biotail;
1881 bio_list_merge(&bio_list, &merge_bio);
1882 copy[i].request->bio = NULL;
1883 blk_end_request_all(copy[i].request, 0);
1888 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1890 /* Now safe for us to use the shared ring */
1891 info->connected = BLKIF_STATE_CONNECTED;
1893 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1894 struct blkfront_ring_info *rinfo;
1896 rinfo = &info->rinfo[r_index];
1897 /* Kick any other new requests queued since we resumed */
1898 kick_pending_request_queues(rinfo);
1901 list_for_each_entry_safe(req, n, &requests, queuelist) {
1902 /* Requeue pending requests (flush or discard) */
1903 list_del_init(&req->queuelist);
1904 BUG_ON(req->nr_phys_segments > segs);
1905 blk_mq_requeue_request(req);
1907 blk_mq_kick_requeue_list(info->rq);
1909 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1910 /* Traverse the list of pending bios and re-queue them */
1911 if (bio_segments(bio) > segs) {
1913 * This bio has more segments than what we can
1914 * handle, we have to split it.
1916 pending = (bio_segments(bio) + segs - 1) / segs;
1917 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1918 BUG_ON(split_bio == NULL);
1919 atomic_set(&split_bio->pending, pending);
1920 split_bio->bio = bio;
1921 for (i = 0; i < pending; i++) {
1922 offset = (i * segs * XEN_PAGE_SIZE) >> 9;
1923 size = min((unsigned int)(segs * XEN_PAGE_SIZE) >> 9,
1924 (unsigned int)bio_sectors(bio) - offset);
1925 cloned_bio = bio_clone(bio, GFP_NOIO);
1926 BUG_ON(cloned_bio == NULL);
1927 bio_trim(cloned_bio, offset, size);
1928 cloned_bio->bi_private = split_bio;
1929 cloned_bio->bi_end_io = split_bio_end;
1930 submit_bio(cloned_bio->bi_rw, cloned_bio);
1933 * Now we have to wait for all those smaller bios to
1934 * end, so we can also end the "parent" bio.
1938 /* We don't need to split this bio */
1939 submit_bio(bio->bi_rw, bio);
1946 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1947 * driver restart. We tear down our blkif structure and recreate it, but
1948 * leave the device-layer structures intact so that this is transparent to the
1949 * rest of the kernel.
1951 static int blkfront_resume(struct xenbus_device *dev)
1953 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1956 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1958 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1960 err = talk_to_blkback(dev, info);
1963 * We have to wait for the backend to switch to
1964 * connected state, since we want to read which
1965 * features it supports.
1971 static void blkfront_closing(struct blkfront_info *info)
1973 struct xenbus_device *xbdev = info->xbdev;
1974 struct block_device *bdev = NULL;
1976 mutex_lock(&info->mutex);
1978 if (xbdev->state == XenbusStateClosing) {
1979 mutex_unlock(&info->mutex);
1984 bdev = bdget_disk(info->gd, 0);
1986 mutex_unlock(&info->mutex);
1989 xenbus_frontend_closed(xbdev);
1993 mutex_lock(&bdev->bd_mutex);
1995 if (bdev->bd_openers) {
1996 xenbus_dev_error(xbdev, -EBUSY,
1997 "Device in use; refusing to close");
1998 xenbus_switch_state(xbdev, XenbusStateClosing);
2000 xlvbd_release_gendisk(info);
2001 xenbus_frontend_closed(xbdev);
2004 mutex_unlock(&bdev->bd_mutex);
2008 static void blkfront_setup_discard(struct blkfront_info *info)
2011 unsigned int discard_granularity;
2012 unsigned int discard_alignment;
2013 unsigned int discard_secure;
2015 info->feature_discard = 1;
2016 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2017 "discard-granularity", "%u", &discard_granularity,
2018 "discard-alignment", "%u", &discard_alignment,
2021 info->discard_granularity = discard_granularity;
2022 info->discard_alignment = discard_alignment;
2024 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2025 "discard-secure", "%d", &discard_secure,
2028 info->feature_secdiscard = !!discard_secure;
2031 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
2033 unsigned int psegs, grants;
2035 struct blkfront_info *info = rinfo->dev_info;
2037 if (info->max_indirect_segments == 0)
2038 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2040 grants = info->max_indirect_segments;
2041 psegs = grants / GRANTS_PER_PSEG;
2043 err = fill_grant_buffer(rinfo,
2044 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
2048 if (!info->feature_persistent && info->max_indirect_segments) {
2050 * We are using indirect descriptors but not persistent
2051 * grants, we need to allocate a set of pages that can be
2052 * used for mapping indirect grefs
2054 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
2056 BUG_ON(!list_empty(&rinfo->indirect_pages));
2057 for (i = 0; i < num; i++) {
2058 struct page *indirect_page = alloc_page(GFP_NOIO);
2061 list_add(&indirect_page->lru, &rinfo->indirect_pages);
2065 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2066 rinfo->shadow[i].grants_used = kzalloc(
2067 sizeof(rinfo->shadow[i].grants_used[0]) * grants,
2069 rinfo->shadow[i].sg = kzalloc(sizeof(rinfo->shadow[i].sg[0]) * psegs, GFP_NOIO);
2070 if (info->max_indirect_segments)
2071 rinfo->shadow[i].indirect_grants = kzalloc(
2072 sizeof(rinfo->shadow[i].indirect_grants[0]) *
2073 INDIRECT_GREFS(grants),
2075 if ((rinfo->shadow[i].grants_used == NULL) ||
2076 (rinfo->shadow[i].sg == NULL) ||
2077 (info->max_indirect_segments &&
2078 (rinfo->shadow[i].indirect_grants == NULL)))
2080 sg_init_table(rinfo->shadow[i].sg, psegs);
2087 for (i = 0; i < BLK_RING_SIZE(info); i++) {
2088 kfree(rinfo->shadow[i].grants_used);
2089 rinfo->shadow[i].grants_used = NULL;
2090 kfree(rinfo->shadow[i].sg);
2091 rinfo->shadow[i].sg = NULL;
2092 kfree(rinfo->shadow[i].indirect_grants);
2093 rinfo->shadow[i].indirect_grants = NULL;
2095 if (!list_empty(&rinfo->indirect_pages)) {
2096 struct page *indirect_page, *n;
2097 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
2098 list_del(&indirect_page->lru);
2099 __free_page(indirect_page);
2106 * Gather all backend feature-*
2108 static void blkfront_gather_backend_features(struct blkfront_info *info)
2111 int barrier, flush, discard, persistent;
2112 unsigned int indirect_segments;
2114 info->feature_flush = 0;
2116 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2117 "feature-barrier", "%d", &barrier,
2121 * If there's no "feature-barrier" defined, then it means
2122 * we're dealing with a very old backend which writes
2123 * synchronously; nothing to do.
2125 * If there are barriers, then we use flush.
2127 if (!err && barrier)
2128 info->feature_flush = REQ_FLUSH | REQ_FUA;
2130 * And if there is "feature-flush-cache" use that above
2133 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2134 "feature-flush-cache", "%d", &flush,
2138 info->feature_flush = REQ_FLUSH;
2140 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2141 "feature-discard", "%d", &discard,
2144 if (!err && discard)
2145 blkfront_setup_discard(info);
2147 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2148 "feature-persistent", "%u", &persistent,
2151 info->feature_persistent = 0;
2153 info->feature_persistent = persistent;
2155 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2156 "feature-max-indirect-segments", "%u", &indirect_segments,
2159 info->max_indirect_segments = 0;
2161 info->max_indirect_segments = min(indirect_segments,
2162 xen_blkif_max_segments);
2166 * Invoked when the backend is finally 'ready' (and has told produced
2167 * the details about the physical device - #sectors, size, etc).
2169 static void blkfront_connect(struct blkfront_info *info)
2171 unsigned long long sectors;
2172 unsigned long sector_size;
2173 unsigned int physical_sector_size;
2177 switch (info->connected) {
2178 case BLKIF_STATE_CONNECTED:
2180 * Potentially, the back-end may be signalling
2181 * a capacity change; update the capacity.
2183 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2184 "sectors", "%Lu", §ors);
2185 if (XENBUS_EXIST_ERR(err))
2187 printk(KERN_INFO "Setting capacity to %Lu\n",
2189 set_capacity(info->gd, sectors);
2190 revalidate_disk(info->gd);
2193 case BLKIF_STATE_SUSPENDED:
2195 * If we are recovering from suspension, we need to wait
2196 * for the backend to announce it's features before
2197 * reconnecting, at least we need to know if the backend
2198 * supports indirect descriptors, and how many.
2200 blkif_recover(info);
2207 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2208 __func__, info->xbdev->otherend);
2210 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2211 "sectors", "%llu", §ors,
2212 "info", "%u", &binfo,
2213 "sector-size", "%lu", §or_size,
2216 xenbus_dev_fatal(info->xbdev, err,
2217 "reading backend fields at %s",
2218 info->xbdev->otherend);
2223 * physcial-sector-size is a newer field, so old backends may not
2224 * provide this. Assume physical sector size to be the same as
2225 * sector_size in that case.
2227 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2228 "physical-sector-size", "%u", &physical_sector_size);
2230 physical_sector_size = sector_size;
2232 blkfront_gather_backend_features(info);
2233 for (i = 0; i < info->nr_rings; i++) {
2234 err = blkfront_setup_indirect(&info->rinfo[i]);
2236 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2237 info->xbdev->otherend);
2238 blkif_free(info, 0);
2243 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2244 physical_sector_size);
2246 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2247 info->xbdev->otherend);
2251 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2253 /* Kick pending requests. */
2254 info->connected = BLKIF_STATE_CONNECTED;
2255 for (i = 0; i < info->nr_rings; i++)
2256 kick_pending_request_queues(&info->rinfo[i]);
2264 * Callback received when the backend's state changes.
2266 static void blkback_changed(struct xenbus_device *dev,
2267 enum xenbus_state backend_state)
2269 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2271 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
2273 switch (backend_state) {
2274 case XenbusStateInitWait:
2275 if (dev->state != XenbusStateInitialising)
2277 if (talk_to_blkback(dev, info)) {
2279 dev_set_drvdata(&dev->dev, NULL);
2282 case XenbusStateInitialising:
2283 case XenbusStateInitialised:
2284 case XenbusStateReconfiguring:
2285 case XenbusStateReconfigured:
2286 case XenbusStateUnknown:
2289 case XenbusStateConnected:
2290 blkfront_connect(info);
2293 case XenbusStateClosed:
2294 if (dev->state == XenbusStateClosed)
2296 /* Missed the backend's Closing state -- fallthrough */
2297 case XenbusStateClosing:
2299 blkfront_closing(info);
2304 static int blkfront_remove(struct xenbus_device *xbdev)
2306 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2307 struct block_device *bdev = NULL;
2308 struct gendisk *disk;
2310 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
2312 blkif_free(info, 0);
2314 mutex_lock(&info->mutex);
2318 bdev = bdget_disk(disk, 0);
2321 mutex_unlock(&info->mutex);
2329 * The xbdev was removed before we reached the Closed
2330 * state. See if it's safe to remove the disk. If the bdev
2331 * isn't closed yet, we let release take care of it.
2334 mutex_lock(&bdev->bd_mutex);
2335 info = disk->private_data;
2337 dev_warn(disk_to_dev(disk),
2338 "%s was hot-unplugged, %d stale handles\n",
2339 xbdev->nodename, bdev->bd_openers);
2341 if (info && !bdev->bd_openers) {
2342 xlvbd_release_gendisk(info);
2343 disk->private_data = NULL;
2347 mutex_unlock(&bdev->bd_mutex);
2353 static int blkfront_is_ready(struct xenbus_device *dev)
2355 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
2357 return info->is_ready && info->xbdev;
2360 static int blkif_open(struct block_device *bdev, fmode_t mode)
2362 struct gendisk *disk = bdev->bd_disk;
2363 struct blkfront_info *info;
2366 mutex_lock(&blkfront_mutex);
2368 info = disk->private_data;
2375 mutex_lock(&info->mutex);
2378 /* xbdev is closed */
2381 mutex_unlock(&info->mutex);
2384 mutex_unlock(&blkfront_mutex);
2388 static void blkif_release(struct gendisk *disk, fmode_t mode)
2390 struct blkfront_info *info = disk->private_data;
2391 struct block_device *bdev;
2392 struct xenbus_device *xbdev;
2394 mutex_lock(&blkfront_mutex);
2396 bdev = bdget_disk(disk, 0);
2399 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2402 if (bdev->bd_openers)
2406 * Check if we have been instructed to close. We will have
2407 * deferred this request, because the bdev was still open.
2410 mutex_lock(&info->mutex);
2411 xbdev = info->xbdev;
2413 if (xbdev && xbdev->state == XenbusStateClosing) {
2414 /* pending switch to state closed */
2415 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2416 xlvbd_release_gendisk(info);
2417 xenbus_frontend_closed(info->xbdev);
2420 mutex_unlock(&info->mutex);
2423 /* sudden device removal */
2424 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2425 xlvbd_release_gendisk(info);
2426 disk->private_data = NULL;
2433 mutex_unlock(&blkfront_mutex);
2436 static const struct block_device_operations xlvbd_block_fops =
2438 .owner = THIS_MODULE,
2440 .release = blkif_release,
2441 .getgeo = blkif_getgeo,
2442 .ioctl = blkif_ioctl,
2446 static const struct xenbus_device_id blkfront_ids[] = {
2451 static struct xenbus_driver blkfront_driver = {
2452 .ids = blkfront_ids,
2453 .probe = blkfront_probe,
2454 .remove = blkfront_remove,
2455 .resume = blkfront_resume,
2456 .otherend_changed = blkback_changed,
2457 .is_ready = blkfront_is_ready,
2460 static int __init xlblk_init(void)
2463 int nr_cpus = num_online_cpus();
2468 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
2469 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2470 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
2471 xen_blkif_max_ring_order = 0;
2474 if (xen_blkif_max_queues > nr_cpus) {
2475 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2476 xen_blkif_max_queues, nr_cpus);
2477 xen_blkif_max_queues = nr_cpus;
2480 if (!xen_has_pv_disk_devices())
2483 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2484 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2485 XENVBD_MAJOR, DEV_NAME);
2489 ret = xenbus_register_frontend(&blkfront_driver);
2491 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2497 module_init(xlblk_init);
2500 static void __exit xlblk_exit(void)
2502 xenbus_unregister_driver(&blkfront_driver);
2503 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2506 module_exit(xlblk_exit);
2508 MODULE_DESCRIPTION("Xen virtual block device frontend");
2509 MODULE_LICENSE("GPL");
2510 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2511 MODULE_ALIAS("xen:vbd");
2512 MODULE_ALIAS("xenblk");