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>
23 #include <xen/xenbus.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 void update_blkif_status(blkif_t *blkif)
50 /* Not ready to connect? */
51 if (!blkif->irq || !blkif->vbd.bdev)
54 /* Already connected? */
55 if (blkif->be->dev->state == XenbusStateConnected)
58 /* Attempt to connect: exit if we fail to. */
60 if (blkif->be->dev->state != XenbusStateConnected)
63 blkif->xenblkd = kthread_run(blkif_schedule, blkif,
66 blkif->be->major, blkif->be->minor);
67 if (IS_ERR(blkif->xenblkd)) {
68 err = PTR_ERR(blkif->xenblkd);
69 blkif->xenblkd = NULL;
70 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
75 /****************************************************************
76 * sysfs interface for VBD I/O requests
79 #define VBD_SHOW(name, format, args...) \
80 static ssize_t show_##name(struct device *_dev, \
81 struct device_attribute *attr, \
84 struct xenbus_device *dev = to_xenbus_device(_dev); \
85 struct backend_info *be = dev->dev.driver_data; \
87 return sprintf(buf, format, ##args); \
89 DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
91 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
92 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
93 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
95 static struct attribute *vbdstat_attrs[] = {
96 &dev_attr_oo_req.attr,
97 &dev_attr_rd_req.attr,
98 &dev_attr_wr_req.attr,
102 static struct attribute_group vbdstat_group = {
103 .name = "statistics",
104 .attrs = vbdstat_attrs,
107 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
108 VBD_SHOW(mode, "%s\n", be->mode);
110 int xenvbd_sysfs_addif(struct xenbus_device *dev)
114 error = device_create_file(&dev->dev, &dev_attr_physical_device);
118 error = device_create_file(&dev->dev, &dev_attr_mode);
122 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
128 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
129 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
130 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
134 void xenvbd_sysfs_delif(struct xenbus_device *dev)
136 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
137 device_remove_file(&dev->dev, &dev_attr_mode);
138 device_remove_file(&dev->dev, &dev_attr_physical_device);
141 static int blkback_remove(struct xenbus_device *dev)
143 struct backend_info *be = dev->dev.driver_data;
147 if (be->backend_watch.node) {
148 unregister_xenbus_watch(&be->backend_watch);
149 kfree(be->backend_watch.node);
150 be->backend_watch.node = NULL;
154 blkif_disconnect(be->blkif);
155 vbd_free(&be->blkif->vbd);
156 blkif_free(be->blkif);
160 if (be->major || be->minor)
161 xenvbd_sysfs_delif(dev);
164 dev->dev.driver_data = NULL;
170 * Entry point to this code when a new device is created. Allocate the basic
171 * structures, and watch the store waiting for the hotplug scripts to tell us
172 * the device's physical major and minor numbers. Switch to InitWait.
174 static int blkback_probe(struct xenbus_device *dev,
175 const struct xenbus_device_id *id)
178 struct backend_info *be = kzalloc(sizeof(struct backend_info),
181 xenbus_dev_fatal(dev, -ENOMEM,
182 "allocating backend structure");
186 dev->dev.driver_data = be;
188 be->blkif = blkif_alloc(dev->otherend_id);
189 if (IS_ERR(be->blkif)) {
190 err = PTR_ERR(be->blkif);
192 xenbus_dev_fatal(dev, err, "creating block interface");
196 /* setup back pointer */
199 err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
200 &be->backend_watch, backend_changed);
204 err = xenbus_switch_state(dev, XenbusStateInitWait);
218 * Callback received when the hotplug scripts have placed the physical-device
219 * node. Read it and the mode node, and create a vbd. If the frontend is
222 static void backend_changed(struct xenbus_watch *watch,
223 const char **vec, unsigned int len)
228 struct backend_info *be
229 = container_of(watch, struct backend_info, backend_watch);
230 struct xenbus_device *dev = be->dev;
234 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
236 if (XENBUS_EXIST_ERR(err)) {
237 /* Since this watch will fire once immediately after it is
238 registered, we expect this. Ignore it, and wait for the
243 xenbus_dev_fatal(dev, err, "reading physical-device");
247 if ((be->major || be->minor) &&
248 ((be->major != major) || (be->minor != minor))) {
250 "blkback: changing physical device (from %x:%x to "
251 "%x:%x) not supported.\n", be->major, be->minor,
256 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
257 if (IS_ERR(be->mode)) {
258 err = PTR_ERR(be->mode);
260 xenbus_dev_fatal(dev, err, "reading mode");
264 if (be->major == 0 && be->minor == 0) {
265 /* Front end dir is a number, which is used as the handle. */
267 char *p = strrchr(dev->otherend, '/') + 1;
268 long handle = simple_strtoul(p, NULL, 0);
273 err = vbd_create(be->blkif, handle, major, minor,
274 (NULL == strchr(be->mode, 'w')));
276 be->major = be->minor = 0;
277 xenbus_dev_fatal(dev, err, "creating vbd structure");
281 err = xenvbd_sysfs_addif(dev);
283 vbd_free(&be->blkif->vbd);
284 be->major = be->minor = 0;
285 xenbus_dev_fatal(dev, err, "creating sysfs entries");
289 /* We're potentially connected now */
290 update_blkif_status(be->blkif);
296 * Callback received when the frontend's state changes.
298 static void frontend_changed(struct xenbus_device *dev,
299 enum xenbus_state frontend_state)
301 struct backend_info *be = dev->dev.driver_data;
306 switch (frontend_state) {
307 case XenbusStateInitialising:
310 case XenbusStateInitialised:
311 case XenbusStateConnected:
312 /* Ensure we connect even when two watches fire in
313 close successsion and we miss the intermediate value
314 of frontend_state. */
315 if (dev->state == XenbusStateConnected)
318 err = connect_ring(be);
321 update_blkif_status(be->blkif);
324 case XenbusStateClosing:
325 blkif_disconnect(be->blkif);
326 xenbus_switch_state(dev, XenbusStateClosing);
329 case XenbusStateClosed:
330 device_unregister(&dev->dev);
333 case XenbusStateUnknown:
334 case XenbusStateInitWait:
336 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
343 /* ** Connection ** */
347 * Write the physical details regarding the block device to the store, and
348 * switch to Connected state.
350 static void connect(struct backend_info *be)
352 struct xenbus_transaction xbt;
354 struct xenbus_device *dev = be->dev;
356 DPRINTK("%s", dev->otherend);
358 /* Supply the information about the device the frontend needs */
360 err = xenbus_transaction_start(&xbt);
363 xenbus_dev_fatal(dev, err, "starting transaction");
367 err = xenbus_printf(xbt, dev->nodename, "sectors", "%lu",
368 vbd_size(&be->blkif->vbd));
370 xenbus_dev_fatal(dev, err, "writing %s/sectors",
375 /* FIXME: use a typename instead */
376 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
377 vbd_info(&be->blkif->vbd));
379 xenbus_dev_fatal(dev, err, "writing %s/info",
383 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
384 vbd_secsize(&be->blkif->vbd));
386 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
391 err = xenbus_transaction_end(xbt, 0);
395 xenbus_dev_fatal(dev, err, "ending transaction");
397 err = xenbus_switch_state(dev, XenbusStateConnected);
399 xenbus_dev_fatal(dev, err, "switching to Connected state",
404 xenbus_transaction_end(xbt, 1);
408 static int connect_ring(struct backend_info *be)
410 struct xenbus_device *dev = be->dev;
411 unsigned long ring_ref;
415 DPRINTK("%s", dev->otherend);
417 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
418 "event-channel", "%u", &evtchn, NULL);
420 xenbus_dev_fatal(dev, err,
421 "reading %s/ring-ref and event-channel",
426 /* Map the shared frame, irq etc. */
427 err = blkif_map(be->blkif, ring_ref, evtchn);
429 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
438 /* ** Driver Registration ** */
441 static struct xenbus_device_id blkback_ids[] = {
447 static struct xenbus_driver blkback = {
449 .owner = THIS_MODULE,
451 .probe = blkback_probe,
452 .remove = blkback_remove,
453 .otherend_changed = frontend_changed
457 void blkif_xenbus_init(void)
459 xenbus_register_backend(&blkback);