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