Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / block / xen-blkfront.c
blob9279b8782599484d1887348b95f8f4385f8a1155
1 /*
2 * blkfront.c
4 * XenLinux virtual block device driver.
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
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:
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:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
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.
38 #include <linux/interrupt.h>
39 #include <linux/blkdev.h>
40 <<<<<<< HEAD:drivers/block/xen-blkfront.c
41 =======
42 #include <linux/hdreg.h>
43 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/xen-blkfront.c
44 #include <linux/module.h>
46 #include <xen/xenbus.h>
47 #include <xen/grant_table.h>
48 #include <xen/events.h>
49 #include <xen/page.h>
51 #include <xen/interface/grant_table.h>
52 #include <xen/interface/io/blkif.h>
54 #include <asm/xen/hypervisor.h>
56 enum blkif_state {
57 BLKIF_STATE_DISCONNECTED,
58 BLKIF_STATE_CONNECTED,
59 BLKIF_STATE_SUSPENDED,
62 struct blk_shadow {
63 struct blkif_request req;
64 unsigned long request;
65 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
68 static struct block_device_operations xlvbd_block_fops;
70 #define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
73 * We have one of these per vbd, whether ide, scsi or 'other'. They
74 * hang in private_data off the gendisk structure. We may end up
75 * putting all kinds of interesting stuff here :-)
77 struct blkfront_info
79 struct xenbus_device *xbdev;
80 dev_t dev;
81 struct gendisk *gd;
82 int vdevice;
83 blkif_vdev_t handle;
84 enum blkif_state connected;
85 int ring_ref;
86 struct blkif_front_ring ring;
87 unsigned int evtchn, irq;
88 struct request_queue *rq;
89 struct work_struct work;
90 struct gnttab_free_callback callback;
91 struct blk_shadow shadow[BLK_RING_SIZE];
92 unsigned long shadow_free;
93 int feature_barrier;
95 /**
96 * The number of people holding this device open. We won't allow a
97 * hot-unplug unless this is 0.
99 int users;
102 static DEFINE_SPINLOCK(blkif_io_lock);
104 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
105 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
106 #define GRANT_INVALID_REF 0
108 #define PARTS_PER_DISK 16
110 #define BLKIF_MAJOR(dev) ((dev)>>8)
111 #define BLKIF_MINOR(dev) ((dev) & 0xff)
113 #define DEV_NAME "xvd" /* name in /dev */
115 /* Information about our VBDs. */
116 #define MAX_VBDS 64
117 static LIST_HEAD(vbds_list);
119 static int get_id_from_freelist(struct blkfront_info *info)
121 unsigned long free = info->shadow_free;
122 BUG_ON(free > BLK_RING_SIZE);
123 info->shadow_free = info->shadow[free].req.id;
124 info->shadow[free].req.id = 0x0fffffee; /* debug */
125 return free;
128 static void add_id_to_freelist(struct blkfront_info *info,
129 unsigned long id)
131 info->shadow[id].req.id = info->shadow_free;
132 info->shadow[id].request = 0;
133 info->shadow_free = id;
136 static void blkif_restart_queue_callback(void *arg)
138 struct blkfront_info *info = (struct blkfront_info *)arg;
139 schedule_work(&info->work);
142 <<<<<<< HEAD:drivers/block/xen-blkfront.c
143 =======
144 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
146 /* We don't have real geometry info, but let's at least return
147 values consistent with the size of the device */
148 sector_t nsect = get_capacity(bd->bd_disk);
149 sector_t cylinders = nsect;
151 hg->heads = 0xff;
152 hg->sectors = 0x3f;
153 sector_div(cylinders, hg->heads * hg->sectors);
154 hg->cylinders = cylinders;
155 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
156 hg->cylinders = 0xffff;
157 return 0;
160 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/xen-blkfront.c
162 * blkif_queue_request
164 * request block io
166 * id: for guest use only.
167 * operation: BLKIF_OP_{READ,WRITE,PROBE}
168 * buffer: buffer to read/write into. this should be a
169 * virtual address in the guest os.
171 static int blkif_queue_request(struct request *req)
173 struct blkfront_info *info = req->rq_disk->private_data;
174 unsigned long buffer_mfn;
175 struct blkif_request *ring_req;
176 struct req_iterator iter;
177 struct bio_vec *bvec;
178 unsigned long id;
179 unsigned int fsect, lsect;
180 int ref;
181 grant_ref_t gref_head;
183 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
184 return 1;
186 if (gnttab_alloc_grant_references(
187 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
188 gnttab_request_free_callback(
189 &info->callback,
190 blkif_restart_queue_callback,
191 info,
192 BLKIF_MAX_SEGMENTS_PER_REQUEST);
193 return 1;
196 /* Fill out a communications ring structure. */
197 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
198 id = get_id_from_freelist(info);
199 info->shadow[id].request = (unsigned long)req;
201 ring_req->id = id;
202 ring_req->sector_number = (blkif_sector_t)req->sector;
203 ring_req->handle = info->handle;
205 ring_req->operation = rq_data_dir(req) ?
206 BLKIF_OP_WRITE : BLKIF_OP_READ;
207 if (blk_barrier_rq(req))
208 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
210 ring_req->nr_segments = 0;
211 rq_for_each_segment(bvec, req, iter) {
212 BUG_ON(ring_req->nr_segments == BLKIF_MAX_SEGMENTS_PER_REQUEST);
213 buffer_mfn = pfn_to_mfn(page_to_pfn(bvec->bv_page));
214 fsect = bvec->bv_offset >> 9;
215 lsect = fsect + (bvec->bv_len >> 9) - 1;
216 /* install a grant reference. */
217 ref = gnttab_claim_grant_reference(&gref_head);
218 BUG_ON(ref == -ENOSPC);
220 gnttab_grant_foreign_access_ref(
221 ref,
222 info->xbdev->otherend_id,
223 buffer_mfn,
224 rq_data_dir(req) );
226 info->shadow[id].frame[ring_req->nr_segments] =
227 mfn_to_pfn(buffer_mfn);
229 ring_req->seg[ring_req->nr_segments] =
230 (struct blkif_request_segment) {
231 .gref = ref,
232 .first_sect = fsect,
233 .last_sect = lsect };
235 ring_req->nr_segments++;
238 info->ring.req_prod_pvt++;
240 /* Keep a private copy so we can reissue requests when recovering. */
241 info->shadow[id].req = *ring_req;
243 gnttab_free_grant_references(gref_head);
245 return 0;
249 static inline void flush_requests(struct blkfront_info *info)
251 int notify;
253 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
255 if (notify)
256 notify_remote_via_irq(info->irq);
260 * do_blkif_request
261 * read a block; request is in a request queue
263 static void do_blkif_request(struct request_queue *rq)
265 struct blkfront_info *info = NULL;
266 struct request *req;
267 int queued;
269 pr_debug("Entered do_blkif_request\n");
271 queued = 0;
273 while ((req = elv_next_request(rq)) != NULL) {
274 info = req->rq_disk->private_data;
275 if (!blk_fs_request(req)) {
276 end_request(req, 0);
277 continue;
280 if (RING_FULL(&info->ring))
281 goto wait;
283 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
284 "(%u/%li) buffer:%p [%s]\n",
285 req, req->cmd, (unsigned long)req->sector,
286 req->current_nr_sectors,
287 req->nr_sectors, req->buffer,
288 rq_data_dir(req) ? "write" : "read");
291 blkdev_dequeue_request(req);
292 if (blkif_queue_request(req)) {
293 blk_requeue_request(rq, req);
294 wait:
295 /* Avoid pointless unplugs. */
296 blk_stop_queue(rq);
297 break;
300 queued++;
303 if (queued != 0)
304 flush_requests(info);
307 static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
309 struct request_queue *rq;
311 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
312 if (rq == NULL)
313 return -1;
315 elevator_init(rq, "noop");
317 /* Hard sector size and max sectors impersonate the equiv. hardware. */
318 blk_queue_hardsect_size(rq, sector_size);
319 blk_queue_max_sectors(rq, 512);
321 /* Each segment in a request is up to an aligned page in size. */
322 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
323 blk_queue_max_segment_size(rq, PAGE_SIZE);
325 /* Ensure a merged request will fit in a single I/O ring slot. */
326 blk_queue_max_phys_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
327 blk_queue_max_hw_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
329 /* Make sure buffer addresses are sector-aligned. */
330 blk_queue_dma_alignment(rq, 511);
332 gd->queue = rq;
334 return 0;
338 static int xlvbd_barrier(struct blkfront_info *info)
340 int err;
342 err = blk_queue_ordered(info->rq,
343 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE,
344 NULL);
346 if (err)
347 return err;
349 printk(KERN_INFO "blkfront: %s: barriers %s\n",
350 info->gd->disk_name,
351 info->feature_barrier ? "enabled" : "disabled");
352 return 0;
356 static int xlvbd_alloc_gendisk(int minor, blkif_sector_t capacity,
357 int vdevice, u16 vdisk_info, u16 sector_size,
358 struct blkfront_info *info)
360 struct gendisk *gd;
361 int nr_minors = 1;
362 int err = -ENODEV;
364 BUG_ON(info->gd != NULL);
365 BUG_ON(info->rq != NULL);
367 if ((minor % PARTS_PER_DISK) == 0)
368 nr_minors = PARTS_PER_DISK;
370 gd = alloc_disk(nr_minors);
371 if (gd == NULL)
372 goto out;
374 if (nr_minors > 1)
375 sprintf(gd->disk_name, "%s%c", DEV_NAME,
376 'a' + minor / PARTS_PER_DISK);
377 else
378 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
379 'a' + minor / PARTS_PER_DISK,
380 minor % PARTS_PER_DISK);
382 gd->major = XENVBD_MAJOR;
383 gd->first_minor = minor;
384 gd->fops = &xlvbd_block_fops;
385 gd->private_data = info;
386 gd->driverfs_dev = &(info->xbdev->dev);
387 set_capacity(gd, capacity);
389 if (xlvbd_init_blk_queue(gd, sector_size)) {
390 del_gendisk(gd);
391 goto out;
394 info->rq = gd->queue;
395 info->gd = gd;
397 if (info->feature_barrier)
398 xlvbd_barrier(info);
400 if (vdisk_info & VDISK_READONLY)
401 set_disk_ro(gd, 1);
403 if (vdisk_info & VDISK_REMOVABLE)
404 gd->flags |= GENHD_FL_REMOVABLE;
406 if (vdisk_info & VDISK_CDROM)
407 gd->flags |= GENHD_FL_CD;
409 return 0;
411 out:
412 return err;
415 static void kick_pending_request_queues(struct blkfront_info *info)
417 if (!RING_FULL(&info->ring)) {
418 /* Re-enable calldowns. */
419 blk_start_queue(info->rq);
420 /* Kick things off immediately. */
421 do_blkif_request(info->rq);
425 static void blkif_restart_queue(struct work_struct *work)
427 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
429 spin_lock_irq(&blkif_io_lock);
430 if (info->connected == BLKIF_STATE_CONNECTED)
431 kick_pending_request_queues(info);
432 spin_unlock_irq(&blkif_io_lock);
435 static void blkif_free(struct blkfront_info *info, int suspend)
437 /* Prevent new requests being issued until we fix things up. */
438 spin_lock_irq(&blkif_io_lock);
439 info->connected = suspend ?
440 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
441 /* No more blkif_request(). */
442 if (info->rq)
443 blk_stop_queue(info->rq);
444 /* No more gnttab callback work. */
445 gnttab_cancel_free_callback(&info->callback);
446 spin_unlock_irq(&blkif_io_lock);
448 /* Flush gnttab callback work. Must be done with no locks held. */
449 flush_scheduled_work();
451 /* Free resources associated with old device channel. */
452 if (info->ring_ref != GRANT_INVALID_REF) {
453 gnttab_end_foreign_access(info->ring_ref, 0,
454 (unsigned long)info->ring.sring);
455 info->ring_ref = GRANT_INVALID_REF;
456 info->ring.sring = NULL;
458 if (info->irq)
459 unbind_from_irqhandler(info->irq, info);
460 info->evtchn = info->irq = 0;
464 static void blkif_completion(struct blk_shadow *s)
466 int i;
467 for (i = 0; i < s->req.nr_segments; i++)
468 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
471 static irqreturn_t blkif_interrupt(int irq, void *dev_id)
473 struct request *req;
474 struct blkif_response *bret;
475 RING_IDX i, rp;
476 unsigned long flags;
477 struct blkfront_info *info = (struct blkfront_info *)dev_id;
478 int error;
480 spin_lock_irqsave(&blkif_io_lock, flags);
482 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
483 spin_unlock_irqrestore(&blkif_io_lock, flags);
484 return IRQ_HANDLED;
487 again:
488 rp = info->ring.sring->rsp_prod;
489 rmb(); /* Ensure we see queued responses up to 'rp'. */
491 for (i = info->ring.rsp_cons; i != rp; i++) {
492 unsigned long id;
493 int ret;
495 bret = RING_GET_RESPONSE(&info->ring, i);
496 id = bret->id;
497 req = (struct request *)info->shadow[id].request;
499 blkif_completion(&info->shadow[id]);
501 add_id_to_freelist(info, id);
503 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
504 switch (bret->operation) {
505 case BLKIF_OP_WRITE_BARRIER:
506 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
507 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
508 info->gd->disk_name);
509 error = -EOPNOTSUPP;
510 info->feature_barrier = 0;
511 xlvbd_barrier(info);
513 /* fall through */
514 case BLKIF_OP_READ:
515 case BLKIF_OP_WRITE:
516 if (unlikely(bret->status != BLKIF_RSP_OKAY))
517 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
518 "request: %x\n", bret->status);
520 ret = __blk_end_request(req, error, blk_rq_bytes(req));
521 BUG_ON(ret);
522 break;
523 default:
524 BUG();
528 info->ring.rsp_cons = i;
530 if (i != info->ring.req_prod_pvt) {
531 int more_to_do;
532 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
533 if (more_to_do)
534 goto again;
535 } else
536 info->ring.sring->rsp_event = i + 1;
538 kick_pending_request_queues(info);
540 spin_unlock_irqrestore(&blkif_io_lock, flags);
542 return IRQ_HANDLED;
546 static int setup_blkring(struct xenbus_device *dev,
547 struct blkfront_info *info)
549 struct blkif_sring *sring;
550 int err;
552 info->ring_ref = GRANT_INVALID_REF;
554 sring = (struct blkif_sring *)__get_free_page(GFP_KERNEL);
555 if (!sring) {
556 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
557 return -ENOMEM;
559 SHARED_RING_INIT(sring);
560 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
562 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
563 if (err < 0) {
564 free_page((unsigned long)sring);
565 info->ring.sring = NULL;
566 goto fail;
568 info->ring_ref = err;
570 err = xenbus_alloc_evtchn(dev, &info->evtchn);
571 if (err)
572 goto fail;
574 err = bind_evtchn_to_irqhandler(info->evtchn,
575 blkif_interrupt,
576 IRQF_SAMPLE_RANDOM, "blkif", info);
577 if (err <= 0) {
578 xenbus_dev_fatal(dev, err,
579 "bind_evtchn_to_irqhandler failed");
580 goto fail;
582 info->irq = err;
584 return 0;
585 fail:
586 blkif_free(info, 0);
587 return err;
591 /* Common code used when first setting up, and when resuming. */
592 static int talk_to_backend(struct xenbus_device *dev,
593 struct blkfront_info *info)
595 const char *message = NULL;
596 struct xenbus_transaction xbt;
597 int err;
599 /* Create shared ring, alloc event channel. */
600 err = setup_blkring(dev, info);
601 if (err)
602 goto out;
604 again:
605 err = xenbus_transaction_start(&xbt);
606 if (err) {
607 xenbus_dev_fatal(dev, err, "starting transaction");
608 goto destroy_blkring;
611 err = xenbus_printf(xbt, dev->nodename,
612 "ring-ref", "%u", info->ring_ref);
613 if (err) {
614 message = "writing ring-ref";
615 goto abort_transaction;
617 err = xenbus_printf(xbt, dev->nodename,
618 "event-channel", "%u", info->evtchn);
619 if (err) {
620 message = "writing event-channel";
621 goto abort_transaction;
624 err = xenbus_transaction_end(xbt, 0);
625 if (err) {
626 if (err == -EAGAIN)
627 goto again;
628 xenbus_dev_fatal(dev, err, "completing transaction");
629 goto destroy_blkring;
632 xenbus_switch_state(dev, XenbusStateInitialised);
634 return 0;
636 abort_transaction:
637 xenbus_transaction_end(xbt, 1);
638 if (message)
639 xenbus_dev_fatal(dev, err, "%s", message);
640 destroy_blkring:
641 blkif_free(info, 0);
642 out:
643 return err;
648 * Entry point to this code when a new device is created. Allocate the basic
649 * structures and the ring buffer for communication with the backend, and
650 * inform the backend of the appropriate details for those. Switch to
651 * Initialised state.
653 static int blkfront_probe(struct xenbus_device *dev,
654 const struct xenbus_device_id *id)
656 int err, vdevice, i;
657 struct blkfront_info *info;
659 /* FIXME: Use dynamic device id if this is not set. */
660 err = xenbus_scanf(XBT_NIL, dev->nodename,
661 "virtual-device", "%i", &vdevice);
662 if (err != 1) {
663 xenbus_dev_fatal(dev, err, "reading virtual-device");
664 return err;
667 info = kzalloc(sizeof(*info), GFP_KERNEL);
668 if (!info) {
669 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
670 return -ENOMEM;
673 info->xbdev = dev;
674 info->vdevice = vdevice;
675 info->connected = BLKIF_STATE_DISCONNECTED;
676 INIT_WORK(&info->work, blkif_restart_queue);
678 for (i = 0; i < BLK_RING_SIZE; i++)
679 info->shadow[i].req.id = i+1;
680 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
682 /* Front end dir is a number, which is used as the id. */
683 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
684 dev->dev.driver_data = info;
686 err = talk_to_backend(dev, info);
687 if (err) {
688 kfree(info);
689 dev->dev.driver_data = NULL;
690 return err;
693 return 0;
697 static int blkif_recover(struct blkfront_info *info)
699 int i;
700 struct blkif_request *req;
701 struct blk_shadow *copy;
702 int j;
704 /* Stage 1: Make a safe copy of the shadow state. */
705 copy = kmalloc(sizeof(info->shadow), GFP_KERNEL);
706 if (!copy)
707 return -ENOMEM;
708 memcpy(copy, info->shadow, sizeof(info->shadow));
710 /* Stage 2: Set up free list. */
711 memset(&info->shadow, 0, sizeof(info->shadow));
712 for (i = 0; i < BLK_RING_SIZE; i++)
713 info->shadow[i].req.id = i+1;
714 info->shadow_free = info->ring.req_prod_pvt;
715 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
717 /* Stage 3: Find pending requests and requeue them. */
718 for (i = 0; i < BLK_RING_SIZE; i++) {
719 /* Not in use? */
720 if (copy[i].request == 0)
721 continue;
723 /* Grab a request slot and copy shadow state into it. */
724 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
725 *req = copy[i].req;
727 /* We get a new request id, and must reset the shadow state. */
728 req->id = get_id_from_freelist(info);
729 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
731 /* Rewrite any grant references invalidated by susp/resume. */
732 for (j = 0; j < req->nr_segments; j++)
733 gnttab_grant_foreign_access_ref(
734 req->seg[j].gref,
735 info->xbdev->otherend_id,
736 pfn_to_mfn(info->shadow[req->id].frame[j]),
737 rq_data_dir(
738 (struct request *)
739 info->shadow[req->id].request));
740 info->shadow[req->id].req = *req;
742 info->ring.req_prod_pvt++;
745 kfree(copy);
747 xenbus_switch_state(info->xbdev, XenbusStateConnected);
749 spin_lock_irq(&blkif_io_lock);
751 /* Now safe for us to use the shared ring */
752 info->connected = BLKIF_STATE_CONNECTED;
754 /* Send off requeued requests */
755 flush_requests(info);
757 /* Kick any other new requests queued since we resumed */
758 kick_pending_request_queues(info);
760 spin_unlock_irq(&blkif_io_lock);
762 return 0;
766 * We are reconnecting to the backend, due to a suspend/resume, or a backend
767 * driver restart. We tear down our blkif structure and recreate it, but
768 * leave the device-layer structures intact so that this is transparent to the
769 * rest of the kernel.
771 static int blkfront_resume(struct xenbus_device *dev)
773 struct blkfront_info *info = dev->dev.driver_data;
774 int err;
776 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
778 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
780 err = talk_to_backend(dev, info);
781 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
782 err = blkif_recover(info);
784 return err;
789 * Invoked when the backend is finally 'ready' (and has told produced
790 * the details about the physical device - #sectors, size, etc).
792 static void blkfront_connect(struct blkfront_info *info)
794 unsigned long long sectors;
795 unsigned long sector_size;
796 unsigned int binfo;
797 int err;
799 if ((info->connected == BLKIF_STATE_CONNECTED) ||
800 (info->connected == BLKIF_STATE_SUSPENDED) )
801 return;
803 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
804 __func__, info->xbdev->otherend);
806 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
807 "sectors", "%llu", &sectors,
808 "info", "%u", &binfo,
809 "sector-size", "%lu", &sector_size,
810 NULL);
811 if (err) {
812 xenbus_dev_fatal(info->xbdev, err,
813 "reading backend fields at %s",
814 info->xbdev->otherend);
815 return;
818 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
819 "feature-barrier", "%lu", &info->feature_barrier,
820 NULL);
821 if (err)
822 info->feature_barrier = 0;
824 err = xlvbd_alloc_gendisk(BLKIF_MINOR(info->vdevice),
825 sectors, info->vdevice,
826 binfo, sector_size, info);
827 if (err) {
828 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
829 info->xbdev->otherend);
830 return;
833 xenbus_switch_state(info->xbdev, XenbusStateConnected);
835 /* Kick pending requests. */
836 spin_lock_irq(&blkif_io_lock);
837 info->connected = BLKIF_STATE_CONNECTED;
838 kick_pending_request_queues(info);
839 spin_unlock_irq(&blkif_io_lock);
841 add_disk(info->gd);
845 * Handle the change of state of the backend to Closing. We must delete our
846 * device-layer structures now, to ensure that writes are flushed through to
847 * the backend. Once is this done, we can switch to Closed in
848 * acknowledgement.
850 static void blkfront_closing(struct xenbus_device *dev)
852 struct blkfront_info *info = dev->dev.driver_data;
853 unsigned long flags;
855 dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
857 if (info->rq == NULL)
858 goto out;
860 spin_lock_irqsave(&blkif_io_lock, flags);
862 del_gendisk(info->gd);
864 /* No more blkif_request(). */
865 blk_stop_queue(info->rq);
867 /* No more gnttab callback work. */
868 gnttab_cancel_free_callback(&info->callback);
869 spin_unlock_irqrestore(&blkif_io_lock, flags);
871 /* Flush gnttab callback work. Must be done with no locks held. */
872 flush_scheduled_work();
874 blk_cleanup_queue(info->rq);
875 info->rq = NULL;
877 out:
878 xenbus_frontend_closed(dev);
882 * Callback received when the backend's state changes.
884 static void backend_changed(struct xenbus_device *dev,
885 enum xenbus_state backend_state)
887 struct blkfront_info *info = dev->dev.driver_data;
888 struct block_device *bd;
890 dev_dbg(&dev->dev, "blkfront:backend_changed.\n");
892 switch (backend_state) {
893 case XenbusStateInitialising:
894 case XenbusStateInitWait:
895 case XenbusStateInitialised:
896 case XenbusStateUnknown:
897 case XenbusStateClosed:
898 break;
900 case XenbusStateConnected:
901 blkfront_connect(info);
902 break;
904 case XenbusStateClosing:
905 bd = bdget(info->dev);
906 if (bd == NULL)
907 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
909 mutex_lock(&bd->bd_mutex);
910 if (info->users > 0)
911 xenbus_dev_error(dev, -EBUSY,
912 "Device in use; refusing to close");
913 else
914 blkfront_closing(dev);
915 mutex_unlock(&bd->bd_mutex);
916 bdput(bd);
917 break;
921 static int blkfront_remove(struct xenbus_device *dev)
923 struct blkfront_info *info = dev->dev.driver_data;
925 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
927 blkif_free(info, 0);
929 kfree(info);
931 return 0;
934 static int blkif_open(struct inode *inode, struct file *filep)
936 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
937 info->users++;
938 return 0;
941 static int blkif_release(struct inode *inode, struct file *filep)
943 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
944 info->users--;
945 if (info->users == 0) {
946 /* Check whether we have been instructed to close. We will
947 have ignored this request initially, as the device was
948 still mounted. */
949 struct xenbus_device *dev = info->xbdev;
950 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
952 if (state == XenbusStateClosing)
953 blkfront_closing(dev);
955 return 0;
958 static struct block_device_operations xlvbd_block_fops =
960 .owner = THIS_MODULE,
961 .open = blkif_open,
962 .release = blkif_release,
963 <<<<<<< HEAD:drivers/block/xen-blkfront.c
964 =======
965 .getgeo = blkif_getgeo,
966 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/block/xen-blkfront.c
970 static struct xenbus_device_id blkfront_ids[] = {
971 { "vbd" },
972 { "" }
975 static struct xenbus_driver blkfront = {
976 .name = "vbd",
977 .owner = THIS_MODULE,
978 .ids = blkfront_ids,
979 .probe = blkfront_probe,
980 .remove = blkfront_remove,
981 .resume = blkfront_resume,
982 .otherend_changed = backend_changed,
985 static int __init xlblk_init(void)
987 if (!is_running_on_xen())
988 return -ENODEV;
990 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
991 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
992 XENVBD_MAJOR, DEV_NAME);
993 return -ENODEV;
996 return xenbus_register_frontend(&blkfront);
998 module_init(xlblk_init);
1001 static void xlblk_exit(void)
1003 return xenbus_unregister_driver(&blkfront);
1005 module_exit(xlblk_exit);
1007 MODULE_DESCRIPTION("Xen virtual block device frontend");
1008 MODULE_LICENSE("GPL");
1009 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);