block: xen-blkback: use API provided by xenbus module to map rings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / xen-blkback / xenbus.c
blob69233dd42212b2fd2ef9a7ac4124b1d65ecedc26
1 /* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
17 #include <stdarg.h>
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <xen/events.h>
21 #include <xen/grant_table.h>
22 #include "common.h"
24 struct backend_info {
25 struct xenbus_device *dev;
26 struct xen_blkif *blkif;
27 struct xenbus_watch backend_watch;
28 unsigned major;
29 unsigned minor;
30 char *mode;
33 static struct kmem_cache *xen_blkif_cachep;
34 static void connect(struct backend_info *);
35 static int connect_ring(struct backend_info *);
36 static void backend_changed(struct xenbus_watch *, const char **,
37 unsigned int);
39 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
41 return be->dev;
44 static int blkback_name(struct xen_blkif *blkif, char *buf)
46 char *devpath, *devname;
47 struct xenbus_device *dev = blkif->be->dev;
49 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
50 if (IS_ERR(devpath))
51 return PTR_ERR(devpath);
53 devname = strstr(devpath, "/dev/");
54 if (devname != NULL)
55 devname += strlen("/dev/");
56 else
57 devname = devpath;
59 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
60 kfree(devpath);
62 return 0;
65 static void xen_update_blkif_status(struct xen_blkif *blkif)
67 int err;
68 char name[TASK_COMM_LEN];
70 /* Not ready to connect? */
71 if (!blkif->irq || !blkif->vbd.bdev)
72 return;
74 /* Already connected? */
75 if (blkif->be->dev->state == XenbusStateConnected)
76 return;
78 /* Attempt to connect: exit if we fail to. */
79 connect(blkif->be);
80 if (blkif->be->dev->state != XenbusStateConnected)
81 return;
83 err = blkback_name(blkif, name);
84 if (err) {
85 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
86 return;
89 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
90 if (err) {
91 xenbus_dev_error(blkif->be->dev, err, "block flush");
92 return;
94 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
96 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name);
97 if (IS_ERR(blkif->xenblkd)) {
98 err = PTR_ERR(blkif->xenblkd);
99 blkif->xenblkd = NULL;
100 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
104 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
106 struct xen_blkif *blkif;
108 blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL);
109 if (!blkif)
110 return ERR_PTR(-ENOMEM);
112 memset(blkif, 0, sizeof(*blkif));
113 blkif->domid = domid;
114 spin_lock_init(&blkif->blk_ring_lock);
115 atomic_set(&blkif->refcnt, 1);
116 init_waitqueue_head(&blkif->wq);
117 blkif->st_print = jiffies;
118 init_waitqueue_head(&blkif->waiting_to_free);
120 return blkif;
123 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
124 unsigned int evtchn)
126 int err;
128 /* Already connected through? */
129 if (blkif->irq)
130 return 0;
132 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
133 if (err < 0)
134 return err;
136 switch (blkif->blk_protocol) {
137 case BLKIF_PROTOCOL_NATIVE:
139 struct blkif_sring *sring;
140 sring = (struct blkif_sring *)blkif->blk_ring;
141 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
142 break;
144 case BLKIF_PROTOCOL_X86_32:
146 struct blkif_x86_32_sring *sring_x86_32;
147 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
148 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
149 break;
151 case BLKIF_PROTOCOL_X86_64:
153 struct blkif_x86_64_sring *sring_x86_64;
154 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
155 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
156 break;
158 default:
159 BUG();
162 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
163 xen_blkif_be_int, 0,
164 "blkif-backend", blkif);
165 if (err < 0) {
166 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
167 blkif->blk_rings.common.sring = NULL;
168 return err;
170 blkif->irq = err;
172 return 0;
175 static void xen_blkif_disconnect(struct xen_blkif *blkif)
177 if (blkif->xenblkd) {
178 kthread_stop(blkif->xenblkd);
179 blkif->xenblkd = NULL;
182 atomic_dec(&blkif->refcnt);
183 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
184 atomic_inc(&blkif->refcnt);
186 if (blkif->irq) {
187 unbind_from_irqhandler(blkif->irq, blkif);
188 blkif->irq = 0;
191 if (blkif->blk_rings.common.sring) {
192 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
193 blkif->blk_rings.common.sring = NULL;
197 void xen_blkif_free(struct xen_blkif *blkif)
199 if (!atomic_dec_and_test(&blkif->refcnt))
200 BUG();
201 kmem_cache_free(xen_blkif_cachep, blkif);
204 int __init xen_blkif_interface_init(void)
206 xen_blkif_cachep = kmem_cache_create("blkif_cache",
207 sizeof(struct xen_blkif),
208 0, 0, NULL);
209 if (!xen_blkif_cachep)
210 return -ENOMEM;
212 return 0;
216 * sysfs interface for VBD I/O requests
219 #define VBD_SHOW(name, format, args...) \
220 static ssize_t show_##name(struct device *_dev, \
221 struct device_attribute *attr, \
222 char *buf) \
224 struct xenbus_device *dev = to_xenbus_device(_dev); \
225 struct backend_info *be = dev_get_drvdata(&dev->dev); \
227 return sprintf(buf, format, ##args); \
229 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
231 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
232 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
233 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
234 VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
235 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
236 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
238 static struct attribute *xen_vbdstat_attrs[] = {
239 &dev_attr_oo_req.attr,
240 &dev_attr_rd_req.attr,
241 &dev_attr_wr_req.attr,
242 &dev_attr_f_req.attr,
243 &dev_attr_rd_sect.attr,
244 &dev_attr_wr_sect.attr,
245 NULL
248 static struct attribute_group xen_vbdstat_group = {
249 .name = "statistics",
250 .attrs = xen_vbdstat_attrs,
253 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
254 VBD_SHOW(mode, "%s\n", be->mode);
256 int xenvbd_sysfs_addif(struct xenbus_device *dev)
258 int error;
260 error = device_create_file(&dev->dev, &dev_attr_physical_device);
261 if (error)
262 goto fail1;
264 error = device_create_file(&dev->dev, &dev_attr_mode);
265 if (error)
266 goto fail2;
268 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
269 if (error)
270 goto fail3;
272 return 0;
274 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
275 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
276 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
277 return error;
280 void xenvbd_sysfs_delif(struct xenbus_device *dev)
282 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
283 device_remove_file(&dev->dev, &dev_attr_mode);
284 device_remove_file(&dev->dev, &dev_attr_physical_device);
288 static void xen_vbd_free(struct xen_vbd *vbd)
290 if (vbd->bdev)
291 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
292 vbd->bdev = NULL;
295 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
296 unsigned major, unsigned minor, int readonly,
297 int cdrom)
299 struct xen_vbd *vbd;
300 struct block_device *bdev;
301 struct request_queue *q;
303 vbd = &blkif->vbd;
304 vbd->handle = handle;
305 vbd->readonly = readonly;
306 vbd->type = 0;
308 vbd->pdevice = MKDEV(major, minor);
310 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
311 FMODE_READ : FMODE_WRITE, NULL);
313 if (IS_ERR(bdev)) {
314 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
315 vbd->pdevice);
316 return -ENOENT;
319 vbd->bdev = bdev;
320 if (vbd->bdev->bd_disk == NULL) {
321 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
322 vbd->pdevice);
323 xen_vbd_free(vbd);
324 return -ENOENT;
326 vbd->size = vbd_sz(vbd);
328 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
329 vbd->type |= VDISK_CDROM;
330 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
331 vbd->type |= VDISK_REMOVABLE;
333 q = bdev_get_queue(bdev);
334 if (q && q->flush_flags)
335 vbd->flush_support = true;
337 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
338 handle, blkif->domid);
339 return 0;
341 static int xen_blkbk_remove(struct xenbus_device *dev)
343 struct backend_info *be = dev_get_drvdata(&dev->dev);
345 DPRINTK("");
347 if (be->major || be->minor)
348 xenvbd_sysfs_delif(dev);
350 if (be->backend_watch.node) {
351 unregister_xenbus_watch(&be->backend_watch);
352 kfree(be->backend_watch.node);
353 be->backend_watch.node = NULL;
356 if (be->blkif) {
357 xen_blkif_disconnect(be->blkif);
358 xen_vbd_free(&be->blkif->vbd);
359 xen_blkif_free(be->blkif);
360 be->blkif = NULL;
363 kfree(be);
364 dev_set_drvdata(&dev->dev, NULL);
365 return 0;
368 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
369 struct backend_info *be, int state)
371 struct xenbus_device *dev = be->dev;
372 int err;
374 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
375 "%d", state);
376 if (err)
377 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
379 return err;
383 * Entry point to this code when a new device is created. Allocate the basic
384 * structures, and watch the store waiting for the hotplug scripts to tell us
385 * the device's physical major and minor numbers. Switch to InitWait.
387 static int xen_blkbk_probe(struct xenbus_device *dev,
388 const struct xenbus_device_id *id)
390 int err;
391 struct backend_info *be = kzalloc(sizeof(struct backend_info),
392 GFP_KERNEL);
393 if (!be) {
394 xenbus_dev_fatal(dev, -ENOMEM,
395 "allocating backend structure");
396 return -ENOMEM;
398 be->dev = dev;
399 dev_set_drvdata(&dev->dev, be);
401 be->blkif = xen_blkif_alloc(dev->otherend_id);
402 if (IS_ERR(be->blkif)) {
403 err = PTR_ERR(be->blkif);
404 be->blkif = NULL;
405 xenbus_dev_fatal(dev, err, "creating block interface");
406 goto fail;
409 /* setup back pointer */
410 be->blkif->be = be;
412 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
413 "%s/%s", dev->nodename, "physical-device");
414 if (err)
415 goto fail;
417 err = xenbus_switch_state(dev, XenbusStateInitWait);
418 if (err)
419 goto fail;
421 return 0;
423 fail:
424 DPRINTK("failed");
425 xen_blkbk_remove(dev);
426 return err;
431 * Callback received when the hotplug scripts have placed the physical-device
432 * node. Read it and the mode node, and create a vbd. If the frontend is
433 * ready, connect.
435 static void backend_changed(struct xenbus_watch *watch,
436 const char **vec, unsigned int len)
438 int err;
439 unsigned major;
440 unsigned minor;
441 struct backend_info *be
442 = container_of(watch, struct backend_info, backend_watch);
443 struct xenbus_device *dev = be->dev;
444 int cdrom = 0;
445 char *device_type;
447 DPRINTK("");
449 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
450 &major, &minor);
451 if (XENBUS_EXIST_ERR(err)) {
453 * Since this watch will fire once immediately after it is
454 * registered, we expect this. Ignore it, and wait for the
455 * hotplug scripts.
457 return;
459 if (err != 2) {
460 xenbus_dev_fatal(dev, err, "reading physical-device");
461 return;
464 if ((be->major || be->minor) &&
465 ((be->major != major) || (be->minor != minor))) {
466 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
467 be->major, be->minor, major, minor);
468 return;
471 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
472 if (IS_ERR(be->mode)) {
473 err = PTR_ERR(be->mode);
474 be->mode = NULL;
475 xenbus_dev_fatal(dev, err, "reading mode");
476 return;
479 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
480 if (!IS_ERR(device_type)) {
481 cdrom = strcmp(device_type, "cdrom") == 0;
482 kfree(device_type);
485 if (be->major == 0 && be->minor == 0) {
486 /* Front end dir is a number, which is used as the handle. */
488 char *p = strrchr(dev->otherend, '/') + 1;
489 long handle;
490 err = strict_strtoul(p, 0, &handle);
491 if (err)
492 return;
494 be->major = major;
495 be->minor = minor;
497 err = xen_vbd_create(be->blkif, handle, major, minor,
498 (NULL == strchr(be->mode, 'w')), cdrom);
499 if (err) {
500 be->major = 0;
501 be->minor = 0;
502 xenbus_dev_fatal(dev, err, "creating vbd structure");
503 return;
506 err = xenvbd_sysfs_addif(dev);
507 if (err) {
508 xen_vbd_free(&be->blkif->vbd);
509 be->major = 0;
510 be->minor = 0;
511 xenbus_dev_fatal(dev, err, "creating sysfs entries");
512 return;
515 /* We're potentially connected now */
516 xen_update_blkif_status(be->blkif);
522 * Callback received when the frontend's state changes.
524 static void frontend_changed(struct xenbus_device *dev,
525 enum xenbus_state frontend_state)
527 struct backend_info *be = dev_get_drvdata(&dev->dev);
528 int err;
530 DPRINTK("%s", xenbus_strstate(frontend_state));
532 switch (frontend_state) {
533 case XenbusStateInitialising:
534 if (dev->state == XenbusStateClosed) {
535 pr_info(DRV_PFX "%s: prepare for reconnect\n",
536 dev->nodename);
537 xenbus_switch_state(dev, XenbusStateInitWait);
539 break;
541 case XenbusStateInitialised:
542 case XenbusStateConnected:
544 * Ensure we connect even when two watches fire in
545 * close successsion and we miss the intermediate value
546 * of frontend_state.
548 if (dev->state == XenbusStateConnected)
549 break;
552 * Enforce precondition before potential leak point.
553 * xen_blkif_disconnect() is idempotent.
555 xen_blkif_disconnect(be->blkif);
557 err = connect_ring(be);
558 if (err)
559 break;
560 xen_update_blkif_status(be->blkif);
561 break;
563 case XenbusStateClosing:
564 xenbus_switch_state(dev, XenbusStateClosing);
565 break;
567 case XenbusStateClosed:
568 xen_blkif_disconnect(be->blkif);
569 xenbus_switch_state(dev, XenbusStateClosed);
570 if (xenbus_dev_is_online(dev))
571 break;
572 /* fall through if not online */
573 case XenbusStateUnknown:
574 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
575 device_unregister(&dev->dev);
576 break;
578 default:
579 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
580 frontend_state);
581 break;
586 /* ** Connection ** */
590 * Write the physical details regarding the block device to the store, and
591 * switch to Connected state.
593 static void connect(struct backend_info *be)
595 struct xenbus_transaction xbt;
596 int err;
597 struct xenbus_device *dev = be->dev;
599 DPRINTK("%s", dev->otherend);
601 /* Supply the information about the device the frontend needs */
602 again:
603 err = xenbus_transaction_start(&xbt);
604 if (err) {
605 xenbus_dev_fatal(dev, err, "starting transaction");
606 return;
609 err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
610 if (err)
611 goto abort;
613 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
614 (unsigned long long)vbd_sz(&be->blkif->vbd));
615 if (err) {
616 xenbus_dev_fatal(dev, err, "writing %s/sectors",
617 dev->nodename);
618 goto abort;
621 /* FIXME: use a typename instead */
622 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
623 be->blkif->vbd.type |
624 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
625 if (err) {
626 xenbus_dev_fatal(dev, err, "writing %s/info",
627 dev->nodename);
628 goto abort;
630 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
631 (unsigned long)
632 bdev_logical_block_size(be->blkif->vbd.bdev));
633 if (err) {
634 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
635 dev->nodename);
636 goto abort;
639 err = xenbus_transaction_end(xbt, 0);
640 if (err == -EAGAIN)
641 goto again;
642 if (err)
643 xenbus_dev_fatal(dev, err, "ending transaction");
645 err = xenbus_switch_state(dev, XenbusStateConnected);
646 if (err)
647 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
648 dev->nodename);
650 return;
651 abort:
652 xenbus_transaction_end(xbt, 1);
656 static int connect_ring(struct backend_info *be)
658 struct xenbus_device *dev = be->dev;
659 unsigned long ring_ref;
660 unsigned int evtchn;
661 char protocol[64] = "";
662 int err;
664 DPRINTK("%s", dev->otherend);
666 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
667 &ring_ref, "event-channel", "%u", &evtchn, NULL);
668 if (err) {
669 xenbus_dev_fatal(dev, err,
670 "reading %s/ring-ref and event-channel",
671 dev->otherend);
672 return err;
675 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
676 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
677 "%63s", protocol, NULL);
678 if (err)
679 strcpy(protocol, "unspecified, assuming native");
680 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
681 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
682 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
683 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
684 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
685 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
686 else {
687 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
688 return -1;
690 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n",
691 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
693 /* Map the shared frame, irq etc. */
694 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
695 if (err) {
696 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
697 ring_ref, evtchn);
698 return err;
701 return 0;
705 /* ** Driver Registration ** */
708 static const struct xenbus_device_id xen_blkbk_ids[] = {
709 { "vbd" },
710 { "" }
714 static struct xenbus_driver xen_blkbk = {
715 .name = "vbd",
716 .owner = THIS_MODULE,
717 .ids = xen_blkbk_ids,
718 .probe = xen_blkbk_probe,
719 .remove = xen_blkbk_remove,
720 .otherend_changed = frontend_changed
724 int xen_blkif_xenbus_init(void)
726 return xenbus_register_backend(&xen_blkbk);