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