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/module.h>
22 #include <linux/kthread.h>
24 #include "../core/domctl.h"
27 #define DPRINTK(fmt, args...) \
28 pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
29 __FUNCTION__, __LINE__, ##args)
33 struct xenbus_device *dev;
35 struct xenbus_watch backend_watch;
41 static void connect(struct backend_info *);
42 static int connect_ring(struct backend_info *);
43 static void backend_changed(struct xenbus_watch *, const char **,
46 static int blkback_name(blkif_t *blkif, char *buf)
48 char *devpath, *devname;
49 struct xenbus_device *dev = blkif->be->dev;
51 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
53 return PTR_ERR(devpath);
55 if ((devname = strstr(devpath, "/dev/")) != NULL)
56 devname += strlen("/dev/");
60 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
66 static void update_blkif_status(blkif_t *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 blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
91 if (IS_ERR(blkif->xenblkd)) {
92 err = PTR_ERR(blkif->xenblkd);
93 blkif->xenblkd = NULL;
94 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
99 /****************************************************************
100 * sysfs interface for VBD I/O requests
103 #define VBD_SHOW(name, format, args...) \
104 static ssize_t show_##name(struct device *_dev, \
105 struct device_attribute *attr, \
108 struct xenbus_device *dev = to_xenbus_device(_dev); \
109 struct backend_info *be = dev->dev.driver_data; \
111 return sprintf(buf, format, ##args); \
113 DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
115 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
116 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
117 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
118 VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
119 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
120 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
122 static struct attribute *vbdstat_attrs[] = {
123 &dev_attr_oo_req.attr,
124 &dev_attr_rd_req.attr,
125 &dev_attr_wr_req.attr,
126 &dev_attr_br_req.attr,
127 &dev_attr_rd_sect.attr,
128 &dev_attr_wr_sect.attr,
132 static struct attribute_group vbdstat_group = {
133 .name = "statistics",
134 .attrs = vbdstat_attrs,
137 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
138 VBD_SHOW(mode, "%s\n", be->mode);
140 int xenvbd_sysfs_addif(struct xenbus_device *dev)
144 error = device_create_file(&dev->dev, &dev_attr_physical_device);
148 error = device_create_file(&dev->dev, &dev_attr_mode);
152 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
158 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
159 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
160 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
164 void xenvbd_sysfs_delif(struct xenbus_device *dev)
166 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
167 device_remove_file(&dev->dev, &dev_attr_mode);
168 device_remove_file(&dev->dev, &dev_attr_physical_device);
171 static int blkback_remove(struct xenbus_device *dev)
173 struct backend_info *be = dev->dev.driver_data;
177 if (be->backend_watch.node) {
178 unregister_xenbus_watch(&be->backend_watch);
179 kfree(be->backend_watch.node);
180 be->backend_watch.node = NULL;
184 blkif_disconnect(be->blkif);
185 vbd_free(&be->blkif->vbd);
186 blkif_free(be->blkif);
190 if (be->major || be->minor)
191 xenvbd_sysfs_delif(dev);
194 dev->dev.driver_data = NULL;
198 int blkback_barrier(struct xenbus_transaction xbt,
199 struct backend_info *be, int state)
201 struct xenbus_device *dev = be->dev;
204 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
207 xenbus_dev_fatal(dev, err, "writing feature-barrier");
213 * Entry point to this code when a new device is created. Allocate the basic
214 * structures, and watch the store waiting for the hotplug scripts to tell us
215 * the device's physical major and minor numbers. Switch to InitWait.
217 static int blkback_probe(struct xenbus_device *dev,
218 const struct xenbus_device_id *id)
221 struct backend_info *be = kzalloc(sizeof(struct backend_info),
224 xenbus_dev_fatal(dev, -ENOMEM,
225 "allocating backend structure");
229 dev->dev.driver_data = be;
231 be->blkif = blkif_alloc(dev->otherend_id);
232 if (IS_ERR(be->blkif)) {
233 err = PTR_ERR(be->blkif);
235 xenbus_dev_fatal(dev, err, "creating block interface");
239 /* setup back pointer */
242 err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
243 &be->backend_watch, backend_changed);
247 err = xenbus_switch_state(dev, XenbusStateInitWait);
261 * Callback received when the hotplug scripts have placed the physical-device
262 * node. Read it and the mode node, and create a vbd. If the frontend is
265 static void backend_changed(struct xenbus_watch *watch,
266 const char **vec, unsigned int len)
271 struct backend_info *be
272 = container_of(watch, struct backend_info, backend_watch);
273 struct xenbus_device *dev = be->dev;
277 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
279 if (XENBUS_EXIST_ERR(err)) {
280 /* Since this watch will fire once immediately after it is
281 registered, we expect this. Ignore it, and wait for the
286 xenbus_dev_fatal(dev, err, "reading physical-device");
290 if ((be->major || be->minor) &&
291 ((be->major != major) || (be->minor != minor))) {
293 "blkback: changing physical device (from %x:%x to "
294 "%x:%x) not supported.\n", be->major, be->minor,
299 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
300 if (IS_ERR(be->mode)) {
301 err = PTR_ERR(be->mode);
303 xenbus_dev_fatal(dev, err, "reading mode");
307 if (be->major == 0 && be->minor == 0) {
308 /* Front end dir is a number, which is used as the handle. */
310 char *p = strrchr(dev->otherend, '/') + 1;
311 long handle = simple_strtoul(p, NULL, 0);
316 err = vbd_create(be->blkif, handle, major, minor,
317 (NULL == strchr(be->mode, 'w')));
319 be->major = be->minor = 0;
320 xenbus_dev_fatal(dev, err, "creating vbd structure");
324 err = xenvbd_sysfs_addif(dev);
326 vbd_free(&be->blkif->vbd);
327 be->major = be->minor = 0;
328 xenbus_dev_fatal(dev, err, "creating sysfs entries");
332 /* We're potentially connected now */
333 update_blkif_status(be->blkif);
339 * Callback received when the frontend's state changes.
341 static void frontend_changed(struct xenbus_device *dev,
342 enum xenbus_state frontend_state)
344 struct backend_info *be = dev->dev.driver_data;
347 DPRINTK("%s", xenbus_strstate(frontend_state));
349 switch (frontend_state) {
350 case XenbusStateInitialising:
351 if (dev->state == XenbusStateClosed) {
352 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
353 __FUNCTION__, dev->nodename);
354 xenbus_switch_state(dev, XenbusStateInitWait);
358 case XenbusStateInitialised:
359 case XenbusStateConnected:
360 /* Ensure we connect even when two watches fire in
361 close successsion and we miss the intermediate value
362 of frontend_state. */
363 if (dev->state == XenbusStateConnected)
366 err = connect_ring(be);
369 update_blkif_status(be->blkif);
372 case XenbusStateClosing:
373 blkif_disconnect(be->blkif);
374 xenbus_switch_state(dev, XenbusStateClosing);
377 case XenbusStateClosed:
378 xenbus_switch_state(dev, XenbusStateClosed);
379 if (xenbus_dev_is_online(dev))
381 /* fall through if not online */
382 case XenbusStateUnknown:
383 device_unregister(&dev->dev);
387 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
394 /* ** Connection ** */
398 * Write the physical details regarding the block device to the store, and
399 * switch to Connected state.
401 static void connect(struct backend_info *be)
403 struct xenbus_transaction xbt;
405 struct xenbus_device *dev = be->dev;
407 DPRINTK("%s", dev->otherend);
409 /* Supply the information about the device the frontend needs */
411 err = xenbus_transaction_start(&xbt);
413 xenbus_dev_fatal(dev, err, "starting transaction");
417 err = blkback_barrier(xbt, be, 1);
421 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
422 vbd_size(&be->blkif->vbd));
424 xenbus_dev_fatal(dev, err, "writing %s/sectors",
429 /* FIXME: use a typename instead */
430 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
431 vbd_info(&be->blkif->vbd));
433 xenbus_dev_fatal(dev, err, "writing %s/info",
437 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
438 vbd_secsize(&be->blkif->vbd));
440 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
445 err = xenbus_transaction_end(xbt, 0);
449 xenbus_dev_fatal(dev, err, "ending transaction");
451 err = xenbus_switch_state(dev, XenbusStateConnected);
453 xenbus_dev_fatal(dev, err, "switching to Connected state",
458 xenbus_transaction_end(xbt, 1);
461 static int connect_ring(struct backend_info *be)
463 struct xenbus_device *dev = be->dev;
464 unsigned long ring_ref;
466 char protocol[64] = "";
469 DPRINTK("%s", dev->otherend);
471 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
472 "event-channel", "%u", &evtchn, NULL);
474 xenbus_dev_fatal(dev, err,
475 "reading %s/ring-ref and event-channel",
480 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
481 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
482 "%63s", protocol, NULL);
484 strcpy(protocol, "unspecified");
485 be->blkif->blk_protocol = xen_guest_blkif_protocol(be->blkif->domid);
487 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
488 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
489 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
490 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
491 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
492 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
493 #if 1 /* maintain compatibility with early sles10-sp1 and paravirt netware betas */
494 else if (0 == strcmp(protocol, "1"))
495 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
496 else if (0 == strcmp(protocol, "2"))
497 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
500 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
504 "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
505 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
507 /* Map the shared frame, irq etc. */
508 err = blkif_map(be->blkif, ring_ref, evtchn);
510 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
519 /* ** Driver Registration ** */
522 static struct xenbus_device_id blkback_ids[] = {
528 static struct xenbus_driver blkback = {
530 .owner = THIS_MODULE,
532 .probe = blkback_probe,
533 .remove = blkback_remove,
534 .otherend_changed = frontend_changed
538 void blkif_xenbus_init(void)
540 xenbus_register_backend(&blkback);