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>
26 #define DPRINTK(fmt, args...) \
27 pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
28 __FUNCTION__, __LINE__, ##args)
32 struct xenbus_device *dev;
34 struct xenbus_watch backend_watch;
40 static void connect(struct backend_info *);
41 static int connect_ring(struct backend_info *);
42 static void backend_changed(struct xenbus_watch *, const char **,
45 static int blkback_name(blkif_t *blkif, char *buf)
47 char *devpath, *devname;
48 struct xenbus_device *dev = blkif->be->dev;
50 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
52 return PTR_ERR(devpath);
54 if ((devname = strstr(devpath, "/dev/")) != NULL)
55 devname += strlen("/dev/");
59 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
65 static void update_blkif_status(blkif_t *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 blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
90 if (IS_ERR(blkif->xenblkd)) {
91 err = PTR_ERR(blkif->xenblkd);
92 blkif->xenblkd = NULL;
93 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
98 /****************************************************************
99 * sysfs interface for VBD I/O requests
102 #define VBD_SHOW(name, format, args...) \
103 static ssize_t show_##name(struct device *_dev, \
104 struct device_attribute *attr, \
107 struct xenbus_device *dev = to_xenbus_device(_dev); \
108 struct backend_info *be = dev->dev.driver_data; \
110 return sprintf(buf, format, ##args); \
112 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
114 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
115 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
116 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
117 VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
118 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
119 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
121 static struct attribute *vbdstat_attrs[] = {
122 &dev_attr_oo_req.attr,
123 &dev_attr_rd_req.attr,
124 &dev_attr_wr_req.attr,
125 &dev_attr_br_req.attr,
126 &dev_attr_rd_sect.attr,
127 &dev_attr_wr_sect.attr,
131 static struct attribute_group vbdstat_group = {
132 .name = "statistics",
133 .attrs = vbdstat_attrs,
136 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
137 VBD_SHOW(mode, "%s\n", be->mode);
139 int xenvbd_sysfs_addif(struct xenbus_device *dev)
143 error = device_create_file(&dev->dev, &dev_attr_physical_device);
147 error = device_create_file(&dev->dev, &dev_attr_mode);
151 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
157 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
158 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
159 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
163 void xenvbd_sysfs_delif(struct xenbus_device *dev)
165 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
166 device_remove_file(&dev->dev, &dev_attr_mode);
167 device_remove_file(&dev->dev, &dev_attr_physical_device);
170 static int blkback_remove(struct xenbus_device *dev)
172 struct backend_info *be = dev->dev.driver_data;
176 if (be->major || be->minor)
177 xenvbd_sysfs_delif(dev);
179 if (be->backend_watch.node) {
180 unregister_xenbus_watch(&be->backend_watch);
181 kfree(be->backend_watch.node);
182 be->backend_watch.node = NULL;
186 blkif_disconnect(be->blkif);
187 vbd_free(&be->blkif->vbd);
188 blkif_free(be->blkif);
193 dev->dev.driver_data = NULL;
197 int blkback_barrier(struct xenbus_transaction xbt,
198 struct backend_info *be, int state)
200 struct xenbus_device *dev = be->dev;
203 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
206 xenbus_dev_fatal(dev, err, "writing feature-barrier");
212 * Entry point to this code when a new device is created. Allocate the basic
213 * structures, and watch the store waiting for the hotplug scripts to tell us
214 * the device's physical major and minor numbers. Switch to InitWait.
216 static int blkback_probe(struct xenbus_device *dev,
217 const struct xenbus_device_id *id)
220 struct backend_info *be = kzalloc(sizeof(struct backend_info),
223 xenbus_dev_fatal(dev, -ENOMEM,
224 "allocating backend structure");
228 dev->dev.driver_data = be;
230 be->blkif = blkif_alloc(dev->otherend_id);
231 if (IS_ERR(be->blkif)) {
232 err = PTR_ERR(be->blkif);
234 xenbus_dev_fatal(dev, err, "creating block interface");
238 /* setup back pointer */
241 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
242 "%s/%s", dev->nodename, "physical-device");
246 err = xenbus_switch_state(dev, XenbusStateInitWait);
260 * Callback received when the hotplug scripts have placed the physical-device
261 * node. Read it and the mode node, and create a vbd. If the frontend is
264 static void backend_changed(struct xenbus_watch *watch,
265 const char **vec, unsigned int len)
270 struct backend_info *be
271 = container_of(watch, struct backend_info, backend_watch);
272 struct xenbus_device *dev = be->dev;
278 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
280 if (XENBUS_EXIST_ERR(err)) {
281 /* Since this watch will fire once immediately after it is
282 registered, we expect this. Ignore it, and wait for the
287 xenbus_dev_fatal(dev, err, "reading physical-device");
291 if ((be->major || be->minor) &&
292 ((be->major != major) || (be->minor != minor))) {
294 "blkback: changing physical device (from %x:%x to "
295 "%x:%x) not supported.\n", be->major, be->minor,
300 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
301 if (IS_ERR(be->mode)) {
302 err = PTR_ERR(be->mode);
304 xenbus_dev_fatal(dev, err, "reading mode");
308 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
309 if (!IS_ERR(device_type)) {
310 cdrom = strcmp(device_type, "cdrom") == 0;
314 if (be->major == 0 && be->minor == 0) {
315 /* Front end dir is a number, which is used as the handle. */
317 char *p = strrchr(dev->otherend, '/') + 1;
318 long handle = simple_strtoul(p, NULL, 0);
323 err = vbd_create(be->blkif, handle, major, minor,
324 (NULL == strchr(be->mode, 'w')), cdrom);
326 be->major = be->minor = 0;
327 xenbus_dev_fatal(dev, err, "creating vbd structure");
331 err = xenvbd_sysfs_addif(dev);
333 vbd_free(&be->blkif->vbd);
334 be->major = be->minor = 0;
335 xenbus_dev_fatal(dev, err, "creating sysfs entries");
339 /* We're potentially connected now */
340 update_blkif_status(be->blkif);
346 * Callback received when the frontend's state changes.
348 static void frontend_changed(struct xenbus_device *dev,
349 enum xenbus_state frontend_state)
351 struct backend_info *be = dev->dev.driver_data;
354 DPRINTK("%s", xenbus_strstate(frontend_state));
356 switch (frontend_state) {
357 case XenbusStateInitialising:
358 if (dev->state == XenbusStateClosed) {
359 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
360 __FUNCTION__, dev->nodename);
361 xenbus_switch_state(dev, XenbusStateInitWait);
365 case XenbusStateInitialised:
366 case XenbusStateConnected:
367 /* Ensure we connect even when two watches fire in
368 close successsion and we miss the intermediate value
369 of frontend_state. */
370 if (dev->state == XenbusStateConnected)
373 err = connect_ring(be);
376 update_blkif_status(be->blkif);
379 case XenbusStateClosing:
380 blkif_disconnect(be->blkif);
381 xenbus_switch_state(dev, XenbusStateClosing);
384 case XenbusStateClosed:
385 xenbus_switch_state(dev, XenbusStateClosed);
386 if (xenbus_dev_is_online(dev))
388 /* fall through if not online */
389 case XenbusStateUnknown:
390 device_unregister(&dev->dev);
394 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
401 /* ** Connection ** */
405 * Write the physical details regarding the block device to the store, and
406 * switch to Connected state.
408 static void connect(struct backend_info *be)
410 struct xenbus_transaction xbt;
412 struct xenbus_device *dev = be->dev;
414 DPRINTK("%s", dev->otherend);
416 /* Supply the information about the device the frontend needs */
418 err = xenbus_transaction_start(&xbt);
420 xenbus_dev_fatal(dev, err, "starting transaction");
424 err = blkback_barrier(xbt, be, 1);
428 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
429 vbd_size(&be->blkif->vbd));
431 xenbus_dev_fatal(dev, err, "writing %s/sectors",
436 /* FIXME: use a typename instead */
437 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
438 vbd_info(&be->blkif->vbd));
440 xenbus_dev_fatal(dev, err, "writing %s/info",
444 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
445 vbd_secsize(&be->blkif->vbd));
447 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
452 err = xenbus_transaction_end(xbt, 0);
456 xenbus_dev_fatal(dev, err, "ending transaction");
458 err = xenbus_switch_state(dev, XenbusStateConnected);
460 xenbus_dev_fatal(dev, err, "switching to Connected state",
465 xenbus_transaction_end(xbt, 1);
469 static int connect_ring(struct backend_info *be)
471 struct xenbus_device *dev = be->dev;
472 unsigned long ring_ref;
474 char protocol[64] = "";
477 DPRINTK("%s", dev->otherend);
479 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
480 "event-channel", "%u", &evtchn, NULL);
482 xenbus_dev_fatal(dev, err,
483 "reading %s/ring-ref and event-channel",
488 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
489 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
490 "%63s", protocol, NULL);
492 strcpy(protocol, "unspecified, assuming native");
493 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
494 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
495 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
496 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
497 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
498 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 const 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)
541 (void)xenbus_register_backend(&blkback);