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 **,
39 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
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);
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 xen_blkif *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");
105 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
107 struct xen_blkif *blkif;
108 struct pending_req *req, *n;
111 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
113 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
115 return ERR_PTR(-ENOMEM);
117 blkif->domid = domid;
118 spin_lock_init(&blkif->blk_ring_lock);
119 atomic_set(&blkif->refcnt, 1);
120 init_waitqueue_head(&blkif->wq);
121 init_completion(&blkif->drain_complete);
122 atomic_set(&blkif->drain, 0);
123 blkif->st_print = jiffies;
124 init_waitqueue_head(&blkif->waiting_to_free);
125 blkif->persistent_gnts.rb_node = NULL;
126 spin_lock_init(&blkif->free_pages_lock);
127 INIT_LIST_HEAD(&blkif->free_pages);
128 blkif->free_pages_num = 0;
129 atomic_set(&blkif->persistent_gnt_in_use, 0);
131 INIT_LIST_HEAD(&blkif->pending_free);
133 for (i = 0; i < XEN_BLKIF_REQS; i++) {
134 req = kzalloc(sizeof(*req), GFP_KERNEL);
137 list_add_tail(&req->free_list,
138 &blkif->pending_free);
139 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
140 req->segments[j] = kzalloc(sizeof(*req->segments[0]),
142 if (!req->segments[j])
145 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
146 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
148 if (!req->indirect_pages[j])
152 spin_lock_init(&blkif->pending_free_lock);
153 init_waitqueue_head(&blkif->pending_free_wq);
158 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
159 list_del(&req->free_list);
160 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
161 if (!req->segments[j])
163 kfree(req->segments[j]);
165 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
166 if (!req->indirect_pages[j])
168 kfree(req->indirect_pages[j]);
173 kmem_cache_free(xen_blkif_cachep, blkif);
175 return ERR_PTR(-ENOMEM);
178 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
183 /* Already connected through? */
187 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
191 switch (blkif->blk_protocol) {
192 case BLKIF_PROTOCOL_NATIVE:
194 struct blkif_sring *sring;
195 sring = (struct blkif_sring *)blkif->blk_ring;
196 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
199 case BLKIF_PROTOCOL_X86_32:
201 struct blkif_x86_32_sring *sring_x86_32;
202 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
203 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
206 case BLKIF_PROTOCOL_X86_64:
208 struct blkif_x86_64_sring *sring_x86_64;
209 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
210 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
217 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
219 "blkif-backend", blkif);
221 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
222 blkif->blk_rings.common.sring = NULL;
230 static void xen_blkif_disconnect(struct xen_blkif *blkif)
232 if (blkif->xenblkd) {
233 kthread_stop(blkif->xenblkd);
234 blkif->xenblkd = NULL;
237 atomic_dec(&blkif->refcnt);
238 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
239 atomic_inc(&blkif->refcnt);
242 unbind_from_irqhandler(blkif->irq, blkif);
246 if (blkif->blk_rings.common.sring) {
247 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
248 blkif->blk_rings.common.sring = NULL;
252 static void xen_blkif_free(struct xen_blkif *blkif)
254 struct pending_req *req, *n;
257 if (!atomic_dec_and_test(&blkif->refcnt))
260 /* Check that there is no request in use */
261 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
262 list_del(&req->free_list);
264 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
265 kfree(req->segments[j]);
267 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
268 kfree(req->indirect_pages[j]);
274 WARN_ON(i != XEN_BLKIF_REQS);
276 kmem_cache_free(xen_blkif_cachep, blkif);
279 int __init xen_blkif_interface_init(void)
281 xen_blkif_cachep = kmem_cache_create("blkif_cache",
282 sizeof(struct xen_blkif),
284 if (!xen_blkif_cachep)
291 * sysfs interface for VBD I/O requests
294 #define VBD_SHOW(name, format, args...) \
295 static ssize_t show_##name(struct device *_dev, \
296 struct device_attribute *attr, \
299 struct xenbus_device *dev = to_xenbus_device(_dev); \
300 struct backend_info *be = dev_get_drvdata(&dev->dev); \
302 return sprintf(buf, format, ##args); \
304 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
306 VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
307 VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
308 VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
309 VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
310 VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
311 VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
312 VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
314 static struct attribute *xen_vbdstat_attrs[] = {
315 &dev_attr_oo_req.attr,
316 &dev_attr_rd_req.attr,
317 &dev_attr_wr_req.attr,
318 &dev_attr_f_req.attr,
319 &dev_attr_ds_req.attr,
320 &dev_attr_rd_sect.attr,
321 &dev_attr_wr_sect.attr,
325 static struct attribute_group xen_vbdstat_group = {
326 .name = "statistics",
327 .attrs = xen_vbdstat_attrs,
330 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
331 VBD_SHOW(mode, "%s\n", be->mode);
333 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
337 error = device_create_file(&dev->dev, &dev_attr_physical_device);
341 error = device_create_file(&dev->dev, &dev_attr_mode);
345 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
351 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
352 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
353 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
357 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
359 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
360 device_remove_file(&dev->dev, &dev_attr_mode);
361 device_remove_file(&dev->dev, &dev_attr_physical_device);
365 static void xen_vbd_free(struct xen_vbd *vbd)
368 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
372 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
373 unsigned major, unsigned minor, int readonly,
377 struct block_device *bdev;
378 struct request_queue *q;
381 vbd->handle = handle;
382 vbd->readonly = readonly;
385 vbd->pdevice = MKDEV(major, minor);
387 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
388 FMODE_READ : FMODE_WRITE, NULL);
391 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
397 if (vbd->bdev->bd_disk == NULL) {
398 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
403 vbd->size = vbd_sz(vbd);
405 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
406 vbd->type |= VDISK_CDROM;
407 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
408 vbd->type |= VDISK_REMOVABLE;
410 q = bdev_get_queue(bdev);
411 if (q && q->flush_flags)
412 vbd->flush_support = true;
414 if (q && blk_queue_secdiscard(q))
415 vbd->discard_secure = true;
417 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
418 handle, blkif->domid);
421 static int xen_blkbk_remove(struct xenbus_device *dev)
423 struct backend_info *be = dev_get_drvdata(&dev->dev);
427 if (be->major || be->minor)
428 xenvbd_sysfs_delif(dev);
430 if (be->backend_watch.node) {
431 unregister_xenbus_watch(&be->backend_watch);
432 kfree(be->backend_watch.node);
433 be->backend_watch.node = NULL;
437 xen_blkif_disconnect(be->blkif);
438 xen_vbd_free(&be->blkif->vbd);
439 xen_blkif_free(be->blkif);
445 dev_set_drvdata(&dev->dev, NULL);
449 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
450 struct backend_info *be, int state)
452 struct xenbus_device *dev = be->dev;
455 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
458 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
463 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
465 struct xenbus_device *dev = be->dev;
466 struct xen_blkif *blkif = be->blkif;
469 struct block_device *bdev = be->blkif->vbd.bdev;
470 struct request_queue *q = bdev_get_queue(bdev);
472 if (blk_queue_discard(q)) {
473 err = xenbus_printf(xbt, dev->nodename,
474 "discard-granularity", "%u",
475 q->limits.discard_granularity);
477 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
480 err = xenbus_printf(xbt, dev->nodename,
481 "discard-alignment", "%u",
482 q->limits.discard_alignment);
484 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
489 err = xenbus_printf(xbt, dev->nodename,
490 "discard-secure", "%d",
491 blkif->vbd.discard_secure);
493 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
497 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
500 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
502 int xen_blkbk_barrier(struct xenbus_transaction xbt,
503 struct backend_info *be, int state)
505 struct xenbus_device *dev = be->dev;
508 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
511 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
517 * Entry point to this code when a new device is created. Allocate the basic
518 * structures, and watch the store waiting for the hotplug scripts to tell us
519 * the device's physical major and minor numbers. Switch to InitWait.
521 static int xen_blkbk_probe(struct xenbus_device *dev,
522 const struct xenbus_device_id *id)
525 struct backend_info *be = kzalloc(sizeof(struct backend_info),
528 xenbus_dev_fatal(dev, -ENOMEM,
529 "allocating backend structure");
533 dev_set_drvdata(&dev->dev, be);
535 be->blkif = xen_blkif_alloc(dev->otherend_id);
536 if (IS_ERR(be->blkif)) {
537 err = PTR_ERR(be->blkif);
539 xenbus_dev_fatal(dev, err, "creating block interface");
543 /* setup back pointer */
546 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
547 "%s/%s", dev->nodename, "physical-device");
551 err = xenbus_switch_state(dev, XenbusStateInitWait);
559 xen_blkbk_remove(dev);
565 * Callback received when the hotplug scripts have placed the physical-device
566 * node. Read it and the mode node, and create a vbd. If the frontend is
569 static void backend_changed(struct xenbus_watch *watch,
570 const char **vec, unsigned int len)
575 struct backend_info *be
576 = container_of(watch, struct backend_info, backend_watch);
577 struct xenbus_device *dev = be->dev;
579 unsigned long handle;
584 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
586 if (XENBUS_EXIST_ERR(err)) {
588 * Since this watch will fire once immediately after it is
589 * registered, we expect this. Ignore it, and wait for the
595 xenbus_dev_fatal(dev, err, "reading physical-device");
599 if (be->major | be->minor) {
600 if (be->major != major || be->minor != minor)
601 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
602 be->major, be->minor, major, minor);
606 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
607 if (IS_ERR(be->mode)) {
608 err = PTR_ERR(be->mode);
610 xenbus_dev_fatal(dev, err, "reading mode");
614 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
615 if (!IS_ERR(device_type)) {
616 cdrom = strcmp(device_type, "cdrom") == 0;
620 /* Front end dir is a number, which is used as the handle. */
621 err = strict_strtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
628 err = xen_vbd_create(be->blkif, handle, major, minor,
629 !strchr(be->mode, 'w'), cdrom);
632 xenbus_dev_fatal(dev, err, "creating vbd structure");
634 err = xenvbd_sysfs_addif(dev);
636 xen_vbd_free(&be->blkif->vbd);
637 xenbus_dev_fatal(dev, err, "creating sysfs entries");
647 /* We're potentially connected now */
648 xen_update_blkif_status(be->blkif);
654 * Callback received when the frontend's state changes.
656 static void frontend_changed(struct xenbus_device *dev,
657 enum xenbus_state frontend_state)
659 struct backend_info *be = dev_get_drvdata(&dev->dev);
662 DPRINTK("%s", xenbus_strstate(frontend_state));
664 switch (frontend_state) {
665 case XenbusStateInitialising:
666 if (dev->state == XenbusStateClosed) {
667 pr_info(DRV_PFX "%s: prepare for reconnect\n",
669 xenbus_switch_state(dev, XenbusStateInitWait);
673 case XenbusStateInitialised:
674 case XenbusStateConnected:
676 * Ensure we connect even when two watches fire in
677 * close succession and we miss the intermediate value
680 if (dev->state == XenbusStateConnected)
684 * Enforce precondition before potential leak point.
685 * xen_blkif_disconnect() is idempotent.
687 xen_blkif_disconnect(be->blkif);
689 err = connect_ring(be);
692 xen_update_blkif_status(be->blkif);
695 case XenbusStateClosing:
696 xenbus_switch_state(dev, XenbusStateClosing);
699 case XenbusStateClosed:
700 xen_blkif_disconnect(be->blkif);
701 xenbus_switch_state(dev, XenbusStateClosed);
702 if (xenbus_dev_is_online(dev))
704 /* fall through if not online */
705 case XenbusStateUnknown:
706 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
707 device_unregister(&dev->dev);
711 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
718 /* ** Connection ** */
722 * Write the physical details regarding the block device to the store, and
723 * switch to Connected state.
725 static void connect(struct backend_info *be)
727 struct xenbus_transaction xbt;
729 struct xenbus_device *dev = be->dev;
731 DPRINTK("%s", dev->otherend);
733 /* Supply the information about the device the frontend needs */
735 err = xenbus_transaction_start(&xbt);
737 xenbus_dev_fatal(dev, err, "starting transaction");
741 /* If we can't advertise it is OK. */
742 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
744 xen_blkbk_discard(xbt, be);
746 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
748 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
750 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
754 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
755 MAX_INDIRECT_SEGMENTS);
757 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
760 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
761 (unsigned long long)vbd_sz(&be->blkif->vbd));
763 xenbus_dev_fatal(dev, err, "writing %s/sectors",
768 /* FIXME: use a typename instead */
769 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
770 be->blkif->vbd.type |
771 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
773 xenbus_dev_fatal(dev, err, "writing %s/info",
777 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
779 bdev_logical_block_size(be->blkif->vbd.bdev));
781 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
785 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
786 bdev_physical_block_size(be->blkif->vbd.bdev));
788 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
791 err = xenbus_transaction_end(xbt, 0);
795 xenbus_dev_fatal(dev, err, "ending transaction");
797 err = xenbus_switch_state(dev, XenbusStateConnected);
799 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
804 xenbus_transaction_end(xbt, 1);
808 static int connect_ring(struct backend_info *be)
810 struct xenbus_device *dev = be->dev;
811 unsigned long ring_ref;
813 unsigned int pers_grants;
814 char protocol[64] = "";
817 DPRINTK("%s", dev->otherend);
819 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
820 &ring_ref, "event-channel", "%u", &evtchn, NULL);
822 xenbus_dev_fatal(dev, err,
823 "reading %s/ring-ref and event-channel",
828 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
829 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
830 "%63s", protocol, NULL);
832 strcpy(protocol, "unspecified, assuming native");
833 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
834 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
835 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
836 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
837 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
838 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
840 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
843 err = xenbus_gather(XBT_NIL, dev->otherend,
844 "feature-persistent", "%u",
849 be->blkif->vbd.feature_gnt_persistent = pers_grants;
850 be->blkif->vbd.overflow_max_grants = 0;
852 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
853 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
854 pers_grants ? "persistent grants" : "");
856 /* Map the shared frame, irq etc. */
857 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
859 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
868 /* ** Driver Registration ** */
871 static const struct xenbus_device_id xen_blkbk_ids[] = {
877 static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
878 .probe = xen_blkbk_probe,
879 .remove = xen_blkbk_remove,
880 .otherend_changed = frontend_changed
884 int xen_blkif_xenbus_init(void)
886 return xenbus_register_backend(&xen_blkbk_driver);