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