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 xen_blkif *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 **,
38 static void xen_vbd_sync(struct xen_vbd *vbd);
40 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
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);
52 return PTR_ERR(devpath);
54 devname = strstr(devpath, "/dev/");
56 devname += strlen("/dev/");
60 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
66 static void xen_update_blkif_status(struct xen_blkif *blkif)
69 char name[TASK_COMM_LEN];
71 /* Not ready to connect? */
72 if (!blkif->irq || !blkif->vbd.bdev)
75 /* Already connected? */
76 if (blkif->be->dev->state == XenbusStateConnected)
79 /* Attempt to connect: exit if we fail to. */
81 if (blkif->be->dev->state != XenbusStateConnected)
84 err = blkback_name(blkif, name);
86 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
90 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
92 xenbus_dev_error(blkif->be->dev, err, "block flush");
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);
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);
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))
135 DPRINTK("Grant table operation failure !\n");
139 blkif->shmem_ref = shared_page;
140 blkif->shmem_handle = op.handle;
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))
156 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
161 /* Already connected through? */
165 blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE);
166 if (!blkif->blk_ring_area)
169 err = map_frontend_page(blkif, shared_page);
171 free_vm_area(blkif->blk_ring_area);
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);
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);
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);
201 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
203 "blkif-backend", blkif);
205 unmap_frontend_page(blkif);
206 free_vm_area(blkif->blk_ring_area);
207 blkif->blk_rings.common.sring = NULL;
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);
227 unbind_from_irqhandler(blkif->irq, blkif);
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))
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),
251 if (!xen_blkif_cachep)
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, \
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,
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)
302 error = device_create_file(&dev->dev, &dev_attr_physical_device);
306 error = device_create_file(&dev->dev, &dev_attr_mode);
310 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
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);
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)
333 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
337 static void xen_vbd_sync(struct xen_vbd *vbd)
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,
348 struct block_device *bdev;
349 struct request_queue *q;
352 vbd->handle = handle;
353 vbd->readonly = readonly;
356 vbd->pdevice = MKDEV(major, minor);
358 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
359 FMODE_READ : FMODE_WRITE, NULL);
362 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
368 if (vbd->bdev->bd_disk == NULL) {
369 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
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);
389 static int xen_blkbk_remove(struct xenbus_device *dev)
391 struct backend_info *be = dev_get_drvdata(&dev->dev);
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;
405 xen_blkif_disconnect(be->blkif);
406 xen_vbd_free(&be->blkif->vbd);
407 xen_blkif_free(be->blkif);
412 dev_set_drvdata(&dev->dev, NULL);
416 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
417 struct backend_info *be, int state)
419 struct xenbus_device *dev = be->dev;
422 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
425 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
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)
439 struct backend_info *be = kzalloc(sizeof(struct backend_info),
442 xenbus_dev_fatal(dev, -ENOMEM,
443 "allocating backend structure");
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);
453 xenbus_dev_fatal(dev, err, "creating block interface");
457 /* setup back pointer */
460 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
461 "%s/%s", dev->nodename, "physical-device");
465 err = xenbus_switch_state(dev, XenbusStateInitWait);
473 xen_blkbk_remove(dev);
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
483 static void backend_changed(struct xenbus_watch *watch,
484 const char **vec, unsigned int len)
489 struct backend_info *be
490 = container_of(watch, struct backend_info, backend_watch);
491 struct xenbus_device *dev = be->dev;
497 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
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
508 xenbus_dev_fatal(dev, err, "reading physical-device");
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);
519 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
520 if (IS_ERR(be->mode)) {
521 err = PTR_ERR(be->mode);
523 xenbus_dev_fatal(dev, err, "reading mode");
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;
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;
538 err = strict_strtoul(p, 0, &handle);
545 err = xen_vbd_create(be->blkif, handle, major, minor,
546 (NULL == strchr(be->mode, 'w')), cdrom);
550 xenbus_dev_fatal(dev, err, "creating vbd structure");
554 err = xenvbd_sysfs_addif(dev);
556 xen_vbd_free(&be->blkif->vbd);
559 xenbus_dev_fatal(dev, err, "creating sysfs entries");
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);
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",
585 xenbus_switch_state(dev, XenbusStateInitWait);
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
596 if (dev->state == XenbusStateConnected)
600 * Enforce precondition before potential leak point.
601 * xen_blkif_disconnect() is idempotent.
603 xen_blkif_disconnect(be->blkif);
605 err = connect_ring(be);
608 xen_update_blkif_status(be->blkif);
611 case XenbusStateClosing:
612 xenbus_switch_state(dev, XenbusStateClosing);
615 case XenbusStateClosed:
616 xen_blkif_disconnect(be->blkif);
617 xenbus_switch_state(dev, XenbusStateClosed);
618 if (xenbus_dev_is_online(dev))
620 /* fall through if not online */
621 case XenbusStateUnknown:
622 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
623 device_unregister(&dev->dev);
627 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
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;
645 struct xenbus_device *dev = be->dev;
647 DPRINTK("%s", dev->otherend);
649 /* Supply the information about the device the frontend needs */
651 err = xenbus_transaction_start(&xbt);
653 xenbus_dev_fatal(dev, err, "starting transaction");
657 err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
661 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
662 (unsigned long long)vbd_sz(&be->blkif->vbd));
664 xenbus_dev_fatal(dev, err, "writing %s/sectors",
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));
674 xenbus_dev_fatal(dev, err, "writing %s/info",
678 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
680 bdev_logical_block_size(be->blkif->vbd.bdev));
682 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
687 err = xenbus_transaction_end(xbt, 0);
691 xenbus_dev_fatal(dev, err, "ending transaction");
693 err = xenbus_switch_state(dev, XenbusStateConnected);
695 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
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;
709 char protocol[64] = "";
712 DPRINTK("%s", dev->otherend);
714 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
715 &ring_ref, "event-channel", "%u", &evtchn, NULL);
717 xenbus_dev_fatal(dev, err,
718 "reading %s/ring-ref and event-channel",
723 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
724 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
725 "%63s", protocol, NULL);
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;
735 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
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);
744 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
753 /* ** Driver Registration ** */
756 static const struct xenbus_device_id xen_blkbk_ids[] = {
762 static struct xenbus_driver xen_blkbk = {
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);