Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / block / xen-blkback / xenbus.c
blob9fa9d07967165fb250bfbb50b3ecbfa6d8269905
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);
38 static void xen_vbd_sync(struct xen_vbd *vbd);
40 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
42 return be->dev;
45 static int blkback_name(struct xen_blkif *blkif, char *buf)
47 char *devpath, *devname;
48 struct xenbus_device *dev = blkif->be->dev;
50 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
51 if (IS_ERR(devpath))
52 return PTR_ERR(devpath);
54 devname = strstr(devpath, "/dev/");
55 if (devname != NULL)
56 devname += strlen("/dev/");
57 else
58 devname = devpath;
60 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
61 kfree(devpath);
63 return 0;
66 static void xen_update_blkif_status(struct xen_blkif *blkif)
68 int err;
69 char name[TASK_COMM_LEN];
71 /* Not ready to connect? */
72 if (!blkif->irq || !blkif->vbd.bdev)
73 return;
75 /* Already connected? */
76 if (blkif->be->dev->state == XenbusStateConnected)
77 return;
79 /* Attempt to connect: exit if we fail to. */
80 connect(blkif->be);
81 if (blkif->be->dev->state != XenbusStateConnected)
82 return;
84 err = blkback_name(blkif, name);
85 if (err) {
86 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
87 return;
90 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
91 if (err) {
92 xenbus_dev_error(blkif->be->dev, err, "block flush");
93 return;
95 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
97 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name);
98 if (IS_ERR(blkif->xenblkd)) {
99 err = PTR_ERR(blkif->xenblkd);
100 blkif->xenblkd = NULL;
101 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
105 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
107 struct xen_blkif *blkif;
109 blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL);
110 if (!blkif)
111 return ERR_PTR(-ENOMEM);
113 memset(blkif, 0, sizeof(*blkif));
114 blkif->domid = domid;
115 spin_lock_init(&blkif->blk_ring_lock);
116 atomic_set(&blkif->refcnt, 1);
117 init_waitqueue_head(&blkif->wq);
118 blkif->st_print = jiffies;
119 init_waitqueue_head(&blkif->waiting_to_free);
121 return blkif;
124 static int map_frontend_page(struct xen_blkif *blkif, unsigned long shared_page)
126 struct gnttab_map_grant_ref op;
128 gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
129 GNTMAP_host_map, shared_page, blkif->domid);
131 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
132 BUG();
134 if (op.status) {
135 DPRINTK("Grant table operation failure !\n");
136 return op.status;
139 blkif->shmem_ref = shared_page;
140 blkif->shmem_handle = op.handle;
142 return 0;
145 static void unmap_frontend_page(struct xen_blkif *blkif)
147 struct gnttab_unmap_grant_ref op;
149 gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
150 GNTMAP_host_map, blkif->shmem_handle);
152 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
153 BUG();
156 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
157 unsigned int evtchn)
159 int err;
161 /* Already connected through? */
162 if (blkif->irq)
163 return 0;
165 blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE);
166 if (!blkif->blk_ring_area)
167 return -ENOMEM;
169 err = map_frontend_page(blkif, shared_page);
170 if (err) {
171 free_vm_area(blkif->blk_ring_area);
172 return err;
175 switch (blkif->blk_protocol) {
176 case BLKIF_PROTOCOL_NATIVE:
178 struct blkif_sring *sring;
179 sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
180 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
181 break;
183 case BLKIF_PROTOCOL_X86_32:
185 struct blkif_x86_32_sring *sring_x86_32;
186 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
187 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
188 break;
190 case BLKIF_PROTOCOL_X86_64:
192 struct blkif_x86_64_sring *sring_x86_64;
193 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
194 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
195 break;
197 default:
198 BUG();
201 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
202 xen_blkif_be_int, 0,
203 "blkif-backend", blkif);
204 if (err < 0) {
205 unmap_frontend_page(blkif);
206 free_vm_area(blkif->blk_ring_area);
207 blkif->blk_rings.common.sring = NULL;
208 return err;
210 blkif->irq = err;
212 return 0;
215 static void xen_blkif_disconnect(struct xen_blkif *blkif)
217 if (blkif->xenblkd) {
218 kthread_stop(blkif->xenblkd);
219 blkif->xenblkd = NULL;
222 atomic_dec(&blkif->refcnt);
223 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
224 atomic_inc(&blkif->refcnt);
226 if (blkif->irq) {
227 unbind_from_irqhandler(blkif->irq, blkif);
228 blkif->irq = 0;
231 if (blkif->blk_rings.common.sring) {
232 unmap_frontend_page(blkif);
233 free_vm_area(blkif->blk_ring_area);
234 blkif->blk_rings.common.sring = NULL;
236 xen_vbd_sync(&blkif->vbd);
239 void xen_blkif_free(struct xen_blkif *blkif)
241 if (!atomic_dec_and_test(&blkif->refcnt))
242 BUG();
243 kmem_cache_free(xen_blkif_cachep, blkif);
246 int __init xen_blkif_interface_init(void)
248 xen_blkif_cachep = kmem_cache_create("blkif_cache",
249 sizeof(struct xen_blkif),
250 0, 0, NULL);
251 if (!xen_blkif_cachep)
252 return -ENOMEM;
254 return 0;
258 * sysfs interface for VBD I/O requests
261 #define VBD_SHOW(name, format, args...) \
262 static ssize_t show_##name(struct device *_dev, \
263 struct device_attribute *attr, \
264 char *buf) \
266 struct xenbus_device *dev = to_xenbus_device(_dev); \
267 struct backend_info *be = dev_get_drvdata(&dev->dev); \
269 return sprintf(buf, format, ##args); \
271 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
273 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
274 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
275 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
276 VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
277 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
278 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
280 static struct attribute *xen_vbdstat_attrs[] = {
281 &dev_attr_oo_req.attr,
282 &dev_attr_rd_req.attr,
283 &dev_attr_wr_req.attr,
284 &dev_attr_f_req.attr,
285 &dev_attr_rd_sect.attr,
286 &dev_attr_wr_sect.attr,
287 NULL
290 static struct attribute_group xen_vbdstat_group = {
291 .name = "statistics",
292 .attrs = xen_vbdstat_attrs,
295 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
296 VBD_SHOW(mode, "%s\n", be->mode);
298 int xenvbd_sysfs_addif(struct xenbus_device *dev)
300 int error;
302 error = device_create_file(&dev->dev, &dev_attr_physical_device);
303 if (error)
304 goto fail1;
306 error = device_create_file(&dev->dev, &dev_attr_mode);
307 if (error)
308 goto fail2;
310 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
311 if (error)
312 goto fail3;
314 return 0;
316 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
317 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
318 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
319 return error;
322 void xenvbd_sysfs_delif(struct xenbus_device *dev)
324 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
325 device_remove_file(&dev->dev, &dev_attr_mode);
326 device_remove_file(&dev->dev, &dev_attr_physical_device);
330 static void xen_vbd_free(struct xen_vbd *vbd)
332 if (vbd->bdev)
333 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
334 vbd->bdev = NULL;
337 static void xen_vbd_sync(struct xen_vbd *vbd)
339 if (vbd->bdev)
340 fsync_bdev(vbd->bdev);
343 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
344 unsigned major, unsigned minor, int readonly,
345 int cdrom)
347 struct xen_vbd *vbd;
348 struct block_device *bdev;
349 struct request_queue *q;
351 vbd = &blkif->vbd;
352 vbd->handle = handle;
353 vbd->readonly = readonly;
354 vbd->type = 0;
356 vbd->pdevice = MKDEV(major, minor);
358 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
359 FMODE_READ : FMODE_WRITE, NULL);
361 if (IS_ERR(bdev)) {
362 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
363 vbd->pdevice);
364 return -ENOENT;
367 vbd->bdev = bdev;
368 if (vbd->bdev->bd_disk == NULL) {
369 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
370 vbd->pdevice);
371 xen_vbd_free(vbd);
372 return -ENOENT;
374 vbd->size = vbd_sz(vbd);
376 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
377 vbd->type |= VDISK_CDROM;
378 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
379 vbd->type |= VDISK_REMOVABLE;
381 q = bdev_get_queue(bdev);
382 if (q && q->flush_flags)
383 vbd->flush_support = true;
385 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
386 handle, blkif->domid);
387 return 0;
389 static int xen_blkbk_remove(struct xenbus_device *dev)
391 struct backend_info *be = dev_get_drvdata(&dev->dev);
393 DPRINTK("");
395 if (be->major || be->minor)
396 xenvbd_sysfs_delif(dev);
398 if (be->backend_watch.node) {
399 unregister_xenbus_watch(&be->backend_watch);
400 kfree(be->backend_watch.node);
401 be->backend_watch.node = NULL;
404 if (be->blkif) {
405 xen_blkif_disconnect(be->blkif);
406 xen_vbd_free(&be->blkif->vbd);
407 xen_blkif_free(be->blkif);
408 be->blkif = NULL;
411 kfree(be);
412 dev_set_drvdata(&dev->dev, NULL);
413 return 0;
416 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
417 struct backend_info *be, int state)
419 struct xenbus_device *dev = be->dev;
420 int err;
422 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
423 "%d", state);
424 if (err)
425 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
427 return err;
431 * Entry point to this code when a new device is created. Allocate the basic
432 * structures, and watch the store waiting for the hotplug scripts to tell us
433 * the device's physical major and minor numbers. Switch to InitWait.
435 static int xen_blkbk_probe(struct xenbus_device *dev,
436 const struct xenbus_device_id *id)
438 int err;
439 struct backend_info *be = kzalloc(sizeof(struct backend_info),
440 GFP_KERNEL);
441 if (!be) {
442 xenbus_dev_fatal(dev, -ENOMEM,
443 "allocating backend structure");
444 return -ENOMEM;
446 be->dev = dev;
447 dev_set_drvdata(&dev->dev, be);
449 be->blkif = xen_blkif_alloc(dev->otherend_id);
450 if (IS_ERR(be->blkif)) {
451 err = PTR_ERR(be->blkif);
452 be->blkif = NULL;
453 xenbus_dev_fatal(dev, err, "creating block interface");
454 goto fail;
457 /* setup back pointer */
458 be->blkif->be = be;
460 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
461 "%s/%s", dev->nodename, "physical-device");
462 if (err)
463 goto fail;
465 err = xenbus_switch_state(dev, XenbusStateInitWait);
466 if (err)
467 goto fail;
469 return 0;
471 fail:
472 DPRINTK("failed");
473 xen_blkbk_remove(dev);
474 return err;
479 * Callback received when the hotplug scripts have placed the physical-device
480 * node. Read it and the mode node, and create a vbd. If the frontend is
481 * ready, connect.
483 static void backend_changed(struct xenbus_watch *watch,
484 const char **vec, unsigned int len)
486 int err;
487 unsigned major;
488 unsigned minor;
489 struct backend_info *be
490 = container_of(watch, struct backend_info, backend_watch);
491 struct xenbus_device *dev = be->dev;
492 int cdrom = 0;
493 char *device_type;
495 DPRINTK("");
497 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
498 &major, &minor);
499 if (XENBUS_EXIST_ERR(err)) {
501 * Since this watch will fire once immediately after it is
502 * registered, we expect this. Ignore it, and wait for the
503 * hotplug scripts.
505 return;
507 if (err != 2) {
508 xenbus_dev_fatal(dev, err, "reading physical-device");
509 return;
512 if ((be->major || be->minor) &&
513 ((be->major != major) || (be->minor != minor))) {
514 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
515 be->major, be->minor, major, minor);
516 return;
519 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
520 if (IS_ERR(be->mode)) {
521 err = PTR_ERR(be->mode);
522 be->mode = NULL;
523 xenbus_dev_fatal(dev, err, "reading mode");
524 return;
527 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
528 if (!IS_ERR(device_type)) {
529 cdrom = strcmp(device_type, "cdrom") == 0;
530 kfree(device_type);
533 if (be->major == 0 && be->minor == 0) {
534 /* Front end dir is a number, which is used as the handle. */
536 char *p = strrchr(dev->otherend, '/') + 1;
537 long handle;
538 err = strict_strtoul(p, 0, &handle);
539 if (err)
540 return;
542 be->major = major;
543 be->minor = minor;
545 err = xen_vbd_create(be->blkif, handle, major, minor,
546 (NULL == strchr(be->mode, 'w')), cdrom);
547 if (err) {
548 be->major = 0;
549 be->minor = 0;
550 xenbus_dev_fatal(dev, err, "creating vbd structure");
551 return;
554 err = xenvbd_sysfs_addif(dev);
555 if (err) {
556 xen_vbd_free(&be->blkif->vbd);
557 be->major = 0;
558 be->minor = 0;
559 xenbus_dev_fatal(dev, err, "creating sysfs entries");
560 return;
563 /* We're potentially connected now */
564 xen_update_blkif_status(be->blkif);
570 * Callback received when the frontend's state changes.
572 static void frontend_changed(struct xenbus_device *dev,
573 enum xenbus_state frontend_state)
575 struct backend_info *be = dev_get_drvdata(&dev->dev);
576 int err;
578 DPRINTK("%s", xenbus_strstate(frontend_state));
580 switch (frontend_state) {
581 case XenbusStateInitialising:
582 if (dev->state == XenbusStateClosed) {
583 pr_info(DRV_PFX "%s: prepare for reconnect\n",
584 dev->nodename);
585 xenbus_switch_state(dev, XenbusStateInitWait);
587 break;
589 case XenbusStateInitialised:
590 case XenbusStateConnected:
592 * Ensure we connect even when two watches fire in
593 * close successsion and we miss the intermediate value
594 * of frontend_state.
596 if (dev->state == XenbusStateConnected)
597 break;
600 * Enforce precondition before potential leak point.
601 * xen_blkif_disconnect() is idempotent.
603 xen_blkif_disconnect(be->blkif);
605 err = connect_ring(be);
606 if (err)
607 break;
608 xen_update_blkif_status(be->blkif);
609 break;
611 case XenbusStateClosing:
612 xenbus_switch_state(dev, XenbusStateClosing);
613 break;
615 case XenbusStateClosed:
616 xen_blkif_disconnect(be->blkif);
617 xenbus_switch_state(dev, XenbusStateClosed);
618 if (xenbus_dev_is_online(dev))
619 break;
620 /* fall through if not online */
621 case XenbusStateUnknown:
622 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
623 device_unregister(&dev->dev);
624 break;
626 default:
627 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
628 frontend_state);
629 break;
634 /* ** Connection ** */
638 * Write the physical details regarding the block device to the store, and
639 * switch to Connected state.
641 static void connect(struct backend_info *be)
643 struct xenbus_transaction xbt;
644 int err;
645 struct xenbus_device *dev = be->dev;
647 DPRINTK("%s", dev->otherend);
649 /* Supply the information about the device the frontend needs */
650 again:
651 err = xenbus_transaction_start(&xbt);
652 if (err) {
653 xenbus_dev_fatal(dev, err, "starting transaction");
654 return;
657 err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
658 if (err)
659 goto abort;
661 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
662 (unsigned long long)vbd_sz(&be->blkif->vbd));
663 if (err) {
664 xenbus_dev_fatal(dev, err, "writing %s/sectors",
665 dev->nodename);
666 goto abort;
669 /* FIXME: use a typename instead */
670 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
671 be->blkif->vbd.type |
672 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
673 if (err) {
674 xenbus_dev_fatal(dev, err, "writing %s/info",
675 dev->nodename);
676 goto abort;
678 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
679 (unsigned long)
680 bdev_logical_block_size(be->blkif->vbd.bdev));
681 if (err) {
682 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
683 dev->nodename);
684 goto abort;
687 err = xenbus_transaction_end(xbt, 0);
688 if (err == -EAGAIN)
689 goto again;
690 if (err)
691 xenbus_dev_fatal(dev, err, "ending transaction");
693 err = xenbus_switch_state(dev, XenbusStateConnected);
694 if (err)
695 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
696 dev->nodename);
698 return;
699 abort:
700 xenbus_transaction_end(xbt, 1);
704 static int connect_ring(struct backend_info *be)
706 struct xenbus_device *dev = be->dev;
707 unsigned long ring_ref;
708 unsigned int evtchn;
709 char protocol[64] = "";
710 int err;
712 DPRINTK("%s", dev->otherend);
714 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
715 &ring_ref, "event-channel", "%u", &evtchn, NULL);
716 if (err) {
717 xenbus_dev_fatal(dev, err,
718 "reading %s/ring-ref and event-channel",
719 dev->otherend);
720 return err;
723 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
724 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
725 "%63s", protocol, NULL);
726 if (err)
727 strcpy(protocol, "unspecified, assuming native");
728 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
729 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
730 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
731 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
732 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
733 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
734 else {
735 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
736 return -1;
738 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n",
739 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
741 /* Map the shared frame, irq etc. */
742 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
743 if (err) {
744 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
745 ring_ref, evtchn);
746 return err;
749 return 0;
753 /* ** Driver Registration ** */
756 static const struct xenbus_device_id xen_blkbk_ids[] = {
757 { "vbd" },
758 { "" }
762 static struct xenbus_driver xen_blkbk = {
763 .name = "vbd",
764 .owner = THIS_MODULE,
765 .ids = xen_blkbk_ids,
766 .probe = xen_blkbk_probe,
767 .remove = xen_blkbk_remove,
768 .otherend_changed = frontend_changed
772 int xen_blkif_xenbus_init(void)
774 return xenbus_register_backend(&xen_blkbk);