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> |
9f27ee59 | 46 | |
1ccbf534 | 47 | #include <xen/xen.h> |
9f27ee59 JF |
48 | #include <xen/xenbus.h> |
49 | #include <xen/grant_table.h> | |
50 | #include <xen/events.h> | |
51 | #include <xen/page.h> | |
c1c5413a | 52 | #include <xen/platform_pci.h> |
9f27ee59 JF |
53 | |
54 | #include <xen/interface/grant_table.h> | |
55 | #include <xen/interface/io/blkif.h> | |
3e334239 | 56 | #include <xen/interface/io/protocols.h> |
9f27ee59 JF |
57 | |
58 | #include <asm/xen/hypervisor.h> | |
59 | ||
60 | enum blkif_state { | |
61 | BLKIF_STATE_DISCONNECTED, | |
62 | BLKIF_STATE_CONNECTED, | |
63 | BLKIF_STATE_SUSPENDED, | |
64 | }; | |
65 | ||
66 | struct blk_shadow { | |
67 | struct blkif_request req; | |
68 | unsigned long request; | |
69 | unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
70 | }; | |
71 | ||
2a48fc0a | 72 | static DEFINE_MUTEX(blkfront_mutex); |
83d5cde4 | 73 | static const struct block_device_operations xlvbd_block_fops; |
9f27ee59 JF |
74 | |
75 | #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE) | |
76 | ||
77 | /* | |
78 | * We have one of these per vbd, whether ide, scsi or 'other'. They | |
79 | * hang in private_data off the gendisk structure. We may end up | |
80 | * putting all kinds of interesting stuff here :-) | |
81 | */ | |
82 | struct blkfront_info | |
83 | { | |
b70f5fa0 | 84 | struct mutex mutex; |
9f27ee59 | 85 | struct xenbus_device *xbdev; |
9f27ee59 JF |
86 | struct gendisk *gd; |
87 | int vdevice; | |
88 | blkif_vdev_t handle; | |
89 | enum blkif_state connected; | |
90 | int ring_ref; | |
91 | struct blkif_front_ring ring; | |
9e973e64 | 92 | struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; |
9f27ee59 JF |
93 | unsigned int evtchn, irq; |
94 | struct request_queue *rq; | |
95 | struct work_struct work; | |
96 | struct gnttab_free_callback callback; | |
97 | struct blk_shadow shadow[BLK_RING_SIZE]; | |
98 | unsigned long shadow_free; | |
4913efe4 | 99 | unsigned int feature_flush; |
1d78d705 | 100 | int is_ready; |
9f27ee59 JF |
101 | }; |
102 | ||
103 | static DEFINE_SPINLOCK(blkif_io_lock); | |
104 | ||
0e345826 JB |
105 | static unsigned int nr_minors; |
106 | static unsigned long *minors; | |
107 | static DEFINE_SPINLOCK(minor_lock); | |
108 | ||
9f27ee59 JF |
109 | #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ |
110 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) | |
111 | #define GRANT_INVALID_REF 0 | |
112 | ||
113 | #define PARTS_PER_DISK 16 | |
9246b5f0 | 114 | #define PARTS_PER_EXT_DISK 256 |
9f27ee59 JF |
115 | |
116 | #define BLKIF_MAJOR(dev) ((dev)>>8) | |
117 | #define BLKIF_MINOR(dev) ((dev) & 0xff) | |
118 | ||
9246b5f0 CL |
119 | #define EXT_SHIFT 28 |
120 | #define EXTENDED (1<<EXT_SHIFT) | |
121 | #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) | |
122 | #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) | |
9f27ee59 | 123 | |
9246b5f0 | 124 | #define DEV_NAME "xvd" /* name in /dev */ |
9f27ee59 JF |
125 | |
126 | static int get_id_from_freelist(struct blkfront_info *info) | |
127 | { | |
128 | unsigned long free = info->shadow_free; | |
b9ed7252 | 129 | BUG_ON(free >= BLK_RING_SIZE); |
9f27ee59 JF |
130 | info->shadow_free = info->shadow[free].req.id; |
131 | info->shadow[free].req.id = 0x0fffffee; /* debug */ | |
132 | return free; | |
133 | } | |
134 | ||
135 | static void add_id_to_freelist(struct blkfront_info *info, | |
136 | unsigned long id) | |
137 | { | |
138 | info->shadow[id].req.id = info->shadow_free; | |
139 | info->shadow[id].request = 0; | |
140 | info->shadow_free = id; | |
141 | } | |
142 | ||
0e345826 JB |
143 | static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) |
144 | { | |
145 | unsigned int end = minor + nr; | |
146 | int rc; | |
147 | ||
148 | if (end > nr_minors) { | |
149 | unsigned long *bitmap, *old; | |
150 | ||
151 | bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap), | |
152 | GFP_KERNEL); | |
153 | if (bitmap == NULL) | |
154 | return -ENOMEM; | |
155 | ||
156 | spin_lock(&minor_lock); | |
157 | if (end > nr_minors) { | |
158 | old = minors; | |
159 | memcpy(bitmap, minors, | |
160 | BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); | |
161 | minors = bitmap; | |
162 | nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; | |
163 | } else | |
164 | old = bitmap; | |
165 | spin_unlock(&minor_lock); | |
166 | kfree(old); | |
167 | } | |
168 | ||
169 | spin_lock(&minor_lock); | |
170 | if (find_next_bit(minors, end, minor) >= end) { | |
171 | for (; minor < end; ++minor) | |
172 | __set_bit(minor, minors); | |
173 | rc = 0; | |
174 | } else | |
175 | rc = -EBUSY; | |
176 | spin_unlock(&minor_lock); | |
177 | ||
178 | return rc; | |
179 | } | |
180 | ||
181 | static void xlbd_release_minors(unsigned int minor, unsigned int nr) | |
182 | { | |
183 | unsigned int end = minor + nr; | |
184 | ||
185 | BUG_ON(end > nr_minors); | |
186 | spin_lock(&minor_lock); | |
187 | for (; minor < end; ++minor) | |
188 | __clear_bit(minor, minors); | |
189 | spin_unlock(&minor_lock); | |
190 | } | |
191 | ||
9f27ee59 JF |
192 | static void blkif_restart_queue_callback(void *arg) |
193 | { | |
194 | struct blkfront_info *info = (struct blkfront_info *)arg; | |
195 | schedule_work(&info->work); | |
196 | } | |
197 | ||
afe42d7d | 198 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
597592d9 IC |
199 | { |
200 | /* We don't have real geometry info, but let's at least return | |
201 | values consistent with the size of the device */ | |
202 | sector_t nsect = get_capacity(bd->bd_disk); | |
203 | sector_t cylinders = nsect; | |
204 | ||
205 | hg->heads = 0xff; | |
206 | hg->sectors = 0x3f; | |
207 | sector_div(cylinders, hg->heads * hg->sectors); | |
208 | hg->cylinders = cylinders; | |
209 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) | |
210 | hg->cylinders = 0xffff; | |
211 | return 0; | |
212 | } | |
213 | ||
a63c848b | 214 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
62aa0054 | 215 | unsigned command, unsigned long argument) |
440a01a7 | 216 | { |
a63c848b | 217 | struct blkfront_info *info = bdev->bd_disk->private_data; |
440a01a7 CL |
218 | int i; |
219 | ||
220 | dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n", | |
221 | command, (long)argument); | |
222 | ||
223 | switch (command) { | |
224 | case CDROMMULTISESSION: | |
225 | dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n"); | |
226 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) | |
227 | if (put_user(0, (char __user *)(argument + i))) | |
228 | return -EFAULT; | |
229 | return 0; | |
230 | ||
231 | case CDROM_GET_CAPABILITY: { | |
232 | struct gendisk *gd = info->gd; | |
233 | if (gd->flags & GENHD_FL_CD) | |
234 | return 0; | |
235 | return -EINVAL; | |
236 | } | |
237 | ||
238 | default: | |
239 | /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", | |
240 | command);*/ | |
241 | return -EINVAL; /* same return as native Linux */ | |
242 | } | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
9f27ee59 | 247 | /* |
c64e38ea JF |
248 | * Generate a Xen blkfront IO request from a blk layer request. Reads |
249 | * and writes are handled as expected. Since we lack a loose flush | |
250 | * request, we map flushes into a full ordered barrier. | |
9f27ee59 | 251 | * |
c64e38ea | 252 | * @req: a request struct |
9f27ee59 JF |
253 | */ |
254 | static int blkif_queue_request(struct request *req) | |
255 | { | |
256 | struct blkfront_info *info = req->rq_disk->private_data; | |
257 | unsigned long buffer_mfn; | |
258 | struct blkif_request *ring_req; | |
9f27ee59 JF |
259 | unsigned long id; |
260 | unsigned int fsect, lsect; | |
9e973e64 | 261 | int i, ref; |
9f27ee59 | 262 | grant_ref_t gref_head; |
9e973e64 | 263 | struct scatterlist *sg; |
9f27ee59 JF |
264 | |
265 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) | |
266 | return 1; | |
267 | ||
268 | if (gnttab_alloc_grant_references( | |
269 | BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) { | |
270 | gnttab_request_free_callback( | |
271 | &info->callback, | |
272 | blkif_restart_queue_callback, | |
273 | info, | |
274 | BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
275 | return 1; | |
276 | } | |
277 | ||
278 | /* Fill out a communications ring structure. */ | |
279 | ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
280 | id = get_id_from_freelist(info); | |
281 | info->shadow[id].request = (unsigned long)req; | |
282 | ||
283 | ring_req->id = id; | |
83096ebf | 284 | ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req); |
9f27ee59 JF |
285 | ring_req->handle = info->handle; |
286 | ||
287 | ring_req->operation = rq_data_dir(req) ? | |
288 | BLKIF_OP_WRITE : BLKIF_OP_READ; | |
c64e38ea | 289 | if (req->cmd_flags & REQ_FLUSH) |
9f27ee59 JF |
290 | ring_req->operation = BLKIF_OP_WRITE_BARRIER; |
291 | ||
9e973e64 JA |
292 | ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg); |
293 | BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
294 | ||
295 | for_each_sg(info->sg, sg, ring_req->nr_segments, i) { | |
296 | buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg))); | |
297 | fsect = sg->offset >> 9; | |
298 | lsect = fsect + (sg->length >> 9) - 1; | |
6c92e699 JA |
299 | /* install a grant reference. */ |
300 | ref = gnttab_claim_grant_reference(&gref_head); | |
301 | BUG_ON(ref == -ENOSPC); | |
302 | ||
303 | gnttab_grant_foreign_access_ref( | |
9f27ee59 JF |
304 | ref, |
305 | info->xbdev->otherend_id, | |
306 | buffer_mfn, | |
307 | rq_data_dir(req) ); | |
308 | ||
9e973e64 JA |
309 | info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn); |
310 | ring_req->seg[i] = | |
9f27ee59 JF |
311 | (struct blkif_request_segment) { |
312 | .gref = ref, | |
313 | .first_sect = fsect, | |
314 | .last_sect = lsect }; | |
9f27ee59 JF |
315 | } |
316 | ||
317 | info->ring.req_prod_pvt++; | |
318 | ||
319 | /* Keep a private copy so we can reissue requests when recovering. */ | |
320 | info->shadow[id].req = *ring_req; | |
321 | ||
322 | gnttab_free_grant_references(gref_head); | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
327 | ||
328 | static inline void flush_requests(struct blkfront_info *info) | |
329 | { | |
330 | int notify; | |
331 | ||
332 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); | |
333 | ||
334 | if (notify) | |
335 | notify_remote_via_irq(info->irq); | |
336 | } | |
337 | ||
338 | /* | |
339 | * do_blkif_request | |
340 | * read a block; request is in a request queue | |
341 | */ | |
165125e1 | 342 | static void do_blkif_request(struct request_queue *rq) |
9f27ee59 JF |
343 | { |
344 | struct blkfront_info *info = NULL; | |
345 | struct request *req; | |
346 | int queued; | |
347 | ||
348 | pr_debug("Entered do_blkif_request\n"); | |
349 | ||
350 | queued = 0; | |
351 | ||
9934c8c0 | 352 | while ((req = blk_peek_request(rq)) != NULL) { |
9f27ee59 | 353 | info = req->rq_disk->private_data; |
9f27ee59 JF |
354 | |
355 | if (RING_FULL(&info->ring)) | |
356 | goto wait; | |
357 | ||
9934c8c0 | 358 | blk_start_request(req); |
296b2f6a | 359 | |
33659ebb | 360 | if (req->cmd_type != REQ_TYPE_FS) { |
296b2f6a TH |
361 | __blk_end_request_all(req, -EIO); |
362 | continue; | |
363 | } | |
364 | ||
9f27ee59 | 365 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " |
83096ebf TH |
366 | "(%u/%u) buffer:%p [%s]\n", |
367 | req, req->cmd, (unsigned long)blk_rq_pos(req), | |
368 | blk_rq_cur_sectors(req), blk_rq_sectors(req), | |
369 | req->buffer, rq_data_dir(req) ? "write" : "read"); | |
9f27ee59 | 370 | |
9f27ee59 JF |
371 | if (blkif_queue_request(req)) { |
372 | blk_requeue_request(rq, req); | |
373 | wait: | |
374 | /* Avoid pointless unplugs. */ | |
375 | blk_stop_queue(rq); | |
376 | break; | |
377 | } | |
378 | ||
379 | queued++; | |
380 | } | |
381 | ||
382 | if (queued != 0) | |
383 | flush_requests(info); | |
384 | } | |
385 | ||
386 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) | |
387 | { | |
165125e1 | 388 | struct request_queue *rq; |
9f27ee59 JF |
389 | |
390 | rq = blk_init_queue(do_blkif_request, &blkif_io_lock); | |
391 | if (rq == NULL) | |
392 | return -1; | |
393 | ||
66d352e1 | 394 | queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq); |
9f27ee59 JF |
395 | |
396 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ | |
e1defc4f | 397 | blk_queue_logical_block_size(rq, sector_size); |
086fa5ff | 398 | blk_queue_max_hw_sectors(rq, 512); |
9f27ee59 JF |
399 | |
400 | /* Each segment in a request is up to an aligned page in size. */ | |
401 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); | |
402 | blk_queue_max_segment_size(rq, PAGE_SIZE); | |
403 | ||
404 | /* Ensure a merged request will fit in a single I/O ring slot. */ | |
8a78362c | 405 | blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); |
9f27ee59 JF |
406 | |
407 | /* Make sure buffer addresses are sector-aligned. */ | |
408 | blk_queue_dma_alignment(rq, 511); | |
409 | ||
1c91fe1a IC |
410 | /* Make sure we don't use bounce buffers. */ |
411 | blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY); | |
412 | ||
9f27ee59 JF |
413 | gd->queue = rq; |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | ||
4913efe4 | 419 | static void xlvbd_flush(struct blkfront_info *info) |
9f27ee59 | 420 | { |
4913efe4 | 421 | blk_queue_flush(info->rq, info->feature_flush); |
9f27ee59 | 422 | printk(KERN_INFO "blkfront: %s: barriers %s\n", |
4913efe4 TH |
423 | info->gd->disk_name, |
424 | info->feature_flush ? "enabled" : "disabled"); | |
9f27ee59 JF |
425 | } |
426 | ||
427 | ||
9246b5f0 CL |
428 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
429 | struct blkfront_info *info, | |
430 | u16 vdisk_info, u16 sector_size) | |
9f27ee59 JF |
431 | { |
432 | struct gendisk *gd; | |
433 | int nr_minors = 1; | |
434 | int err = -ENODEV; | |
9246b5f0 CL |
435 | unsigned int offset; |
436 | int minor; | |
437 | int nr_parts; | |
9f27ee59 JF |
438 | |
439 | BUG_ON(info->gd != NULL); | |
440 | BUG_ON(info->rq != NULL); | |
441 | ||
9246b5f0 CL |
442 | if ((info->vdevice>>EXT_SHIFT) > 1) { |
443 | /* this is above the extended range; something is wrong */ | |
444 | printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); | |
445 | return -ENODEV; | |
446 | } | |
447 | ||
448 | if (!VDEV_IS_EXTENDED(info->vdevice)) { | |
449 | minor = BLKIF_MINOR(info->vdevice); | |
450 | nr_parts = PARTS_PER_DISK; | |
451 | } else { | |
452 | minor = BLKIF_MINOR_EXT(info->vdevice); | |
453 | nr_parts = PARTS_PER_EXT_DISK; | |
454 | } | |
455 | ||
456 | if ((minor % nr_parts) == 0) | |
457 | nr_minors = nr_parts; | |
9f27ee59 | 458 | |
0e345826 JB |
459 | err = xlbd_reserve_minors(minor, nr_minors); |
460 | if (err) | |
461 | goto out; | |
462 | err = -ENODEV; | |
463 | ||
9f27ee59 JF |
464 | gd = alloc_disk(nr_minors); |
465 | if (gd == NULL) | |
0e345826 | 466 | goto release; |
9f27ee59 | 467 | |
9246b5f0 CL |
468 | offset = minor / nr_parts; |
469 | ||
470 | if (nr_minors > 1) { | |
471 | if (offset < 26) | |
472 | sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset); | |
473 | else | |
474 | sprintf(gd->disk_name, "%s%c%c", DEV_NAME, | |
475 | 'a' + ((offset / 26)-1), 'a' + (offset % 26)); | |
476 | } else { | |
477 | if (offset < 26) | |
478 | sprintf(gd->disk_name, "%s%c%d", DEV_NAME, | |
479 | 'a' + offset, | |
480 | minor & (nr_parts - 1)); | |
481 | else | |
482 | sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME, | |
483 | 'a' + ((offset / 26) - 1), | |
484 | 'a' + (offset % 26), | |
485 | minor & (nr_parts - 1)); | |
486 | } | |
9f27ee59 JF |
487 | |
488 | gd->major = XENVBD_MAJOR; | |
489 | gd->first_minor = minor; | |
490 | gd->fops = &xlvbd_block_fops; | |
491 | gd->private_data = info; | |
492 | gd->driverfs_dev = &(info->xbdev->dev); | |
493 | set_capacity(gd, capacity); | |
494 | ||
495 | if (xlvbd_init_blk_queue(gd, sector_size)) { | |
496 | del_gendisk(gd); | |
0e345826 | 497 | goto release; |
9f27ee59 JF |
498 | } |
499 | ||
500 | info->rq = gd->queue; | |
501 | info->gd = gd; | |
502 | ||
4913efe4 | 503 | xlvbd_flush(info); |
9f27ee59 JF |
504 | |
505 | if (vdisk_info & VDISK_READONLY) | |
506 | set_disk_ro(gd, 1); | |
507 | ||
508 | if (vdisk_info & VDISK_REMOVABLE) | |
509 | gd->flags |= GENHD_FL_REMOVABLE; | |
510 | ||
511 | if (vdisk_info & VDISK_CDROM) | |
512 | gd->flags |= GENHD_FL_CD; | |
513 | ||
514 | return 0; | |
515 | ||
0e345826 JB |
516 | release: |
517 | xlbd_release_minors(minor, nr_minors); | |
9f27ee59 JF |
518 | out: |
519 | return err; | |
520 | } | |
521 | ||
a66b5aeb DS |
522 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
523 | { | |
524 | unsigned int minor, nr_minors; | |
525 | unsigned long flags; | |
526 | ||
527 | if (info->rq == NULL) | |
528 | return; | |
529 | ||
530 | spin_lock_irqsave(&blkif_io_lock, flags); | |
531 | ||
532 | /* No more blkif_request(). */ | |
533 | blk_stop_queue(info->rq); | |
534 | ||
535 | /* No more gnttab callback work. */ | |
536 | gnttab_cancel_free_callback(&info->callback); | |
537 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
538 | ||
539 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
540 | flush_scheduled_work(); | |
541 | ||
542 | del_gendisk(info->gd); | |
543 | ||
544 | minor = info->gd->first_minor; | |
545 | nr_minors = info->gd->minors; | |
546 | xlbd_release_minors(minor, nr_minors); | |
547 | ||
548 | blk_cleanup_queue(info->rq); | |
549 | info->rq = NULL; | |
550 | ||
551 | put_disk(info->gd); | |
552 | info->gd = NULL; | |
553 | } | |
554 | ||
9f27ee59 JF |
555 | static void kick_pending_request_queues(struct blkfront_info *info) |
556 | { | |
557 | if (!RING_FULL(&info->ring)) { | |
558 | /* Re-enable calldowns. */ | |
559 | blk_start_queue(info->rq); | |
560 | /* Kick things off immediately. */ | |
561 | do_blkif_request(info->rq); | |
562 | } | |
563 | } | |
564 | ||
565 | static void blkif_restart_queue(struct work_struct *work) | |
566 | { | |
567 | struct blkfront_info *info = container_of(work, struct blkfront_info, work); | |
568 | ||
569 | spin_lock_irq(&blkif_io_lock); | |
570 | if (info->connected == BLKIF_STATE_CONNECTED) | |
571 | kick_pending_request_queues(info); | |
572 | spin_unlock_irq(&blkif_io_lock); | |
573 | } | |
574 | ||
575 | static void blkif_free(struct blkfront_info *info, int suspend) | |
576 | { | |
577 | /* Prevent new requests being issued until we fix things up. */ | |
578 | spin_lock_irq(&blkif_io_lock); | |
579 | info->connected = suspend ? | |
580 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; | |
581 | /* No more blkif_request(). */ | |
582 | if (info->rq) | |
583 | blk_stop_queue(info->rq); | |
584 | /* No more gnttab callback work. */ | |
585 | gnttab_cancel_free_callback(&info->callback); | |
586 | spin_unlock_irq(&blkif_io_lock); | |
587 | ||
588 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
589 | flush_scheduled_work(); | |
590 | ||
591 | /* Free resources associated with old device channel. */ | |
592 | if (info->ring_ref != GRANT_INVALID_REF) { | |
593 | gnttab_end_foreign_access(info->ring_ref, 0, | |
594 | (unsigned long)info->ring.sring); | |
595 | info->ring_ref = GRANT_INVALID_REF; | |
596 | info->ring.sring = NULL; | |
597 | } | |
598 | if (info->irq) | |
599 | unbind_from_irqhandler(info->irq, info); | |
600 | info->evtchn = info->irq = 0; | |
601 | ||
602 | } | |
603 | ||
604 | static void blkif_completion(struct blk_shadow *s) | |
605 | { | |
606 | int i; | |
607 | for (i = 0; i < s->req.nr_segments; i++) | |
608 | gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL); | |
609 | } | |
610 | ||
611 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) | |
612 | { | |
613 | struct request *req; | |
614 | struct blkif_response *bret; | |
615 | RING_IDX i, rp; | |
616 | unsigned long flags; | |
617 | struct blkfront_info *info = (struct blkfront_info *)dev_id; | |
f530f036 | 618 | int error; |
9f27ee59 JF |
619 | |
620 | spin_lock_irqsave(&blkif_io_lock, flags); | |
621 | ||
622 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { | |
623 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
624 | return IRQ_HANDLED; | |
625 | } | |
626 | ||
627 | again: | |
628 | rp = info->ring.sring->rsp_prod; | |
629 | rmb(); /* Ensure we see queued responses up to 'rp'. */ | |
630 | ||
631 | for (i = info->ring.rsp_cons; i != rp; i++) { | |
632 | unsigned long id; | |
9f27ee59 JF |
633 | |
634 | bret = RING_GET_RESPONSE(&info->ring, i); | |
635 | id = bret->id; | |
636 | req = (struct request *)info->shadow[id].request; | |
637 | ||
638 | blkif_completion(&info->shadow[id]); | |
639 | ||
640 | add_id_to_freelist(info, id); | |
641 | ||
f530f036 | 642 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
9f27ee59 JF |
643 | switch (bret->operation) { |
644 | case BLKIF_OP_WRITE_BARRIER: | |
645 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { | |
646 | printk(KERN_WARNING "blkfront: %s: write barrier op failed\n", | |
647 | info->gd->disk_name); | |
f530f036 | 648 | error = -EOPNOTSUPP; |
4913efe4 TH |
649 | info->feature_flush = 0; |
650 | xlvbd_flush(info); | |
9f27ee59 JF |
651 | } |
652 | /* fall through */ | |
653 | case BLKIF_OP_READ: | |
654 | case BLKIF_OP_WRITE: | |
655 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) | |
656 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " | |
657 | "request: %x\n", bret->status); | |
658 | ||
40cbbb78 | 659 | __blk_end_request_all(req, error); |
9f27ee59 JF |
660 | break; |
661 | default: | |
662 | BUG(); | |
663 | } | |
664 | } | |
665 | ||
666 | info->ring.rsp_cons = i; | |
667 | ||
668 | if (i != info->ring.req_prod_pvt) { | |
669 | int more_to_do; | |
670 | RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); | |
671 | if (more_to_do) | |
672 | goto again; | |
673 | } else | |
674 | info->ring.sring->rsp_event = i + 1; | |
675 | ||
676 | kick_pending_request_queues(info); | |
677 | ||
678 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
679 | ||
680 | return IRQ_HANDLED; | |
681 | } | |
682 | ||
683 | ||
684 | static int setup_blkring(struct xenbus_device *dev, | |
685 | struct blkfront_info *info) | |
686 | { | |
687 | struct blkif_sring *sring; | |
688 | int err; | |
689 | ||
690 | info->ring_ref = GRANT_INVALID_REF; | |
691 | ||
a144ff09 | 692 | sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH); |
9f27ee59 JF |
693 | if (!sring) { |
694 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); | |
695 | return -ENOMEM; | |
696 | } | |
697 | SHARED_RING_INIT(sring); | |
698 | FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); | |
9e973e64 JA |
699 | |
700 | sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
9f27ee59 JF |
701 | |
702 | err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); | |
703 | if (err < 0) { | |
704 | free_page((unsigned long)sring); | |
705 | info->ring.sring = NULL; | |
706 | goto fail; | |
707 | } | |
708 | info->ring_ref = err; | |
709 | ||
710 | err = xenbus_alloc_evtchn(dev, &info->evtchn); | |
711 | if (err) | |
712 | goto fail; | |
713 | ||
714 | err = bind_evtchn_to_irqhandler(info->evtchn, | |
715 | blkif_interrupt, | |
716 | IRQF_SAMPLE_RANDOM, "blkif", info); | |
717 | if (err <= 0) { | |
718 | xenbus_dev_fatal(dev, err, | |
719 | "bind_evtchn_to_irqhandler failed"); | |
720 | goto fail; | |
721 | } | |
722 | info->irq = err; | |
723 | ||
724 | return 0; | |
725 | fail: | |
726 | blkif_free(info, 0); | |
727 | return err; | |
728 | } | |
729 | ||
730 | ||
731 | /* Common code used when first setting up, and when resuming. */ | |
203fd61f | 732 | static int talk_to_blkback(struct xenbus_device *dev, |
9f27ee59 JF |
733 | struct blkfront_info *info) |
734 | { | |
735 | const char *message = NULL; | |
736 | struct xenbus_transaction xbt; | |
737 | int err; | |
738 | ||
739 | /* Create shared ring, alloc event channel. */ | |
740 | err = setup_blkring(dev, info); | |
741 | if (err) | |
742 | goto out; | |
743 | ||
744 | again: | |
745 | err = xenbus_transaction_start(&xbt); | |
746 | if (err) { | |
747 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
748 | goto destroy_blkring; | |
749 | } | |
750 | ||
751 | err = xenbus_printf(xbt, dev->nodename, | |
752 | "ring-ref", "%u", info->ring_ref); | |
753 | if (err) { | |
754 | message = "writing ring-ref"; | |
755 | goto abort_transaction; | |
756 | } | |
757 | err = xenbus_printf(xbt, dev->nodename, | |
758 | "event-channel", "%u", info->evtchn); | |
759 | if (err) { | |
760 | message = "writing event-channel"; | |
761 | goto abort_transaction; | |
762 | } | |
3e334239 MA |
763 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
764 | XEN_IO_PROTO_ABI_NATIVE); | |
765 | if (err) { | |
766 | message = "writing protocol"; | |
767 | goto abort_transaction; | |
768 | } | |
9f27ee59 JF |
769 | |
770 | err = xenbus_transaction_end(xbt, 0); | |
771 | if (err) { | |
772 | if (err == -EAGAIN) | |
773 | goto again; | |
774 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
775 | goto destroy_blkring; | |
776 | } | |
777 | ||
778 | xenbus_switch_state(dev, XenbusStateInitialised); | |
779 | ||
780 | return 0; | |
781 | ||
782 | abort_transaction: | |
783 | xenbus_transaction_end(xbt, 1); | |
784 | if (message) | |
785 | xenbus_dev_fatal(dev, err, "%s", message); | |
786 | destroy_blkring: | |
787 | blkif_free(info, 0); | |
788 | out: | |
789 | return err; | |
790 | } | |
791 | ||
9f27ee59 JF |
792 | /** |
793 | * Entry point to this code when a new device is created. Allocate the basic | |
794 | * structures and the ring buffer for communication with the backend, and | |
795 | * inform the backend of the appropriate details for those. Switch to | |
796 | * Initialised state. | |
797 | */ | |
798 | static int blkfront_probe(struct xenbus_device *dev, | |
799 | const struct xenbus_device_id *id) | |
800 | { | |
801 | int err, vdevice, i; | |
802 | struct blkfront_info *info; | |
803 | ||
804 | /* FIXME: Use dynamic device id if this is not set. */ | |
805 | err = xenbus_scanf(XBT_NIL, dev->nodename, | |
806 | "virtual-device", "%i", &vdevice); | |
807 | if (err != 1) { | |
9246b5f0 CL |
808 | /* go looking in the extended area instead */ |
809 | err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", | |
810 | "%i", &vdevice); | |
811 | if (err != 1) { | |
812 | xenbus_dev_fatal(dev, err, "reading virtual-device"); | |
813 | return err; | |
814 | } | |
9f27ee59 JF |
815 | } |
816 | ||
b98a409b SS |
817 | if (xen_hvm_domain()) { |
818 | char *type; | |
819 | int len; | |
820 | /* no unplug has been done: do not hook devices != xen vbds */ | |
1dc7ce99 | 821 | if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) { |
b98a409b SS |
822 | int major; |
823 | ||
824 | if (!VDEV_IS_EXTENDED(vdevice)) | |
825 | major = BLKIF_MAJOR(vdevice); | |
826 | else | |
827 | major = XENVBD_MAJOR; | |
828 | ||
829 | if (major != XENVBD_MAJOR) { | |
830 | printk(KERN_INFO | |
831 | "%s: HVM does not support vbd %d as xen block device\n", | |
832 | __FUNCTION__, vdevice); | |
833 | return -ENODEV; | |
834 | } | |
835 | } | |
836 | /* do not create a PV cdrom device if we are an HVM guest */ | |
837 | type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); | |
838 | if (IS_ERR(type)) | |
839 | return -ENODEV; | |
840 | if (strncmp(type, "cdrom", 5) == 0) { | |
841 | kfree(type); | |
c1c5413a SS |
842 | return -ENODEV; |
843 | } | |
b98a409b | 844 | kfree(type); |
c1c5413a | 845 | } |
9f27ee59 JF |
846 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
847 | if (!info) { | |
848 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); | |
849 | return -ENOMEM; | |
850 | } | |
851 | ||
b70f5fa0 | 852 | mutex_init(&info->mutex); |
9f27ee59 JF |
853 | info->xbdev = dev; |
854 | info->vdevice = vdevice; | |
855 | info->connected = BLKIF_STATE_DISCONNECTED; | |
856 | INIT_WORK(&info->work, blkif_restart_queue); | |
857 | ||
858 | for (i = 0; i < BLK_RING_SIZE; i++) | |
859 | info->shadow[i].req.id = i+1; | |
860 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
861 | ||
862 | /* Front end dir is a number, which is used as the id. */ | |
863 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); | |
a1b4b12b | 864 | dev_set_drvdata(&dev->dev, info); |
9f27ee59 | 865 | |
203fd61f | 866 | err = talk_to_blkback(dev, info); |
9f27ee59 JF |
867 | if (err) { |
868 | kfree(info); | |
a1b4b12b | 869 | dev_set_drvdata(&dev->dev, NULL); |
9f27ee59 JF |
870 | return err; |
871 | } | |
872 | ||
873 | return 0; | |
874 | } | |
875 | ||
876 | ||
877 | static int blkif_recover(struct blkfront_info *info) | |
878 | { | |
879 | int i; | |
880 | struct blkif_request *req; | |
881 | struct blk_shadow *copy; | |
882 | int j; | |
883 | ||
884 | /* Stage 1: Make a safe copy of the shadow state. */ | |
a144ff09 IC |
885 | copy = kmalloc(sizeof(info->shadow), |
886 | GFP_NOIO | __GFP_REPEAT | __GFP_HIGH); | |
9f27ee59 JF |
887 | if (!copy) |
888 | return -ENOMEM; | |
889 | memcpy(copy, info->shadow, sizeof(info->shadow)); | |
890 | ||
891 | /* Stage 2: Set up free list. */ | |
892 | memset(&info->shadow, 0, sizeof(info->shadow)); | |
893 | for (i = 0; i < BLK_RING_SIZE; i++) | |
894 | info->shadow[i].req.id = i+1; | |
895 | info->shadow_free = info->ring.req_prod_pvt; | |
896 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
897 | ||
898 | /* Stage 3: Find pending requests and requeue them. */ | |
899 | for (i = 0; i < BLK_RING_SIZE; i++) { | |
900 | /* Not in use? */ | |
901 | if (copy[i].request == 0) | |
902 | continue; | |
903 | ||
904 | /* Grab a request slot and copy shadow state into it. */ | |
905 | req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
906 | *req = copy[i].req; | |
907 | ||
908 | /* We get a new request id, and must reset the shadow state. */ | |
909 | req->id = get_id_from_freelist(info); | |
910 | memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); | |
911 | ||
912 | /* Rewrite any grant references invalidated by susp/resume. */ | |
913 | for (j = 0; j < req->nr_segments; j++) | |
914 | gnttab_grant_foreign_access_ref( | |
915 | req->seg[j].gref, | |
916 | info->xbdev->otherend_id, | |
917 | pfn_to_mfn(info->shadow[req->id].frame[j]), | |
918 | rq_data_dir( | |
919 | (struct request *) | |
920 | info->shadow[req->id].request)); | |
921 | info->shadow[req->id].req = *req; | |
922 | ||
923 | info->ring.req_prod_pvt++; | |
924 | } | |
925 | ||
926 | kfree(copy); | |
927 | ||
928 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
929 | ||
930 | spin_lock_irq(&blkif_io_lock); | |
931 | ||
932 | /* Now safe for us to use the shared ring */ | |
933 | info->connected = BLKIF_STATE_CONNECTED; | |
934 | ||
935 | /* Send off requeued requests */ | |
936 | flush_requests(info); | |
937 | ||
938 | /* Kick any other new requests queued since we resumed */ | |
939 | kick_pending_request_queues(info); | |
940 | ||
941 | spin_unlock_irq(&blkif_io_lock); | |
942 | ||
943 | return 0; | |
944 | } | |
945 | ||
946 | /** | |
947 | * We are reconnecting to the backend, due to a suspend/resume, or a backend | |
948 | * driver restart. We tear down our blkif structure and recreate it, but | |
949 | * leave the device-layer structures intact so that this is transparent to the | |
950 | * rest of the kernel. | |
951 | */ | |
952 | static int blkfront_resume(struct xenbus_device *dev) | |
953 | { | |
a1b4b12b | 954 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
9f27ee59 JF |
955 | int err; |
956 | ||
957 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); | |
958 | ||
959 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); | |
960 | ||
203fd61f | 961 | err = talk_to_blkback(dev, info); |
9f27ee59 JF |
962 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) |
963 | err = blkif_recover(info); | |
964 | ||
965 | return err; | |
966 | } | |
967 | ||
b70f5fa0 DS |
968 | static void |
969 | blkfront_closing(struct blkfront_info *info) | |
970 | { | |
971 | struct xenbus_device *xbdev = info->xbdev; | |
972 | struct block_device *bdev = NULL; | |
973 | ||
974 | mutex_lock(&info->mutex); | |
975 | ||
976 | if (xbdev->state == XenbusStateClosing) { | |
977 | mutex_unlock(&info->mutex); | |
978 | return; | |
979 | } | |
980 | ||
981 | if (info->gd) | |
982 | bdev = bdget_disk(info->gd, 0); | |
983 | ||
984 | mutex_unlock(&info->mutex); | |
985 | ||
986 | if (!bdev) { | |
987 | xenbus_frontend_closed(xbdev); | |
988 | return; | |
989 | } | |
990 | ||
991 | mutex_lock(&bdev->bd_mutex); | |
992 | ||
7b32d104 | 993 | if (bdev->bd_openers) { |
b70f5fa0 DS |
994 | xenbus_dev_error(xbdev, -EBUSY, |
995 | "Device in use; refusing to close"); | |
996 | xenbus_switch_state(xbdev, XenbusStateClosing); | |
997 | } else { | |
998 | xlvbd_release_gendisk(info); | |
999 | xenbus_frontend_closed(xbdev); | |
1000 | } | |
1001 | ||
1002 | mutex_unlock(&bdev->bd_mutex); | |
1003 | bdput(bdev); | |
1004 | } | |
9f27ee59 JF |
1005 | |
1006 | /* | |
1007 | * Invoked when the backend is finally 'ready' (and has told produced | |
1008 | * the details about the physical device - #sectors, size, etc). | |
1009 | */ | |
1010 | static void blkfront_connect(struct blkfront_info *info) | |
1011 | { | |
1012 | unsigned long long sectors; | |
1013 | unsigned long sector_size; | |
1014 | unsigned int binfo; | |
1015 | int err; | |
7901d141 | 1016 | int barrier; |
9f27ee59 | 1017 | |
1fa73be6 S |
1018 | switch (info->connected) { |
1019 | case BLKIF_STATE_CONNECTED: | |
1020 | /* | |
1021 | * Potentially, the back-end may be signalling | |
1022 | * a capacity change; update the capacity. | |
1023 | */ | |
1024 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
1025 | "sectors", "%Lu", §ors); | |
1026 | if (XENBUS_EXIST_ERR(err)) | |
1027 | return; | |
1028 | printk(KERN_INFO "Setting capacity to %Lu\n", | |
1029 | sectors); | |
1030 | set_capacity(info->gd, sectors); | |
2def141e | 1031 | revalidate_disk(info->gd); |
1fa73be6 S |
1032 | |
1033 | /* fall through */ | |
1034 | case BLKIF_STATE_SUSPENDED: | |
9f27ee59 JF |
1035 | return; |
1036 | ||
b4dddb49 JF |
1037 | default: |
1038 | break; | |
1fa73be6 | 1039 | } |
9f27ee59 JF |
1040 | |
1041 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", | |
1042 | __func__, info->xbdev->otherend); | |
1043 | ||
1044 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
1045 | "sectors", "%llu", §ors, | |
1046 | "info", "%u", &binfo, | |
1047 | "sector-size", "%lu", §or_size, | |
1048 | NULL); | |
1049 | if (err) { | |
1050 | xenbus_dev_fatal(info->xbdev, err, | |
1051 | "reading backend fields at %s", | |
1052 | info->xbdev->otherend); | |
1053 | return; | |
1054 | } | |
1055 | ||
1056 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
7901d141 | 1057 | "feature-barrier", "%lu", &barrier, |
9f27ee59 | 1058 | NULL); |
7901d141 JF |
1059 | |
1060 | /* | |
1061 | * If there's no "feature-barrier" defined, then it means | |
1062 | * we're dealing with a very old backend which writes | |
4913efe4 | 1063 | * synchronously; nothing to do. |
7901d141 | 1064 | * |
6958f145 | 1065 | * If there are barriers, then we use flush. |
7901d141 | 1066 | */ |
4913efe4 | 1067 | info->feature_flush = 0; |
005a1d15 | 1068 | |
4913efe4 TH |
1069 | if (!err && barrier) |
1070 | info->feature_flush = REQ_FLUSH; | |
9f27ee59 | 1071 | |
9246b5f0 | 1072 | err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size); |
9f27ee59 JF |
1073 | if (err) { |
1074 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", | |
1075 | info->xbdev->otherend); | |
1076 | return; | |
1077 | } | |
1078 | ||
1079 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
1080 | ||
1081 | /* Kick pending requests. */ | |
1082 | spin_lock_irq(&blkif_io_lock); | |
1083 | info->connected = BLKIF_STATE_CONNECTED; | |
1084 | kick_pending_request_queues(info); | |
1085 | spin_unlock_irq(&blkif_io_lock); | |
1086 | ||
1087 | add_disk(info->gd); | |
1d78d705 CL |
1088 | |
1089 | info->is_ready = 1; | |
9f27ee59 JF |
1090 | } |
1091 | ||
9f27ee59 JF |
1092 | /** |
1093 | * Callback received when the backend's state changes. | |
1094 | */ | |
203fd61f | 1095 | static void blkback_changed(struct xenbus_device *dev, |
9f27ee59 JF |
1096 | enum xenbus_state backend_state) |
1097 | { | |
a1b4b12b | 1098 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
9f27ee59 | 1099 | |
203fd61f | 1100 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
9f27ee59 JF |
1101 | |
1102 | switch (backend_state) { | |
1103 | case XenbusStateInitialising: | |
1104 | case XenbusStateInitWait: | |
1105 | case XenbusStateInitialised: | |
b78c9512 NI |
1106 | case XenbusStateReconfiguring: |
1107 | case XenbusStateReconfigured: | |
9f27ee59 JF |
1108 | case XenbusStateUnknown: |
1109 | case XenbusStateClosed: | |
1110 | break; | |
1111 | ||
1112 | case XenbusStateConnected: | |
1113 | blkfront_connect(info); | |
1114 | break; | |
1115 | ||
1116 | case XenbusStateClosing: | |
b70f5fa0 | 1117 | blkfront_closing(info); |
9f27ee59 JF |
1118 | break; |
1119 | } | |
1120 | } | |
1121 | ||
fa1bd359 | 1122 | static int blkfront_remove(struct xenbus_device *xbdev) |
9f27ee59 | 1123 | { |
fa1bd359 DS |
1124 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
1125 | struct block_device *bdev = NULL; | |
1126 | struct gendisk *disk; | |
9f27ee59 | 1127 | |
fa1bd359 | 1128 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
9f27ee59 JF |
1129 | |
1130 | blkif_free(info, 0); | |
1131 | ||
fa1bd359 DS |
1132 | mutex_lock(&info->mutex); |
1133 | ||
1134 | disk = info->gd; | |
1135 | if (disk) | |
1136 | bdev = bdget_disk(disk, 0); | |
1137 | ||
1138 | info->xbdev = NULL; | |
1139 | mutex_unlock(&info->mutex); | |
1140 | ||
1141 | if (!bdev) { | |
1142 | kfree(info); | |
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * The xbdev was removed before we reached the Closed | |
1148 | * state. See if it's safe to remove the disk. If the bdev | |
1149 | * isn't closed yet, we let release take care of it. | |
1150 | */ | |
1151 | ||
1152 | mutex_lock(&bdev->bd_mutex); | |
1153 | info = disk->private_data; | |
1154 | ||
d54142c7 DS |
1155 | dev_warn(disk_to_dev(disk), |
1156 | "%s was hot-unplugged, %d stale handles\n", | |
1157 | xbdev->nodename, bdev->bd_openers); | |
1158 | ||
7b32d104 | 1159 | if (info && !bdev->bd_openers) { |
fa1bd359 DS |
1160 | xlvbd_release_gendisk(info); |
1161 | disk->private_data = NULL; | |
0e345826 | 1162 | kfree(info); |
fa1bd359 DS |
1163 | } |
1164 | ||
1165 | mutex_unlock(&bdev->bd_mutex); | |
1166 | bdput(bdev); | |
9f27ee59 JF |
1167 | |
1168 | return 0; | |
1169 | } | |
1170 | ||
1d78d705 CL |
1171 | static int blkfront_is_ready(struct xenbus_device *dev) |
1172 | { | |
a1b4b12b | 1173 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
1d78d705 | 1174 | |
5d7ed20e | 1175 | return info->is_ready && info->xbdev; |
1d78d705 CL |
1176 | } |
1177 | ||
a63c848b | 1178 | static int blkif_open(struct block_device *bdev, fmode_t mode) |
9f27ee59 | 1179 | { |
13961743 DS |
1180 | struct gendisk *disk = bdev->bd_disk; |
1181 | struct blkfront_info *info; | |
1182 | int err = 0; | |
6e9624b8 | 1183 | |
2a48fc0a | 1184 | mutex_lock(&blkfront_mutex); |
6e9624b8 | 1185 | |
13961743 DS |
1186 | info = disk->private_data; |
1187 | if (!info) { | |
1188 | /* xbdev gone */ | |
1189 | err = -ERESTARTSYS; | |
1190 | goto out; | |
1191 | } | |
1192 | ||
1193 | mutex_lock(&info->mutex); | |
1194 | ||
1195 | if (!info->gd) | |
1196 | /* xbdev is closed */ | |
1197 | err = -ERESTARTSYS; | |
1198 | ||
1199 | mutex_unlock(&info->mutex); | |
1200 | ||
13961743 | 1201 | out: |
2a48fc0a | 1202 | mutex_unlock(&blkfront_mutex); |
13961743 | 1203 | return err; |
9f27ee59 JF |
1204 | } |
1205 | ||
a63c848b | 1206 | static int blkif_release(struct gendisk *disk, fmode_t mode) |
9f27ee59 | 1207 | { |
a63c848b | 1208 | struct blkfront_info *info = disk->private_data; |
7fd152f4 DS |
1209 | struct block_device *bdev; |
1210 | struct xenbus_device *xbdev; | |
1211 | ||
2a48fc0a | 1212 | mutex_lock(&blkfront_mutex); |
7fd152f4 DS |
1213 | |
1214 | bdev = bdget_disk(disk, 0); | |
1215 | bdput(bdev); | |
1216 | ||
acfca3c6 DS |
1217 | if (bdev->bd_openers) |
1218 | goto out; | |
1219 | ||
7fd152f4 DS |
1220 | /* |
1221 | * Check if we have been instructed to close. We will have | |
1222 | * deferred this request, because the bdev was still open. | |
1223 | */ | |
1224 | ||
1225 | mutex_lock(&info->mutex); | |
1226 | xbdev = info->xbdev; | |
1227 | ||
1228 | if (xbdev && xbdev->state == XenbusStateClosing) { | |
1229 | /* pending switch to state closed */ | |
d54142c7 | 1230 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
7fd152f4 DS |
1231 | xlvbd_release_gendisk(info); |
1232 | xenbus_frontend_closed(info->xbdev); | |
1233 | } | |
1234 | ||
1235 | mutex_unlock(&info->mutex); | |
1236 | ||
1237 | if (!xbdev) { | |
1238 | /* sudden device removal */ | |
d54142c7 | 1239 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
7fd152f4 DS |
1240 | xlvbd_release_gendisk(info); |
1241 | disk->private_data = NULL; | |
1242 | kfree(info); | |
9f27ee59 | 1243 | } |
7fd152f4 | 1244 | |
a4cc14ec | 1245 | out: |
2a48fc0a | 1246 | mutex_unlock(&blkfront_mutex); |
9f27ee59 JF |
1247 | return 0; |
1248 | } | |
1249 | ||
83d5cde4 | 1250 | static const struct block_device_operations xlvbd_block_fops = |
9f27ee59 JF |
1251 | { |
1252 | .owner = THIS_MODULE, | |
a63c848b AV |
1253 | .open = blkif_open, |
1254 | .release = blkif_release, | |
597592d9 | 1255 | .getgeo = blkif_getgeo, |
8a6cfeb6 | 1256 | .ioctl = blkif_ioctl, |
9f27ee59 JF |
1257 | }; |
1258 | ||
1259 | ||
ec9c42ec | 1260 | static const struct xenbus_device_id blkfront_ids[] = { |
9f27ee59 JF |
1261 | { "vbd" }, |
1262 | { "" } | |
1263 | }; | |
1264 | ||
1265 | static struct xenbus_driver blkfront = { | |
1266 | .name = "vbd", | |
1267 | .owner = THIS_MODULE, | |
1268 | .ids = blkfront_ids, | |
1269 | .probe = blkfront_probe, | |
1270 | .remove = blkfront_remove, | |
1271 | .resume = blkfront_resume, | |
203fd61f | 1272 | .otherend_changed = blkback_changed, |
1d78d705 | 1273 | .is_ready = blkfront_is_ready, |
9f27ee59 JF |
1274 | }; |
1275 | ||
1276 | static int __init xlblk_init(void) | |
1277 | { | |
6e833587 | 1278 | if (!xen_domain()) |
9f27ee59 JF |
1279 | return -ENODEV; |
1280 | ||
1281 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { | |
1282 | printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", | |
1283 | XENVBD_MAJOR, DEV_NAME); | |
1284 | return -ENODEV; | |
1285 | } | |
1286 | ||
1287 | return xenbus_register_frontend(&blkfront); | |
1288 | } | |
1289 | module_init(xlblk_init); | |
1290 | ||
1291 | ||
5a60d0cd | 1292 | static void __exit xlblk_exit(void) |
9f27ee59 JF |
1293 | { |
1294 | return xenbus_unregister_driver(&blkfront); | |
1295 | } | |
1296 | module_exit(xlblk_exit); | |
1297 | ||
1298 | MODULE_DESCRIPTION("Xen virtual block device frontend"); | |
1299 | MODULE_LICENSE("GPL"); | |
1300 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); | |
d2f0c52b | 1301 | MODULE_ALIAS("xen:vbd"); |
4f93f09b | 1302 | MODULE_ALIAS("xenblk"); |