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