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