direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c @ 12496:0c0ef61de06b

[BLK] Handle block devices with more than 2^32 sectors.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Tue Nov 21 10:16:58 2006 +0000 (2006-11-21)
parents 4eaadb2ae198
children c98a8e2c62d1
line source
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 */
38 #include <linux/version.h>
39 #include "block.h"
40 #include <linux/cdrom.h>
41 #include <linux/sched.h>
42 #include <linux/interrupt.h>
43 #include <scsi/scsi.h>
44 #include <xen/evtchn.h>
45 #include <xen/xenbus.h>
46 #include <xen/interface/grant_table.h>
47 #include <xen/gnttab.h>
48 #include <asm/hypervisor.h>
49 #include <asm/maddr.h>
51 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
52 #include <xen/platform-compat.h>
53 #endif
55 #define BLKIF_STATE_DISCONNECTED 0
56 #define BLKIF_STATE_CONNECTED 1
57 #define BLKIF_STATE_SUSPENDED 2
59 #define MAXIMUM_OUTSTANDING_BLOCK_REQS \
60 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
61 #define GRANT_INVALID_REF 0
63 static void connect(struct blkfront_info *);
64 static void blkfront_closing(struct xenbus_device *);
65 static int blkfront_remove(struct xenbus_device *);
66 static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
67 static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
69 static void kick_pending_request_queues(struct blkfront_info *);
71 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs);
72 static void blkif_restart_queue(void *arg);
73 static void blkif_recover(struct blkfront_info *);
74 static void blkif_completion(struct blk_shadow *);
75 static void blkif_free(struct blkfront_info *, int);
78 /**
79 * Entry point to this code when a new device is created. Allocate the basic
80 * structures and the ring buffer for communication with the backend, and
81 * inform the backend of the appropriate details for those. Switch to
82 * Initialised state.
83 */
84 static int blkfront_probe(struct xenbus_device *dev,
85 const struct xenbus_device_id *id)
86 {
87 int err, vdevice, i;
88 struct blkfront_info *info;
90 /* FIXME: Use dynamic device id if this is not set. */
91 err = xenbus_scanf(XBT_NIL, dev->nodename,
92 "virtual-device", "%i", &vdevice);
93 if (err != 1) {
94 xenbus_dev_fatal(dev, err, "reading virtual-device");
95 return err;
96 }
98 info = kzalloc(sizeof(*info), GFP_KERNEL);
99 if (!info) {
100 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
101 return -ENOMEM;
102 }
104 info->xbdev = dev;
105 info->vdevice = vdevice;
106 info->connected = BLKIF_STATE_DISCONNECTED;
107 INIT_WORK(&info->work, blkif_restart_queue, (void *)info);
109 for (i = 0; i < BLK_RING_SIZE; i++)
110 info->shadow[i].req.id = i+1;
111 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
113 /* Front end dir is a number, which is used as the id. */
114 info->handle = simple_strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
115 dev->dev.driver_data = info;
117 err = talk_to_backend(dev, info);
118 if (err) {
119 kfree(info);
120 dev->dev.driver_data = NULL;
121 return err;
122 }
124 return 0;
125 }
128 /**
129 * We are reconnecting to the backend, due to a suspend/resume, or a backend
130 * driver restart. We tear down our blkif structure and recreate it, but
131 * leave the device-layer structures intact so that this is transparent to the
132 * rest of the kernel.
133 */
134 static int blkfront_resume(struct xenbus_device *dev)
135 {
136 struct blkfront_info *info = dev->dev.driver_data;
137 int err;
139 DPRINTK("blkfront_resume: %s\n", dev->nodename);
141 blkif_free(info, 1);
143 err = talk_to_backend(dev, info);
144 if (!err)
145 blkif_recover(info);
147 return err;
148 }
151 /* Common code used when first setting up, and when resuming. */
152 static int talk_to_backend(struct xenbus_device *dev,
153 struct blkfront_info *info)
154 {
155 const char *message = NULL;
156 struct xenbus_transaction xbt;
157 int err;
159 /* Create shared ring, alloc event channel. */
160 err = setup_blkring(dev, info);
161 if (err)
162 goto out;
164 again:
165 err = xenbus_transaction_start(&xbt);
166 if (err) {
167 xenbus_dev_fatal(dev, err, "starting transaction");
168 goto destroy_blkring;
169 }
171 err = xenbus_printf(xbt, dev->nodename,
172 "ring-ref","%u", info->ring_ref);
173 if (err) {
174 message = "writing ring-ref";
175 goto abort_transaction;
176 }
177 err = xenbus_printf(xbt, dev->nodename,
178 "event-channel", "%u", info->evtchn);
179 if (err) {
180 message = "writing event-channel";
181 goto abort_transaction;
182 }
184 err = xenbus_transaction_end(xbt, 0);
185 if (err) {
186 if (err == -EAGAIN)
187 goto again;
188 xenbus_dev_fatal(dev, err, "completing transaction");
189 goto destroy_blkring;
190 }
192 xenbus_switch_state(dev, XenbusStateInitialised);
194 return 0;
196 abort_transaction:
197 xenbus_transaction_end(xbt, 1);
198 if (message)
199 xenbus_dev_fatal(dev, err, "%s", message);
200 destroy_blkring:
201 blkif_free(info, 0);
202 out:
203 return err;
204 }
207 static int setup_blkring(struct xenbus_device *dev,
208 struct blkfront_info *info)
209 {
210 blkif_sring_t *sring;
211 int err;
213 info->ring_ref = GRANT_INVALID_REF;
215 sring = (blkif_sring_t *)__get_free_page(GFP_KERNEL);
216 if (!sring) {
217 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
218 return -ENOMEM;
219 }
220 SHARED_RING_INIT(sring);
221 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
223 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
224 if (err < 0) {
225 free_page((unsigned long)sring);
226 info->ring.sring = NULL;
227 goto fail;
228 }
229 info->ring_ref = err;
231 err = xenbus_alloc_evtchn(dev, &info->evtchn);
232 if (err)
233 goto fail;
235 err = bind_evtchn_to_irqhandler(
236 info->evtchn, blkif_int, SA_SAMPLE_RANDOM, "blkif", info);
237 if (err <= 0) {
238 xenbus_dev_fatal(dev, err,
239 "bind_evtchn_to_irqhandler failed");
240 goto fail;
241 }
242 info->irq = err;
244 return 0;
245 fail:
246 blkif_free(info, 0);
247 return err;
248 }
251 /**
252 * Callback received when the backend's state changes.
253 */
254 static void backend_changed(struct xenbus_device *dev,
255 enum xenbus_state backend_state)
256 {
257 struct blkfront_info *info = dev->dev.driver_data;
258 struct block_device *bd;
260 DPRINTK("blkfront:backend_changed.\n");
262 switch (backend_state) {
263 case XenbusStateInitialising:
264 case XenbusStateInitWait:
265 case XenbusStateInitialised:
266 case XenbusStateUnknown:
267 case XenbusStateClosed:
268 break;
270 case XenbusStateConnected:
271 connect(info);
272 break;
274 case XenbusStateClosing:
275 bd = bdget(info->dev);
276 if (bd == NULL)
277 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
279 down(&bd->bd_sem);
280 if (info->users > 0)
281 xenbus_dev_error(dev, -EBUSY,
282 "Device in use; refusing to close");
283 else
284 blkfront_closing(dev);
285 up(&bd->bd_sem);
286 bdput(bd);
287 break;
288 }
289 }
292 /* ** Connection ** */
295 /*
296 * Invoked when the backend is finally 'ready' (and has told produced
297 * the details about the physical device - #sectors, size, etc).
298 */
299 static void connect(struct blkfront_info *info)
300 {
301 unsigned long long sectors;
302 unsigned long sector_size;
303 unsigned int binfo;
304 int err;
306 if ((info->connected == BLKIF_STATE_CONNECTED) ||
307 (info->connected == BLKIF_STATE_SUSPENDED) )
308 return;
310 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
312 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
313 "sectors", "%llu", &sectors,
314 "info", "%u", &binfo,
315 "sector-size", "%lu", &sector_size,
316 NULL);
317 if (err) {
318 xenbus_dev_fatal(info->xbdev, err,
319 "reading backend fields at %s",
320 info->xbdev->otherend);
321 return;
322 }
324 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
325 "feature-barrier", "%lu", &info->feature_barrier,
326 NULL);
327 if (err)
328 info->feature_barrier = 0;
330 err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
331 if (err) {
332 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
333 info->xbdev->otherend);
334 return;
335 }
337 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
339 /* Kick pending requests. */
340 spin_lock_irq(&blkif_io_lock);
341 info->connected = BLKIF_STATE_CONNECTED;
342 kick_pending_request_queues(info);
343 spin_unlock_irq(&blkif_io_lock);
345 add_disk(info->gd);
346 }
348 /**
349 * Handle the change of state of the backend to Closing. We must delete our
350 * device-layer structures now, to ensure that writes are flushed through to
351 * the backend. Once is this done, we can switch to Closed in
352 * acknowledgement.
353 */
354 static void blkfront_closing(struct xenbus_device *dev)
355 {
356 struct blkfront_info *info = dev->dev.driver_data;
357 unsigned long flags;
359 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
361 if (info->rq == NULL)
362 return;
364 spin_lock_irqsave(&blkif_io_lock, flags);
365 /* No more blkif_request(). */
366 blk_stop_queue(info->rq);
367 /* No more gnttab callback work. */
368 gnttab_cancel_free_callback(&info->callback);
369 spin_unlock_irqrestore(&blkif_io_lock, flags);
371 /* Flush gnttab callback work. Must be done with no locks held. */
372 flush_scheduled_work();
374 xlvbd_del(info);
376 xenbus_frontend_closed(dev);
377 }
380 static int blkfront_remove(struct xenbus_device *dev)
381 {
382 struct blkfront_info *info = dev->dev.driver_data;
384 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
386 blkif_free(info, 0);
388 kfree(info);
390 return 0;
391 }
394 static inline int GET_ID_FROM_FREELIST(
395 struct blkfront_info *info)
396 {
397 unsigned long free = info->shadow_free;
398 BUG_ON(free > BLK_RING_SIZE);
399 info->shadow_free = info->shadow[free].req.id;
400 info->shadow[free].req.id = 0x0fffffee; /* debug */
401 return free;
402 }
404 static inline void ADD_ID_TO_FREELIST(
405 struct blkfront_info *info, unsigned long id)
406 {
407 info->shadow[id].req.id = info->shadow_free;
408 info->shadow[id].request = 0;
409 info->shadow_free = id;
410 }
412 static inline void flush_requests(struct blkfront_info *info)
413 {
414 int notify;
416 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
418 if (notify)
419 notify_remote_via_irq(info->irq);
420 }
422 static void kick_pending_request_queues(struct blkfront_info *info)
423 {
424 if (!RING_FULL(&info->ring)) {
425 /* Re-enable calldowns. */
426 blk_start_queue(info->rq);
427 /* Kick things off immediately. */
428 do_blkif_request(info->rq);
429 }
430 }
432 static void blkif_restart_queue(void *arg)
433 {
434 struct blkfront_info *info = (struct blkfront_info *)arg;
435 spin_lock_irq(&blkif_io_lock);
436 if (info->connected == BLKIF_STATE_CONNECTED)
437 kick_pending_request_queues(info);
438 spin_unlock_irq(&blkif_io_lock);
439 }
441 static void blkif_restart_queue_callback(void *arg)
442 {
443 struct blkfront_info *info = (struct blkfront_info *)arg;
444 schedule_work(&info->work);
445 }
447 int blkif_open(struct inode *inode, struct file *filep)
448 {
449 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
450 info->users++;
451 return 0;
452 }
455 int blkif_release(struct inode *inode, struct file *filep)
456 {
457 struct blkfront_info *info = inode->i_bdev->bd_disk->private_data;
458 info->users--;
459 if (info->users == 0) {
460 /* Check whether we have been instructed to close. We will
461 have ignored this request initially, as the device was
462 still mounted. */
463 struct xenbus_device * dev = info->xbdev;
464 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
466 if (state == XenbusStateClosing)
467 blkfront_closing(dev);
468 }
469 return 0;
470 }
473 int blkif_ioctl(struct inode *inode, struct file *filep,
474 unsigned command, unsigned long argument)
475 {
476 int i;
478 DPRINTK_IOCTL("command: 0x%x, argument: 0x%lx, dev: 0x%04x\n",
479 command, (long)argument, inode->i_rdev);
481 switch (command) {
482 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
483 case HDIO_GETGEO: {
484 struct block_device *bd = inode->i_bdev;
485 struct hd_geometry geo;
486 int ret;
488 if (!argument)
489 return -EINVAL;
491 geo.start = get_start_sect(bd);
492 ret = blkif_getgeo(bd, &geo);
493 if (ret)
494 return ret;
496 if (copy_to_user((struct hd_geometry __user *)argument, &geo,
497 sizeof(geo)))
498 return -EFAULT;
500 return 0;
501 }
502 #endif
503 case CDROMMULTISESSION:
504 DPRINTK("FIXME: support multisession CDs later\n");
505 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
506 if (put_user(0, (char __user *)(argument + i)))
507 return -EFAULT;
508 return 0;
510 default:
511 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
512 command);*/
513 return -EINVAL; /* same return as native Linux */
514 }
516 return 0;
517 }
520 int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
521 {
522 /* We don't have real geometry info, but let's at least return
523 values consistent with the size of the device */
524 sector_t nsect = get_capacity(bd->bd_disk);
525 sector_t cylinders = nsect;
527 hg->heads = 0xff;
528 hg->sectors = 0x3f;
529 sector_div(cylinders, hg->heads * hg->sectors);
530 hg->cylinders = cylinders;
531 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
532 hg->cylinders = 0xffff;
533 return 0;
534 }
537 /*
538 * blkif_queue_request
539 *
540 * request block io
541 *
542 * id: for guest use only.
543 * operation: BLKIF_OP_{READ,WRITE,PROBE}
544 * buffer: buffer to read/write into. this should be a
545 * virtual address in the guest os.
546 */
547 static int blkif_queue_request(struct request *req)
548 {
549 struct blkfront_info *info = req->rq_disk->private_data;
550 unsigned long buffer_mfn;
551 blkif_request_t *ring_req;
552 struct bio *bio;
553 struct bio_vec *bvec;
554 int idx;
555 unsigned long id;
556 unsigned int fsect, lsect;
557 int ref;
558 grant_ref_t gref_head;
560 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
561 return 1;
563 if (gnttab_alloc_grant_references(
564 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
565 gnttab_request_free_callback(
566 &info->callback,
567 blkif_restart_queue_callback,
568 info,
569 BLKIF_MAX_SEGMENTS_PER_REQUEST);
570 return 1;
571 }
573 /* Fill out a communications ring structure. */
574 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
575 id = GET_ID_FROM_FREELIST(info);
576 info->shadow[id].request = (unsigned long)req;
578 ring_req->id = id;
579 ring_req->sector_number = (blkif_sector_t)req->sector;
580 ring_req->handle = info->handle;
582 ring_req->operation = rq_data_dir(req) ?
583 BLKIF_OP_WRITE : BLKIF_OP_READ;
584 if (blk_barrier_rq(req))
585 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
587 ring_req->nr_segments = 0;
588 rq_for_each_bio (bio, req) {
589 bio_for_each_segment (bvec, bio, idx) {
590 BUG_ON(ring_req->nr_segments
591 == BLKIF_MAX_SEGMENTS_PER_REQUEST);
592 buffer_mfn = page_to_phys(bvec->bv_page) >> PAGE_SHIFT;
593 fsect = bvec->bv_offset >> 9;
594 lsect = fsect + (bvec->bv_len >> 9) - 1;
595 /* install a grant reference. */
596 ref = gnttab_claim_grant_reference(&gref_head);
597 BUG_ON(ref == -ENOSPC);
599 gnttab_grant_foreign_access_ref(
600 ref,
601 info->xbdev->otherend_id,
602 buffer_mfn,
603 rq_data_dir(req) );
605 info->shadow[id].frame[ring_req->nr_segments] =
606 mfn_to_pfn(buffer_mfn);
608 ring_req->seg[ring_req->nr_segments] =
609 (struct blkif_request_segment) {
610 .gref = ref,
611 .first_sect = fsect,
612 .last_sect = lsect };
614 ring_req->nr_segments++;
615 }
616 }
618 info->ring.req_prod_pvt++;
620 /* Keep a private copy so we can reissue requests when recovering. */
621 info->shadow[id].req = *ring_req;
623 gnttab_free_grant_references(gref_head);
625 return 0;
626 }
628 /*
629 * do_blkif_request
630 * read a block; request is in a request queue
631 */
632 void do_blkif_request(request_queue_t *rq)
633 {
634 struct blkfront_info *info = NULL;
635 struct request *req;
636 int queued;
638 DPRINTK("Entered do_blkif_request\n");
640 queued = 0;
642 while ((req = elv_next_request(rq)) != NULL) {
643 info = req->rq_disk->private_data;
644 if (!blk_fs_request(req)) {
645 end_request(req, 0);
646 continue;
647 }
649 if (RING_FULL(&info->ring))
650 goto wait;
652 DPRINTK("do_blk_req %p: cmd %p, sec %lx, "
653 "(%u/%li) buffer:%p [%s]\n",
654 req, req->cmd, req->sector, req->current_nr_sectors,
655 req->nr_sectors, req->buffer,
656 rq_data_dir(req) ? "write" : "read");
659 blkdev_dequeue_request(req);
660 if (blkif_queue_request(req)) {
661 blk_requeue_request(rq, req);
662 wait:
663 /* Avoid pointless unplugs. */
664 blk_stop_queue(rq);
665 break;
666 }
668 queued++;
669 }
671 if (queued != 0)
672 flush_requests(info);
673 }
676 static irqreturn_t blkif_int(int irq, void *dev_id, struct pt_regs *ptregs)
677 {
678 struct request *req;
679 blkif_response_t *bret;
680 RING_IDX i, rp;
681 unsigned long flags;
682 struct blkfront_info *info = (struct blkfront_info *)dev_id;
683 int uptodate;
685 spin_lock_irqsave(&blkif_io_lock, flags);
687 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
688 spin_unlock_irqrestore(&blkif_io_lock, flags);
689 return IRQ_HANDLED;
690 }
692 again:
693 rp = info->ring.sring->rsp_prod;
694 rmb(); /* Ensure we see queued responses up to 'rp'. */
696 for (i = info->ring.rsp_cons; i != rp; i++) {
697 unsigned long id;
698 int ret;
700 bret = RING_GET_RESPONSE(&info->ring, i);
701 id = bret->id;
702 req = (struct request *)info->shadow[id].request;
704 blkif_completion(&info->shadow[id]);
706 ADD_ID_TO_FREELIST(info, id);
708 uptodate = (bret->status == BLKIF_RSP_OKAY);
709 switch (bret->operation) {
710 case BLKIF_OP_WRITE_BARRIER:
711 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
712 printk("blkfront: %s: write barrier op failed\n",
713 info->gd->disk_name);
714 uptodate = -EOPNOTSUPP;
715 info->feature_barrier = 0;
716 xlvbd_barrier(info);
717 }
718 /* fall through */
719 case BLKIF_OP_READ:
720 case BLKIF_OP_WRITE:
721 if (unlikely(bret->status != BLKIF_RSP_OKAY))
722 DPRINTK("Bad return from blkdev data "
723 "request: %x\n", bret->status);
725 ret = end_that_request_first(req, uptodate,
726 req->hard_nr_sectors);
727 BUG_ON(ret);
728 end_that_request_last(req, uptodate);
729 break;
730 default:
731 BUG();
732 }
733 }
735 info->ring.rsp_cons = i;
737 if (i != info->ring.req_prod_pvt) {
738 int more_to_do;
739 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
740 if (more_to_do)
741 goto again;
742 } else
743 info->ring.sring->rsp_event = i + 1;
745 kick_pending_request_queues(info);
747 spin_unlock_irqrestore(&blkif_io_lock, flags);
749 return IRQ_HANDLED;
750 }
752 static void blkif_free(struct blkfront_info *info, int suspend)
753 {
754 /* Prevent new requests being issued until we fix things up. */
755 spin_lock_irq(&blkif_io_lock);
756 info->connected = suspend ?
757 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
758 /* No more blkif_request(). */
759 if (info->rq)
760 blk_stop_queue(info->rq);
761 /* No more gnttab callback work. */
762 gnttab_cancel_free_callback(&info->callback);
763 spin_unlock_irq(&blkif_io_lock);
765 /* Flush gnttab callback work. Must be done with no locks held. */
766 flush_scheduled_work();
768 /* Free resources associated with old device channel. */
769 if (info->ring_ref != GRANT_INVALID_REF) {
770 gnttab_end_foreign_access(info->ring_ref, 0,
771 (unsigned long)info->ring.sring);
772 info->ring_ref = GRANT_INVALID_REF;
773 info->ring.sring = NULL;
774 }
775 if (info->irq)
776 unbind_from_irqhandler(info->irq, info);
777 info->evtchn = info->irq = 0;
779 }
781 static void blkif_completion(struct blk_shadow *s)
782 {
783 int i;
784 for (i = 0; i < s->req.nr_segments; i++)
785 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
786 }
788 static void blkif_recover(struct blkfront_info *info)
789 {
790 int i;
791 blkif_request_t *req;
792 struct blk_shadow *copy;
793 int j;
795 /* Stage 1: Make a safe copy of the shadow state. */
796 copy = kmalloc(sizeof(info->shadow), GFP_KERNEL | __GFP_NOFAIL);
797 memcpy(copy, info->shadow, sizeof(info->shadow));
799 /* Stage 2: Set up free list. */
800 memset(&info->shadow, 0, sizeof(info->shadow));
801 for (i = 0; i < BLK_RING_SIZE; i++)
802 info->shadow[i].req.id = i+1;
803 info->shadow_free = info->ring.req_prod_pvt;
804 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
806 /* Stage 3: Find pending requests and requeue them. */
807 for (i = 0; i < BLK_RING_SIZE; i++) {
808 /* Not in use? */
809 if (copy[i].request == 0)
810 continue;
812 /* Grab a request slot and copy shadow state into it. */
813 req = RING_GET_REQUEST(
814 &info->ring, info->ring.req_prod_pvt);
815 *req = copy[i].req;
817 /* We get a new request id, and must reset the shadow state. */
818 req->id = GET_ID_FROM_FREELIST(info);
819 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
821 /* Rewrite any grant references invalidated by susp/resume. */
822 for (j = 0; j < req->nr_segments; j++)
823 gnttab_grant_foreign_access_ref(
824 req->seg[j].gref,
825 info->xbdev->otherend_id,
826 pfn_to_mfn(info->shadow[req->id].frame[j]),
827 rq_data_dir(
828 (struct request *)
829 info->shadow[req->id].request));
830 info->shadow[req->id].req = *req;
832 info->ring.req_prod_pvt++;
833 }
835 kfree(copy);
837 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
839 spin_lock_irq(&blkif_io_lock);
841 /* Now safe for us to use the shared ring */
842 info->connected = BLKIF_STATE_CONNECTED;
844 /* Send off requeued requests */
845 flush_requests(info);
847 /* Kick any other new requests queued since we resumed */
848 kick_pending_request_queues(info);
850 spin_unlock_irq(&blkif_io_lock);
851 }
854 /* ** Driver Registration ** */
857 static struct xenbus_device_id blkfront_ids[] = {
858 { "vbd" },
859 { "" }
860 };
863 static struct xenbus_driver blkfront = {
864 .name = "vbd",
865 .owner = THIS_MODULE,
866 .ids = blkfront_ids,
867 .probe = blkfront_probe,
868 .remove = blkfront_remove,
869 .resume = blkfront_resume,
870 .otherend_changed = backend_changed,
871 };
874 static int __init xlblk_init(void)
875 {
876 if (!is_running_on_xen())
877 return -ENODEV;
879 return xenbus_register_frontend(&blkfront);
880 }
881 module_init(xlblk_init);
884 static void xlblk_exit(void)
885 {
886 return xenbus_unregister_driver(&blkfront);
887 }
888 module_exit(xlblk_exit);
890 MODULE_LICENSE("Dual BSD/GPL");