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