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