Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * blkfront.c |
| 3 | * |
| 4 | * XenLinux virtual block device driver. |
| 5 | * |
| 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 |
| 12 | * |
| 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: |
| 18 | * |
| 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: |
| 25 | * |
| 26 | * The above copyright notice and this permission notice shall be included in |
| 27 | * all copies or substantial portions of the Software. |
| 28 | * |
| 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 |
| 35 | * IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/blkdev.h> |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 40 | #include <linux/hdreg.h> |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 41 | #include <linux/cdrom.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 42 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 44 | #include <linux/mutex.h> |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 45 | #include <linux/scatterlist.h> |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 46 | #include <linux/bitmap.h> |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 47 | #include <linux/llist.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 48 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 49 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 50 | #include <xen/xenbus.h> |
| 51 | #include <xen/grant_table.h> |
| 52 | #include <xen/events.h> |
| 53 | #include <xen/page.h> |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 54 | #include <xen/platform_pci.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 55 | |
| 56 | #include <xen/interface/grant_table.h> |
| 57 | #include <xen/interface/io/blkif.h> |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 58 | #include <xen/interface/io/protocols.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 59 | |
| 60 | #include <asm/xen/hypervisor.h> |
| 61 | |
| 62 | enum blkif_state { |
| 63 | BLKIF_STATE_DISCONNECTED, |
| 64 | BLKIF_STATE_CONNECTED, |
| 65 | BLKIF_STATE_SUSPENDED, |
| 66 | }; |
| 67 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 68 | struct grant { |
| 69 | grant_ref_t gref; |
| 70 | unsigned long pfn; |
| 71 | struct llist_node node; |
| 72 | }; |
| 73 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 74 | struct blk_shadow { |
| 75 | struct blkif_request req; |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 76 | struct request *request; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 77 | unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 78 | struct grant *grants_used[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 81 | static DEFINE_MUTEX(blkfront_mutex); |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 82 | static const struct block_device_operations xlvbd_block_fops; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 83 | |
Jeremy Fitzhardinge | 667c78af | 2010-12-08 12:39:12 -0800 | [diff] [blame] | 84 | #define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * We have one of these per vbd, whether ide, scsi or 'other'. They |
| 88 | * hang in private_data off the gendisk structure. We may end up |
| 89 | * putting all kinds of interesting stuff here :-) |
| 90 | */ |
| 91 | struct blkfront_info |
| 92 | { |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 93 | spinlock_t io_lock; |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 94 | struct mutex mutex; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 95 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 96 | struct gendisk *gd; |
| 97 | int vdevice; |
| 98 | blkif_vdev_t handle; |
| 99 | enum blkif_state connected; |
| 100 | int ring_ref; |
| 101 | struct blkif_front_ring ring; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 102 | struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 103 | unsigned int evtchn, irq; |
| 104 | struct request_queue *rq; |
| 105 | struct work_struct work; |
| 106 | struct gnttab_free_callback callback; |
| 107 | struct blk_shadow shadow[BLK_RING_SIZE]; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 108 | struct llist_head persistent_gnts; |
| 109 | unsigned int persistent_gnts_c; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 110 | unsigned long shadow_free; |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 111 | unsigned int feature_flush; |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 112 | unsigned int flush_op; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 113 | unsigned int feature_discard:1; |
| 114 | unsigned int feature_secdiscard:1; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 115 | unsigned int discard_granularity; |
| 116 | unsigned int discard_alignment; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 117 | unsigned int feature_persistent:1; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 118 | int is_ready; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 121 | static unsigned int nr_minors; |
| 122 | static unsigned long *minors; |
| 123 | static DEFINE_SPINLOCK(minor_lock); |
| 124 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 125 | #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ |
| 126 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) |
| 127 | #define GRANT_INVALID_REF 0 |
| 128 | |
| 129 | #define PARTS_PER_DISK 16 |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 130 | #define PARTS_PER_EXT_DISK 256 |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 131 | |
| 132 | #define BLKIF_MAJOR(dev) ((dev)>>8) |
| 133 | #define BLKIF_MINOR(dev) ((dev) & 0xff) |
| 134 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 135 | #define EXT_SHIFT 28 |
| 136 | #define EXTENDED (1<<EXT_SHIFT) |
| 137 | #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) |
| 138 | #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 139 | #define EMULATED_HD_DISK_MINOR_OFFSET (0) |
| 140 | #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256) |
Stefan Bader | 196cfe2 | 2011-07-14 15:30:22 +0200 | [diff] [blame] | 141 | #define EMULATED_SD_DISK_MINOR_OFFSET (0) |
| 142 | #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 143 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 144 | #define DEV_NAME "xvd" /* name in /dev */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 145 | |
| 146 | static int get_id_from_freelist(struct blkfront_info *info) |
| 147 | { |
| 148 | unsigned long free = info->shadow_free; |
Roel Kluin | b9ed725 | 2009-05-22 09:25:32 +0200 | [diff] [blame] | 149 | BUG_ON(free >= BLK_RING_SIZE); |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 150 | info->shadow_free = info->shadow[free].req.u.rw.id; |
| 151 | info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 152 | return free; |
| 153 | } |
| 154 | |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 155 | static int add_id_to_freelist(struct blkfront_info *info, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 156 | unsigned long id) |
| 157 | { |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 158 | if (info->shadow[id].req.u.rw.id != id) |
| 159 | return -EINVAL; |
| 160 | if (info->shadow[id].request == NULL) |
| 161 | return -EINVAL; |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 162 | info->shadow[id].req.u.rw.id = info->shadow_free; |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 163 | info->shadow[id].request = NULL; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 164 | info->shadow_free = id; |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 165 | return 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 168 | static const char *op_name(int op) |
| 169 | { |
| 170 | static const char *const names[] = { |
| 171 | [BLKIF_OP_READ] = "read", |
| 172 | [BLKIF_OP_WRITE] = "write", |
| 173 | [BLKIF_OP_WRITE_BARRIER] = "barrier", |
| 174 | [BLKIF_OP_FLUSH_DISKCACHE] = "flush", |
| 175 | [BLKIF_OP_DISCARD] = "discard" }; |
| 176 | |
| 177 | if (op < 0 || op >= ARRAY_SIZE(names)) |
| 178 | return "unknown"; |
| 179 | |
| 180 | if (!names[op]) |
| 181 | return "reserved"; |
| 182 | |
| 183 | return names[op]; |
| 184 | } |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 185 | static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) |
| 186 | { |
| 187 | unsigned int end = minor + nr; |
| 188 | int rc; |
| 189 | |
| 190 | if (end > nr_minors) { |
| 191 | unsigned long *bitmap, *old; |
| 192 | |
Thomas Meyer | f094148 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 193 | bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap), |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 194 | GFP_KERNEL); |
| 195 | if (bitmap == NULL) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | spin_lock(&minor_lock); |
| 199 | if (end > nr_minors) { |
| 200 | old = minors; |
| 201 | memcpy(bitmap, minors, |
| 202 | BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); |
| 203 | minors = bitmap; |
| 204 | nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; |
| 205 | } else |
| 206 | old = bitmap; |
| 207 | spin_unlock(&minor_lock); |
| 208 | kfree(old); |
| 209 | } |
| 210 | |
| 211 | spin_lock(&minor_lock); |
| 212 | if (find_next_bit(minors, end, minor) >= end) { |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 213 | bitmap_set(minors, minor, nr); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 214 | rc = 0; |
| 215 | } else |
| 216 | rc = -EBUSY; |
| 217 | spin_unlock(&minor_lock); |
| 218 | |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | static void xlbd_release_minors(unsigned int minor, unsigned int nr) |
| 223 | { |
| 224 | unsigned int end = minor + nr; |
| 225 | |
| 226 | BUG_ON(end > nr_minors); |
| 227 | spin_lock(&minor_lock); |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 228 | bitmap_clear(minors, minor, nr); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 229 | spin_unlock(&minor_lock); |
| 230 | } |
| 231 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 232 | static void blkif_restart_queue_callback(void *arg) |
| 233 | { |
| 234 | struct blkfront_info *info = (struct blkfront_info *)arg; |
| 235 | schedule_work(&info->work); |
| 236 | } |
| 237 | |
Harvey Harrison | afe42d7 | 2008-04-29 00:59:47 -0700 | [diff] [blame] | 238 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 239 | { |
| 240 | /* We don't have real geometry info, but let's at least return |
| 241 | values consistent with the size of the device */ |
| 242 | sector_t nsect = get_capacity(bd->bd_disk); |
| 243 | sector_t cylinders = nsect; |
| 244 | |
| 245 | hg->heads = 0xff; |
| 246 | hg->sectors = 0x3f; |
| 247 | sector_div(cylinders, hg->heads * hg->sectors); |
| 248 | hg->cylinders = cylinders; |
| 249 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) |
| 250 | hg->cylinders = 0xffff; |
| 251 | return 0; |
| 252 | } |
| 253 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 254 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
Adrian Bunk | 62aa005 | 2008-08-04 11:59:05 +0200 | [diff] [blame] | 255 | unsigned command, unsigned long argument) |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 256 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 257 | struct blkfront_info *info = bdev->bd_disk->private_data; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 258 | int i; |
| 259 | |
| 260 | dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n", |
| 261 | command, (long)argument); |
| 262 | |
| 263 | switch (command) { |
| 264 | case CDROMMULTISESSION: |
| 265 | dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n"); |
| 266 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) |
| 267 | if (put_user(0, (char __user *)(argument + i))) |
| 268 | return -EFAULT; |
| 269 | return 0; |
| 270 | |
| 271 | case CDROM_GET_CAPABILITY: { |
| 272 | struct gendisk *gd = info->gd; |
| 273 | if (gd->flags & GENHD_FL_CD) |
| 274 | return 0; |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | |
| 278 | default: |
| 279 | /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", |
| 280 | command);*/ |
| 281 | return -EINVAL; /* same return as native Linux */ |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 287 | /* |
Jeremy Fitzhardinge | c64e38e | 2010-11-01 14:32:27 -0400 | [diff] [blame] | 288 | * Generate a Xen blkfront IO request from a blk layer request. Reads |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 289 | * and writes are handled as expected. |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 290 | * |
Jeremy Fitzhardinge | c64e38e | 2010-11-01 14:32:27 -0400 | [diff] [blame] | 291 | * @req: a request struct |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 292 | */ |
| 293 | static int blkif_queue_request(struct request *req) |
| 294 | { |
| 295 | struct blkfront_info *info = req->rq_disk->private_data; |
| 296 | unsigned long buffer_mfn; |
| 297 | struct blkif_request *ring_req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 298 | unsigned long id; |
| 299 | unsigned int fsect, lsect; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 300 | int i, ref; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Used to store if we are able to queue the request by just using |
| 304 | * existing persistent grants, or if we have to get new grants, |
| 305 | * as there are not sufficiently many free. |
| 306 | */ |
| 307 | bool new_persistent_gnts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 308 | grant_ref_t gref_head; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 309 | struct page *granted_page; |
| 310 | struct grant *gnt_list_entry = NULL; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 311 | struct scatterlist *sg; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 312 | |
| 313 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) |
| 314 | return 1; |
| 315 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 316 | /* Check if we have enought grants to allocate a requests */ |
| 317 | if (info->persistent_gnts_c < BLKIF_MAX_SEGMENTS_PER_REQUEST) { |
| 318 | new_persistent_gnts = 1; |
| 319 | if (gnttab_alloc_grant_references( |
| 320 | BLKIF_MAX_SEGMENTS_PER_REQUEST - info->persistent_gnts_c, |
| 321 | &gref_head) < 0) { |
| 322 | gnttab_request_free_callback( |
| 323 | &info->callback, |
| 324 | blkif_restart_queue_callback, |
| 325 | info, |
| 326 | BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 327 | return 1; |
| 328 | } |
| 329 | } else |
| 330 | new_persistent_gnts = 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 331 | |
| 332 | /* Fill out a communications ring structure. */ |
| 333 | ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); |
| 334 | id = get_id_from_freelist(info); |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 335 | info->shadow[id].request = req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 336 | |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 337 | ring_req->u.rw.id = id; |
Owen Smith | 51de695 | 2010-12-22 15:05:00 +0000 | [diff] [blame] | 338 | ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 339 | ring_req->u.rw.handle = info->handle; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 340 | |
| 341 | ring_req->operation = rq_data_dir(req) ? |
| 342 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
Jeremy Fitzhardinge | be2f837 | 2010-11-02 10:38:33 -0400 | [diff] [blame] | 343 | |
| 344 | if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) { |
| 345 | /* |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 346 | * Ideally we can do an unordered flush-to-disk. In case the |
| 347 | * backend onlysupports barriers, use that. A barrier request |
Jeremy Fitzhardinge | be2f837 | 2010-11-02 10:38:33 -0400 | [diff] [blame] | 348 | * a superset of FUA, so we can implement it the same |
| 349 | * way. (It's also a FLUSH+FUA, since it is |
| 350 | * guaranteed ordered WRT previous writes.) |
| 351 | */ |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 352 | ring_req->operation = info->flush_op; |
Jeremy Fitzhardinge | be2f837 | 2010-11-02 10:38:33 -0400 | [diff] [blame] | 353 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 354 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 355 | if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) { |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 356 | /* id, sector_number and handle are set above. */ |
| 357 | ring_req->operation = BLKIF_OP_DISCARD; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 358 | ring_req->u.discard.nr_sectors = blk_rq_sectors(req); |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 359 | if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard) |
| 360 | ring_req->u.discard.flag = BLKIF_DISCARD_SECURE; |
| 361 | else |
| 362 | ring_req->u.discard.flag = 0; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 363 | } else { |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 364 | ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req, |
| 365 | info->sg); |
| 366 | BUG_ON(ring_req->u.rw.nr_segments > |
| 367 | BLKIF_MAX_SEGMENTS_PER_REQUEST); |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 368 | |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 369 | for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) { |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 370 | fsect = sg->offset >> 9; |
| 371 | lsect = fsect + (sg->length >> 9) - 1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 372 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 373 | if (info->persistent_gnts_c) { |
| 374 | BUG_ON(llist_empty(&info->persistent_gnts)); |
| 375 | gnt_list_entry = llist_entry( |
| 376 | llist_del_first(&info->persistent_gnts), |
| 377 | struct grant, node); |
| 378 | |
| 379 | ref = gnt_list_entry->gref; |
| 380 | buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn); |
| 381 | info->persistent_gnts_c--; |
| 382 | } else { |
| 383 | ref = gnttab_claim_grant_reference(&gref_head); |
| 384 | BUG_ON(ref == -ENOSPC); |
| 385 | |
| 386 | gnt_list_entry = |
| 387 | kmalloc(sizeof(struct grant), |
| 388 | GFP_ATOMIC); |
| 389 | if (!gnt_list_entry) |
| 390 | return -ENOMEM; |
| 391 | |
| 392 | granted_page = alloc_page(GFP_ATOMIC); |
| 393 | if (!granted_page) { |
| 394 | kfree(gnt_list_entry); |
| 395 | return -ENOMEM; |
| 396 | } |
| 397 | |
| 398 | gnt_list_entry->pfn = |
| 399 | page_to_pfn(granted_page); |
| 400 | gnt_list_entry->gref = ref; |
| 401 | |
| 402 | buffer_mfn = pfn_to_mfn(page_to_pfn( |
| 403 | granted_page)); |
| 404 | gnttab_grant_foreign_access_ref(ref, |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 405 | info->xbdev->otherend_id, |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 406 | buffer_mfn, 0); |
| 407 | } |
| 408 | |
| 409 | info->shadow[id].grants_used[i] = gnt_list_entry; |
| 410 | |
| 411 | if (rq_data_dir(req)) { |
| 412 | char *bvec_data; |
| 413 | void *shared_data; |
| 414 | |
| 415 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
| 416 | |
| 417 | shared_data = kmap_atomic( |
| 418 | pfn_to_page(gnt_list_entry->pfn)); |
| 419 | bvec_data = kmap_atomic(sg_page(sg)); |
| 420 | |
| 421 | /* |
| 422 | * this does not wipe data stored outside the |
| 423 | * range sg->offset..sg->offset+sg->length. |
| 424 | * Therefore, blkback *could* see data from |
| 425 | * previous requests. This is OK as long as |
| 426 | * persistent grants are shared with just one |
| 427 | * domain. It may need refactoring if this |
| 428 | * changes |
| 429 | */ |
| 430 | memcpy(shared_data + sg->offset, |
| 431 | bvec_data + sg->offset, |
| 432 | sg->length); |
| 433 | |
| 434 | kunmap_atomic(bvec_data); |
| 435 | kunmap_atomic(shared_data); |
| 436 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 437 | |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 438 | info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn); |
| 439 | ring_req->u.rw.seg[i] = |
| 440 | (struct blkif_request_segment) { |
| 441 | .gref = ref, |
| 442 | .first_sect = fsect, |
| 443 | .last_sect = lsect }; |
| 444 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | info->ring.req_prod_pvt++; |
| 448 | |
| 449 | /* Keep a private copy so we can reissue requests when recovering. */ |
| 450 | info->shadow[id].req = *ring_req; |
| 451 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 452 | if (new_persistent_gnts) |
| 453 | gnttab_free_grant_references(gref_head); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | static inline void flush_requests(struct blkfront_info *info) |
| 460 | { |
| 461 | int notify; |
| 462 | |
| 463 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); |
| 464 | |
| 465 | if (notify) |
| 466 | notify_remote_via_irq(info->irq); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * do_blkif_request |
| 471 | * read a block; request is in a request queue |
| 472 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 473 | static void do_blkif_request(struct request_queue *rq) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 474 | { |
| 475 | struct blkfront_info *info = NULL; |
| 476 | struct request *req; |
| 477 | int queued; |
| 478 | |
| 479 | pr_debug("Entered do_blkif_request\n"); |
| 480 | |
| 481 | queued = 0; |
| 482 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 483 | while ((req = blk_peek_request(rq)) != NULL) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 484 | info = req->rq_disk->private_data; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 485 | |
| 486 | if (RING_FULL(&info->ring)) |
| 487 | goto wait; |
| 488 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 489 | blk_start_request(req); |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 490 | |
Konrad Rzeszutek Wilk | d11e615 | 2011-09-16 15:15:14 -0400 | [diff] [blame] | 491 | if ((req->cmd_type != REQ_TYPE_FS) || |
| 492 | ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) && |
| 493 | !info->flush_op)) { |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 494 | __blk_end_request_all(req, -EIO); |
| 495 | continue; |
| 496 | } |
| 497 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 498 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 499 | "(%u/%u) buffer:%p [%s]\n", |
| 500 | req, req->cmd, (unsigned long)blk_rq_pos(req), |
| 501 | blk_rq_cur_sectors(req), blk_rq_sectors(req), |
| 502 | req->buffer, rq_data_dir(req) ? "write" : "read"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 503 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 504 | if (blkif_queue_request(req)) { |
| 505 | blk_requeue_request(rq, req); |
| 506 | wait: |
| 507 | /* Avoid pointless unplugs. */ |
| 508 | blk_stop_queue(rq); |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | queued++; |
| 513 | } |
| 514 | |
| 515 | if (queued != 0) |
| 516 | flush_requests(info); |
| 517 | } |
| 518 | |
| 519 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) |
| 520 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 521 | struct request_queue *rq; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 522 | struct blkfront_info *info = gd->private_data; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 523 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 524 | rq = blk_init_queue(do_blkif_request, &info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 525 | if (rq == NULL) |
| 526 | return -1; |
| 527 | |
Fernando Luis Vázquez Cao | 66d352e | 2008-10-27 18:45:54 +0900 | [diff] [blame] | 528 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 529 | |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 530 | if (info->feature_discard) { |
| 531 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq); |
| 532 | blk_queue_max_discard_sectors(rq, get_capacity(gd)); |
| 533 | rq->limits.discard_granularity = info->discard_granularity; |
| 534 | rq->limits.discard_alignment = info->discard_alignment; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 535 | if (info->feature_secdiscard) |
| 536 | queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq); |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 537 | } |
| 538 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 539 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 540 | blk_queue_logical_block_size(rq, sector_size); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 541 | blk_queue_max_hw_sectors(rq, 512); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 542 | |
| 543 | /* Each segment in a request is up to an aligned page in size. */ |
| 544 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); |
| 545 | blk_queue_max_segment_size(rq, PAGE_SIZE); |
| 546 | |
| 547 | /* Ensure a merged request will fit in a single I/O ring slot. */ |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 548 | blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 549 | |
| 550 | /* Make sure buffer addresses are sector-aligned. */ |
| 551 | blk_queue_dma_alignment(rq, 511); |
| 552 | |
Ian Campbell | 1c91fe1 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 553 | /* Make sure we don't use bounce buffers. */ |
| 554 | blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY); |
| 555 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 556 | gd->queue = rq; |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 562 | static void xlvbd_flush(struct blkfront_info *info) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 563 | { |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 564 | blk_queue_flush(info->rq, info->feature_flush); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 565 | printk(KERN_INFO "blkfront: %s: %s: %s %s\n", |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 566 | info->gd->disk_name, |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 567 | info->flush_op == BLKIF_OP_WRITE_BARRIER ? |
| 568 | "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ? |
| 569 | "flush diskcache" : "barrier or flush"), |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 570 | info->feature_flush ? "enabled" : "disabled", |
| 571 | info->feature_persistent ? "using persistent grants" : ""); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 574 | static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) |
| 575 | { |
| 576 | int major; |
| 577 | major = BLKIF_MAJOR(vdevice); |
| 578 | *minor = BLKIF_MINOR(vdevice); |
| 579 | switch (major) { |
| 580 | case XEN_IDE0_MAJOR: |
| 581 | *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET; |
| 582 | *minor = ((*minor / 64) * PARTS_PER_DISK) + |
| 583 | EMULATED_HD_DISK_MINOR_OFFSET; |
| 584 | break; |
| 585 | case XEN_IDE1_MAJOR: |
| 586 | *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET; |
| 587 | *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) + |
| 588 | EMULATED_HD_DISK_MINOR_OFFSET; |
| 589 | break; |
| 590 | case XEN_SCSI_DISK0_MAJOR: |
| 591 | *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET; |
| 592 | *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET; |
| 593 | break; |
| 594 | case XEN_SCSI_DISK1_MAJOR: |
| 595 | case XEN_SCSI_DISK2_MAJOR: |
| 596 | case XEN_SCSI_DISK3_MAJOR: |
| 597 | case XEN_SCSI_DISK4_MAJOR: |
| 598 | case XEN_SCSI_DISK5_MAJOR: |
| 599 | case XEN_SCSI_DISK6_MAJOR: |
| 600 | case XEN_SCSI_DISK7_MAJOR: |
| 601 | *offset = (*minor / PARTS_PER_DISK) + |
| 602 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) + |
| 603 | EMULATED_SD_DISK_NAME_OFFSET; |
| 604 | *minor = *minor + |
| 605 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) + |
| 606 | EMULATED_SD_DISK_MINOR_OFFSET; |
| 607 | break; |
| 608 | case XEN_SCSI_DISK8_MAJOR: |
| 609 | case XEN_SCSI_DISK9_MAJOR: |
| 610 | case XEN_SCSI_DISK10_MAJOR: |
| 611 | case XEN_SCSI_DISK11_MAJOR: |
| 612 | case XEN_SCSI_DISK12_MAJOR: |
| 613 | case XEN_SCSI_DISK13_MAJOR: |
| 614 | case XEN_SCSI_DISK14_MAJOR: |
| 615 | case XEN_SCSI_DISK15_MAJOR: |
| 616 | *offset = (*minor / PARTS_PER_DISK) + |
| 617 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) + |
| 618 | EMULATED_SD_DISK_NAME_OFFSET; |
| 619 | *minor = *minor + |
| 620 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) + |
| 621 | EMULATED_SD_DISK_MINOR_OFFSET; |
| 622 | break; |
| 623 | case XENVBD_MAJOR: |
| 624 | *offset = *minor / PARTS_PER_DISK; |
| 625 | break; |
| 626 | default: |
| 627 | printk(KERN_WARNING "blkfront: your disk configuration is " |
| 628 | "incorrect, please use an xvd device instead\n"); |
| 629 | return -ENODEV; |
| 630 | } |
| 631 | return 0; |
| 632 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 633 | |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 634 | static char *encode_disk_name(char *ptr, unsigned int n) |
| 635 | { |
| 636 | if (n >= 26) |
| 637 | ptr = encode_disk_name(ptr, n / 26 - 1); |
| 638 | *ptr = 'a' + n % 26; |
| 639 | return ptr + 1; |
| 640 | } |
| 641 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 642 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
| 643 | struct blkfront_info *info, |
| 644 | u16 vdisk_info, u16 sector_size) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 645 | { |
| 646 | struct gendisk *gd; |
| 647 | int nr_minors = 1; |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 648 | int err; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 649 | unsigned int offset; |
| 650 | int minor; |
| 651 | int nr_parts; |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 652 | char *ptr; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 653 | |
| 654 | BUG_ON(info->gd != NULL); |
| 655 | BUG_ON(info->rq != NULL); |
| 656 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 657 | if ((info->vdevice>>EXT_SHIFT) > 1) { |
| 658 | /* this is above the extended range; something is wrong */ |
| 659 | printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); |
| 660 | return -ENODEV; |
| 661 | } |
| 662 | |
| 663 | if (!VDEV_IS_EXTENDED(info->vdevice)) { |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 664 | err = xen_translate_vdev(info->vdevice, &minor, &offset); |
| 665 | if (err) |
| 666 | return err; |
| 667 | nr_parts = PARTS_PER_DISK; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 668 | } else { |
| 669 | minor = BLKIF_MINOR_EXT(info->vdevice); |
| 670 | nr_parts = PARTS_PER_EXT_DISK; |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 671 | offset = minor / nr_parts; |
Stefan Bader | 89153b5 | 2011-07-14 15:30:37 +0200 | [diff] [blame] | 672 | if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4) |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 673 | printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with " |
| 674 | "emulated IDE disks,\n\t choose an xvd device name" |
| 675 | "from xvde on\n", info->vdevice); |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 676 | } |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 677 | if (minor >> MINORBITS) { |
| 678 | pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n", |
| 679 | info->vdevice, minor); |
| 680 | return -ENODEV; |
| 681 | } |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 682 | |
| 683 | if ((minor % nr_parts) == 0) |
| 684 | nr_minors = nr_parts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 685 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 686 | err = xlbd_reserve_minors(minor, nr_minors); |
| 687 | if (err) |
| 688 | goto out; |
| 689 | err = -ENODEV; |
| 690 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 691 | gd = alloc_disk(nr_minors); |
| 692 | if (gd == NULL) |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 693 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 694 | |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 695 | strcpy(gd->disk_name, DEV_NAME); |
| 696 | ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset); |
| 697 | BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN); |
| 698 | if (nr_minors > 1) |
| 699 | *ptr = 0; |
| 700 | else |
| 701 | snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr, |
| 702 | "%d", minor & (nr_parts - 1)); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 703 | |
| 704 | gd->major = XENVBD_MAJOR; |
| 705 | gd->first_minor = minor; |
| 706 | gd->fops = &xlvbd_block_fops; |
| 707 | gd->private_data = info; |
| 708 | gd->driverfs_dev = &(info->xbdev->dev); |
| 709 | set_capacity(gd, capacity); |
| 710 | |
| 711 | if (xlvbd_init_blk_queue(gd, sector_size)) { |
| 712 | del_gendisk(gd); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 713 | goto release; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | info->rq = gd->queue; |
| 717 | info->gd = gd; |
| 718 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 719 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 720 | |
| 721 | if (vdisk_info & VDISK_READONLY) |
| 722 | set_disk_ro(gd, 1); |
| 723 | |
| 724 | if (vdisk_info & VDISK_REMOVABLE) |
| 725 | gd->flags |= GENHD_FL_REMOVABLE; |
| 726 | |
| 727 | if (vdisk_info & VDISK_CDROM) |
| 728 | gd->flags |= GENHD_FL_CD; |
| 729 | |
| 730 | return 0; |
| 731 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 732 | release: |
| 733 | xlbd_release_minors(minor, nr_minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 734 | out: |
| 735 | return err; |
| 736 | } |
| 737 | |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 738 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
| 739 | { |
| 740 | unsigned int minor, nr_minors; |
| 741 | unsigned long flags; |
| 742 | |
| 743 | if (info->rq == NULL) |
| 744 | return; |
| 745 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 746 | spin_lock_irqsave(&info->io_lock, flags); |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 747 | |
| 748 | /* No more blkif_request(). */ |
| 749 | blk_stop_queue(info->rq); |
| 750 | |
| 751 | /* No more gnttab callback work. */ |
| 752 | gnttab_cancel_free_callback(&info->callback); |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 753 | spin_unlock_irqrestore(&info->io_lock, flags); |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 754 | |
| 755 | /* Flush gnttab callback work. Must be done with no locks held. */ |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 756 | flush_work(&info->work); |
Daniel Stodden | a66b5ae | 2010-08-07 18:33:17 +0200 | [diff] [blame] | 757 | |
| 758 | del_gendisk(info->gd); |
| 759 | |
| 760 | minor = info->gd->first_minor; |
| 761 | nr_minors = info->gd->minors; |
| 762 | xlbd_release_minors(minor, nr_minors); |
| 763 | |
| 764 | blk_cleanup_queue(info->rq); |
| 765 | info->rq = NULL; |
| 766 | |
| 767 | put_disk(info->gd); |
| 768 | info->gd = NULL; |
| 769 | } |
| 770 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 771 | static void kick_pending_request_queues(struct blkfront_info *info) |
| 772 | { |
| 773 | if (!RING_FULL(&info->ring)) { |
| 774 | /* Re-enable calldowns. */ |
| 775 | blk_start_queue(info->rq); |
| 776 | /* Kick things off immediately. */ |
| 777 | do_blkif_request(info->rq); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | static void blkif_restart_queue(struct work_struct *work) |
| 782 | { |
| 783 | struct blkfront_info *info = container_of(work, struct blkfront_info, work); |
| 784 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 785 | spin_lock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 786 | if (info->connected == BLKIF_STATE_CONNECTED) |
| 787 | kick_pending_request_queues(info); |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 788 | spin_unlock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static void blkif_free(struct blkfront_info *info, int suspend) |
| 792 | { |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 793 | struct llist_node *all_gnts; |
| 794 | struct grant *persistent_gnt; |
| 795 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 796 | /* Prevent new requests being issued until we fix things up. */ |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 797 | spin_lock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 798 | info->connected = suspend ? |
| 799 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; |
| 800 | /* No more blkif_request(). */ |
| 801 | if (info->rq) |
| 802 | blk_stop_queue(info->rq); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 803 | |
| 804 | /* Remove all persistent grants */ |
| 805 | if (info->persistent_gnts_c) { |
| 806 | all_gnts = llist_del_all(&info->persistent_gnts); |
| 807 | llist_for_each_entry(persistent_gnt, all_gnts, node) { |
| 808 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
Roger Pau Monne | 07c540a | 2012-11-16 19:26:47 +0100 | [diff] [blame] | 809 | __free_page(pfn_to_page(persistent_gnt->pfn)); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 810 | kfree(persistent_gnt); |
| 811 | } |
| 812 | info->persistent_gnts_c = 0; |
| 813 | } |
| 814 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 815 | /* No more gnttab callback work. */ |
| 816 | gnttab_cancel_free_callback(&info->callback); |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 817 | spin_unlock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 818 | |
| 819 | /* Flush gnttab callback work. Must be done with no locks held. */ |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 820 | flush_work(&info->work); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 821 | |
| 822 | /* Free resources associated with old device channel. */ |
| 823 | if (info->ring_ref != GRANT_INVALID_REF) { |
| 824 | gnttab_end_foreign_access(info->ring_ref, 0, |
| 825 | (unsigned long)info->ring.sring); |
| 826 | info->ring_ref = GRANT_INVALID_REF; |
| 827 | info->ring.sring = NULL; |
| 828 | } |
| 829 | if (info->irq) |
| 830 | unbind_from_irqhandler(info->irq, info); |
| 831 | info->evtchn = info->irq = 0; |
| 832 | |
| 833 | } |
| 834 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 835 | static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info, |
| 836 | struct blkif_response *bret) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 837 | { |
| 838 | int i; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 839 | struct bio_vec *bvec; |
| 840 | struct req_iterator iter; |
| 841 | unsigned long flags; |
| 842 | char *bvec_data; |
| 843 | void *shared_data; |
| 844 | unsigned int offset = 0; |
| 845 | |
| 846 | if (bret->operation == BLKIF_OP_READ) { |
| 847 | /* |
| 848 | * Copy the data received from the backend into the bvec. |
| 849 | * Since bv_offset can be different than 0, and bv_len different |
| 850 | * than PAGE_SIZE, we have to keep track of the current offset, |
| 851 | * to be sure we are copying the data from the right shared page. |
| 852 | */ |
| 853 | rq_for_each_segment(bvec, s->request, iter) { |
| 854 | BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE); |
| 855 | i = offset >> PAGE_SHIFT; |
Roger Pau Monne | cb5bd4d | 2012-11-02 16:43:04 +0100 | [diff] [blame] | 856 | BUG_ON(i >= s->req.u.rw.nr_segments); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 857 | shared_data = kmap_atomic( |
| 858 | pfn_to_page(s->grants_used[i]->pfn)); |
| 859 | bvec_data = bvec_kmap_irq(bvec, &flags); |
| 860 | memcpy(bvec_data, shared_data + bvec->bv_offset, |
| 861 | bvec->bv_len); |
| 862 | bvec_kunmap_irq(bvec_data, &flags); |
| 863 | kunmap_atomic(shared_data); |
| 864 | offset += bvec->bv_len; |
| 865 | } |
| 866 | } |
| 867 | /* Add the persistent grant into the list of free grants */ |
| 868 | for (i = 0; i < s->req.u.rw.nr_segments; i++) { |
| 869 | llist_add(&s->grants_used[i]->node, &info->persistent_gnts); |
| 870 | info->persistent_gnts_c++; |
| 871 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) |
| 875 | { |
| 876 | struct request *req; |
| 877 | struct blkif_response *bret; |
| 878 | RING_IDX i, rp; |
| 879 | unsigned long flags; |
| 880 | struct blkfront_info *info = (struct blkfront_info *)dev_id; |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 881 | int error; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 882 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 883 | spin_lock_irqsave(&info->io_lock, flags); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 884 | |
| 885 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 886 | spin_unlock_irqrestore(&info->io_lock, flags); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 887 | return IRQ_HANDLED; |
| 888 | } |
| 889 | |
| 890 | again: |
| 891 | rp = info->ring.sring->rsp_prod; |
| 892 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
| 893 | |
| 894 | for (i = info->ring.rsp_cons; i != rp; i++) { |
| 895 | unsigned long id; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 896 | |
| 897 | bret = RING_GET_RESPONSE(&info->ring, i); |
| 898 | id = bret->id; |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 899 | /* |
| 900 | * The backend has messed up and given us an id that we would |
| 901 | * never have given to it (we stamp it up to BLK_RING_SIZE - |
| 902 | * look in get_id_from_freelist. |
| 903 | */ |
| 904 | if (id >= BLK_RING_SIZE) { |
| 905 | WARN(1, "%s: response to %s has incorrect id (%ld)\n", |
| 906 | info->gd->disk_name, op_name(bret->operation), id); |
| 907 | /* We can't safely get the 'struct request' as |
| 908 | * the id is busted. */ |
| 909 | continue; |
| 910 | } |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 911 | req = info->shadow[id].request; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 912 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 913 | if (bret->operation != BLKIF_OP_DISCARD) |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 914 | blkif_completion(&info->shadow[id], info, bret); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 915 | |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 916 | if (add_id_to_freelist(info, id)) { |
| 917 | WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", |
| 918 | info->gd->disk_name, op_name(bret->operation), id); |
| 919 | continue; |
| 920 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 921 | |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 922 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 923 | switch (bret->operation) { |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 924 | case BLKIF_OP_DISCARD: |
| 925 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { |
| 926 | struct request_queue *rq = info->rq; |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 927 | printk(KERN_WARNING "blkfront: %s: %s op failed\n", |
| 928 | info->gd->disk_name, op_name(bret->operation)); |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 929 | error = -EOPNOTSUPP; |
| 930 | info->feature_discard = 0; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 931 | info->feature_secdiscard = 0; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 932 | queue_flag_clear(QUEUE_FLAG_DISCARD, rq); |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 933 | queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq); |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 934 | } |
| 935 | __blk_end_request_all(req, error); |
| 936 | break; |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 937 | case BLKIF_OP_FLUSH_DISKCACHE: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 938 | case BLKIF_OP_WRITE_BARRIER: |
| 939 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 940 | printk(KERN_WARNING "blkfront: %s: %s op failed\n", |
| 941 | info->gd->disk_name, op_name(bret->operation)); |
Kiyoshi Ueda | f530f036 | 2007-12-11 17:47:36 -0500 | [diff] [blame] | 942 | error = -EOPNOTSUPP; |
Jeremy Fitzhardinge | dcb8bae | 2010-11-02 11:55:58 -0400 | [diff] [blame] | 943 | } |
| 944 | if (unlikely(bret->status == BLKIF_RSP_ERROR && |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 945 | info->shadow[id].req.u.rw.nr_segments == 0)) { |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 946 | printk(KERN_WARNING "blkfront: %s: empty %s op failed\n", |
| 947 | info->gd->disk_name, op_name(bret->operation)); |
Jeremy Fitzhardinge | dcb8bae | 2010-11-02 11:55:58 -0400 | [diff] [blame] | 948 | error = -EOPNOTSUPP; |
| 949 | } |
| 950 | if (unlikely(error)) { |
| 951 | if (error == -EOPNOTSUPP) |
| 952 | error = 0; |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 953 | info->feature_flush = 0; |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 954 | info->flush_op = 0; |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 955 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 956 | } |
| 957 | /* fall through */ |
| 958 | case BLKIF_OP_READ: |
| 959 | case BLKIF_OP_WRITE: |
| 960 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) |
| 961 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " |
| 962 | "request: %x\n", bret->status); |
| 963 | |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 964 | __blk_end_request_all(req, error); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 965 | break; |
| 966 | default: |
| 967 | BUG(); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | info->ring.rsp_cons = i; |
| 972 | |
| 973 | if (i != info->ring.req_prod_pvt) { |
| 974 | int more_to_do; |
| 975 | RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); |
| 976 | if (more_to_do) |
| 977 | goto again; |
| 978 | } else |
| 979 | info->ring.sring->rsp_event = i + 1; |
| 980 | |
| 981 | kick_pending_request_queues(info); |
| 982 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 983 | spin_unlock_irqrestore(&info->io_lock, flags); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 984 | |
| 985 | return IRQ_HANDLED; |
| 986 | } |
| 987 | |
| 988 | |
| 989 | static int setup_blkring(struct xenbus_device *dev, |
| 990 | struct blkfront_info *info) |
| 991 | { |
| 992 | struct blkif_sring *sring; |
| 993 | int err; |
| 994 | |
| 995 | info->ring_ref = GRANT_INVALID_REF; |
| 996 | |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 997 | sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 998 | if (!sring) { |
| 999 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); |
| 1000 | return -ENOMEM; |
| 1001 | } |
| 1002 | SHARED_RING_INIT(sring); |
| 1003 | FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); |
| 1004 | |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 1005 | sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
| 1006 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1007 | err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); |
| 1008 | if (err < 0) { |
| 1009 | free_page((unsigned long)sring); |
| 1010 | info->ring.sring = NULL; |
| 1011 | goto fail; |
| 1012 | } |
| 1013 | info->ring_ref = err; |
| 1014 | |
| 1015 | err = xenbus_alloc_evtchn(dev, &info->evtchn); |
| 1016 | if (err) |
| 1017 | goto fail; |
| 1018 | |
Theodore Ts'o | 89c30f1 | 2012-07-17 13:46:19 -0400 | [diff] [blame] | 1019 | err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0, |
| 1020 | "blkif", info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1021 | if (err <= 0) { |
| 1022 | xenbus_dev_fatal(dev, err, |
| 1023 | "bind_evtchn_to_irqhandler failed"); |
| 1024 | goto fail; |
| 1025 | } |
| 1026 | info->irq = err; |
| 1027 | |
| 1028 | return 0; |
| 1029 | fail: |
| 1030 | blkif_free(info, 0); |
| 1031 | return err; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1036 | static int talk_to_blkback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1037 | struct blkfront_info *info) |
| 1038 | { |
| 1039 | const char *message = NULL; |
| 1040 | struct xenbus_transaction xbt; |
| 1041 | int err; |
| 1042 | |
| 1043 | /* Create shared ring, alloc event channel. */ |
| 1044 | err = setup_blkring(dev, info); |
| 1045 | if (err) |
| 1046 | goto out; |
| 1047 | |
| 1048 | again: |
| 1049 | err = xenbus_transaction_start(&xbt); |
| 1050 | if (err) { |
| 1051 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 1052 | goto destroy_blkring; |
| 1053 | } |
| 1054 | |
| 1055 | err = xenbus_printf(xbt, dev->nodename, |
| 1056 | "ring-ref", "%u", info->ring_ref); |
| 1057 | if (err) { |
| 1058 | message = "writing ring-ref"; |
| 1059 | goto abort_transaction; |
| 1060 | } |
| 1061 | err = xenbus_printf(xbt, dev->nodename, |
| 1062 | "event-channel", "%u", info->evtchn); |
| 1063 | if (err) { |
| 1064 | message = "writing event-channel"; |
| 1065 | goto abort_transaction; |
| 1066 | } |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 1067 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
| 1068 | XEN_IO_PROTO_ABI_NATIVE); |
| 1069 | if (err) { |
| 1070 | message = "writing protocol"; |
| 1071 | goto abort_transaction; |
| 1072 | } |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1073 | err = xenbus_printf(xbt, dev->nodename, |
Roger Pau Monne | cb5bd4d | 2012-11-02 16:43:04 +0100 | [diff] [blame] | 1074 | "feature-persistent", "%u", 1); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1075 | if (err) |
| 1076 | dev_warn(&dev->dev, |
| 1077 | "writing persistent grants feature to xenbus"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1078 | |
| 1079 | err = xenbus_transaction_end(xbt, 0); |
| 1080 | if (err) { |
| 1081 | if (err == -EAGAIN) |
| 1082 | goto again; |
| 1083 | xenbus_dev_fatal(dev, err, "completing transaction"); |
| 1084 | goto destroy_blkring; |
| 1085 | } |
| 1086 | |
| 1087 | xenbus_switch_state(dev, XenbusStateInitialised); |
| 1088 | |
| 1089 | return 0; |
| 1090 | |
| 1091 | abort_transaction: |
| 1092 | xenbus_transaction_end(xbt, 1); |
| 1093 | if (message) |
| 1094 | xenbus_dev_fatal(dev, err, "%s", message); |
| 1095 | destroy_blkring: |
| 1096 | blkif_free(info, 0); |
| 1097 | out: |
| 1098 | return err; |
| 1099 | } |
| 1100 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1101 | /** |
| 1102 | * Entry point to this code when a new device is created. Allocate the basic |
| 1103 | * structures and the ring buffer for communication with the backend, and |
| 1104 | * inform the backend of the appropriate details for those. Switch to |
| 1105 | * Initialised state. |
| 1106 | */ |
| 1107 | static int blkfront_probe(struct xenbus_device *dev, |
| 1108 | const struct xenbus_device_id *id) |
| 1109 | { |
| 1110 | int err, vdevice, i; |
| 1111 | struct blkfront_info *info; |
| 1112 | |
| 1113 | /* FIXME: Use dynamic device id if this is not set. */ |
| 1114 | err = xenbus_scanf(XBT_NIL, dev->nodename, |
| 1115 | "virtual-device", "%i", &vdevice); |
| 1116 | if (err != 1) { |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1117 | /* go looking in the extended area instead */ |
| 1118 | err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", |
| 1119 | "%i", &vdevice); |
| 1120 | if (err != 1) { |
| 1121 | xenbus_dev_fatal(dev, err, "reading virtual-device"); |
| 1122 | return err; |
| 1123 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1126 | if (xen_hvm_domain()) { |
| 1127 | char *type; |
| 1128 | int len; |
| 1129 | /* no unplug has been done: do not hook devices != xen vbds */ |
Ian Campbell | 1dc7ce9 | 2010-08-23 11:59:29 +0100 | [diff] [blame] | 1130 | if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) { |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1131 | int major; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1132 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1133 | if (!VDEV_IS_EXTENDED(vdevice)) |
| 1134 | major = BLKIF_MAJOR(vdevice); |
| 1135 | else |
| 1136 | major = XENVBD_MAJOR; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1137 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1138 | if (major != XENVBD_MAJOR) { |
| 1139 | printk(KERN_INFO |
| 1140 | "%s: HVM does not support vbd %d as xen block device\n", |
| 1141 | __FUNCTION__, vdevice); |
| 1142 | return -ENODEV; |
| 1143 | } |
| 1144 | } |
| 1145 | /* do not create a PV cdrom device if we are an HVM guest */ |
| 1146 | type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); |
| 1147 | if (IS_ERR(type)) |
| 1148 | return -ENODEV; |
| 1149 | if (strncmp(type, "cdrom", 5) == 0) { |
| 1150 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1151 | return -ENODEV; |
| 1152 | } |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1153 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1154 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1155 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 1156 | if (!info) { |
| 1157 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); |
| 1158 | return -ENOMEM; |
| 1159 | } |
| 1160 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1161 | mutex_init(&info->mutex); |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 1162 | spin_lock_init(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1163 | info->xbdev = dev; |
| 1164 | info->vdevice = vdevice; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1165 | init_llist_head(&info->persistent_gnts); |
| 1166 | info->persistent_gnts_c = 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1167 | info->connected = BLKIF_STATE_DISCONNECTED; |
| 1168 | INIT_WORK(&info->work, blkif_restart_queue); |
| 1169 | |
| 1170 | for (i = 0; i < BLK_RING_SIZE; i++) |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 1171 | info->shadow[i].req.u.rw.id = i+1; |
| 1172 | info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1173 | |
| 1174 | /* Front end dir is a number, which is used as the id. */ |
| 1175 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1176 | dev_set_drvdata(&dev->dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1177 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1178 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1179 | if (err) { |
| 1180 | kfree(info); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1181 | dev_set_drvdata(&dev->dev, NULL); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1182 | return err; |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | static int blkif_recover(struct blkfront_info *info) |
| 1190 | { |
| 1191 | int i; |
| 1192 | struct blkif_request *req; |
| 1193 | struct blk_shadow *copy; |
| 1194 | int j; |
| 1195 | |
| 1196 | /* Stage 1: Make a safe copy of the shadow state. */ |
Ian Campbell | a144ff0 | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1197 | copy = kmalloc(sizeof(info->shadow), |
| 1198 | GFP_NOIO | __GFP_REPEAT | __GFP_HIGH); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1199 | if (!copy) |
| 1200 | return -ENOMEM; |
| 1201 | memcpy(copy, info->shadow, sizeof(info->shadow)); |
| 1202 | |
| 1203 | /* Stage 2: Set up free list. */ |
| 1204 | memset(&info->shadow, 0, sizeof(info->shadow)); |
| 1205 | for (i = 0; i < BLK_RING_SIZE; i++) |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 1206 | info->shadow[i].req.u.rw.id = i+1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1207 | info->shadow_free = info->ring.req_prod_pvt; |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 1208 | info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1209 | |
| 1210 | /* Stage 3: Find pending requests and requeue them. */ |
| 1211 | for (i = 0; i < BLK_RING_SIZE; i++) { |
| 1212 | /* Not in use? */ |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 1213 | if (!copy[i].request) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1214 | continue; |
| 1215 | |
| 1216 | /* Grab a request slot and copy shadow state into it. */ |
| 1217 | req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); |
| 1218 | *req = copy[i].req; |
| 1219 | |
| 1220 | /* We get a new request id, and must reset the shadow state. */ |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 1221 | req->u.rw.id = get_id_from_freelist(info); |
| 1222 | memcpy(&info->shadow[req->u.rw.id], ©[i], sizeof(copy[i])); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1223 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1224 | if (req->operation != BLKIF_OP_DISCARD) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1225 | /* Rewrite any grant references invalidated by susp/resume. */ |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1226 | for (j = 0; j < req->u.rw.nr_segments; j++) |
| 1227 | gnttab_grant_foreign_access_ref( |
| 1228 | req->u.rw.seg[j].gref, |
| 1229 | info->xbdev->otherend_id, |
| 1230 | pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]), |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1231 | 0); |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1232 | } |
Konrad Rzeszutek Wilk | 97e3683 | 2011-10-12 12:12:36 -0400 | [diff] [blame] | 1233 | info->shadow[req->u.rw.id].req = *req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1234 | |
| 1235 | info->ring.req_prod_pvt++; |
| 1236 | } |
| 1237 | |
| 1238 | kfree(copy); |
| 1239 | |
| 1240 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 1241 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 1242 | spin_lock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1243 | |
| 1244 | /* Now safe for us to use the shared ring */ |
| 1245 | info->connected = BLKIF_STATE_CONNECTED; |
| 1246 | |
| 1247 | /* Send off requeued requests */ |
| 1248 | flush_requests(info); |
| 1249 | |
| 1250 | /* Kick any other new requests queued since we resumed */ |
| 1251 | kick_pending_request_queues(info); |
| 1252 | |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 1253 | spin_unlock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1254 | |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
| 1260 | * driver restart. We tear down our blkif structure and recreate it, but |
| 1261 | * leave the device-layer structures intact so that this is transparent to the |
| 1262 | * rest of the kernel. |
| 1263 | */ |
| 1264 | static int blkfront_resume(struct xenbus_device *dev) |
| 1265 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1266 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1267 | int err; |
| 1268 | |
| 1269 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); |
| 1270 | |
| 1271 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); |
| 1272 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1273 | err = talk_to_blkback(dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1274 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) |
| 1275 | err = blkif_recover(info); |
| 1276 | |
| 1277 | return err; |
| 1278 | } |
| 1279 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1280 | static void |
| 1281 | blkfront_closing(struct blkfront_info *info) |
| 1282 | { |
| 1283 | struct xenbus_device *xbdev = info->xbdev; |
| 1284 | struct block_device *bdev = NULL; |
| 1285 | |
| 1286 | mutex_lock(&info->mutex); |
| 1287 | |
| 1288 | if (xbdev->state == XenbusStateClosing) { |
| 1289 | mutex_unlock(&info->mutex); |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | if (info->gd) |
| 1294 | bdev = bdget_disk(info->gd, 0); |
| 1295 | |
| 1296 | mutex_unlock(&info->mutex); |
| 1297 | |
| 1298 | if (!bdev) { |
| 1299 | xenbus_frontend_closed(xbdev); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | mutex_lock(&bdev->bd_mutex); |
| 1304 | |
Daniel Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 1305 | if (bdev->bd_openers) { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1306 | xenbus_dev_error(xbdev, -EBUSY, |
| 1307 | "Device in use; refusing to close"); |
| 1308 | xenbus_switch_state(xbdev, XenbusStateClosing); |
| 1309 | } else { |
| 1310 | xlvbd_release_gendisk(info); |
| 1311 | xenbus_frontend_closed(xbdev); |
| 1312 | } |
| 1313 | |
| 1314 | mutex_unlock(&bdev->bd_mutex); |
| 1315 | bdput(bdev); |
| 1316 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1317 | |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1318 | static void blkfront_setup_discard(struct blkfront_info *info) |
| 1319 | { |
| 1320 | int err; |
| 1321 | char *type; |
| 1322 | unsigned int discard_granularity; |
| 1323 | unsigned int discard_alignment; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1324 | unsigned int discard_secure; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1325 | |
| 1326 | type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL); |
| 1327 | if (IS_ERR(type)) |
| 1328 | return; |
| 1329 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1330 | info->feature_secdiscard = 0; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1331 | if (strncmp(type, "phy", 3) == 0) { |
| 1332 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1333 | "discard-granularity", "%u", &discard_granularity, |
| 1334 | "discard-alignment", "%u", &discard_alignment, |
| 1335 | NULL); |
| 1336 | if (!err) { |
| 1337 | info->feature_discard = 1; |
| 1338 | info->discard_granularity = discard_granularity; |
| 1339 | info->discard_alignment = discard_alignment; |
| 1340 | } |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1341 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1342 | "discard-secure", "%d", &discard_secure, |
| 1343 | NULL); |
| 1344 | if (!err) |
| 1345 | info->feature_secdiscard = discard_secure; |
| 1346 | |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1347 | } else if (strncmp(type, "file", 4) == 0) |
| 1348 | info->feature_discard = 1; |
| 1349 | |
| 1350 | kfree(type); |
| 1351 | } |
| 1352 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1353 | /* |
| 1354 | * Invoked when the backend is finally 'ready' (and has told produced |
| 1355 | * the details about the physical device - #sectors, size, etc). |
| 1356 | */ |
| 1357 | static void blkfront_connect(struct blkfront_info *info) |
| 1358 | { |
| 1359 | unsigned long long sectors; |
| 1360 | unsigned long sector_size; |
| 1361 | unsigned int binfo; |
| 1362 | int err; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1363 | int barrier, flush, discard, persistent; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1364 | |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1365 | switch (info->connected) { |
| 1366 | case BLKIF_STATE_CONNECTED: |
| 1367 | /* |
| 1368 | * Potentially, the back-end may be signalling |
| 1369 | * a capacity change; update the capacity. |
| 1370 | */ |
| 1371 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 1372 | "sectors", "%Lu", §ors); |
| 1373 | if (XENBUS_EXIST_ERR(err)) |
| 1374 | return; |
| 1375 | printk(KERN_INFO "Setting capacity to %Lu\n", |
| 1376 | sectors); |
| 1377 | set_capacity(info->gd, sectors); |
K. Y. Srinivasan | 2def141 | 2010-03-18 15:00:54 -0700 | [diff] [blame] | 1378 | revalidate_disk(info->gd); |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1379 | |
| 1380 | /* fall through */ |
| 1381 | case BLKIF_STATE_SUSPENDED: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1382 | return; |
| 1383 | |
Jeremy Fitzhardinge | b4dddb4 | 2010-03-11 15:10:40 -0800 | [diff] [blame] | 1384 | default: |
| 1385 | break; |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 1386 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1387 | |
| 1388 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", |
| 1389 | __func__, info->xbdev->otherend); |
| 1390 | |
| 1391 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1392 | "sectors", "%llu", §ors, |
| 1393 | "info", "%u", &binfo, |
| 1394 | "sector-size", "%lu", §or_size, |
| 1395 | NULL); |
| 1396 | if (err) { |
| 1397 | xenbus_dev_fatal(info->xbdev, err, |
| 1398 | "reading backend fields at %s", |
| 1399 | info->xbdev->otherend); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 1403 | info->feature_flush = 0; |
| 1404 | info->flush_op = 0; |
| 1405 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1406 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
Marek Marczykowski | 4352b47 | 2011-05-03 12:04:52 -0400 | [diff] [blame] | 1407 | "feature-barrier", "%d", &barrier, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1408 | NULL); |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1409 | |
| 1410 | /* |
| 1411 | * If there's no "feature-barrier" defined, then it means |
| 1412 | * we're dealing with a very old backend which writes |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1413 | * synchronously; nothing to do. |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1414 | * |
Tejun Heo | 6958f14 | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1415 | * If there are barriers, then we use flush. |
Jeremy Fitzhardinge | 7901d14 | 2010-07-28 10:49:29 -0700 | [diff] [blame] | 1416 | */ |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 1417 | if (!err && barrier) { |
Jeremy Fitzhardinge | be2f837 | 2010-11-02 10:38:33 -0400 | [diff] [blame] | 1418 | info->feature_flush = REQ_FLUSH | REQ_FUA; |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 1419 | info->flush_op = BLKIF_OP_WRITE_BARRIER; |
| 1420 | } |
| 1421 | /* |
| 1422 | * And if there is "feature-flush-cache" use that above |
| 1423 | * barriers. |
| 1424 | */ |
| 1425 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1426 | "feature-flush-cache", "%d", &flush, |
| 1427 | NULL); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1428 | |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 1429 | if (!err && flush) { |
| 1430 | info->feature_flush = REQ_FLUSH; |
| 1431 | info->flush_op = BLKIF_OP_FLUSH_DISKCACHE; |
| 1432 | } |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1433 | |
| 1434 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1435 | "feature-discard", "%d", &discard, |
| 1436 | NULL); |
| 1437 | |
| 1438 | if (!err && discard) |
| 1439 | blkfront_setup_discard(info); |
| 1440 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1441 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 1442 | "feature-persistent", "%u", &persistent, |
| 1443 | NULL); |
| 1444 | if (err) |
| 1445 | info->feature_persistent = 0; |
| 1446 | else |
| 1447 | info->feature_persistent = persistent; |
| 1448 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1449 | err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1450 | if (err) { |
| 1451 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", |
| 1452 | info->xbdev->otherend); |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 1457 | |
| 1458 | /* Kick pending requests. */ |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 1459 | spin_lock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1460 | info->connected = BLKIF_STATE_CONNECTED; |
| 1461 | kick_pending_request_queues(info); |
Steven Noonan | 3467811 | 2012-02-17 12:04:44 -0800 | [diff] [blame] | 1462 | spin_unlock_irq(&info->io_lock); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1463 | |
| 1464 | add_disk(info->gd); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1465 | |
| 1466 | info->is_ready = 1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /** |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1470 | * Callback received when the backend's state changes. |
| 1471 | */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1472 | static void blkback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1473 | enum xenbus_state backend_state) |
| 1474 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1475 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1476 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1477 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1478 | |
| 1479 | switch (backend_state) { |
| 1480 | case XenbusStateInitialising: |
| 1481 | case XenbusStateInitWait: |
| 1482 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 1483 | case XenbusStateReconfiguring: |
| 1484 | case XenbusStateReconfigured: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1485 | case XenbusStateUnknown: |
| 1486 | case XenbusStateClosed: |
| 1487 | break; |
| 1488 | |
| 1489 | case XenbusStateConnected: |
| 1490 | blkfront_connect(info); |
| 1491 | break; |
| 1492 | |
| 1493 | case XenbusStateClosing: |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1494 | blkfront_closing(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1499 | static int blkfront_remove(struct xenbus_device *xbdev) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1500 | { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1501 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
| 1502 | struct block_device *bdev = NULL; |
| 1503 | struct gendisk *disk; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1504 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1505 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1506 | |
| 1507 | blkif_free(info, 0); |
| 1508 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1509 | mutex_lock(&info->mutex); |
| 1510 | |
| 1511 | disk = info->gd; |
| 1512 | if (disk) |
| 1513 | bdev = bdget_disk(disk, 0); |
| 1514 | |
| 1515 | info->xbdev = NULL; |
| 1516 | mutex_unlock(&info->mutex); |
| 1517 | |
| 1518 | if (!bdev) { |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1519 | kfree(info); |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * The xbdev was removed before we reached the Closed |
| 1525 | * state. See if it's safe to remove the disk. If the bdev |
| 1526 | * isn't closed yet, we let release take care of it. |
| 1527 | */ |
| 1528 | |
| 1529 | mutex_lock(&bdev->bd_mutex); |
| 1530 | info = disk->private_data; |
| 1531 | |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1532 | dev_warn(disk_to_dev(disk), |
| 1533 | "%s was hot-unplugged, %d stale handles\n", |
| 1534 | xbdev->nodename, bdev->bd_openers); |
| 1535 | |
Daniel Stodden | 7b32d10 | 2010-04-30 22:01:23 +0000 | [diff] [blame] | 1536 | if (info && !bdev->bd_openers) { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 1537 | xlvbd_release_gendisk(info); |
| 1538 | disk->private_data = NULL; |
| 1539 | kfree(info); |
| 1540 | } |
| 1541 | |
| 1542 | mutex_unlock(&bdev->bd_mutex); |
| 1543 | bdput(bdev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1548 | static int blkfront_is_ready(struct xenbus_device *dev) |
| 1549 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1550 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1551 | |
Jan Beulich | 5d7ed20 | 2010-08-07 18:31:12 +0200 | [diff] [blame] | 1552 | return info->is_ready && info->xbdev; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1555 | static int blkif_open(struct block_device *bdev, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1556 | { |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1557 | struct gendisk *disk = bdev->bd_disk; |
| 1558 | struct blkfront_info *info; |
| 1559 | int err = 0; |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1560 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1561 | mutex_lock(&blkfront_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1562 | |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1563 | info = disk->private_data; |
| 1564 | if (!info) { |
| 1565 | /* xbdev gone */ |
| 1566 | err = -ERESTARTSYS; |
| 1567 | goto out; |
| 1568 | } |
| 1569 | |
| 1570 | mutex_lock(&info->mutex); |
| 1571 | |
| 1572 | if (!info->gd) |
| 1573 | /* xbdev is closed */ |
| 1574 | err = -ERESTARTSYS; |
| 1575 | |
| 1576 | mutex_unlock(&info->mutex); |
| 1577 | |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1578 | out: |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1579 | mutex_unlock(&blkfront_mutex); |
Daniel Stodden | 1396174 | 2010-08-07 18:36:53 +0200 | [diff] [blame] | 1580 | return err; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1583 | static int blkif_release(struct gendisk *disk, fmode_t mode) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1584 | { |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1585 | struct blkfront_info *info = disk->private_data; |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1586 | struct block_device *bdev; |
| 1587 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1588 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1589 | mutex_lock(&blkfront_mutex); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1590 | |
| 1591 | bdev = bdget_disk(disk, 0); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1592 | |
Daniel Stodden | acfca3c | 2010-08-07 18:47:26 +0200 | [diff] [blame] | 1593 | if (bdev->bd_openers) |
| 1594 | goto out; |
| 1595 | |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1596 | /* |
| 1597 | * Check if we have been instructed to close. We will have |
| 1598 | * deferred this request, because the bdev was still open. |
| 1599 | */ |
| 1600 | |
| 1601 | mutex_lock(&info->mutex); |
| 1602 | xbdev = info->xbdev; |
| 1603 | |
| 1604 | if (xbdev && xbdev->state == XenbusStateClosing) { |
| 1605 | /* pending switch to state closed */ |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1606 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1607 | xlvbd_release_gendisk(info); |
| 1608 | xenbus_frontend_closed(info->xbdev); |
| 1609 | } |
| 1610 | |
| 1611 | mutex_unlock(&info->mutex); |
| 1612 | |
| 1613 | if (!xbdev) { |
| 1614 | /* sudden device removal */ |
Daniel Stodden | d54142c | 2010-08-07 18:51:21 +0200 | [diff] [blame] | 1615 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1616 | xlvbd_release_gendisk(info); |
| 1617 | disk->private_data = NULL; |
| 1618 | kfree(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1619 | } |
Daniel Stodden | 7fd152f | 2010-08-07 18:45:12 +0200 | [diff] [blame] | 1620 | |
Jens Axboe | a4cc14e | 2010-08-08 21:50:05 -0400 | [diff] [blame] | 1621 | out: |
Andrew Jones | dad5cf6 | 2012-02-16 13:16:25 +0100 | [diff] [blame] | 1622 | bdput(bdev); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1623 | mutex_unlock(&blkfront_mutex); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1627 | static const struct block_device_operations xlvbd_block_fops = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1628 | { |
| 1629 | .owner = THIS_MODULE, |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 1630 | .open = blkif_open, |
| 1631 | .release = blkif_release, |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 1632 | .getgeo = blkif_getgeo, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1633 | .ioctl = blkif_ioctl, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1634 | }; |
| 1635 | |
| 1636 | |
Márton Németh | ec9c42e | 2010-01-10 13:39:52 +0100 | [diff] [blame] | 1637 | static const struct xenbus_device_id blkfront_ids[] = { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1638 | { "vbd" }, |
| 1639 | { "" } |
| 1640 | }; |
| 1641 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 1642 | static DEFINE_XENBUS_DRIVER(blkfront, , |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1643 | .probe = blkfront_probe, |
| 1644 | .remove = blkfront_remove, |
| 1645 | .resume = blkfront_resume, |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1646 | .otherend_changed = blkback_changed, |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 1647 | .is_ready = blkfront_is_ready, |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 1648 | ); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1649 | |
| 1650 | static int __init xlblk_init(void) |
| 1651 | { |
Laszlo Ersek | 469738e | 2011-10-07 21:34:38 +0200 | [diff] [blame] | 1652 | int ret; |
| 1653 | |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 1654 | if (!xen_domain()) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1655 | return -ENODEV; |
| 1656 | |
Igor Mammedov | e95ae5a | 2012-03-27 19:31:08 +0200 | [diff] [blame] | 1657 | if (xen_hvm_domain() && !xen_platform_pci_unplug) |
Igor Mammedov | b9136d2 | 2012-03-21 15:08:38 +0100 | [diff] [blame] | 1658 | return -ENODEV; |
| 1659 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1660 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { |
| 1661 | printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", |
| 1662 | XENVBD_MAJOR, DEV_NAME); |
| 1663 | return -ENODEV; |
| 1664 | } |
| 1665 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 1666 | ret = xenbus_register_frontend(&blkfront_driver); |
Laszlo Ersek | 469738e | 2011-10-07 21:34:38 +0200 | [diff] [blame] | 1667 | if (ret) { |
| 1668 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); |
| 1669 | return ret; |
| 1670 | } |
| 1671 | |
| 1672 | return 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1673 | } |
| 1674 | module_init(xlblk_init); |
| 1675 | |
| 1676 | |
Jan Beulich | 5a60d0c | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 1677 | static void __exit xlblk_exit(void) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1678 | { |
Jan Beulich | 8605067 | 2012-04-05 16:04:52 +0100 | [diff] [blame] | 1679 | xenbus_unregister_driver(&blkfront_driver); |
| 1680 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); |
| 1681 | kfree(minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1682 | } |
| 1683 | module_exit(xlblk_exit); |
| 1684 | |
| 1685 | MODULE_DESCRIPTION("Xen virtual block device frontend"); |
| 1686 | MODULE_LICENSE("GPL"); |
| 1687 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 1688 | MODULE_ALIAS("xen:vbd"); |
Mark McLoughlin | 4f93f09b | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 1689 | MODULE_ALIAS("xenblk"); |