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