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