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