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