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/hdreg.h>
41 #include <linux/cdrom.h>
42 #include <linux/module.h>
43 #include <linux/slab.h>
44 #include <linux/mutex.h>
45 #include <linux/scatterlist.h>
46 #include <linux/bitmap.h>
47 #include <linux/list.h>
50 #include <xen/xenbus.h>
51 #include <xen/grant_table.h>
52 #include <xen/events.h>
54 #include <xen/platform_pci.h>
56 #include <xen/interface/grant_table.h>
57 #include <xen/interface/io/blkif.h>
58 #include <xen/interface/io/protocols.h>
60 #include <asm/xen/hypervisor.h>
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
71 struct list_head node;
75 struct blkif_request req;
76 struct request *request;
77 struct grant **grants_used;
78 struct grant **indirect_grants;
79 struct scatterlist *sg;
88 static DEFINE_MUTEX(blkfront_mutex);
89 static const struct block_device_operations xlvbd_block_fops;
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
97 static unsigned int xen_blkif_max_segments = 32;
98 module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
99 MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
101 #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
104 * We have one of these per vbd, whether ide, scsi or 'other'. They
105 * hang in private_data off the gendisk structure. We may end up
106 * putting all kinds of interesting stuff here :-)
112 struct xenbus_device *xbdev;
116 enum blkif_state connected;
118 struct blkif_front_ring ring;
119 unsigned int evtchn, irq;
120 struct request_queue *rq;
121 struct work_struct work;
122 struct gnttab_free_callback callback;
123 struct blk_shadow shadow[BLK_RING_SIZE];
124 struct list_head grants;
125 struct list_head indirect_pages;
126 unsigned int persistent_gnts_c;
127 unsigned long shadow_free;
128 unsigned int feature_flush;
129 unsigned int feature_discard:1;
130 unsigned int feature_secdiscard:1;
131 unsigned int discard_granularity;
132 unsigned int discard_alignment;
133 unsigned int feature_persistent:1;
134 unsigned int max_indirect_segments;
138 static unsigned int nr_minors;
139 static unsigned long *minors;
140 static DEFINE_SPINLOCK(minor_lock);
142 #define GRANT_INVALID_REF 0
144 #define PARTS_PER_DISK 16
145 #define PARTS_PER_EXT_DISK 256
147 #define BLKIF_MAJOR(dev) ((dev)>>8)
148 #define BLKIF_MINOR(dev) ((dev) & 0xff)
151 #define EXTENDED (1<<EXT_SHIFT)
152 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
153 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
154 #define EMULATED_HD_DISK_MINOR_OFFSET (0)
155 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
156 #define EMULATED_SD_DISK_MINOR_OFFSET (0)
157 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
159 #define DEV_NAME "xvd" /* name in /dev */
161 #define SEGS_PER_INDIRECT_FRAME \
162 (PAGE_SIZE/sizeof(struct blkif_request_segment))
163 #define INDIRECT_GREFS(_segs) \
164 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
166 static int blkfront_setup_indirect(struct blkfront_info *info);
168 static int get_id_from_freelist(struct blkfront_info *info)
170 unsigned long free = info->shadow_free;
171 BUG_ON(free >= BLK_RING_SIZE);
172 info->shadow_free = info->shadow[free].req.u.rw.id;
173 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
177 static int add_id_to_freelist(struct blkfront_info *info,
180 if (info->shadow[id].req.u.rw.id != id)
182 if (info->shadow[id].request == NULL)
184 info->shadow[id].req.u.rw.id = info->shadow_free;
185 info->shadow[id].request = NULL;
186 info->shadow_free = id;
190 static int fill_grant_buffer(struct blkfront_info *info, int num)
192 struct page *granted_page;
193 struct grant *gnt_list_entry, *n;
197 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
201 if (info->feature_persistent) {
202 granted_page = alloc_page(GFP_NOIO);
204 kfree(gnt_list_entry);
207 gnt_list_entry->pfn = page_to_pfn(granted_page);
210 gnt_list_entry->gref = GRANT_INVALID_REF;
211 list_add(&gnt_list_entry->node, &info->grants);
218 list_for_each_entry_safe(gnt_list_entry, n,
219 &info->grants, node) {
220 list_del(&gnt_list_entry->node);
221 if (info->feature_persistent)
222 __free_page(pfn_to_page(gnt_list_entry->pfn));
223 kfree(gnt_list_entry);
230 static struct grant *get_grant(grant_ref_t *gref_head,
232 struct blkfront_info *info)
234 struct grant *gnt_list_entry;
235 unsigned long buffer_mfn;
237 BUG_ON(list_empty(&info->grants));
238 gnt_list_entry = list_first_entry(&info->grants, struct grant,
240 list_del(&gnt_list_entry->node);
242 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
243 info->persistent_gnts_c--;
244 return gnt_list_entry;
247 /* Assign a gref to this page */
248 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
249 BUG_ON(gnt_list_entry->gref == -ENOSPC);
250 if (!info->feature_persistent) {
252 gnt_list_entry->pfn = pfn;
254 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
255 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
256 info->xbdev->otherend_id,
258 return gnt_list_entry;
261 static const char *op_name(int op)
263 static const char *const names[] = {
264 [BLKIF_OP_READ] = "read",
265 [BLKIF_OP_WRITE] = "write",
266 [BLKIF_OP_WRITE_BARRIER] = "barrier",
267 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
268 [BLKIF_OP_DISCARD] = "discard" };
270 if (op < 0 || op >= ARRAY_SIZE(names))
278 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
280 unsigned int end = minor + nr;
283 if (end > nr_minors) {
284 unsigned long *bitmap, *old;
286 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
291 spin_lock(&minor_lock);
292 if (end > nr_minors) {
294 memcpy(bitmap, minors,
295 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
297 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
300 spin_unlock(&minor_lock);
304 spin_lock(&minor_lock);
305 if (find_next_bit(minors, end, minor) >= end) {
306 bitmap_set(minors, minor, nr);
310 spin_unlock(&minor_lock);
315 static void xlbd_release_minors(unsigned int minor, unsigned int nr)
317 unsigned int end = minor + nr;
319 BUG_ON(end > nr_minors);
320 spin_lock(&minor_lock);
321 bitmap_clear(minors, minor, nr);
322 spin_unlock(&minor_lock);
325 static void blkif_restart_queue_callback(void *arg)
327 struct blkfront_info *info = (struct blkfront_info *)arg;
328 schedule_work(&info->work);
331 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
333 /* We don't have real geometry info, but let's at least return
334 values consistent with the size of the device */
335 sector_t nsect = get_capacity(bd->bd_disk);
336 sector_t cylinders = nsect;
340 sector_div(cylinders, hg->heads * hg->sectors);
341 hg->cylinders = cylinders;
342 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
343 hg->cylinders = 0xffff;
347 static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
348 unsigned command, unsigned long argument)
350 struct blkfront_info *info = bdev->bd_disk->private_data;
353 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
354 command, (long)argument);
357 case CDROMMULTISESSION:
358 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
359 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
360 if (put_user(0, (char __user *)(argument + i)))
364 case CDROM_GET_CAPABILITY: {
365 struct gendisk *gd = info->gd;
366 if (gd->flags & GENHD_FL_CD)
372 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
374 return -EINVAL; /* same return as native Linux */
381 * Generate a Xen blkfront IO request from a blk layer request. Reads
382 * and writes are handled as expected.
384 * @req: a request struct
386 static int blkif_queue_request(struct request *req)
388 struct blkfront_info *info = req->rq_disk->private_data;
389 struct blkif_request *ring_req;
391 unsigned int fsect, lsect;
393 struct blkif_request_segment *segments = NULL;
396 * Used to store if we are able to queue the request by just using
397 * existing persistent grants, or if we have to get new grants,
398 * as there are not sufficiently many free.
400 bool new_persistent_gnts;
401 grant_ref_t gref_head;
402 struct grant *gnt_list_entry = NULL;
403 struct scatterlist *sg;
406 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
409 max_grefs = req->nr_phys_segments;
410 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
412 * If we are using indirect segments we need to account
413 * for the indirect grefs used in the request.
415 max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
417 /* Check if we have enough grants to allocate a requests */
418 if (info->persistent_gnts_c < max_grefs) {
419 new_persistent_gnts = 1;
420 if (gnttab_alloc_grant_references(
421 max_grefs - info->persistent_gnts_c,
423 gnttab_request_free_callback(
425 blkif_restart_queue_callback,
431 new_persistent_gnts = 0;
433 /* Fill out a communications ring structure. */
434 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
435 id = get_id_from_freelist(info);
436 info->shadow[id].request = req;
438 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
439 ring_req->operation = BLKIF_OP_DISCARD;
440 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
441 ring_req->u.discard.id = id;
442 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
443 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
444 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
446 ring_req->u.discard.flag = 0;
448 BUG_ON(info->max_indirect_segments == 0 &&
449 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
450 BUG_ON(info->max_indirect_segments &&
451 req->nr_phys_segments > info->max_indirect_segments);
452 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
453 ring_req->u.rw.id = id;
454 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
456 * The indirect operation can only be a BLKIF_OP_READ or
459 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
460 ring_req->operation = BLKIF_OP_INDIRECT;
461 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
462 BLKIF_OP_WRITE : BLKIF_OP_READ;
463 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
464 ring_req->u.indirect.handle = info->handle;
465 ring_req->u.indirect.nr_segments = nseg;
467 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
468 ring_req->u.rw.handle = info->handle;
469 ring_req->operation = rq_data_dir(req) ?
470 BLKIF_OP_WRITE : BLKIF_OP_READ;
471 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
473 * Ideally we can do an unordered flush-to-disk. In case the
474 * backend onlysupports barriers, use that. A barrier request
475 * a superset of FUA, so we can implement it the same
476 * way. (It's also a FLUSH+FUA, since it is
477 * guaranteed ordered WRT previous writes.)
479 switch (info->feature_flush &
480 ((REQ_FLUSH|REQ_FUA))) {
481 case REQ_FLUSH|REQ_FUA:
482 ring_req->operation =
483 BLKIF_OP_WRITE_BARRIER;
486 ring_req->operation =
487 BLKIF_OP_FLUSH_DISKCACHE;
490 ring_req->operation = 0;
493 ring_req->u.rw.nr_segments = nseg;
495 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
496 fsect = sg->offset >> 9;
497 lsect = fsect + (sg->length >> 9) - 1;
499 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
500 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
501 unsigned long uninitialized_var(pfn);
504 kunmap_atomic(segments);
506 n = i / SEGS_PER_INDIRECT_FRAME;
507 if (!info->feature_persistent) {
508 struct page *indirect_page;
510 /* Fetch a pre-allocated page to use for indirect grefs */
511 BUG_ON(list_empty(&info->indirect_pages));
512 indirect_page = list_first_entry(&info->indirect_pages,
514 list_del(&indirect_page->lru);
515 pfn = page_to_pfn(indirect_page);
517 gnt_list_entry = get_grant(&gref_head, pfn, info);
518 info->shadow[id].indirect_grants[n] = gnt_list_entry;
519 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
520 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
523 gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
524 ref = gnt_list_entry->gref;
526 info->shadow[id].grants_used[i] = gnt_list_entry;
528 if (rq_data_dir(req) && info->feature_persistent) {
532 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
534 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
535 bvec_data = kmap_atomic(sg_page(sg));
538 * this does not wipe data stored outside the
539 * range sg->offset..sg->offset+sg->length.
540 * Therefore, blkback *could* see data from
541 * previous requests. This is OK as long as
542 * persistent grants are shared with just one
543 * domain. It may need refactoring if this
546 memcpy(shared_data + sg->offset,
547 bvec_data + sg->offset,
550 kunmap_atomic(bvec_data);
551 kunmap_atomic(shared_data);
553 if (ring_req->operation != BLKIF_OP_INDIRECT) {
554 ring_req->u.rw.seg[i] =
555 (struct blkif_request_segment) {
558 .last_sect = lsect };
560 n = i % SEGS_PER_INDIRECT_FRAME;
562 (struct blkif_request_segment) {
565 .last_sect = lsect };
569 kunmap_atomic(segments);
572 info->ring.req_prod_pvt++;
574 /* Keep a private copy so we can reissue requests when recovering. */
575 info->shadow[id].req = *ring_req;
577 if (new_persistent_gnts)
578 gnttab_free_grant_references(gref_head);
584 static inline void flush_requests(struct blkfront_info *info)
588 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
591 notify_remote_via_irq(info->irq);
594 static inline bool blkif_request_flush_invalid(struct request *req,
595 struct blkfront_info *info)
597 return ((req->cmd_type != REQ_TYPE_FS) ||
598 ((req->cmd_flags & REQ_FLUSH) &&
599 !(info->feature_flush & REQ_FLUSH)) ||
600 ((req->cmd_flags & REQ_FUA) &&
601 !(info->feature_flush & REQ_FUA)));
606 * read a block; request is in a request queue
608 static void do_blkif_request(struct request_queue *rq)
610 struct blkfront_info *info = NULL;
614 pr_debug("Entered do_blkif_request\n");
618 while ((req = blk_peek_request(rq)) != NULL) {
619 info = req->rq_disk->private_data;
621 if (RING_FULL(&info->ring))
624 blk_start_request(req);
626 if (blkif_request_flush_invalid(req, info)) {
627 __blk_end_request_all(req, -EOPNOTSUPP);
631 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
633 req, req->cmd, (unsigned long)blk_rq_pos(req),
634 blk_rq_cur_sectors(req), blk_rq_sectors(req),
635 rq_data_dir(req) ? "write" : "read");
637 if (blkif_queue_request(req)) {
638 blk_requeue_request(rq, req);
640 /* Avoid pointless unplugs. */
649 flush_requests(info);
652 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
653 unsigned int physical_sector_size,
654 unsigned int segments)
656 struct request_queue *rq;
657 struct blkfront_info *info = gd->private_data;
659 rq = blk_init_queue(do_blkif_request, &info->io_lock);
663 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
665 if (info->feature_discard) {
666 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
667 blk_queue_max_discard_sectors(rq, get_capacity(gd));
668 rq->limits.discard_granularity = info->discard_granularity;
669 rq->limits.discard_alignment = info->discard_alignment;
670 if (info->feature_secdiscard)
671 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
674 /* Hard sector size and max sectors impersonate the equiv. hardware. */
675 blk_queue_logical_block_size(rq, sector_size);
676 blk_queue_physical_block_size(rq, physical_sector_size);
677 blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
679 /* Each segment in a request is up to an aligned page in size. */
680 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
681 blk_queue_max_segment_size(rq, PAGE_SIZE);
683 /* Ensure a merged request will fit in a single I/O ring slot. */
684 blk_queue_max_segments(rq, segments);
686 /* Make sure buffer addresses are sector-aligned. */
687 blk_queue_dma_alignment(rq, 511);
689 /* Make sure we don't use bounce buffers. */
690 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
697 static const char *flush_info(unsigned int feature_flush)
699 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
700 case REQ_FLUSH|REQ_FUA:
701 return "barrier: enabled;";
703 return "flush diskcache: enabled;";
705 return "barrier or flush: disabled;";
709 static void xlvbd_flush(struct blkfront_info *info)
711 blk_queue_flush(info->rq, info->feature_flush);
712 pr_info("blkfront: %s: %s %s %s %s %s\n",
713 info->gd->disk_name, flush_info(info->feature_flush),
714 "persistent grants:", info->feature_persistent ?
715 "enabled;" : "disabled;", "indirect descriptors:",
716 info->max_indirect_segments ? "enabled;" : "disabled;");
719 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
722 major = BLKIF_MAJOR(vdevice);
723 *minor = BLKIF_MINOR(vdevice);
726 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
727 *minor = ((*minor / 64) * PARTS_PER_DISK) +
728 EMULATED_HD_DISK_MINOR_OFFSET;
731 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
732 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
733 EMULATED_HD_DISK_MINOR_OFFSET;
735 case XEN_SCSI_DISK0_MAJOR:
736 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
737 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
739 case XEN_SCSI_DISK1_MAJOR:
740 case XEN_SCSI_DISK2_MAJOR:
741 case XEN_SCSI_DISK3_MAJOR:
742 case XEN_SCSI_DISK4_MAJOR:
743 case XEN_SCSI_DISK5_MAJOR:
744 case XEN_SCSI_DISK6_MAJOR:
745 case XEN_SCSI_DISK7_MAJOR:
746 *offset = (*minor / PARTS_PER_DISK) +
747 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
748 EMULATED_SD_DISK_NAME_OFFSET;
750 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
751 EMULATED_SD_DISK_MINOR_OFFSET;
753 case XEN_SCSI_DISK8_MAJOR:
754 case XEN_SCSI_DISK9_MAJOR:
755 case XEN_SCSI_DISK10_MAJOR:
756 case XEN_SCSI_DISK11_MAJOR:
757 case XEN_SCSI_DISK12_MAJOR:
758 case XEN_SCSI_DISK13_MAJOR:
759 case XEN_SCSI_DISK14_MAJOR:
760 case XEN_SCSI_DISK15_MAJOR:
761 *offset = (*minor / PARTS_PER_DISK) +
762 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
763 EMULATED_SD_DISK_NAME_OFFSET;
765 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
766 EMULATED_SD_DISK_MINOR_OFFSET;
769 *offset = *minor / PARTS_PER_DISK;
772 printk(KERN_WARNING "blkfront: your disk configuration is "
773 "incorrect, please use an xvd device instead\n");
779 static char *encode_disk_name(char *ptr, unsigned int n)
782 ptr = encode_disk_name(ptr, n / 26 - 1);
787 static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
788 struct blkfront_info *info,
789 u16 vdisk_info, u16 sector_size,
790 unsigned int physical_sector_size)
800 BUG_ON(info->gd != NULL);
801 BUG_ON(info->rq != NULL);
803 if ((info->vdevice>>EXT_SHIFT) > 1) {
804 /* this is above the extended range; something is wrong */
805 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
809 if (!VDEV_IS_EXTENDED(info->vdevice)) {
810 err = xen_translate_vdev(info->vdevice, &minor, &offset);
813 nr_parts = PARTS_PER_DISK;
815 minor = BLKIF_MINOR_EXT(info->vdevice);
816 nr_parts = PARTS_PER_EXT_DISK;
817 offset = minor / nr_parts;
818 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
819 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
820 "emulated IDE disks,\n\t choose an xvd device name"
821 "from xvde on\n", info->vdevice);
823 if (minor >> MINORBITS) {
824 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
825 info->vdevice, minor);
829 if ((minor % nr_parts) == 0)
830 nr_minors = nr_parts;
832 err = xlbd_reserve_minors(minor, nr_minors);
837 gd = alloc_disk(nr_minors);
841 strcpy(gd->disk_name, DEV_NAME);
842 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
843 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
847 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
848 "%d", minor & (nr_parts - 1));
850 gd->major = XENVBD_MAJOR;
851 gd->first_minor = minor;
852 gd->fops = &xlvbd_block_fops;
853 gd->private_data = info;
854 gd->driverfs_dev = &(info->xbdev->dev);
855 set_capacity(gd, capacity);
857 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
858 info->max_indirect_segments ? :
859 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
864 info->rq = gd->queue;
869 if (vdisk_info & VDISK_READONLY)
872 if (vdisk_info & VDISK_REMOVABLE)
873 gd->flags |= GENHD_FL_REMOVABLE;
875 if (vdisk_info & VDISK_CDROM)
876 gd->flags |= GENHD_FL_CD;
881 xlbd_release_minors(minor, nr_minors);
886 static void xlvbd_release_gendisk(struct blkfront_info *info)
888 unsigned int minor, nr_minors;
891 if (info->rq == NULL)
894 spin_lock_irqsave(&info->io_lock, flags);
896 /* No more blkif_request(). */
897 blk_stop_queue(info->rq);
899 /* No more gnttab callback work. */
900 gnttab_cancel_free_callback(&info->callback);
901 spin_unlock_irqrestore(&info->io_lock, flags);
903 /* Flush gnttab callback work. Must be done with no locks held. */
904 flush_work(&info->work);
906 del_gendisk(info->gd);
908 minor = info->gd->first_minor;
909 nr_minors = info->gd->minors;
910 xlbd_release_minors(minor, nr_minors);
912 blk_cleanup_queue(info->rq);
919 static void kick_pending_request_queues(struct blkfront_info *info)
921 if (!RING_FULL(&info->ring)) {
922 /* Re-enable calldowns. */
923 blk_start_queue(info->rq);
924 /* Kick things off immediately. */
925 do_blkif_request(info->rq);
929 static void blkif_restart_queue(struct work_struct *work)
931 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
933 spin_lock_irq(&info->io_lock);
934 if (info->connected == BLKIF_STATE_CONNECTED)
935 kick_pending_request_queues(info);
936 spin_unlock_irq(&info->io_lock);
939 static void blkif_free(struct blkfront_info *info, int suspend)
941 struct grant *persistent_gnt;
945 /* Prevent new requests being issued until we fix things up. */
946 spin_lock_irq(&info->io_lock);
947 info->connected = suspend ?
948 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
949 /* No more blkif_request(). */
951 blk_stop_queue(info->rq);
953 /* Remove all persistent grants */
954 if (!list_empty(&info->grants)) {
955 list_for_each_entry_safe(persistent_gnt, n,
956 &info->grants, node) {
957 list_del(&persistent_gnt->node);
958 if (persistent_gnt->gref != GRANT_INVALID_REF) {
959 gnttab_end_foreign_access(persistent_gnt->gref,
961 info->persistent_gnts_c--;
963 if (info->feature_persistent)
964 __free_page(pfn_to_page(persistent_gnt->pfn));
965 kfree(persistent_gnt);
968 BUG_ON(info->persistent_gnts_c != 0);
971 * Remove indirect pages, this only happens when using indirect
972 * descriptors but not persistent grants
974 if (!list_empty(&info->indirect_pages)) {
975 struct page *indirect_page, *n;
977 BUG_ON(info->feature_persistent);
978 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
979 list_del(&indirect_page->lru);
980 __free_page(indirect_page);
984 for (i = 0; i < BLK_RING_SIZE; i++) {
986 * Clear persistent grants present in requests already
989 if (!info->shadow[i].request)
992 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
993 info->shadow[i].req.u.indirect.nr_segments :
994 info->shadow[i].req.u.rw.nr_segments;
995 for (j = 0; j < segs; j++) {
996 persistent_gnt = info->shadow[i].grants_used[j];
997 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
998 if (info->feature_persistent)
999 __free_page(pfn_to_page(persistent_gnt->pfn));
1000 kfree(persistent_gnt);
1003 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1005 * If this is not an indirect operation don't try to
1006 * free indirect segments
1010 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1011 persistent_gnt = info->shadow[i].indirect_grants[j];
1012 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1013 __free_page(pfn_to_page(persistent_gnt->pfn));
1014 kfree(persistent_gnt);
1018 kfree(info->shadow[i].grants_used);
1019 info->shadow[i].grants_used = NULL;
1020 kfree(info->shadow[i].indirect_grants);
1021 info->shadow[i].indirect_grants = NULL;
1022 kfree(info->shadow[i].sg);
1023 info->shadow[i].sg = NULL;
1026 /* No more gnttab callback work. */
1027 gnttab_cancel_free_callback(&info->callback);
1028 spin_unlock_irq(&info->io_lock);
1030 /* Flush gnttab callback work. Must be done with no locks held. */
1031 flush_work(&info->work);
1033 /* Free resources associated with old device channel. */
1034 if (info->ring_ref != GRANT_INVALID_REF) {
1035 gnttab_end_foreign_access(info->ring_ref, 0,
1036 (unsigned long)info->ring.sring);
1037 info->ring_ref = GRANT_INVALID_REF;
1038 info->ring.sring = NULL;
1041 unbind_from_irqhandler(info->irq, info);
1042 info->evtchn = info->irq = 0;
1046 static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1047 struct blkif_response *bret)
1050 struct scatterlist *sg;
1055 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
1056 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
1058 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
1059 for_each_sg(s->sg, sg, nseg, i) {
1060 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
1061 shared_data = kmap_atomic(
1062 pfn_to_page(s->grants_used[i]->pfn));
1063 bvec_data = kmap_atomic(sg_page(sg));
1064 memcpy(bvec_data + sg->offset,
1065 shared_data + sg->offset,
1067 kunmap_atomic(bvec_data);
1068 kunmap_atomic(shared_data);
1071 /* Add the persistent grant into the list of free grants */
1072 for (i = 0; i < nseg; i++) {
1073 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1075 * If the grant is still mapped by the backend (the
1076 * backend has chosen to make this grant persistent)
1077 * we add it at the head of the list, so it will be
1080 if (!info->feature_persistent)
1081 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1082 s->grants_used[i]->gref);
1083 list_add(&s->grants_used[i]->node, &info->grants);
1084 info->persistent_gnts_c++;
1087 * If the grant is not mapped by the backend we end the
1088 * foreign access and add it to the tail of the list,
1089 * so it will not be picked again unless we run out of
1090 * persistent grants.
1092 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1093 s->grants_used[i]->gref = GRANT_INVALID_REF;
1094 list_add_tail(&s->grants_used[i]->node, &info->grants);
1097 if (s->req.operation == BLKIF_OP_INDIRECT) {
1098 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
1099 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
1100 if (!info->feature_persistent)
1101 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1102 s->indirect_grants[i]->gref);
1103 list_add(&s->indirect_grants[i]->node, &info->grants);
1104 info->persistent_gnts_c++;
1106 struct page *indirect_page;
1108 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
1110 * Add the used indirect page back to the list of
1111 * available pages for indirect grefs.
1113 indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
1114 list_add(&indirect_page->lru, &info->indirect_pages);
1115 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
1116 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
1122 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1124 struct request *req;
1125 struct blkif_response *bret;
1127 unsigned long flags;
1128 struct blkfront_info *info = (struct blkfront_info *)dev_id;
1131 spin_lock_irqsave(&info->io_lock, flags);
1133 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
1134 spin_unlock_irqrestore(&info->io_lock, flags);
1139 rp = info->ring.sring->rsp_prod;
1140 rmb(); /* Ensure we see queued responses up to 'rp'. */
1142 for (i = info->ring.rsp_cons; i != rp; i++) {
1145 bret = RING_GET_RESPONSE(&info->ring, i);
1148 * The backend has messed up and given us an id that we would
1149 * never have given to it (we stamp it up to BLK_RING_SIZE -
1150 * look in get_id_from_freelist.
1152 if (id >= BLK_RING_SIZE) {
1153 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1154 info->gd->disk_name, op_name(bret->operation), id);
1155 /* We can't safely get the 'struct request' as
1156 * the id is busted. */
1159 req = info->shadow[id].request;
1161 if (bret->operation != BLKIF_OP_DISCARD)
1162 blkif_completion(&info->shadow[id], info, bret);
1164 if (add_id_to_freelist(info, id)) {
1165 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1166 info->gd->disk_name, op_name(bret->operation), id);
1170 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
1171 switch (bret->operation) {
1172 case BLKIF_OP_DISCARD:
1173 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1174 struct request_queue *rq = info->rq;
1175 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1176 info->gd->disk_name, op_name(bret->operation));
1177 error = -EOPNOTSUPP;
1178 info->feature_discard = 0;
1179 info->feature_secdiscard = 0;
1180 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1181 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
1183 __blk_end_request_all(req, error);
1185 case BLKIF_OP_FLUSH_DISKCACHE:
1186 case BLKIF_OP_WRITE_BARRIER:
1187 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1188 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1189 info->gd->disk_name, op_name(bret->operation));
1190 error = -EOPNOTSUPP;
1192 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
1193 info->shadow[id].req.u.rw.nr_segments == 0)) {
1194 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1195 info->gd->disk_name, op_name(bret->operation));
1196 error = -EOPNOTSUPP;
1198 if (unlikely(error)) {
1199 if (error == -EOPNOTSUPP)
1201 info->feature_flush = 0;
1206 case BLKIF_OP_WRITE:
1207 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1208 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1209 "request: %x\n", bret->status);
1211 __blk_end_request_all(req, error);
1218 info->ring.rsp_cons = i;
1220 if (i != info->ring.req_prod_pvt) {
1222 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1226 info->ring.sring->rsp_event = i + 1;
1228 kick_pending_request_queues(info);
1230 spin_unlock_irqrestore(&info->io_lock, flags);
1236 static int setup_blkring(struct xenbus_device *dev,
1237 struct blkfront_info *info)
1239 struct blkif_sring *sring;
1243 info->ring_ref = GRANT_INVALID_REF;
1245 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
1247 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1250 SHARED_RING_INIT(sring);
1251 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1253 err = xenbus_grant_ring(dev, info->ring.sring, 1, &gref);
1255 free_page((unsigned long)sring);
1256 info->ring.sring = NULL;
1259 info->ring_ref = gref;
1261 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1265 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1268 xenbus_dev_fatal(dev, err,
1269 "bind_evtchn_to_irqhandler failed");
1276 blkif_free(info, 0);
1281 /* Common code used when first setting up, and when resuming. */
1282 static int talk_to_blkback(struct xenbus_device *dev,
1283 struct blkfront_info *info)
1285 const char *message = NULL;
1286 struct xenbus_transaction xbt;
1289 /* Create shared ring, alloc event channel. */
1290 err = setup_blkring(dev, info);
1295 err = xenbus_transaction_start(&xbt);
1297 xenbus_dev_fatal(dev, err, "starting transaction");
1298 goto destroy_blkring;
1301 err = xenbus_printf(xbt, dev->nodename,
1302 "ring-ref", "%u", info->ring_ref);
1304 message = "writing ring-ref";
1305 goto abort_transaction;
1307 err = xenbus_printf(xbt, dev->nodename,
1308 "event-channel", "%u", info->evtchn);
1310 message = "writing event-channel";
1311 goto abort_transaction;
1313 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1314 XEN_IO_PROTO_ABI_NATIVE);
1316 message = "writing protocol";
1317 goto abort_transaction;
1319 err = xenbus_printf(xbt, dev->nodename,
1320 "feature-persistent", "%u", 1);
1323 "writing persistent grants feature to xenbus");
1325 err = xenbus_transaction_end(xbt, 0);
1329 xenbus_dev_fatal(dev, err, "completing transaction");
1330 goto destroy_blkring;
1333 xenbus_switch_state(dev, XenbusStateInitialised);
1338 xenbus_transaction_end(xbt, 1);
1340 xenbus_dev_fatal(dev, err, "%s", message);
1342 blkif_free(info, 0);
1348 * Entry point to this code when a new device is created. Allocate the basic
1349 * structures and the ring buffer for communication with the backend, and
1350 * inform the backend of the appropriate details for those. Switch to
1351 * Initialised state.
1353 static int blkfront_probe(struct xenbus_device *dev,
1354 const struct xenbus_device_id *id)
1356 int err, vdevice, i;
1357 struct blkfront_info *info;
1359 /* FIXME: Use dynamic device id if this is not set. */
1360 err = xenbus_scanf(XBT_NIL, dev->nodename,
1361 "virtual-device", "%i", &vdevice);
1363 /* go looking in the extended area instead */
1364 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1367 xenbus_dev_fatal(dev, err, "reading virtual-device");
1372 if (xen_hvm_domain()) {
1375 /* no unplug has been done: do not hook devices != xen vbds */
1376 if (xen_has_pv_and_legacy_disk_devices()) {
1379 if (!VDEV_IS_EXTENDED(vdevice))
1380 major = BLKIF_MAJOR(vdevice);
1382 major = XENVBD_MAJOR;
1384 if (major != XENVBD_MAJOR) {
1386 "%s: HVM does not support vbd %d as xen block device\n",
1391 /* do not create a PV cdrom device if we are an HVM guest */
1392 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1395 if (strncmp(type, "cdrom", 5) == 0) {
1401 info = kzalloc(sizeof(*info), GFP_KERNEL);
1403 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1407 mutex_init(&info->mutex);
1408 spin_lock_init(&info->io_lock);
1410 info->vdevice = vdevice;
1411 INIT_LIST_HEAD(&info->grants);
1412 INIT_LIST_HEAD(&info->indirect_pages);
1413 info->persistent_gnts_c = 0;
1414 info->connected = BLKIF_STATE_DISCONNECTED;
1415 INIT_WORK(&info->work, blkif_restart_queue);
1417 for (i = 0; i < BLK_RING_SIZE; i++)
1418 info->shadow[i].req.u.rw.id = i+1;
1419 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
1421 /* Front end dir is a number, which is used as the id. */
1422 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
1423 dev_set_drvdata(&dev->dev, info);
1425 err = talk_to_blkback(dev, info);
1428 dev_set_drvdata(&dev->dev, NULL);
1435 static void split_bio_end(struct bio *bio, int error)
1437 struct split_bio *split_bio = bio->bi_private;
1440 split_bio->err = error;
1442 if (atomic_dec_and_test(&split_bio->pending)) {
1443 split_bio->bio->bi_phys_segments = 0;
1444 bio_endio(split_bio->bio, split_bio->err);
1450 static int blkif_recover(struct blkfront_info *info)
1453 struct request *req, *n;
1454 struct blk_shadow *copy;
1456 struct bio *bio, *cloned_bio;
1457 struct bio_list bio_list, merge_bio;
1458 unsigned int segs, offset;
1460 struct split_bio *split_bio;
1461 struct list_head requests;
1463 /* Stage 1: Make a safe copy of the shadow state. */
1464 copy = kmemdup(info->shadow, sizeof(info->shadow),
1465 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
1469 /* Stage 2: Set up free list. */
1470 memset(&info->shadow, 0, sizeof(info->shadow));
1471 for (i = 0; i < BLK_RING_SIZE; i++)
1472 info->shadow[i].req.u.rw.id = i+1;
1473 info->shadow_free = info->ring.req_prod_pvt;
1474 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
1476 rc = blkfront_setup_indirect(info);
1482 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1483 blk_queue_max_segments(info->rq, segs);
1484 bio_list_init(&bio_list);
1485 INIT_LIST_HEAD(&requests);
1486 for (i = 0; i < BLK_RING_SIZE; i++) {
1488 if (!copy[i].request)
1492 * Get the bios in the request so we can re-queue them.
1494 if (copy[i].request->cmd_flags &
1495 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1497 * Flush operations don't contain bios, so
1498 * we need to requeue the whole request
1500 list_add(©[i].request->queuelist, &requests);
1503 merge_bio.head = copy[i].request->bio;
1504 merge_bio.tail = copy[i].request->biotail;
1505 bio_list_merge(&bio_list, &merge_bio);
1506 copy[i].request->bio = NULL;
1507 blk_end_request_all(copy[i].request, 0);
1513 * Empty the queue, this is important because we might have
1514 * requests in the queue with more segments than what we
1517 spin_lock_irq(&info->io_lock);
1518 while ((req = blk_fetch_request(info->rq)) != NULL) {
1519 if (req->cmd_flags &
1520 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1521 list_add(&req->queuelist, &requests);
1524 merge_bio.head = req->bio;
1525 merge_bio.tail = req->biotail;
1526 bio_list_merge(&bio_list, &merge_bio);
1528 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1529 pr_alert("diskcache flush request found!\n");
1530 __blk_end_request_all(req, 0);
1532 spin_unlock_irq(&info->io_lock);
1534 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1536 spin_lock_irq(&info->io_lock);
1538 /* Now safe for us to use the shared ring */
1539 info->connected = BLKIF_STATE_CONNECTED;
1541 /* Kick any other new requests queued since we resumed */
1542 kick_pending_request_queues(info);
1544 list_for_each_entry_safe(req, n, &requests, queuelist) {
1545 /* Requeue pending requests (flush or discard) */
1546 list_del_init(&req->queuelist);
1547 BUG_ON(req->nr_phys_segments > segs);
1548 blk_requeue_request(info->rq, req);
1550 spin_unlock_irq(&info->io_lock);
1552 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1553 /* Traverse the list of pending bios and re-queue them */
1554 if (bio_segments(bio) > segs) {
1556 * This bio has more segments than what we can
1557 * handle, we have to split it.
1559 pending = (bio_segments(bio) + segs - 1) / segs;
1560 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1561 BUG_ON(split_bio == NULL);
1562 atomic_set(&split_bio->pending, pending);
1563 split_bio->bio = bio;
1564 for (i = 0; i < pending; i++) {
1565 offset = (i * segs * PAGE_SIZE) >> 9;
1566 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
1567 (unsigned int)bio_sectors(bio) - offset);
1568 cloned_bio = bio_clone(bio, GFP_NOIO);
1569 BUG_ON(cloned_bio == NULL);
1570 bio_trim(cloned_bio, offset, size);
1571 cloned_bio->bi_private = split_bio;
1572 cloned_bio->bi_end_io = split_bio_end;
1573 submit_bio(cloned_bio->bi_rw, cloned_bio);
1576 * Now we have to wait for all those smaller bios to
1577 * end, so we can also end the "parent" bio.
1581 /* We don't need to split this bio */
1582 submit_bio(bio->bi_rw, bio);
1589 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1590 * driver restart. We tear down our blkif structure and recreate it, but
1591 * leave the device-layer structures intact so that this is transparent to the
1592 * rest of the kernel.
1594 static int blkfront_resume(struct xenbus_device *dev)
1596 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1599 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1601 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1603 err = talk_to_blkback(dev, info);
1606 * We have to wait for the backend to switch to
1607 * connected state, since we want to read which
1608 * features it supports.
1615 blkfront_closing(struct blkfront_info *info)
1617 struct xenbus_device *xbdev = info->xbdev;
1618 struct block_device *bdev = NULL;
1620 mutex_lock(&info->mutex);
1622 if (xbdev->state == XenbusStateClosing) {
1623 mutex_unlock(&info->mutex);
1628 bdev = bdget_disk(info->gd, 0);
1630 mutex_unlock(&info->mutex);
1633 xenbus_frontend_closed(xbdev);
1637 mutex_lock(&bdev->bd_mutex);
1639 if (bdev->bd_openers) {
1640 xenbus_dev_error(xbdev, -EBUSY,
1641 "Device in use; refusing to close");
1642 xenbus_switch_state(xbdev, XenbusStateClosing);
1644 xlvbd_release_gendisk(info);
1645 xenbus_frontend_closed(xbdev);
1648 mutex_unlock(&bdev->bd_mutex);
1652 static void blkfront_setup_discard(struct blkfront_info *info)
1655 unsigned int discard_granularity;
1656 unsigned int discard_alignment;
1657 unsigned int discard_secure;
1659 info->feature_discard = 1;
1660 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1661 "discard-granularity", "%u", &discard_granularity,
1662 "discard-alignment", "%u", &discard_alignment,
1665 info->discard_granularity = discard_granularity;
1666 info->discard_alignment = discard_alignment;
1668 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1669 "discard-secure", "%d", &discard_secure,
1672 info->feature_secdiscard = !!discard_secure;
1675 static int blkfront_setup_indirect(struct blkfront_info *info)
1677 unsigned int indirect_segments, segs;
1680 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1681 "feature-max-indirect-segments", "%u", &indirect_segments,
1684 info->max_indirect_segments = 0;
1685 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1687 info->max_indirect_segments = min(indirect_segments,
1688 xen_blkif_max_segments);
1689 segs = info->max_indirect_segments;
1692 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE);
1696 if (!info->feature_persistent && info->max_indirect_segments) {
1698 * We are using indirect descriptors but not persistent
1699 * grants, we need to allocate a set of pages that can be
1700 * used for mapping indirect grefs
1702 int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE;
1704 BUG_ON(!list_empty(&info->indirect_pages));
1705 for (i = 0; i < num; i++) {
1706 struct page *indirect_page = alloc_page(GFP_NOIO);
1709 list_add(&indirect_page->lru, &info->indirect_pages);
1713 for (i = 0; i < BLK_RING_SIZE; i++) {
1714 info->shadow[i].grants_used = kzalloc(
1715 sizeof(info->shadow[i].grants_used[0]) * segs,
1717 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
1718 if (info->max_indirect_segments)
1719 info->shadow[i].indirect_grants = kzalloc(
1720 sizeof(info->shadow[i].indirect_grants[0]) *
1721 INDIRECT_GREFS(segs),
1723 if ((info->shadow[i].grants_used == NULL) ||
1724 (info->shadow[i].sg == NULL) ||
1725 (info->max_indirect_segments &&
1726 (info->shadow[i].indirect_grants == NULL)))
1728 sg_init_table(info->shadow[i].sg, segs);
1735 for (i = 0; i < BLK_RING_SIZE; i++) {
1736 kfree(info->shadow[i].grants_used);
1737 info->shadow[i].grants_used = NULL;
1738 kfree(info->shadow[i].sg);
1739 info->shadow[i].sg = NULL;
1740 kfree(info->shadow[i].indirect_grants);
1741 info->shadow[i].indirect_grants = NULL;
1743 if (!list_empty(&info->indirect_pages)) {
1744 struct page *indirect_page, *n;
1745 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1746 list_del(&indirect_page->lru);
1747 __free_page(indirect_page);
1754 * Invoked when the backend is finally 'ready' (and has told produced
1755 * the details about the physical device - #sectors, size, etc).
1757 static void blkfront_connect(struct blkfront_info *info)
1759 unsigned long long sectors;
1760 unsigned long sector_size;
1761 unsigned int physical_sector_size;
1764 int barrier, flush, discard, persistent;
1766 switch (info->connected) {
1767 case BLKIF_STATE_CONNECTED:
1769 * Potentially, the back-end may be signalling
1770 * a capacity change; update the capacity.
1772 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1773 "sectors", "%Lu", §ors);
1774 if (XENBUS_EXIST_ERR(err))
1776 printk(KERN_INFO "Setting capacity to %Lu\n",
1778 set_capacity(info->gd, sectors);
1779 revalidate_disk(info->gd);
1782 case BLKIF_STATE_SUSPENDED:
1784 * If we are recovering from suspension, we need to wait
1785 * for the backend to announce it's features before
1786 * reconnecting, at least we need to know if the backend
1787 * supports indirect descriptors, and how many.
1789 blkif_recover(info);
1796 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1797 __func__, info->xbdev->otherend);
1799 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1800 "sectors", "%llu", §ors,
1801 "info", "%u", &binfo,
1802 "sector-size", "%lu", §or_size,
1805 xenbus_dev_fatal(info->xbdev, err,
1806 "reading backend fields at %s",
1807 info->xbdev->otherend);
1812 * physcial-sector-size is a newer field, so old backends may not
1813 * provide this. Assume physical sector size to be the same as
1814 * sector_size in that case.
1816 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1817 "physical-sector-size", "%u", &physical_sector_size);
1819 physical_sector_size = sector_size;
1821 info->feature_flush = 0;
1823 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1824 "feature-barrier", "%d", &barrier,
1828 * If there's no "feature-barrier" defined, then it means
1829 * we're dealing with a very old backend which writes
1830 * synchronously; nothing to do.
1832 * If there are barriers, then we use flush.
1834 if (!err && barrier)
1835 info->feature_flush = REQ_FLUSH | REQ_FUA;
1837 * And if there is "feature-flush-cache" use that above
1840 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1841 "feature-flush-cache", "%d", &flush,
1845 info->feature_flush = REQ_FLUSH;
1847 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1848 "feature-discard", "%d", &discard,
1851 if (!err && discard)
1852 blkfront_setup_discard(info);
1854 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1855 "feature-persistent", "%u", &persistent,
1858 info->feature_persistent = 0;
1860 info->feature_persistent = persistent;
1862 err = blkfront_setup_indirect(info);
1864 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1865 info->xbdev->otherend);
1869 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
1870 physical_sector_size);
1872 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1873 info->xbdev->otherend);
1877 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1879 /* Kick pending requests. */
1880 spin_lock_irq(&info->io_lock);
1881 info->connected = BLKIF_STATE_CONNECTED;
1882 kick_pending_request_queues(info);
1883 spin_unlock_irq(&info->io_lock);
1891 * Callback received when the backend's state changes.
1893 static void blkback_changed(struct xenbus_device *dev,
1894 enum xenbus_state backend_state)
1896 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1898 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
1900 switch (backend_state) {
1901 case XenbusStateInitialising:
1902 case XenbusStateInitWait:
1903 case XenbusStateInitialised:
1904 case XenbusStateReconfiguring:
1905 case XenbusStateReconfigured:
1906 case XenbusStateUnknown:
1909 case XenbusStateConnected:
1910 blkfront_connect(info);
1913 case XenbusStateClosed:
1914 if (dev->state == XenbusStateClosed)
1916 /* Missed the backend's Closing state -- fallthrough */
1917 case XenbusStateClosing:
1918 blkfront_closing(info);
1923 static int blkfront_remove(struct xenbus_device *xbdev)
1925 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1926 struct block_device *bdev = NULL;
1927 struct gendisk *disk;
1929 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
1931 blkif_free(info, 0);
1933 mutex_lock(&info->mutex);
1937 bdev = bdget_disk(disk, 0);
1940 mutex_unlock(&info->mutex);
1948 * The xbdev was removed before we reached the Closed
1949 * state. See if it's safe to remove the disk. If the bdev
1950 * isn't closed yet, we let release take care of it.
1953 mutex_lock(&bdev->bd_mutex);
1954 info = disk->private_data;
1956 dev_warn(disk_to_dev(disk),
1957 "%s was hot-unplugged, %d stale handles\n",
1958 xbdev->nodename, bdev->bd_openers);
1960 if (info && !bdev->bd_openers) {
1961 xlvbd_release_gendisk(info);
1962 disk->private_data = NULL;
1966 mutex_unlock(&bdev->bd_mutex);
1972 static int blkfront_is_ready(struct xenbus_device *dev)
1974 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1976 return info->is_ready && info->xbdev;
1979 static int blkif_open(struct block_device *bdev, fmode_t mode)
1981 struct gendisk *disk = bdev->bd_disk;
1982 struct blkfront_info *info;
1985 mutex_lock(&blkfront_mutex);
1987 info = disk->private_data;
1994 mutex_lock(&info->mutex);
1997 /* xbdev is closed */
2000 mutex_unlock(&info->mutex);
2003 mutex_unlock(&blkfront_mutex);
2007 static void blkif_release(struct gendisk *disk, fmode_t mode)
2009 struct blkfront_info *info = disk->private_data;
2010 struct block_device *bdev;
2011 struct xenbus_device *xbdev;
2013 mutex_lock(&blkfront_mutex);
2015 bdev = bdget_disk(disk, 0);
2018 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2021 if (bdev->bd_openers)
2025 * Check if we have been instructed to close. We will have
2026 * deferred this request, because the bdev was still open.
2029 mutex_lock(&info->mutex);
2030 xbdev = info->xbdev;
2032 if (xbdev && xbdev->state == XenbusStateClosing) {
2033 /* pending switch to state closed */
2034 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2035 xlvbd_release_gendisk(info);
2036 xenbus_frontend_closed(info->xbdev);
2039 mutex_unlock(&info->mutex);
2042 /* sudden device removal */
2043 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
2044 xlvbd_release_gendisk(info);
2045 disk->private_data = NULL;
2052 mutex_unlock(&blkfront_mutex);
2055 static const struct block_device_operations xlvbd_block_fops =
2057 .owner = THIS_MODULE,
2059 .release = blkif_release,
2060 .getgeo = blkif_getgeo,
2061 .ioctl = blkif_ioctl,
2065 static const struct xenbus_device_id blkfront_ids[] = {
2070 static struct xenbus_driver blkfront_driver = {
2071 .ids = blkfront_ids,
2072 .probe = blkfront_probe,
2073 .remove = blkfront_remove,
2074 .resume = blkfront_resume,
2075 .otherend_changed = blkback_changed,
2076 .is_ready = blkfront_is_ready,
2079 static int __init xlblk_init(void)
2086 if (!xen_has_pv_disk_devices())
2089 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2090 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2091 XENVBD_MAJOR, DEV_NAME);
2095 ret = xenbus_register_frontend(&blkfront_driver);
2097 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2103 module_init(xlblk_init);
2106 static void __exit xlblk_exit(void)
2108 xenbus_unregister_driver(&blkfront_driver);
2109 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2112 module_exit(xlblk_exit);
2114 MODULE_DESCRIPTION("Xen virtual block device frontend");
2115 MODULE_LICENSE("GPL");
2116 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
2117 MODULE_ALIAS("xen:vbd");
2118 MODULE_ALIAS("xenblk");