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