- Updated to 2.6.22-rc2-git7:
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / blkback / xenbus.c
1 /*  Xenbus code for blkif backend
2     Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3     Copyright (C) 2005 XenSource Ltd
4
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.
9
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.
14
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
18 */
19
20 #include <stdarg.h>
21 #include <linux/module.h>
22 #include <linux/kthread.h>
23 #include "common.h"
24 #include "../core/domctl.h"
25
26 #undef DPRINTK
27 #define DPRINTK(fmt, args...)                           \
28         pr_debug("blkback/xenbus (%s:%d) " fmt ".\n",   \
29                  __FUNCTION__, __LINE__, ##args)
30
31 struct backend_info
32 {
33         struct xenbus_device *dev;
34         blkif_t *blkif;
35         struct xenbus_watch backend_watch;
36         unsigned major;
37         unsigned minor;
38         char *mode;
39 };
40
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 **,
44                             unsigned int);
45
46 static int blkback_name(blkif_t *blkif, char *buf)
47 {
48         char *devpath, *devname;
49         struct xenbus_device *dev = blkif->be->dev;
50
51         devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
52         if (IS_ERR(devpath)) 
53                 return PTR_ERR(devpath);
54         
55         if ((devname = strstr(devpath, "/dev/")) != NULL)
56                 devname += strlen("/dev/");
57         else
58                 devname  = devpath;
59
60         snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
61         kfree(devpath);
62         
63         return 0;
64 }
65
66 static void update_blkif_status(blkif_t *blkif)
67
68         int err;
69         char name[TASK_COMM_LEN];
70
71         /* Not ready to connect? */
72         if (!blkif->irq || !blkif->vbd.bdev)
73                 return;
74
75         /* Already connected? */
76         if (blkif->be->dev->state == XenbusStateConnected)
77                 return;
78
79         /* Attempt to connect: exit if we fail to. */
80         connect(blkif->be);
81         if (blkif->be->dev->state != XenbusStateConnected)
82                 return;
83
84         err = blkback_name(blkif, name);
85         if (err) {
86                 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
87                 return;
88         }
89
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");
95         }
96 }
97
98
99 /****************************************************************
100  *  sysfs interface for VBD I/O requests
101  */
102
103 #define VBD_SHOW(name, format, args...)                                 \
104         static ssize_t show_##name(struct device *_dev,                 \
105                                    struct device_attribute *attr,       \
106                                    char *buf)                           \
107         {                                                               \
108                 struct xenbus_device *dev = to_xenbus_device(_dev);     \
109                 struct backend_info *be = dev->dev.driver_data;         \
110                                                                         \
111                 return sprintf(buf, format, ##args);                    \
112         }                                                               \
113         DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
114
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);
121
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,
129         NULL
130 };
131
132 static struct attribute_group vbdstat_group = {
133         .name = "statistics",
134         .attrs = vbdstat_attrs,
135 };
136
137 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
138 VBD_SHOW(mode, "%s\n", be->mode);
139
140 int xenvbd_sysfs_addif(struct xenbus_device *dev)
141 {
142         int error;
143         
144         error = device_create_file(&dev->dev, &dev_attr_physical_device);
145         if (error)
146                 goto fail1;
147
148         error = device_create_file(&dev->dev, &dev_attr_mode);
149         if (error)
150                 goto fail2;
151
152         error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
153         if (error)
154                 goto fail3;
155
156         return 0;
157
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);
161         return error;
162 }
163
164 void xenvbd_sysfs_delif(struct xenbus_device *dev)
165 {
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);
169 }
170
171 static int blkback_remove(struct xenbus_device *dev)
172 {
173         struct backend_info *be = dev->dev.driver_data;
174
175         DPRINTK("");
176
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;
181         }
182
183         if (be->blkif) {
184                 blkif_disconnect(be->blkif);
185                 vbd_free(&be->blkif->vbd);
186                 blkif_free(be->blkif);
187                 be->blkif = NULL;
188         }
189
190         if (be->major || be->minor)
191                 xenvbd_sysfs_delif(dev);
192
193         kfree(be);
194         dev->dev.driver_data = NULL;
195         return 0;
196 }
197
198 int blkback_barrier(struct xenbus_transaction xbt,
199                     struct backend_info *be, int state)
200 {
201         struct xenbus_device *dev = be->dev;
202         int err;
203
204         err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
205                             "%d", state);
206         if (err)
207                 xenbus_dev_fatal(dev, err, "writing feature-barrier");
208
209         return err;
210 }
211
212 /**
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.
216  */
217 static int blkback_probe(struct xenbus_device *dev,
218                          const struct xenbus_device_id *id)
219 {
220         int err;
221         struct backend_info *be = kzalloc(sizeof(struct backend_info),
222                                           GFP_KERNEL);
223         if (!be) {
224                 xenbus_dev_fatal(dev, -ENOMEM,
225                                  "allocating backend structure");
226                 return -ENOMEM;
227         }
228         be->dev = dev;
229         dev->dev.driver_data = be;
230
231         be->blkif = blkif_alloc(dev->otherend_id);
232         if (IS_ERR(be->blkif)) {
233                 err = PTR_ERR(be->blkif);
234                 be->blkif = NULL;
235                 xenbus_dev_fatal(dev, err, "creating block interface");
236                 goto fail;
237         }
238
239         /* setup back pointer */
240         be->blkif->be = be;
241
242         err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
243                                  &be->backend_watch, backend_changed);
244         if (err)
245                 goto fail;
246
247         err = xenbus_switch_state(dev, XenbusStateInitWait);
248         if (err)
249                 goto fail;
250
251         return 0;
252
253 fail:
254         DPRINTK("failed");
255         blkback_remove(dev);
256         return err;
257 }
258
259
260 /**
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
263  * ready, connect.
264  */
265 static void backend_changed(struct xenbus_watch *watch,
266                             const char **vec, unsigned int len)
267 {
268         int err;
269         unsigned major;
270         unsigned minor;
271         struct backend_info *be
272                 = container_of(watch, struct backend_info, backend_watch);
273         struct xenbus_device *dev = be->dev;
274
275         DPRINTK("");
276
277         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
278                            &major, &minor);
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
282                    hotplug scripts. */
283                 return;
284         }
285         if (err != 2) {
286                 xenbus_dev_fatal(dev, err, "reading physical-device");
287                 return;
288         }
289
290         if ((be->major || be->minor) &&
291             ((be->major != major) || (be->minor != minor))) {
292                 printk(KERN_WARNING
293                        "blkback: changing physical device (from %x:%x to "
294                        "%x:%x) not supported.\n", be->major, be->minor,
295                        major, minor);
296                 return;
297         }
298
299         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
300         if (IS_ERR(be->mode)) {
301                 err = PTR_ERR(be->mode);
302                 be->mode = NULL;
303                 xenbus_dev_fatal(dev, err, "reading mode");
304                 return;
305         }
306
307         if (be->major == 0 && be->minor == 0) {
308                 /* Front end dir is a number, which is used as the handle. */
309
310                 char *p = strrchr(dev->otherend, '/') + 1;
311                 long handle = simple_strtoul(p, NULL, 0);
312
313                 be->major = major;
314                 be->minor = minor;
315
316                 err = vbd_create(be->blkif, handle, major, minor,
317                                  (NULL == strchr(be->mode, 'w')));
318                 if (err) {
319                         be->major = be->minor = 0;
320                         xenbus_dev_fatal(dev, err, "creating vbd structure");
321                         return;
322                 }
323
324                 err = xenvbd_sysfs_addif(dev);
325                 if (err) {
326                         vbd_free(&be->blkif->vbd);
327                         be->major = be->minor = 0;
328                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
329                         return;
330                 }
331
332                 /* We're potentially connected now */
333                 update_blkif_status(be->blkif);
334         }
335 }
336
337
338 /**
339  * Callback received when the frontend's state changes.
340  */
341 static void frontend_changed(struct xenbus_device *dev,
342                              enum xenbus_state frontend_state)
343 {
344         struct backend_info *be = dev->dev.driver_data;
345         int err;
346
347         DPRINTK("%s", xenbus_strstate(frontend_state));
348
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);
355                 }
356                 break;
357
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)
364                         break;
365
366                 err = connect_ring(be);
367                 if (err)
368                         break;
369                 update_blkif_status(be->blkif);
370                 break;
371
372         case XenbusStateClosing:
373                 blkif_disconnect(be->blkif);
374                 xenbus_switch_state(dev, XenbusStateClosing);
375                 break;
376
377         case XenbusStateClosed:
378                 xenbus_switch_state(dev, XenbusStateClosed);
379                 if (xenbus_dev_is_online(dev))
380                         break;
381                 /* fall through if not online */
382         case XenbusStateUnknown:
383                 device_unregister(&dev->dev);
384                 break;
385
386         default:
387                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
388                                  frontend_state);
389                 break;
390         }
391 }
392
393
394 /* ** Connection ** */
395
396
397 /**
398  * Write the physical details regarding the block device to the store, and
399  * switch to Connected state.
400  */
401 static void connect(struct backend_info *be)
402 {
403         struct xenbus_transaction xbt;
404         int err;
405         struct xenbus_device *dev = be->dev;
406
407         DPRINTK("%s", dev->otherend);
408
409         /* Supply the information about the device the frontend needs */
410 again:
411         err = xenbus_transaction_start(&xbt);
412         if (err) {
413                 xenbus_dev_fatal(dev, err, "starting transaction");
414                 return;
415         }
416
417         err = blkback_barrier(xbt, be, 1);
418         if (err)
419                 goto abort;
420
421         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
422                             vbd_size(&be->blkif->vbd));
423         if (err) {
424                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
425                                  dev->nodename);
426                 goto abort;
427         }
428
429         /* FIXME: use a typename instead */
430         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
431                             vbd_info(&be->blkif->vbd));
432         if (err) {
433                 xenbus_dev_fatal(dev, err, "writing %s/info",
434                                  dev->nodename);
435                 goto abort;
436         }
437         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
438                             vbd_secsize(&be->blkif->vbd));
439         if (err) {
440                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
441                                  dev->nodename);
442                 goto abort;
443         }
444
445         err = xenbus_transaction_end(xbt, 0);
446         if (err == -EAGAIN)
447                 goto again;
448         if (err)
449                 xenbus_dev_fatal(dev, err, "ending transaction");
450
451         err = xenbus_switch_state(dev, XenbusStateConnected);
452         if (err)
453                 xenbus_dev_fatal(dev, err, "switching to Connected state",
454                                  dev->nodename);
455
456         return;
457  abort:
458         xenbus_transaction_end(xbt, 1);
459 }
460
461 static int connect_ring(struct backend_info *be)
462 {
463         struct xenbus_device *dev = be->dev;
464         unsigned long ring_ref;
465         unsigned int evtchn;
466         char protocol[64] = "";
467         int err;
468
469         DPRINTK("%s", dev->otherend);
470
471         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
472                             "event-channel", "%u", &evtchn, NULL);
473         if (err) {
474                 xenbus_dev_fatal(dev, err,
475                                  "reading %s/ring-ref and event-channel",
476                                  dev->otherend);
477                 return err;
478         }
479
480         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
481         err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
482                             "%63s", protocol, NULL);
483         if (err) {
484                 strcpy(protocol, "unspecified");
485                 be->blkif->blk_protocol = xen_guest_blkif_protocol(be->blkif->domid);
486         }
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;
498 #endif
499         else {
500                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
501                 return -1;
502         }
503         printk(KERN_INFO
504                "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
505                ring_ref, evtchn, be->blkif->blk_protocol, protocol);
506
507         /* Map the shared frame, irq etc. */
508         err = blkif_map(be->blkif, ring_ref, evtchn);
509         if (err) {
510                 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
511                                  ring_ref, evtchn);
512                 return err;
513         }
514
515         return 0;
516 }
517
518
519 /* ** Driver Registration ** */
520
521
522 static struct xenbus_device_id blkback_ids[] = {
523         { "vbd" },
524         { "" }
525 };
526
527
528 static struct xenbus_driver blkback = {
529         .name = "vbd",
530         .owner = THIS_MODULE,
531         .ids = blkback_ids,
532         .probe = blkback_probe,
533         .remove = blkback_remove,
534         .otherend_changed = frontend_changed
535 };
536
537
538 void blkif_xenbus_init(void)
539 {
540         xenbus_register_backend(&blkback);
541 }