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