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");
104 static struct xen_blkif
*xen_blkif_alloc(domid_t domid
)
106 struct xen_blkif
*blkif
;
108 blkif
= kmem_cache_zalloc(xen_blkif_cachep
, GFP_KERNEL
);
110 return ERR_PTR(-ENOMEM
);
112 blkif
->domid
= domid
;
113 spin_lock_init(&blkif
->blk_ring_lock
);
114 atomic_set(&blkif
->refcnt
, 1);
115 init_waitqueue_head(&blkif
->wq
);
116 init_completion(&blkif
->drain_complete
);
117 atomic_set(&blkif
->drain
, 0);
118 blkif
->st_print
= jiffies
;
119 init_waitqueue_head(&blkif
->waiting_to_free
);
124 static int xen_blkif_map(struct xen_blkif
*blkif
, unsigned long shared_page
,
129 /* Already connected through? */
133 err
= xenbus_map_ring_valloc(blkif
->be
->dev
, shared_page
, &blkif
->blk_ring
);
137 switch (blkif
->blk_protocol
) {
138 case BLKIF_PROTOCOL_NATIVE
:
140 struct blkif_sring
*sring
;
141 sring
= (struct blkif_sring
*)blkif
->blk_ring
;
142 BACK_RING_INIT(&blkif
->blk_rings
.native
, sring
, PAGE_SIZE
);
145 case BLKIF_PROTOCOL_X86_32
:
147 struct blkif_x86_32_sring
*sring_x86_32
;
148 sring_x86_32
= (struct blkif_x86_32_sring
*)blkif
->blk_ring
;
149 BACK_RING_INIT(&blkif
->blk_rings
.x86_32
, sring_x86_32
, PAGE_SIZE
);
152 case BLKIF_PROTOCOL_X86_64
:
154 struct blkif_x86_64_sring
*sring_x86_64
;
155 sring_x86_64
= (struct blkif_x86_64_sring
*)blkif
->blk_ring
;
156 BACK_RING_INIT(&blkif
->blk_rings
.x86_64
, sring_x86_64
, PAGE_SIZE
);
163 err
= bind_interdomain_evtchn_to_irqhandler(blkif
->domid
, evtchn
,
165 "blkif-backend", blkif
);
167 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
168 blkif
->blk_rings
.common
.sring
= NULL
;
176 static void xen_blkif_disconnect(struct xen_blkif
*blkif
)
178 if (blkif
->xenblkd
) {
179 kthread_stop(blkif
->xenblkd
);
180 blkif
->xenblkd
= NULL
;
183 atomic_dec(&blkif
->refcnt
);
184 wait_event(blkif
->waiting_to_free
, atomic_read(&blkif
->refcnt
) == 0);
185 atomic_inc(&blkif
->refcnt
);
188 unbind_from_irqhandler(blkif
->irq
, blkif
);
192 if (blkif
->blk_rings
.common
.sring
) {
193 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
194 blkif
->blk_rings
.common
.sring
= NULL
;
198 static void xen_blkif_free(struct xen_blkif
*blkif
)
200 if (!atomic_dec_and_test(&blkif
->refcnt
))
202 kmem_cache_free(xen_blkif_cachep
, blkif
);
205 int __init
xen_blkif_interface_init(void)
207 xen_blkif_cachep
= kmem_cache_create("blkif_cache",
208 sizeof(struct xen_blkif
),
210 if (!xen_blkif_cachep
)
217 * sysfs interface for VBD I/O requests
220 #define VBD_SHOW(name, format, args...) \
221 static ssize_t show_##name(struct device *_dev, \
222 struct device_attribute *attr, \
225 struct xenbus_device *dev = to_xenbus_device(_dev); \
226 struct backend_info *be = dev_get_drvdata(&dev->dev); \
228 return sprintf(buf, format, ##args); \
230 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
232 VBD_SHOW(oo_req
, "%d\n", be
->blkif
->st_oo_req
);
233 VBD_SHOW(rd_req
, "%d\n", be
->blkif
->st_rd_req
);
234 VBD_SHOW(wr_req
, "%d\n", be
->blkif
->st_wr_req
);
235 VBD_SHOW(f_req
, "%d\n", be
->blkif
->st_f_req
);
236 VBD_SHOW(ds_req
, "%d\n", be
->blkif
->st_ds_req
);
237 VBD_SHOW(rd_sect
, "%d\n", be
->blkif
->st_rd_sect
);
238 VBD_SHOW(wr_sect
, "%d\n", be
->blkif
->st_wr_sect
);
240 static struct attribute
*xen_vbdstat_attrs
[] = {
241 &dev_attr_oo_req
.attr
,
242 &dev_attr_rd_req
.attr
,
243 &dev_attr_wr_req
.attr
,
244 &dev_attr_f_req
.attr
,
245 &dev_attr_ds_req
.attr
,
246 &dev_attr_rd_sect
.attr
,
247 &dev_attr_wr_sect
.attr
,
251 static struct attribute_group xen_vbdstat_group
= {
252 .name
= "statistics",
253 .attrs
= xen_vbdstat_attrs
,
256 VBD_SHOW(physical_device
, "%x:%x\n", be
->major
, be
->minor
);
257 VBD_SHOW(mode
, "%s\n", be
->mode
);
259 static int xenvbd_sysfs_addif(struct xenbus_device
*dev
)
263 error
= device_create_file(&dev
->dev
, &dev_attr_physical_device
);
267 error
= device_create_file(&dev
->dev
, &dev_attr_mode
);
271 error
= sysfs_create_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
277 fail3
: sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
278 fail2
: device_remove_file(&dev
->dev
, &dev_attr_mode
);
279 fail1
: device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
283 static void xenvbd_sysfs_delif(struct xenbus_device
*dev
)
285 sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
286 device_remove_file(&dev
->dev
, &dev_attr_mode
);
287 device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
291 static void xen_vbd_free(struct xen_vbd
*vbd
)
294 blkdev_put(vbd
->bdev
, vbd
->readonly
? FMODE_READ
: FMODE_WRITE
);
298 static int xen_vbd_create(struct xen_blkif
*blkif
, blkif_vdev_t handle
,
299 unsigned major
, unsigned minor
, int readonly
,
303 struct block_device
*bdev
;
304 struct request_queue
*q
;
307 vbd
->handle
= handle
;
308 vbd
->readonly
= readonly
;
311 vbd
->pdevice
= MKDEV(major
, minor
);
313 bdev
= blkdev_get_by_dev(vbd
->pdevice
, vbd
->readonly
?
314 FMODE_READ
: FMODE_WRITE
, NULL
);
317 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
323 if (vbd
->bdev
->bd_disk
== NULL
) {
324 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
329 vbd
->size
= vbd_sz(vbd
);
331 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_CD
|| cdrom
)
332 vbd
->type
|= VDISK_CDROM
;
333 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_REMOVABLE
)
334 vbd
->type
|= VDISK_REMOVABLE
;
336 q
= bdev_get_queue(bdev
);
337 if (q
&& q
->flush_flags
)
338 vbd
->flush_support
= true;
340 if (q
&& blk_queue_secdiscard(q
))
341 vbd
->discard_secure
= true;
343 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
344 handle
, blkif
->domid
);
347 static int xen_blkbk_remove(struct xenbus_device
*dev
)
349 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
353 if (be
->major
|| be
->minor
)
354 xenvbd_sysfs_delif(dev
);
356 if (be
->backend_watch
.node
) {
357 unregister_xenbus_watch(&be
->backend_watch
);
358 kfree(be
->backend_watch
.node
);
359 be
->backend_watch
.node
= NULL
;
363 xen_blkif_disconnect(be
->blkif
);
364 xen_vbd_free(&be
->blkif
->vbd
);
365 xen_blkif_free(be
->blkif
);
370 dev_set_drvdata(&dev
->dev
, NULL
);
374 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
375 struct backend_info
*be
, int state
)
377 struct xenbus_device
*dev
= be
->dev
;
380 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-flush-cache",
383 dev_warn(&dev
->dev
, "writing feature-flush-cache (%d)", err
);
388 static void xen_blkbk_discard(struct xenbus_transaction xbt
, struct backend_info
*be
)
390 struct xenbus_device
*dev
= be
->dev
;
391 struct xen_blkif
*blkif
= be
->blkif
;
394 struct block_device
*bdev
= be
->blkif
->vbd
.bdev
;
395 struct request_queue
*q
= bdev_get_queue(bdev
);
397 if (blk_queue_discard(q
)) {
398 err
= xenbus_printf(xbt
, dev
->nodename
,
399 "discard-granularity", "%u",
400 q
->limits
.discard_granularity
);
402 dev_warn(&dev
->dev
, "writing discard-granularity (%d)", err
);
405 err
= xenbus_printf(xbt
, dev
->nodename
,
406 "discard-alignment", "%u",
407 q
->limits
.discard_alignment
);
409 dev_warn(&dev
->dev
, "writing discard-alignment (%d)", err
);
414 err
= xenbus_printf(xbt
, dev
->nodename
,
415 "discard-secure", "%d",
416 blkif
->vbd
.discard_secure
);
418 dev_warn(&dev
->dev
, "writing discard-secure (%d)", err
);
422 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-discard",
425 dev_warn(&dev
->dev
, "writing feature-discard (%d)", err
);
427 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
428 struct backend_info
*be
, int state
)
430 struct xenbus_device
*dev
= be
->dev
;
433 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-barrier",
436 dev_warn(&dev
->dev
, "writing feature-barrier (%d)", err
);
442 * Entry point to this code when a new device is created. Allocate the basic
443 * structures, and watch the store waiting for the hotplug scripts to tell us
444 * the device's physical major and minor numbers. Switch to InitWait.
446 static int xen_blkbk_probe(struct xenbus_device
*dev
,
447 const struct xenbus_device_id
*id
)
450 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
453 xenbus_dev_fatal(dev
, -ENOMEM
,
454 "allocating backend structure");
458 dev_set_drvdata(&dev
->dev
, be
);
460 be
->blkif
= xen_blkif_alloc(dev
->otherend_id
);
461 if (IS_ERR(be
->blkif
)) {
462 err
= PTR_ERR(be
->blkif
);
464 xenbus_dev_fatal(dev
, err
, "creating block interface");
468 /* setup back pointer */
471 err
= xenbus_watch_pathfmt(dev
, &be
->backend_watch
, backend_changed
,
472 "%s/%s", dev
->nodename
, "physical-device");
476 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
484 xen_blkbk_remove(dev
);
490 * Callback received when the hotplug scripts have placed the physical-device
491 * node. Read it and the mode node, and create a vbd. If the frontend is
494 static void backend_changed(struct xenbus_watch
*watch
,
495 const char **vec
, unsigned int len
)
500 struct backend_info
*be
501 = container_of(watch
, struct backend_info
, backend_watch
);
502 struct xenbus_device
*dev
= be
->dev
;
508 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "physical-device", "%x:%x",
510 if (XENBUS_EXIST_ERR(err
)) {
512 * Since this watch will fire once immediately after it is
513 * registered, we expect this. Ignore it, and wait for the
519 xenbus_dev_fatal(dev
, err
, "reading physical-device");
523 if ((be
->major
|| be
->minor
) &&
524 ((be
->major
!= major
) || (be
->minor
!= minor
))) {
525 pr_warn(DRV_PFX
"changing physical device (from %x:%x to %x:%x) not supported.\n",
526 be
->major
, be
->minor
, major
, minor
);
530 be
->mode
= xenbus_read(XBT_NIL
, dev
->nodename
, "mode", NULL
);
531 if (IS_ERR(be
->mode
)) {
532 err
= PTR_ERR(be
->mode
);
534 xenbus_dev_fatal(dev
, err
, "reading mode");
538 device_type
= xenbus_read(XBT_NIL
, dev
->otherend
, "device-type", NULL
);
539 if (!IS_ERR(device_type
)) {
540 cdrom
= strcmp(device_type
, "cdrom") == 0;
544 if (be
->major
== 0 && be
->minor
== 0) {
545 /* Front end dir is a number, which is used as the handle. */
547 char *p
= strrchr(dev
->otherend
, '/') + 1;
549 err
= strict_strtoul(p
, 0, &handle
);
556 err
= xen_vbd_create(be
->blkif
, handle
, major
, minor
,
557 (NULL
== strchr(be
->mode
, 'w')), cdrom
);
561 xenbus_dev_fatal(dev
, err
, "creating vbd structure");
565 err
= xenvbd_sysfs_addif(dev
);
567 xen_vbd_free(&be
->blkif
->vbd
);
570 xenbus_dev_fatal(dev
, err
, "creating sysfs entries");
574 /* We're potentially connected now */
575 xen_update_blkif_status(be
->blkif
);
581 * Callback received when the frontend's state changes.
583 static void frontend_changed(struct xenbus_device
*dev
,
584 enum xenbus_state frontend_state
)
586 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
589 DPRINTK("%s", xenbus_strstate(frontend_state
));
591 switch (frontend_state
) {
592 case XenbusStateInitialising
:
593 if (dev
->state
== XenbusStateClosed
) {
594 pr_info(DRV_PFX
"%s: prepare for reconnect\n",
596 xenbus_switch_state(dev
, XenbusStateInitWait
);
600 case XenbusStateInitialised
:
601 case XenbusStateConnected
:
603 * Ensure we connect even when two watches fire in
604 * close succession and we miss the intermediate value
607 if (dev
->state
== XenbusStateConnected
)
611 * Enforce precondition before potential leak point.
612 * xen_blkif_disconnect() is idempotent.
614 xen_blkif_disconnect(be
->blkif
);
616 err
= connect_ring(be
);
619 xen_update_blkif_status(be
->blkif
);
622 case XenbusStateClosing
:
623 xenbus_switch_state(dev
, XenbusStateClosing
);
626 case XenbusStateClosed
:
627 xen_blkif_disconnect(be
->blkif
);
628 xenbus_switch_state(dev
, XenbusStateClosed
);
629 if (xenbus_dev_is_online(dev
))
631 /* fall through if not online */
632 case XenbusStateUnknown
:
633 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
634 device_unregister(&dev
->dev
);
638 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
645 /* ** Connection ** */
649 * Write the physical details regarding the block device to the store, and
650 * switch to Connected state.
652 static void connect(struct backend_info
*be
)
654 struct xenbus_transaction xbt
;
656 struct xenbus_device
*dev
= be
->dev
;
658 DPRINTK("%s", dev
->otherend
);
660 /* Supply the information about the device the frontend needs */
662 err
= xenbus_transaction_start(&xbt
);
664 xenbus_dev_fatal(dev
, err
, "starting transaction");
668 /* If we can't advertise it is OK. */
669 xen_blkbk_flush_diskcache(xbt
, be
, be
->blkif
->vbd
.flush_support
);
671 xen_blkbk_discard(xbt
, be
);
673 xen_blkbk_barrier(xbt
, be
, be
->blkif
->vbd
.flush_support
);
675 err
= xenbus_printf(xbt
, dev
->nodename
, "sectors", "%llu",
676 (unsigned long long)vbd_sz(&be
->blkif
->vbd
));
678 xenbus_dev_fatal(dev
, err
, "writing %s/sectors",
683 /* FIXME: use a typename instead */
684 err
= xenbus_printf(xbt
, dev
->nodename
, "info", "%u",
685 be
->blkif
->vbd
.type
|
686 (be
->blkif
->vbd
.readonly
? VDISK_READONLY
: 0));
688 xenbus_dev_fatal(dev
, err
, "writing %s/info",
692 err
= xenbus_printf(xbt
, dev
->nodename
, "sector-size", "%lu",
694 bdev_logical_block_size(be
->blkif
->vbd
.bdev
));
696 xenbus_dev_fatal(dev
, err
, "writing %s/sector-size",
701 err
= xenbus_transaction_end(xbt
, 0);
705 xenbus_dev_fatal(dev
, err
, "ending transaction");
707 err
= xenbus_switch_state(dev
, XenbusStateConnected
);
709 xenbus_dev_fatal(dev
, err
, "%s: switching to Connected state",
714 xenbus_transaction_end(xbt
, 1);
718 static int connect_ring(struct backend_info
*be
)
720 struct xenbus_device
*dev
= be
->dev
;
721 unsigned long ring_ref
;
723 char protocol
[64] = "";
726 DPRINTK("%s", dev
->otherend
);
728 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "ring-ref", "%lu",
729 &ring_ref
, "event-channel", "%u", &evtchn
, NULL
);
731 xenbus_dev_fatal(dev
, err
,
732 "reading %s/ring-ref and event-channel",
737 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
738 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "protocol",
739 "%63s", protocol
, NULL
);
741 strcpy(protocol
, "unspecified, assuming native");
742 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_NATIVE
))
743 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
744 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_32
))
745 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_32
;
746 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_64
))
747 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_64
;
749 xenbus_dev_fatal(dev
, err
, "unknown fe protocol %s", protocol
);
752 pr_info(DRV_PFX
"ring-ref %ld, event-channel %d, protocol %d (%s)\n",
753 ring_ref
, evtchn
, be
->blkif
->blk_protocol
, protocol
);
755 /* Map the shared frame, irq etc. */
756 err
= xen_blkif_map(be
->blkif
, ring_ref
, evtchn
);
758 xenbus_dev_fatal(dev
, err
, "mapping ring-ref %lu port %u",
767 /* ** Driver Registration ** */
770 static const struct xenbus_device_id xen_blkbk_ids
[] = {
776 static DEFINE_XENBUS_DRIVER(xen_blkbk
, ,
777 .probe
= xen_blkbk_probe
,
778 .remove
= xen_blkbk_remove
,
779 .otherend_changed
= frontend_changed
783 int xen_blkif_xenbus_init(void)
785 return xenbus_register_backend(&xen_blkbk_driver
);