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