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.
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <xen/events.h>
21 #include <xen/grant_table.h>
25 struct xenbus_device *dev;
26 struct blkif_st *blkif;
27 struct xenbus_watch backend_watch;
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 **,
39 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
44 static int blkback_name(struct blkif_st *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);
51 return PTR_ERR(devpath);
53 devname = strstr(devpath, "/dev/");
55 devname += strlen("/dev/");
59 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
65 static void xen_update_blkif_status(struct blkif_st *blkif)
68 char name[TASK_COMM_LEN];
70 /* Not ready to connect? */
71 if (!blkif->irq || !blkif->vbd.bdev)
74 /* Already connected? */
75 if (blkif->be->dev->state == XenbusStateConnected)
78 /* Attempt to connect: exit if we fail to. */
80 if (blkif->be->dev->state != XenbusStateConnected)
83 err = blkback_name(blkif, name);
85 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
89 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
91 xenbus_dev_error(blkif->be->dev, err, "block flush");
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 blkif_st *xen_blkif_alloc(domid_t domid)
106 struct blkif_st *blkif;
108 blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL);
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);
123 static int map_frontend_page(struct blkif_st *blkif, unsigned long shared_page)
125 struct gnttab_map_grant_ref op;
127 gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
128 GNTMAP_host_map, shared_page, blkif->domid);
130 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
134 DPRINTK("Grant table operation failure !\n");
138 blkif->shmem_ref = shared_page;
139 blkif->shmem_handle = op.handle;
144 static void unmap_frontend_page(struct blkif_st *blkif)
146 struct gnttab_unmap_grant_ref op;
148 gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
149 GNTMAP_host_map, blkif->shmem_handle);
151 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
155 static int xen_blkif_map(struct blkif_st *blkif, unsigned long shared_page,
160 /* Already connected through? */
164 blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE);
165 if (!blkif->blk_ring_area)
168 err = map_frontend_page(blkif, shared_page);
170 free_vm_area(blkif->blk_ring_area);
174 switch (blkif->blk_protocol) {
175 case BLKIF_PROTOCOL_NATIVE:
177 struct blkif_sring *sring;
178 sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
179 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
182 case BLKIF_PROTOCOL_X86_32:
184 struct blkif_x86_32_sring *sring_x86_32;
185 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
186 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
189 case BLKIF_PROTOCOL_X86_64:
191 struct blkif_x86_64_sring *sring_x86_64;
192 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
193 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
200 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
202 "blkif-backend", blkif);
204 unmap_frontend_page(blkif);
205 free_vm_area(blkif->blk_ring_area);
206 blkif->blk_rings.common.sring = NULL;
214 static void xen_blkif_disconnect(struct blkif_st *blkif)
216 if (blkif->xenblkd) {
217 kthread_stop(blkif->xenblkd);
218 blkif->xenblkd = NULL;
221 atomic_dec(&blkif->refcnt);
222 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
223 atomic_inc(&blkif->refcnt);
226 unbind_from_irqhandler(blkif->irq, blkif);
230 if (blkif->blk_rings.common.sring) {
231 unmap_frontend_page(blkif);
232 free_vm_area(blkif->blk_ring_area);
233 blkif->blk_rings.common.sring = NULL;
237 void xen_blkif_free(struct blkif_st *blkif)
239 if (!atomic_dec_and_test(&blkif->refcnt))
241 kmem_cache_free(xen_blkif_cachep, blkif);
244 int __init xen_blkif_interface_init(void)
246 xen_blkif_cachep = kmem_cache_create("blkif_cache",
247 sizeof(struct blkif_st),
249 if (!xen_blkif_cachep)
256 * sysfs interface for VBD I/O requests
259 #define VBD_SHOW(name, format, args...) \
260 static ssize_t show_##name(struct device *_dev, \
261 struct device_attribute *attr, \
264 struct xenbus_device *dev = to_xenbus_device(_dev); \
265 struct backend_info *be = dev_get_drvdata(&dev->dev); \
267 return sprintf(buf, format, ##args); \
269 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
271 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
272 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
273 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
274 VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
275 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
276 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
278 static struct attribute *vbdstat_attrs[] = {
279 &dev_attr_oo_req.attr,
280 &dev_attr_rd_req.attr,
281 &dev_attr_wr_req.attr,
282 &dev_attr_f_req.attr,
283 &dev_attr_rd_sect.attr,
284 &dev_attr_wr_sect.attr,
288 static struct attribute_group vbdstat_group = {
289 .name = "statistics",
290 .attrs = vbdstat_attrs,
293 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
294 VBD_SHOW(mode, "%s\n", be->mode);
296 int xenvbd_sysfs_addif(struct xenbus_device *dev)
300 error = device_create_file(&dev->dev, &dev_attr_physical_device);
304 error = device_create_file(&dev->dev, &dev_attr_mode);
308 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
314 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
315 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
316 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
320 void xenvbd_sysfs_delif(struct xenbus_device *dev)
322 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
323 device_remove_file(&dev->dev, &dev_attr_mode);
324 device_remove_file(&dev->dev, &dev_attr_physical_device);
328 static void vbd_free(struct vbd *vbd)
331 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
335 static int vbd_create(struct blkif_st *blkif, blkif_vdev_t handle,
336 unsigned major, unsigned minor, int readonly,
340 struct block_device *bdev;
341 struct request_queue *q;
344 vbd->handle = handle;
345 vbd->readonly = readonly;
348 vbd->pdevice = MKDEV(major, minor);
350 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
351 FMODE_READ : FMODE_WRITE, NULL);
354 DPRINTK("vbd_creat: device %08x could not be opened.\n",
360 vbd->size = vbd_sz(vbd);
362 if (vbd->bdev->bd_disk == NULL) {
363 DPRINTK("vbd_creat: device %08x doesn't exist.\n",
369 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
370 vbd->type |= VDISK_CDROM;
371 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
372 vbd->type |= VDISK_REMOVABLE;
374 q = bdev_get_queue(bdev);
375 if (q && q->flush_flags)
376 vbd->flush_support = true;
378 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
379 handle, blkif->domid);
382 static int xen_blkbk_remove(struct xenbus_device *dev)
384 struct backend_info *be = dev_get_drvdata(&dev->dev);
388 if (be->major || be->minor)
389 xenvbd_sysfs_delif(dev);
391 if (be->backend_watch.node) {
392 unregister_xenbus_watch(&be->backend_watch);
393 kfree(be->backend_watch.node);
394 be->backend_watch.node = NULL;
398 xen_blkif_disconnect(be->blkif);
399 vbd_free(&be->blkif->vbd);
400 xen_blkif_free(be->blkif);
405 dev_set_drvdata(&dev->dev, NULL);
409 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
410 struct backend_info *be, int state)
412 struct xenbus_device *dev = be->dev;
415 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
418 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
424 * Entry point to this code when a new device is created. Allocate the basic
425 * structures, and watch the store waiting for the hotplug scripts to tell us
426 * the device's physical major and minor numbers. Switch to InitWait.
428 static int xen_blkbk_probe(struct xenbus_device *dev,
429 const struct xenbus_device_id *id)
432 struct backend_info *be = kzalloc(sizeof(struct backend_info),
435 xenbus_dev_fatal(dev, -ENOMEM,
436 "allocating backend structure");
440 dev_set_drvdata(&dev->dev, be);
442 be->blkif = xen_blkif_alloc(dev->otherend_id);
443 if (IS_ERR(be->blkif)) {
444 err = PTR_ERR(be->blkif);
446 xenbus_dev_fatal(dev, err, "creating block interface");
450 /* setup back pointer */
453 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
454 "%s/%s", dev->nodename, "physical-device");
458 err = xenbus_switch_state(dev, XenbusStateInitWait);
466 xen_blkbk_remove(dev);
472 * Callback received when the hotplug scripts have placed the physical-device
473 * node. Read it and the mode node, and create a vbd. If the frontend is
476 static void backend_changed(struct xenbus_watch *watch,
477 const char **vec, unsigned int len)
482 struct backend_info *be
483 = container_of(watch, struct backend_info, backend_watch);
484 struct xenbus_device *dev = be->dev;
490 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
492 if (XENBUS_EXIST_ERR(err)) {
494 * Since this watch will fire once immediately after it is
495 * registered, we expect this. Ignore it, and wait for the
501 xenbus_dev_fatal(dev, err, "reading physical-device");
505 if ((be->major || be->minor) &&
506 ((be->major != major) || (be->minor != minor))) {
507 pr_warn("xen-blkback: changing physical device (from %x:%x to %x:%x) not supported.\n",
508 be->major, be->minor, major, minor);
512 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
513 if (IS_ERR(be->mode)) {
514 err = PTR_ERR(be->mode);
516 xenbus_dev_fatal(dev, err, "reading mode");
520 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
521 if (!IS_ERR(device_type)) {
522 cdrom = strcmp(device_type, "cdrom") == 0;
526 if (be->major == 0 && be->minor == 0) {
527 /* Front end dir is a number, which is used as the handle. */
529 char *p = strrchr(dev->otherend, '/') + 1;
531 err = strict_strtoul(p, 0, &handle);
538 err = vbd_create(be->blkif, handle, major, minor,
539 (NULL == strchr(be->mode, 'w')), cdrom);
541 be->major = be->minor = 0;
542 xenbus_dev_fatal(dev, err, "creating vbd structure");
546 err = xenvbd_sysfs_addif(dev);
548 vbd_free(&be->blkif->vbd);
549 be->major = be->minor = 0;
550 xenbus_dev_fatal(dev, err, "creating sysfs entries");
554 /* We're potentially connected now */
555 xen_update_blkif_status(be->blkif);
561 * Callback received when the frontend's state changes.
563 static void frontend_changed(struct xenbus_device *dev,
564 enum xenbus_state frontend_state)
566 struct backend_info *be = dev_get_drvdata(&dev->dev);
569 DPRINTK("%s", xenbus_strstate(frontend_state));
571 switch (frontend_state) {
572 case XenbusStateInitialising:
573 if (dev->state == XenbusStateClosed) {
574 pr_info("xen-blkback: %s: prepare for reconnect\n",
576 xenbus_switch_state(dev, XenbusStateInitWait);
580 case XenbusStateInitialised:
581 case XenbusStateConnected:
583 * Ensure we connect even when two watches fire in
584 * close successsion and we miss the intermediate value
587 if (dev->state == XenbusStateConnected)
591 * Enforce precondition before potential leak point.
592 * blkif_disconnect() is idempotent.
594 xen_blkif_disconnect(be->blkif);
596 err = connect_ring(be);
599 xen_update_blkif_status(be->blkif);
602 case XenbusStateClosing:
603 xen_blkif_disconnect(be->blkif);
604 xenbus_switch_state(dev, XenbusStateClosing);
607 case XenbusStateClosed:
608 xenbus_switch_state(dev, XenbusStateClosed);
609 if (xenbus_dev_is_online(dev))
611 /* fall through if not online */
612 case XenbusStateUnknown:
613 /* implies blkif_disconnect() via blkback_remove() */
614 device_unregister(&dev->dev);
618 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
625 /* ** Connection ** */
629 * Write the physical details regarding the block device to the store, and
630 * switch to Connected state.
632 static void connect(struct backend_info *be)
634 struct xenbus_transaction xbt;
636 struct xenbus_device *dev = be->dev;
638 DPRINTK("%s", dev->otherend);
640 /* Supply the information about the device the frontend needs */
642 err = xenbus_transaction_start(&xbt);
644 xenbus_dev_fatal(dev, err, "starting transaction");
648 err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
652 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
653 (unsigned long long)vbd_sz(&be->blkif->vbd));
655 xenbus_dev_fatal(dev, err, "writing %s/sectors",
660 /* FIXME: use a typename instead */
661 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
662 be->blkif->vbd.type |
663 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
665 xenbus_dev_fatal(dev, err, "writing %s/info",
669 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
671 bdev_logical_block_size(be->blkif->vbd.bdev));
673 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
678 err = xenbus_transaction_end(xbt, 0);
682 xenbus_dev_fatal(dev, err, "ending transaction");
684 err = xenbus_switch_state(dev, XenbusStateConnected);
686 xenbus_dev_fatal(dev, err, "switching to Connected state",
691 xenbus_transaction_end(xbt, 1);
695 static int connect_ring(struct backend_info *be)
697 struct xenbus_device *dev = be->dev;
698 unsigned long ring_ref;
700 char protocol[64] = "";
703 DPRINTK("%s", dev->otherend);
705 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
706 &ring_ref, "event-channel", "%u", &evtchn, NULL);
708 xenbus_dev_fatal(dev, err,
709 "reading %s/ring-ref and event-channel",
714 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
715 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
716 "%63s", protocol, NULL);
718 strcpy(protocol, "unspecified, assuming native");
719 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
720 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
721 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
722 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
723 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
724 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
726 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
729 pr_info("xen-blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
730 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
732 /* Map the shared frame, irq etc. */
733 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
735 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
744 /* ** Driver Registration ** */
747 static const struct xenbus_device_id xen_blkbk_ids[] = {
753 static struct xenbus_driver xen_blkbk = {
755 .owner = THIS_MODULE,
756 .ids = xen_blkbk_ids,
757 .probe = xen_blkbk_probe,
758 .remove = xen_blkbk_remove,
759 .otherend_changed = frontend_changed
763 int xen_blkif_xenbus_init(void)
765 return xenbus_register_backend(&xen_blkbk);