]>
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> |
9f27ee59 JF |
41 | #include <linux/module.h> |
42 | ||
43 | #include <xen/xenbus.h> | |
44 | #include <xen/grant_table.h> | |
45 | #include <xen/events.h> | |
46 | #include <xen/page.h> | |
47 | ||
48 | #include <xen/interface/grant_table.h> | |
49 | #include <xen/interface/io/blkif.h> | |
3e334239 | 50 | #include <xen/interface/io/protocols.h> |
9f27ee59 JF |
51 | |
52 | #include <asm/xen/hypervisor.h> | |
53 | ||
54 | enum blkif_state { | |
55 | BLKIF_STATE_DISCONNECTED, | |
56 | BLKIF_STATE_CONNECTED, | |
57 | BLKIF_STATE_SUSPENDED, | |
58 | }; | |
59 | ||
60 | struct blk_shadow { | |
61 | struct blkif_request req; | |
62 | unsigned long request; | |
63 | unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST]; | |
64 | }; | |
65 | ||
66 | static struct block_device_operations xlvbd_block_fops; | |
67 | ||
68 | #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE) | |
69 | ||
70 | /* | |
71 | * We have one of these per vbd, whether ide, scsi or 'other'. They | |
72 | * hang in private_data off the gendisk structure. We may end up | |
73 | * putting all kinds of interesting stuff here :-) | |
74 | */ | |
75 | struct blkfront_info | |
76 | { | |
77 | struct xenbus_device *xbdev; | |
9f27ee59 JF |
78 | struct gendisk *gd; |
79 | int vdevice; | |
80 | blkif_vdev_t handle; | |
81 | enum blkif_state connected; | |
82 | int ring_ref; | |
83 | struct blkif_front_ring ring; | |
84 | unsigned int evtchn, irq; | |
85 | struct request_queue *rq; | |
86 | struct work_struct work; | |
87 | struct gnttab_free_callback callback; | |
88 | struct blk_shadow shadow[BLK_RING_SIZE]; | |
89 | unsigned long shadow_free; | |
90 | int feature_barrier; | |
1d78d705 | 91 | int is_ready; |
9f27ee59 JF |
92 | |
93 | /** | |
94 | * The number of people holding this device open. We won't allow a | |
95 | * hot-unplug unless this is 0. | |
96 | */ | |
97 | int users; | |
98 | }; | |
99 | ||
100 | static DEFINE_SPINLOCK(blkif_io_lock); | |
101 | ||
102 | #define MAXIMUM_OUTSTANDING_BLOCK_REQS \ | |
103 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE) | |
104 | #define GRANT_INVALID_REF 0 | |
105 | ||
106 | #define PARTS_PER_DISK 16 | |
107 | ||
108 | #define BLKIF_MAJOR(dev) ((dev)>>8) | |
109 | #define BLKIF_MINOR(dev) ((dev) & 0xff) | |
110 | ||
111 | #define DEV_NAME "xvd" /* name in /dev */ | |
112 | ||
113 | /* Information about our VBDs. */ | |
114 | #define MAX_VBDS 64 | |
115 | static LIST_HEAD(vbds_list); | |
116 | ||
117 | static int get_id_from_freelist(struct blkfront_info *info) | |
118 | { | |
119 | unsigned long free = info->shadow_free; | |
120 | BUG_ON(free > BLK_RING_SIZE); | |
121 | info->shadow_free = info->shadow[free].req.id; | |
122 | info->shadow[free].req.id = 0x0fffffee; /* debug */ | |
123 | return free; | |
124 | } | |
125 | ||
126 | static void add_id_to_freelist(struct blkfront_info *info, | |
127 | unsigned long id) | |
128 | { | |
129 | info->shadow[id].req.id = info->shadow_free; | |
130 | info->shadow[id].request = 0; | |
131 | info->shadow_free = id; | |
132 | } | |
133 | ||
134 | static void blkif_restart_queue_callback(void *arg) | |
135 | { | |
136 | struct blkfront_info *info = (struct blkfront_info *)arg; | |
137 | schedule_work(&info->work); | |
138 | } | |
139 | ||
597592d9 IC |
140 | int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
141 | { | |
142 | /* We don't have real geometry info, but let's at least return | |
143 | values consistent with the size of the device */ | |
144 | sector_t nsect = get_capacity(bd->bd_disk); | |
145 | sector_t cylinders = nsect; | |
146 | ||
147 | hg->heads = 0xff; | |
148 | hg->sectors = 0x3f; | |
149 | sector_div(cylinders, hg->heads * hg->sectors); | |
150 | hg->cylinders = cylinders; | |
151 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) | |
152 | hg->cylinders = 0xffff; | |
153 | return 0; | |
154 | } | |
155 | ||
9f27ee59 JF |
156 | /* |
157 | * blkif_queue_request | |
158 | * | |
159 | * request block io | |
160 | * | |
161 | * id: for guest use only. | |
162 | * operation: BLKIF_OP_{READ,WRITE,PROBE} | |
163 | * buffer: buffer to read/write into. this should be a | |
164 | * virtual address in the guest os. | |
165 | */ | |
166 | static int blkif_queue_request(struct request *req) | |
167 | { | |
168 | struct blkfront_info *info = req->rq_disk->private_data; | |
169 | unsigned long buffer_mfn; | |
170 | struct blkif_request *ring_req; | |
5705f702 | 171 | struct req_iterator iter; |
9f27ee59 | 172 | struct bio_vec *bvec; |
9f27ee59 JF |
173 | unsigned long id; |
174 | unsigned int fsect, lsect; | |
175 | int ref; | |
176 | grant_ref_t gref_head; | |
177 | ||
178 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) | |
179 | return 1; | |
180 | ||
181 | if (gnttab_alloc_grant_references( | |
182 | BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) { | |
183 | gnttab_request_free_callback( | |
184 | &info->callback, | |
185 | blkif_restart_queue_callback, | |
186 | info, | |
187 | BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
188 | return 1; | |
189 | } | |
190 | ||
191 | /* Fill out a communications ring structure. */ | |
192 | ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
193 | id = get_id_from_freelist(info); | |
194 | info->shadow[id].request = (unsigned long)req; | |
195 | ||
196 | ring_req->id = id; | |
197 | ring_req->sector_number = (blkif_sector_t)req->sector; | |
198 | ring_req->handle = info->handle; | |
199 | ||
200 | ring_req->operation = rq_data_dir(req) ? | |
201 | BLKIF_OP_WRITE : BLKIF_OP_READ; | |
202 | if (blk_barrier_rq(req)) | |
203 | ring_req->operation = BLKIF_OP_WRITE_BARRIER; | |
204 | ||
205 | ring_req->nr_segments = 0; | |
5705f702 | 206 | rq_for_each_segment(bvec, req, iter) { |
6c92e699 JA |
207 | BUG_ON(ring_req->nr_segments == BLKIF_MAX_SEGMENTS_PER_REQUEST); |
208 | buffer_mfn = pfn_to_mfn(page_to_pfn(bvec->bv_page)); | |
209 | fsect = bvec->bv_offset >> 9; | |
210 | lsect = fsect + (bvec->bv_len >> 9) - 1; | |
211 | /* install a grant reference. */ | |
212 | ref = gnttab_claim_grant_reference(&gref_head); | |
213 | BUG_ON(ref == -ENOSPC); | |
214 | ||
215 | gnttab_grant_foreign_access_ref( | |
9f27ee59 JF |
216 | ref, |
217 | info->xbdev->otherend_id, | |
218 | buffer_mfn, | |
219 | rq_data_dir(req) ); | |
220 | ||
6c92e699 | 221 | info->shadow[id].frame[ring_req->nr_segments] = |
9f27ee59 JF |
222 | mfn_to_pfn(buffer_mfn); |
223 | ||
6c92e699 | 224 | ring_req->seg[ring_req->nr_segments] = |
9f27ee59 JF |
225 | (struct blkif_request_segment) { |
226 | .gref = ref, | |
227 | .first_sect = fsect, | |
228 | .last_sect = lsect }; | |
229 | ||
6c92e699 | 230 | ring_req->nr_segments++; |
9f27ee59 JF |
231 | } |
232 | ||
233 | info->ring.req_prod_pvt++; | |
234 | ||
235 | /* Keep a private copy so we can reissue requests when recovering. */ | |
236 | info->shadow[id].req = *ring_req; | |
237 | ||
238 | gnttab_free_grant_references(gref_head); | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | ||
244 | static inline void flush_requests(struct blkfront_info *info) | |
245 | { | |
246 | int notify; | |
247 | ||
248 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify); | |
249 | ||
250 | if (notify) | |
251 | notify_remote_via_irq(info->irq); | |
252 | } | |
253 | ||
254 | /* | |
255 | * do_blkif_request | |
256 | * read a block; request is in a request queue | |
257 | */ | |
165125e1 | 258 | static void do_blkif_request(struct request_queue *rq) |
9f27ee59 JF |
259 | { |
260 | struct blkfront_info *info = NULL; | |
261 | struct request *req; | |
262 | int queued; | |
263 | ||
264 | pr_debug("Entered do_blkif_request\n"); | |
265 | ||
266 | queued = 0; | |
267 | ||
268 | while ((req = elv_next_request(rq)) != NULL) { | |
269 | info = req->rq_disk->private_data; | |
270 | if (!blk_fs_request(req)) { | |
271 | end_request(req, 0); | |
272 | continue; | |
273 | } | |
274 | ||
275 | if (RING_FULL(&info->ring)) | |
276 | goto wait; | |
277 | ||
278 | pr_debug("do_blk_req %p: cmd %p, sec %lx, " | |
279 | "(%u/%li) buffer:%p [%s]\n", | |
280 | req, req->cmd, (unsigned long)req->sector, | |
281 | req->current_nr_sectors, | |
282 | req->nr_sectors, req->buffer, | |
283 | rq_data_dir(req) ? "write" : "read"); | |
284 | ||
285 | ||
286 | blkdev_dequeue_request(req); | |
287 | if (blkif_queue_request(req)) { | |
288 | blk_requeue_request(rq, req); | |
289 | wait: | |
290 | /* Avoid pointless unplugs. */ | |
291 | blk_stop_queue(rq); | |
292 | break; | |
293 | } | |
294 | ||
295 | queued++; | |
296 | } | |
297 | ||
298 | if (queued != 0) | |
299 | flush_requests(info); | |
300 | } | |
301 | ||
302 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size) | |
303 | { | |
165125e1 | 304 | struct request_queue *rq; |
9f27ee59 JF |
305 | |
306 | rq = blk_init_queue(do_blkif_request, &blkif_io_lock); | |
307 | if (rq == NULL) | |
308 | return -1; | |
309 | ||
310 | elevator_init(rq, "noop"); | |
311 | ||
312 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ | |
313 | blk_queue_hardsect_size(rq, sector_size); | |
314 | blk_queue_max_sectors(rq, 512); | |
315 | ||
316 | /* Each segment in a request is up to an aligned page in size. */ | |
317 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); | |
318 | blk_queue_max_segment_size(rq, PAGE_SIZE); | |
319 | ||
320 | /* Ensure a merged request will fit in a single I/O ring slot. */ | |
321 | blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
322 | blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST); | |
323 | ||
324 | /* Make sure buffer addresses are sector-aligned. */ | |
325 | blk_queue_dma_alignment(rq, 511); | |
326 | ||
327 | gd->queue = rq; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | ||
333 | static int xlvbd_barrier(struct blkfront_info *info) | |
334 | { | |
335 | int err; | |
336 | ||
337 | err = blk_queue_ordered(info->rq, | |
338 | info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE, | |
339 | NULL); | |
340 | ||
341 | if (err) | |
342 | return err; | |
343 | ||
344 | printk(KERN_INFO "blkfront: %s: barriers %s\n", | |
345 | info->gd->disk_name, | |
346 | info->feature_barrier ? "enabled" : "disabled"); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | ||
351 | static int xlvbd_alloc_gendisk(int minor, blkif_sector_t capacity, | |
352 | int vdevice, u16 vdisk_info, u16 sector_size, | |
353 | struct blkfront_info *info) | |
354 | { | |
355 | struct gendisk *gd; | |
356 | int nr_minors = 1; | |
357 | int err = -ENODEV; | |
358 | ||
359 | BUG_ON(info->gd != NULL); | |
360 | BUG_ON(info->rq != NULL); | |
361 | ||
362 | if ((minor % PARTS_PER_DISK) == 0) | |
363 | nr_minors = PARTS_PER_DISK; | |
364 | ||
365 | gd = alloc_disk(nr_minors); | |
366 | if (gd == NULL) | |
367 | goto out; | |
368 | ||
369 | if (nr_minors > 1) | |
370 | sprintf(gd->disk_name, "%s%c", DEV_NAME, | |
371 | 'a' + minor / PARTS_PER_DISK); | |
372 | else | |
373 | sprintf(gd->disk_name, "%s%c%d", DEV_NAME, | |
374 | 'a' + minor / PARTS_PER_DISK, | |
375 | minor % PARTS_PER_DISK); | |
376 | ||
377 | gd->major = XENVBD_MAJOR; | |
378 | gd->first_minor = minor; | |
379 | gd->fops = &xlvbd_block_fops; | |
380 | gd->private_data = info; | |
381 | gd->driverfs_dev = &(info->xbdev->dev); | |
382 | set_capacity(gd, capacity); | |
383 | ||
384 | if (xlvbd_init_blk_queue(gd, sector_size)) { | |
385 | del_gendisk(gd); | |
386 | goto out; | |
387 | } | |
388 | ||
389 | info->rq = gd->queue; | |
390 | info->gd = gd; | |
391 | ||
392 | if (info->feature_barrier) | |
393 | xlvbd_barrier(info); | |
394 | ||
395 | if (vdisk_info & VDISK_READONLY) | |
396 | set_disk_ro(gd, 1); | |
397 | ||
398 | if (vdisk_info & VDISK_REMOVABLE) | |
399 | gd->flags |= GENHD_FL_REMOVABLE; | |
400 | ||
401 | if (vdisk_info & VDISK_CDROM) | |
402 | gd->flags |= GENHD_FL_CD; | |
403 | ||
404 | return 0; | |
405 | ||
406 | out: | |
407 | return err; | |
408 | } | |
409 | ||
410 | static void kick_pending_request_queues(struct blkfront_info *info) | |
411 | { | |
412 | if (!RING_FULL(&info->ring)) { | |
413 | /* Re-enable calldowns. */ | |
414 | blk_start_queue(info->rq); | |
415 | /* Kick things off immediately. */ | |
416 | do_blkif_request(info->rq); | |
417 | } | |
418 | } | |
419 | ||
420 | static void blkif_restart_queue(struct work_struct *work) | |
421 | { | |
422 | struct blkfront_info *info = container_of(work, struct blkfront_info, work); | |
423 | ||
424 | spin_lock_irq(&blkif_io_lock); | |
425 | if (info->connected == BLKIF_STATE_CONNECTED) | |
426 | kick_pending_request_queues(info); | |
427 | spin_unlock_irq(&blkif_io_lock); | |
428 | } | |
429 | ||
430 | static void blkif_free(struct blkfront_info *info, int suspend) | |
431 | { | |
432 | /* Prevent new requests being issued until we fix things up. */ | |
433 | spin_lock_irq(&blkif_io_lock); | |
434 | info->connected = suspend ? | |
435 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; | |
436 | /* No more blkif_request(). */ | |
437 | if (info->rq) | |
438 | blk_stop_queue(info->rq); | |
439 | /* No more gnttab callback work. */ | |
440 | gnttab_cancel_free_callback(&info->callback); | |
441 | spin_unlock_irq(&blkif_io_lock); | |
442 | ||
443 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
444 | flush_scheduled_work(); | |
445 | ||
446 | /* Free resources associated with old device channel. */ | |
447 | if (info->ring_ref != GRANT_INVALID_REF) { | |
448 | gnttab_end_foreign_access(info->ring_ref, 0, | |
449 | (unsigned long)info->ring.sring); | |
450 | info->ring_ref = GRANT_INVALID_REF; | |
451 | info->ring.sring = NULL; | |
452 | } | |
453 | if (info->irq) | |
454 | unbind_from_irqhandler(info->irq, info); | |
455 | info->evtchn = info->irq = 0; | |
456 | ||
457 | } | |
458 | ||
459 | static void blkif_completion(struct blk_shadow *s) | |
460 | { | |
461 | int i; | |
462 | for (i = 0; i < s->req.nr_segments; i++) | |
463 | gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL); | |
464 | } | |
465 | ||
466 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) | |
467 | { | |
468 | struct request *req; | |
469 | struct blkif_response *bret; | |
470 | RING_IDX i, rp; | |
471 | unsigned long flags; | |
472 | struct blkfront_info *info = (struct blkfront_info *)dev_id; | |
f530f036 | 473 | int error; |
9f27ee59 JF |
474 | |
475 | spin_lock_irqsave(&blkif_io_lock, flags); | |
476 | ||
477 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { | |
478 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
479 | return IRQ_HANDLED; | |
480 | } | |
481 | ||
482 | again: | |
483 | rp = info->ring.sring->rsp_prod; | |
484 | rmb(); /* Ensure we see queued responses up to 'rp'. */ | |
485 | ||
486 | for (i = info->ring.rsp_cons; i != rp; i++) { | |
487 | unsigned long id; | |
488 | int ret; | |
489 | ||
490 | bret = RING_GET_RESPONSE(&info->ring, i); | |
491 | id = bret->id; | |
492 | req = (struct request *)info->shadow[id].request; | |
493 | ||
494 | blkif_completion(&info->shadow[id]); | |
495 | ||
496 | add_id_to_freelist(info, id); | |
497 | ||
f530f036 | 498 | error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO; |
9f27ee59 JF |
499 | switch (bret->operation) { |
500 | case BLKIF_OP_WRITE_BARRIER: | |
501 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { | |
502 | printk(KERN_WARNING "blkfront: %s: write barrier op failed\n", | |
503 | info->gd->disk_name); | |
f530f036 | 504 | error = -EOPNOTSUPP; |
9f27ee59 JF |
505 | info->feature_barrier = 0; |
506 | xlvbd_barrier(info); | |
507 | } | |
508 | /* fall through */ | |
509 | case BLKIF_OP_READ: | |
510 | case BLKIF_OP_WRITE: | |
511 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) | |
512 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " | |
513 | "request: %x\n", bret->status); | |
514 | ||
f530f036 | 515 | ret = __blk_end_request(req, error, blk_rq_bytes(req)); |
9f27ee59 | 516 | BUG_ON(ret); |
9f27ee59 JF |
517 | break; |
518 | default: | |
519 | BUG(); | |
520 | } | |
521 | } | |
522 | ||
523 | info->ring.rsp_cons = i; | |
524 | ||
525 | if (i != info->ring.req_prod_pvt) { | |
526 | int more_to_do; | |
527 | RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); | |
528 | if (more_to_do) | |
529 | goto again; | |
530 | } else | |
531 | info->ring.sring->rsp_event = i + 1; | |
532 | ||
533 | kick_pending_request_queues(info); | |
534 | ||
535 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
536 | ||
537 | return IRQ_HANDLED; | |
538 | } | |
539 | ||
540 | ||
541 | static int setup_blkring(struct xenbus_device *dev, | |
542 | struct blkfront_info *info) | |
543 | { | |
544 | struct blkif_sring *sring; | |
545 | int err; | |
546 | ||
547 | info->ring_ref = GRANT_INVALID_REF; | |
548 | ||
549 | sring = (struct blkif_sring *)__get_free_page(GFP_KERNEL); | |
550 | if (!sring) { | |
551 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); | |
552 | return -ENOMEM; | |
553 | } | |
554 | SHARED_RING_INIT(sring); | |
555 | FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); | |
556 | ||
557 | err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); | |
558 | if (err < 0) { | |
559 | free_page((unsigned long)sring); | |
560 | info->ring.sring = NULL; | |
561 | goto fail; | |
562 | } | |
563 | info->ring_ref = err; | |
564 | ||
565 | err = xenbus_alloc_evtchn(dev, &info->evtchn); | |
566 | if (err) | |
567 | goto fail; | |
568 | ||
569 | err = bind_evtchn_to_irqhandler(info->evtchn, | |
570 | blkif_interrupt, | |
571 | IRQF_SAMPLE_RANDOM, "blkif", info); | |
572 | if (err <= 0) { | |
573 | xenbus_dev_fatal(dev, err, | |
574 | "bind_evtchn_to_irqhandler failed"); | |
575 | goto fail; | |
576 | } | |
577 | info->irq = err; | |
578 | ||
579 | return 0; | |
580 | fail: | |
581 | blkif_free(info, 0); | |
582 | return err; | |
583 | } | |
584 | ||
585 | ||
586 | /* Common code used when first setting up, and when resuming. */ | |
587 | static int talk_to_backend(struct xenbus_device *dev, | |
588 | struct blkfront_info *info) | |
589 | { | |
590 | const char *message = NULL; | |
591 | struct xenbus_transaction xbt; | |
592 | int err; | |
593 | ||
594 | /* Create shared ring, alloc event channel. */ | |
595 | err = setup_blkring(dev, info); | |
596 | if (err) | |
597 | goto out; | |
598 | ||
599 | again: | |
600 | err = xenbus_transaction_start(&xbt); | |
601 | if (err) { | |
602 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
603 | goto destroy_blkring; | |
604 | } | |
605 | ||
606 | err = xenbus_printf(xbt, dev->nodename, | |
607 | "ring-ref", "%u", info->ring_ref); | |
608 | if (err) { | |
609 | message = "writing ring-ref"; | |
610 | goto abort_transaction; | |
611 | } | |
612 | err = xenbus_printf(xbt, dev->nodename, | |
613 | "event-channel", "%u", info->evtchn); | |
614 | if (err) { | |
615 | message = "writing event-channel"; | |
616 | goto abort_transaction; | |
617 | } | |
3e334239 MA |
618 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
619 | XEN_IO_PROTO_ABI_NATIVE); | |
620 | if (err) { | |
621 | message = "writing protocol"; | |
622 | goto abort_transaction; | |
623 | } | |
9f27ee59 JF |
624 | |
625 | err = xenbus_transaction_end(xbt, 0); | |
626 | if (err) { | |
627 | if (err == -EAGAIN) | |
628 | goto again; | |
629 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
630 | goto destroy_blkring; | |
631 | } | |
632 | ||
633 | xenbus_switch_state(dev, XenbusStateInitialised); | |
634 | ||
635 | return 0; | |
636 | ||
637 | abort_transaction: | |
638 | xenbus_transaction_end(xbt, 1); | |
639 | if (message) | |
640 | xenbus_dev_fatal(dev, err, "%s", message); | |
641 | destroy_blkring: | |
642 | blkif_free(info, 0); | |
643 | out: | |
644 | return err; | |
645 | } | |
646 | ||
647 | ||
648 | /** | |
649 | * Entry point to this code when a new device is created. Allocate the basic | |
650 | * structures and the ring buffer for communication with the backend, and | |
651 | * inform the backend of the appropriate details for those. Switch to | |
652 | * Initialised state. | |
653 | */ | |
654 | static int blkfront_probe(struct xenbus_device *dev, | |
655 | const struct xenbus_device_id *id) | |
656 | { | |
657 | int err, vdevice, i; | |
658 | struct blkfront_info *info; | |
659 | ||
660 | /* FIXME: Use dynamic device id if this is not set. */ | |
661 | err = xenbus_scanf(XBT_NIL, dev->nodename, | |
662 | "virtual-device", "%i", &vdevice); | |
663 | if (err != 1) { | |
664 | xenbus_dev_fatal(dev, err, "reading virtual-device"); | |
665 | return err; | |
666 | } | |
667 | ||
668 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
669 | if (!info) { | |
670 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); | |
671 | return -ENOMEM; | |
672 | } | |
673 | ||
674 | info->xbdev = dev; | |
675 | info->vdevice = vdevice; | |
676 | info->connected = BLKIF_STATE_DISCONNECTED; | |
677 | INIT_WORK(&info->work, blkif_restart_queue); | |
678 | ||
679 | for (i = 0; i < BLK_RING_SIZE; i++) | |
680 | info->shadow[i].req.id = i+1; | |
681 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
682 | ||
683 | /* Front end dir is a number, which is used as the id. */ | |
684 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); | |
685 | dev->dev.driver_data = info; | |
686 | ||
687 | err = talk_to_backend(dev, info); | |
688 | if (err) { | |
689 | kfree(info); | |
690 | dev->dev.driver_data = NULL; | |
691 | return err; | |
692 | } | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | ||
698 | static int blkif_recover(struct blkfront_info *info) | |
699 | { | |
700 | int i; | |
701 | struct blkif_request *req; | |
702 | struct blk_shadow *copy; | |
703 | int j; | |
704 | ||
705 | /* Stage 1: Make a safe copy of the shadow state. */ | |
706 | copy = kmalloc(sizeof(info->shadow), GFP_KERNEL); | |
707 | if (!copy) | |
708 | return -ENOMEM; | |
709 | memcpy(copy, info->shadow, sizeof(info->shadow)); | |
710 | ||
711 | /* Stage 2: Set up free list. */ | |
712 | memset(&info->shadow, 0, sizeof(info->shadow)); | |
713 | for (i = 0; i < BLK_RING_SIZE; i++) | |
714 | info->shadow[i].req.id = i+1; | |
715 | info->shadow_free = info->ring.req_prod_pvt; | |
716 | info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; | |
717 | ||
718 | /* Stage 3: Find pending requests and requeue them. */ | |
719 | for (i = 0; i < BLK_RING_SIZE; i++) { | |
720 | /* Not in use? */ | |
721 | if (copy[i].request == 0) | |
722 | continue; | |
723 | ||
724 | /* Grab a request slot and copy shadow state into it. */ | |
725 | req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); | |
726 | *req = copy[i].req; | |
727 | ||
728 | /* We get a new request id, and must reset the shadow state. */ | |
729 | req->id = get_id_from_freelist(info); | |
730 | memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); | |
731 | ||
732 | /* Rewrite any grant references invalidated by susp/resume. */ | |
733 | for (j = 0; j < req->nr_segments; j++) | |
734 | gnttab_grant_foreign_access_ref( | |
735 | req->seg[j].gref, | |
736 | info->xbdev->otherend_id, | |
737 | pfn_to_mfn(info->shadow[req->id].frame[j]), | |
738 | rq_data_dir( | |
739 | (struct request *) | |
740 | info->shadow[req->id].request)); | |
741 | info->shadow[req->id].req = *req; | |
742 | ||
743 | info->ring.req_prod_pvt++; | |
744 | } | |
745 | ||
746 | kfree(copy); | |
747 | ||
748 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
749 | ||
750 | spin_lock_irq(&blkif_io_lock); | |
751 | ||
752 | /* Now safe for us to use the shared ring */ | |
753 | info->connected = BLKIF_STATE_CONNECTED; | |
754 | ||
755 | /* Send off requeued requests */ | |
756 | flush_requests(info); | |
757 | ||
758 | /* Kick any other new requests queued since we resumed */ | |
759 | kick_pending_request_queues(info); | |
760 | ||
761 | spin_unlock_irq(&blkif_io_lock); | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
766 | /** | |
767 | * We are reconnecting to the backend, due to a suspend/resume, or a backend | |
768 | * driver restart. We tear down our blkif structure and recreate it, but | |
769 | * leave the device-layer structures intact so that this is transparent to the | |
770 | * rest of the kernel. | |
771 | */ | |
772 | static int blkfront_resume(struct xenbus_device *dev) | |
773 | { | |
774 | struct blkfront_info *info = dev->dev.driver_data; | |
775 | int err; | |
776 | ||
777 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); | |
778 | ||
779 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); | |
780 | ||
781 | err = talk_to_backend(dev, info); | |
782 | if (info->connected == BLKIF_STATE_SUSPENDED && !err) | |
783 | err = blkif_recover(info); | |
784 | ||
785 | return err; | |
786 | } | |
787 | ||
788 | ||
789 | /* | |
790 | * Invoked when the backend is finally 'ready' (and has told produced | |
791 | * the details about the physical device - #sectors, size, etc). | |
792 | */ | |
793 | static void blkfront_connect(struct blkfront_info *info) | |
794 | { | |
795 | unsigned long long sectors; | |
796 | unsigned long sector_size; | |
797 | unsigned int binfo; | |
798 | int err; | |
799 | ||
800 | if ((info->connected == BLKIF_STATE_CONNECTED) || | |
801 | (info->connected == BLKIF_STATE_SUSPENDED) ) | |
802 | return; | |
803 | ||
804 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", | |
805 | __func__, info->xbdev->otherend); | |
806 | ||
807 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
808 | "sectors", "%llu", §ors, | |
809 | "info", "%u", &binfo, | |
810 | "sector-size", "%lu", §or_size, | |
811 | NULL); | |
812 | if (err) { | |
813 | xenbus_dev_fatal(info->xbdev, err, | |
814 | "reading backend fields at %s", | |
815 | info->xbdev->otherend); | |
816 | return; | |
817 | } | |
818 | ||
819 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
820 | "feature-barrier", "%lu", &info->feature_barrier, | |
821 | NULL); | |
822 | if (err) | |
823 | info->feature_barrier = 0; | |
824 | ||
825 | err = xlvbd_alloc_gendisk(BLKIF_MINOR(info->vdevice), | |
826 | sectors, info->vdevice, | |
827 | binfo, sector_size, info); | |
828 | if (err) { | |
829 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", | |
830 | info->xbdev->otherend); | |
831 | return; | |
832 | } | |
833 | ||
834 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
835 | ||
836 | /* Kick pending requests. */ | |
837 | spin_lock_irq(&blkif_io_lock); | |
838 | info->connected = BLKIF_STATE_CONNECTED; | |
839 | kick_pending_request_queues(info); | |
840 | spin_unlock_irq(&blkif_io_lock); | |
841 | ||
842 | add_disk(info->gd); | |
1d78d705 CL |
843 | |
844 | info->is_ready = 1; | |
9f27ee59 JF |
845 | } |
846 | ||
847 | /** | |
848 | * Handle the change of state of the backend to Closing. We must delete our | |
849 | * device-layer structures now, to ensure that writes are flushed through to | |
850 | * the backend. Once is this done, we can switch to Closed in | |
851 | * acknowledgement. | |
852 | */ | |
853 | static void blkfront_closing(struct xenbus_device *dev) | |
854 | { | |
855 | struct blkfront_info *info = dev->dev.driver_data; | |
856 | unsigned long flags; | |
857 | ||
858 | dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename); | |
859 | ||
860 | if (info->rq == NULL) | |
861 | goto out; | |
862 | ||
863 | spin_lock_irqsave(&blkif_io_lock, flags); | |
864 | ||
865 | del_gendisk(info->gd); | |
866 | ||
867 | /* No more blkif_request(). */ | |
868 | blk_stop_queue(info->rq); | |
869 | ||
870 | /* No more gnttab callback work. */ | |
871 | gnttab_cancel_free_callback(&info->callback); | |
872 | spin_unlock_irqrestore(&blkif_io_lock, flags); | |
873 | ||
874 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
875 | flush_scheduled_work(); | |
876 | ||
877 | blk_cleanup_queue(info->rq); | |
878 | info->rq = NULL; | |
879 | ||
880 | out: | |
881 | xenbus_frontend_closed(dev); | |
882 | } | |
883 | ||
884 | /** | |
885 | * Callback received when the backend's state changes. | |
886 | */ | |
887 | static void backend_changed(struct xenbus_device *dev, | |
888 | enum xenbus_state backend_state) | |
889 | { | |
890 | struct blkfront_info *info = dev->dev.driver_data; | |
891 | struct block_device *bd; | |
892 | ||
893 | dev_dbg(&dev->dev, "blkfront:backend_changed.\n"); | |
894 | ||
895 | switch (backend_state) { | |
896 | case XenbusStateInitialising: | |
897 | case XenbusStateInitWait: | |
898 | case XenbusStateInitialised: | |
899 | case XenbusStateUnknown: | |
900 | case XenbusStateClosed: | |
901 | break; | |
902 | ||
903 | case XenbusStateConnected: | |
904 | blkfront_connect(info); | |
905 | break; | |
906 | ||
907 | case XenbusStateClosing: | |
53f0e8af | 908 | bd = bdget_disk(info->gd, 0); |
9f27ee59 JF |
909 | if (bd == NULL) |
910 | xenbus_dev_fatal(dev, -ENODEV, "bdget failed"); | |
911 | ||
912 | mutex_lock(&bd->bd_mutex); | |
913 | if (info->users > 0) | |
914 | xenbus_dev_error(dev, -EBUSY, | |
915 | "Device in use; refusing to close"); | |
916 | else | |
917 | blkfront_closing(dev); | |
918 | mutex_unlock(&bd->bd_mutex); | |
919 | bdput(bd); | |
920 | break; | |
921 | } | |
922 | } | |
923 | ||
924 | static int blkfront_remove(struct xenbus_device *dev) | |
925 | { | |
926 | struct blkfront_info *info = dev->dev.driver_data; | |
927 | ||
928 | dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename); | |
929 | ||
930 | blkif_free(info, 0); | |
931 | ||
932 | kfree(info); | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
1d78d705 CL |
937 | static int blkfront_is_ready(struct xenbus_device *dev) |
938 | { | |
939 | struct blkfront_info *info = dev->dev.driver_data; | |
940 | ||
941 | return info->is_ready; | |
942 | } | |
943 | ||
9f27ee59 JF |
944 | static int blkif_open(struct inode *inode, struct file *filep) |
945 | { | |
946 | struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; | |
947 | info->users++; | |
948 | return 0; | |
949 | } | |
950 | ||
951 | static int blkif_release(struct inode *inode, struct file *filep) | |
952 | { | |
953 | struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; | |
954 | info->users--; | |
955 | if (info->users == 0) { | |
956 | /* Check whether we have been instructed to close. We will | |
957 | have ignored this request initially, as the device was | |
958 | still mounted. */ | |
959 | struct xenbus_device *dev = info->xbdev; | |
960 | enum xenbus_state state = xenbus_read_driver_state(dev->otherend); | |
961 | ||
962 | if (state == XenbusStateClosing) | |
963 | blkfront_closing(dev); | |
964 | } | |
965 | return 0; | |
966 | } | |
967 | ||
968 | static struct block_device_operations xlvbd_block_fops = | |
969 | { | |
970 | .owner = THIS_MODULE, | |
971 | .open = blkif_open, | |
972 | .release = blkif_release, | |
597592d9 | 973 | .getgeo = blkif_getgeo, |
9f27ee59 JF |
974 | }; |
975 | ||
976 | ||
977 | static struct xenbus_device_id blkfront_ids[] = { | |
978 | { "vbd" }, | |
979 | { "" } | |
980 | }; | |
981 | ||
982 | static struct xenbus_driver blkfront = { | |
983 | .name = "vbd", | |
984 | .owner = THIS_MODULE, | |
985 | .ids = blkfront_ids, | |
986 | .probe = blkfront_probe, | |
987 | .remove = blkfront_remove, | |
988 | .resume = blkfront_resume, | |
989 | .otherend_changed = backend_changed, | |
1d78d705 | 990 | .is_ready = blkfront_is_ready, |
9f27ee59 JF |
991 | }; |
992 | ||
993 | static int __init xlblk_init(void) | |
994 | { | |
995 | if (!is_running_on_xen()) | |
996 | return -ENODEV; | |
997 | ||
998 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { | |
999 | printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", | |
1000 | XENVBD_MAJOR, DEV_NAME); | |
1001 | return -ENODEV; | |
1002 | } | |
1003 | ||
1004 | return xenbus_register_frontend(&blkfront); | |
1005 | } | |
1006 | module_init(xlblk_init); | |
1007 | ||
1008 | ||
1009 | static void xlblk_exit(void) | |
1010 | { | |
1011 | return xenbus_unregister_driver(&blkfront); | |
1012 | } | |
1013 | module_exit(xlblk_exit); | |
1014 | ||
1015 | MODULE_DESCRIPTION("Xen virtual block device frontend"); | |
1016 | MODULE_LICENSE("GPL"); | |
1017 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); |