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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kthread.h>
23 #include "../core/domctl.h"
26 #define DPRINTK(fmt, args...) \
27 pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
28 __FUNCTION__, __LINE__, ##args)
30 static void connect(struct backend_info *);
31 static int connect_ring(struct backend_info *);
32 static void backend_changed(struct xenbus_watch *, const char **,
35 static int blkback_name(blkif_t *blkif, char *buf)
37 char *devpath, *devname;
38 struct xenbus_device *dev = blkif->be->dev;
40 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
42 return PTR_ERR(devpath);
44 if ((devname = strstr(devpath, "/dev/")) != NULL)
45 devname += strlen("/dev/");
49 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
55 static void update_blkif_status(blkif_t *blkif)
58 char name[TASK_COMM_LEN];
60 /* Not ready to connect? */
64 /* Already connected? */
65 if (blkif->be->dev->state == XenbusStateConnected)
68 /* Attempt to connect: exit if we fail to. */
70 if (blkif->be->dev->state != XenbusStateConnected)
73 err = blkback_name(blkif, name);
75 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
79 if (blkif->vbd.bdev) {
80 struct address_space *mapping
81 = blkif->vbd.bdev->bd_inode->i_mapping;
83 err = filemap_write_and_wait(mapping);
85 xenbus_dev_error(blkif->be->dev, err, "block flush");
88 invalidate_inode_pages2(mapping);
91 blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
92 if (IS_ERR(blkif->xenblkd)) {
93 err = PTR_ERR(blkif->xenblkd);
94 blkif->xenblkd = NULL;
95 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
100 /****************************************************************
101 * sysfs interface for VBD I/O requests
104 #define VBD_SHOW(name, format, args...) \
105 static ssize_t show_##name(struct device *_dev, \
106 struct device_attribute *attr, \
109 ssize_t ret = -ENODEV; \
110 struct xenbus_device *dev; \
111 struct backend_info *be; \
113 if (!get_device(_dev)) \
115 dev = to_xenbus_device(_dev); \
116 if ((be = dev_get_drvdata(&dev->dev)) != NULL) \
117 ret = sprintf(buf, format, ##args); \
121 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
123 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
124 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
125 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
126 VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
127 VBD_SHOW(fl_req, "%d\n", be->blkif->st_fl_req);
128 VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req);
129 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
130 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
132 static struct attribute *vbdstat_attrs[] = {
133 &dev_attr_oo_req.attr,
134 &dev_attr_rd_req.attr,
135 &dev_attr_wr_req.attr,
136 &dev_attr_br_req.attr,
137 &dev_attr_fl_req.attr,
138 &dev_attr_ds_req.attr,
139 &dev_attr_rd_sect.attr,
140 &dev_attr_wr_sect.attr,
144 static const struct attribute_group vbdstat_group = {
145 .name = "statistics",
146 .attrs = vbdstat_attrs,
149 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
150 VBD_SHOW(mode, "%s\n", be->mode);
152 int xenvbd_sysfs_addif(struct xenbus_device *dev)
156 error = device_create_file(&dev->dev, &dev_attr_physical_device);
160 error = device_create_file(&dev->dev, &dev_attr_mode);
164 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
170 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
171 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
172 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
176 void xenvbd_sysfs_delif(struct xenbus_device *dev)
178 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
179 device_remove_file(&dev->dev, &dev_attr_mode);
180 device_remove_file(&dev->dev, &dev_attr_physical_device);
183 static int blkback_remove(struct xenbus_device *dev)
185 struct backend_info *be = dev_get_drvdata(&dev->dev);
189 if (be->major || be->minor)
190 xenvbd_sysfs_delif(dev);
192 if (be->backend_watch.node) {
193 unregister_xenbus_watch(&be->backend_watch);
194 kfree(be->backend_watch.node);
195 be->backend_watch.node = NULL;
198 if (be->cdrom_watch.node) {
199 unregister_xenbus_watch(&be->cdrom_watch);
200 kfree(be->cdrom_watch.node);
201 be->cdrom_watch.node = NULL;
205 blkif_disconnect(be->blkif);
206 vbd_free(&be->blkif->vbd);
207 blkif_free(be->blkif);
212 dev_set_drvdata(&dev->dev, NULL);
216 int blkback_barrier(struct xenbus_transaction xbt,
217 struct backend_info *be, int state)
219 struct xenbus_device *dev = be->dev;
222 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
225 xenbus_dev_fatal(dev, err, "writing feature-barrier");
230 int blkback_flush_diskcache(struct xenbus_transaction xbt,
231 struct backend_info *be, int state)
233 struct xenbus_device *dev = be->dev;
236 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
239 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
244 static int xen_blkbk_discard(struct xenbus_transaction xbt,
245 struct backend_info *be)
247 struct xenbus_device *dev = be->dev;
248 blkif_t *blkif = be->blkif;
253 type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL);
255 if (strncmp(type, "file", 4) == 0) {
257 blkif->blk_backend_type = BLKIF_BACKEND_FILE;
259 if (strncmp(type, "phy", 3) == 0) {
260 struct request_queue *q;
262 q = bdev_get_queue(blkif->vbd.bdev);
263 if (blk_queue_discard(q)) {
264 err = xenbus_printf(xbt, dev->nodename,
265 "discard-granularity", "%u",
266 q->limits.discard_granularity);
268 xenbus_dev_fatal(dev, err,
269 "writing discard-granularity");
272 err = xenbus_printf(xbt, dev->nodename,
273 "discard-alignment", "%u",
274 q->limits.discard_alignment);
276 xenbus_dev_fatal(dev, err,
277 "writing discard-alignment");
281 blkif->blk_backend_type = BLKIF_BACKEND_PHY;
284 err = xenbus_printf(xbt, dev->nodename,
285 "discard-secure", "%d",
286 blkif->vbd.discard_secure);
288 xenbus_dev_fatal(dev, err,
289 "writing discard-secure");
295 xenbus_dev_fatal(dev, err, "reading type");
299 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
302 xenbus_dev_fatal(dev, err, "writing feature-discard");
310 * Entry point to this code when a new device is created. Allocate the basic
311 * structures, and watch the store waiting for the hotplug scripts to tell us
312 * the device's physical major and minor numbers. Switch to InitWait.
314 static int blkback_probe(struct xenbus_device *dev,
315 const struct xenbus_device_id *id)
318 struct backend_info *be = kzalloc(sizeof(struct backend_info),
321 xenbus_dev_fatal(dev, -ENOMEM,
322 "allocating backend structure");
326 dev_set_drvdata(&dev->dev, be);
328 be->blkif = blkif_alloc(dev->otherend_id);
329 if (IS_ERR(be->blkif)) {
330 err = PTR_ERR(be->blkif);
332 xenbus_dev_fatal(dev, err, "creating block interface");
336 /* setup back pointer */
339 err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
340 &be->backend_watch, backend_changed);
344 err = xenbus_switch_state(dev, XenbusStateInitWait);
358 * Callback received when the hotplug scripts have placed the physical-device
359 * node. Read it and the mode node, and create a vbd. If the frontend is
362 static void backend_changed(struct xenbus_watch *watch,
363 const char **vec, unsigned int len)
368 struct backend_info *be
369 = container_of(watch, struct backend_info, backend_watch);
370 struct xenbus_device *dev = be->dev;
376 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
378 if (XENBUS_EXIST_ERR(err)) {
379 /* Since this watch will fire once immediately after it is
380 registered, we expect this. Ignore it, and wait for the
385 xenbus_dev_fatal(dev, err, "reading physical-device");
389 if ((be->major || be->minor) &&
390 ((be->major != major) || (be->minor != minor))) {
391 pr_warning("blkback: changing physical device (from %x:%x to"
392 " %x:%x) not supported\n", be->major, be->minor,
397 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
398 if (IS_ERR(be->mode)) {
399 err = PTR_ERR(be->mode);
401 xenbus_dev_fatal(dev, err, "reading mode");
405 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
406 if (!IS_ERR(device_type)) {
407 cdrom = strcmp(device_type, "cdrom") == 0;
411 if (be->major == 0 && be->minor == 0) {
412 /* Front end dir is a number, which is used as the handle. */
414 char *p = strrchr(dev->otherend, '/') + 1;
415 long handle = simple_strtoul(p, NULL, 0);
420 err = vbd_create(be->blkif, handle, major, minor,
422 | (strchr(be->mode, 'w') ? FMODE_WRITE : 0)
423 | (strchr(be->mode, '!') ? 0 : FMODE_EXCL),
427 if (be->blkif->vbd.type
428 & (VDISK_CDROM | VDISK_REMOVABLE))
432 be->major = be->minor = 0;
433 xenbus_dev_fatal(dev, err, "creating vbd structure");
437 err = xenvbd_sysfs_addif(dev);
439 vbd_free(&be->blkif->vbd);
440 be->major = be->minor = 0;
441 xenbus_dev_fatal(dev, err, "creating sysfs entries");
445 /* We're potentially connected now */
446 update_blkif_status(be->blkif);
448 /* Add watch for cdrom media status if necessay */
449 cdrom_add_media_watch(be);
455 * Callback received when the frontend's state changes.
457 static void frontend_changed(struct xenbus_device *dev,
458 enum xenbus_state frontend_state)
460 struct backend_info *be = dev_get_drvdata(&dev->dev);
463 DPRINTK("%s", xenbus_strstate(frontend_state));
465 switch (frontend_state) {
466 case XenbusStateInitialising:
467 if (dev->state == XenbusStateClosed) {
468 pr_info("%s: %s: prepare for reconnect\n",
469 __FUNCTION__, dev->nodename);
470 xenbus_switch_state(dev, XenbusStateInitWait);
474 case XenbusStateInitialised:
475 case XenbusStateConnected:
476 /* Ensure we connect even when two watches fire in
477 close successsion and we miss the intermediate value
478 of frontend_state. */
479 if (dev->state == XenbusStateConnected)
482 /* Enforce precondition before potential leak point.
483 * blkif_disconnect() is idempotent.
485 blkif_disconnect(be->blkif);
487 err = connect_ring(be);
490 if (be->blkif->vbd.bdev)
491 update_blkif_status(be->blkif);
494 case XenbusStateClosing:
495 blkif_disconnect(be->blkif);
496 xenbus_switch_state(dev, XenbusStateClosing);
499 case XenbusStateClosed:
500 xenbus_switch_state(dev, XenbusStateClosed);
501 if (xenbus_dev_is_online(dev))
503 /* fall through if not online */
504 case XenbusStateUnknown:
505 /* implies blkif_disconnect() via blkback_remove() */
506 device_unregister(&dev->dev);
510 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
517 /* ** Connection ** */
521 * Write the physical details regarding the block device to the store, and
522 * switch to Connected state.
524 static void connect(struct backend_info *be)
526 struct xenbus_transaction xbt;
528 struct xenbus_device *dev = be->dev;
530 DPRINTK("%s", dev->otherend);
532 /* Supply the information about the device the frontend needs */
534 err = xenbus_transaction_start(&xbt);
536 xenbus_dev_fatal(dev, err, "starting transaction");
540 err = blkback_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
544 err = blkback_barrier(xbt, be, be->blkif->vbd.flush_support);
548 err = xen_blkbk_discard(xbt, be);
550 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
551 vbd_size(&be->blkif->vbd));
553 xenbus_dev_fatal(dev, err, "writing %s/sectors",
558 /* FIXME: use a typename instead */
559 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
560 be->blkif->vbd.type);
562 xenbus_dev_fatal(dev, err, "writing %s/info",
566 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
567 vbd_secsize(&be->blkif->vbd));
569 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
574 err = xenbus_transaction_end(xbt, 0);
578 xenbus_dev_fatal(dev, err, "ending transaction");
580 err = xenbus_switch_state(dev, XenbusStateConnected);
582 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
587 xenbus_transaction_end(xbt, 1);
591 static int connect_ring(struct backend_info *be)
593 struct xenbus_device *dev = be->dev;
594 unsigned int ring_ref, evtchn;
598 DPRINTK("%s", dev->otherend);
600 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref,
601 "event-channel", "%u", &evtchn, NULL);
603 xenbus_dev_fatal(dev, err,
604 "reading %s/ring-ref and event-channel",
609 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
610 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
611 NULL, &protocol, NULL);
614 be->blkif->blk_protocol = xen_guest_blkif_protocol(be->blkif->domid);
616 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
617 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
618 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
619 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
620 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
621 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
623 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
627 pr_info("blkback: ring-ref %u, event-channel %u, protocol %d (%s)\n",
628 ring_ref, evtchn, be->blkif->blk_protocol,
629 protocol ?: "unspecified");
632 /* Map the shared frame, irq etc. */
633 err = blkif_map(be->blkif, ring_ref, evtchn);
635 xenbus_dev_fatal(dev, err, "mapping ring-ref %u port %u",
644 /* ** Driver Registration ** */
647 static const struct xenbus_device_id blkback_ids[] = {
652 static DEFINE_XENBUS_DRIVER(blkback, ,
653 .probe = blkback_probe,
654 .remove = blkback_remove,
655 .otherend_changed = frontend_changed
659 void blkif_xenbus_init(void)
661 WARN_ON(xenbus_register_backend(&blkback_driver));