Update to 3.4-final.
[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/kthread.h>
22 #include "common.h"
23 #include "../core/domctl.h"
24
25 #undef DPRINTK
26 #define DPRINTK(fmt, args...)                           \
27         pr_debug("blkback/xenbus (%s:%d) " fmt ".\n",   \
28                  __FUNCTION__, __LINE__, ##args)
29
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 **,
33                             unsigned int);
34
35 static int blkback_name(blkif_t *blkif, char *buf)
36 {
37         char *devpath, *devname;
38         struct xenbus_device *dev = blkif->be->dev;
39
40         devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
41         if (IS_ERR(devpath)) 
42                 return PTR_ERR(devpath);
43         
44         if ((devname = strstr(devpath, "/dev/")) != NULL)
45                 devname += strlen("/dev/");
46         else
47                 devname  = devpath;
48
49         snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
50         kfree(devpath);
51         
52         return 0;
53 }
54
55 static void update_blkif_status(blkif_t *blkif)
56
57         int err;
58         char name[TASK_COMM_LEN];
59
60         /* Not ready to connect? */
61         if (!blkif->irq)
62                 return;
63
64         /* Already connected? */
65         if (blkif->be->dev->state == XenbusStateConnected)
66                 return;
67
68         /* Attempt to connect: exit if we fail to. */
69         connect(blkif->be);
70         if (blkif->be->dev->state != XenbusStateConnected)
71                 return;
72
73         err = blkback_name(blkif, name);
74         if (err) {
75                 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
76                 return;
77         }
78
79         if (blkif->vbd.bdev) {
80                 struct address_space *mapping
81                         = blkif->vbd.bdev->bd_inode->i_mapping;
82
83                 err = filemap_write_and_wait(mapping);
84                 if (err) {
85                         xenbus_dev_error(blkif->be->dev, err, "block flush");
86                         return;
87                 }
88                 invalidate_inode_pages2(mapping);
89         }
90
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");
96         }
97 }
98
99
100 /****************************************************************
101  *  sysfs interface for VBD I/O requests
102  */
103
104 #define VBD_SHOW(name, format, args...)                                 \
105         static ssize_t show_##name(struct device *_dev,                 \
106                                    struct device_attribute *attr,       \
107                                    char *buf)                           \
108         {                                                               \
109                 ssize_t ret = -ENODEV;                                  \
110                 struct xenbus_device *dev;                              \
111                 struct backend_info *be;                                \
112                                                                         \
113                 if (!get_device(_dev))                                  \
114                         return ret;                                     \
115                 dev = to_xenbus_device(_dev);                           \
116                 if ((be = dev_get_drvdata(&dev->dev)) != NULL)          \
117                         ret = sprintf(buf, format, ##args);             \
118                 put_device(_dev);                                       \
119                 return ret;                                             \
120         }                                                               \
121         static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
122
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);
131
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,
141         NULL
142 };
143
144 static const struct attribute_group vbdstat_group = {
145         .name = "statistics",
146         .attrs = vbdstat_attrs,
147 };
148
149 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
150 VBD_SHOW(mode, "%s\n", be->mode);
151
152 int xenvbd_sysfs_addif(struct xenbus_device *dev)
153 {
154         int error;
155         
156         error = device_create_file(&dev->dev, &dev_attr_physical_device);
157         if (error)
158                 goto fail1;
159
160         error = device_create_file(&dev->dev, &dev_attr_mode);
161         if (error)
162                 goto fail2;
163
164         error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
165         if (error)
166                 goto fail3;
167
168         return 0;
169
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);
173         return error;
174 }
175
176 void xenvbd_sysfs_delif(struct xenbus_device *dev)
177 {
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);
181 }
182
183 static int blkback_remove(struct xenbus_device *dev)
184 {
185         struct backend_info *be = dev_get_drvdata(&dev->dev);
186
187         DPRINTK("");
188
189         if (be->major || be->minor)
190                 xenvbd_sysfs_delif(dev);
191
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;
196         }
197
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;
202         }
203
204         if (be->blkif) {
205                 blkif_disconnect(be->blkif);
206                 vbd_free(&be->blkif->vbd);
207                 blkif_free(be->blkif);
208                 be->blkif = NULL;
209         }
210
211         kfree(be);
212         dev_set_drvdata(&dev->dev, NULL);
213         return 0;
214 }
215
216 void blkback_barrier(struct xenbus_transaction xbt,
217                      struct backend_info *be, int state)
218 {
219         struct xenbus_device *dev = be->dev;
220         int err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
221                                 "%d", state);
222
223         if (err)
224                 xenbus_dev_error(dev, err, "writing feature-barrier");
225 }
226
227 void blkback_flush_diskcache(struct xenbus_transaction xbt,
228                              struct backend_info *be, int state)
229 {
230         struct xenbus_device *dev = be->dev;
231         int err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
232                                 "%d", state);
233
234         if (err)
235                 xenbus_dev_error(dev, err, "writing feature-flush-cache");
236 }
237
238 static void blkback_discard(struct xenbus_transaction xbt,
239                             struct backend_info *be)
240 {
241         struct xenbus_device *dev = be->dev;
242         struct vbd *vbd = &be->blkif->vbd;
243         struct request_queue *q = bdev_get_queue(vbd->bdev);
244         int err, state = 0;
245
246         if (blk_queue_discard(q)) {
247                 err = xenbus_printf(xbt, dev->nodename, "discard-granularity",
248                                     "%u", q->limits.discard_granularity);
249                 if (!err)
250                         state = 1;
251                 else
252                         xenbus_dev_error(dev, err,
253                                          "writing discard-granularity");
254                 err = xenbus_printf(xbt, dev->nodename, "discard-alignment",
255                                     "%u", q->limits.discard_alignment);
256                 if (err) {
257                         xenbus_dev_error(dev, err,
258                                          "writing discard-alignment");
259                         state = 0;
260                 }
261         }
262
263         /* Optional. */
264         if (state) {
265                 err = xenbus_printf(xbt, dev->nodename, "discard-secure",
266                                     "%d", vbd->discard_secure);
267                 if (err)
268                         xenbus_dev_error(dev, err, "writing discard-secure");
269         }
270
271         err = xenbus_printf(xbt, dev->nodename, "feature-discard",
272                             "%d", state);
273         if (err)
274                 xenbus_dev_error(dev, err, "writing feature-discard");
275 }
276
277 /**
278  * Entry point to this code when a new device is created.  Allocate the basic
279  * structures, and watch the store waiting for the hotplug scripts to tell us
280  * the device's physical major and minor numbers.  Switch to InitWait.
281  */
282 static int blkback_probe(struct xenbus_device *dev,
283                          const struct xenbus_device_id *id)
284 {
285         int err;
286         struct backend_info *be = kzalloc(sizeof(struct backend_info),
287                                           GFP_KERNEL);
288         if (!be) {
289                 xenbus_dev_fatal(dev, -ENOMEM,
290                                  "allocating backend structure");
291                 return -ENOMEM;
292         }
293         be->dev = dev;
294         dev_set_drvdata(&dev->dev, be);
295
296         be->blkif = blkif_alloc(dev->otherend_id);
297         if (IS_ERR(be->blkif)) {
298                 err = PTR_ERR(be->blkif);
299                 be->blkif = NULL;
300                 xenbus_dev_fatal(dev, err, "creating block interface");
301                 goto fail;
302         }
303
304         /* setup back pointer */
305         be->blkif->be = be;
306
307         err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
308                                  &be->backend_watch, backend_changed);
309         if (err)
310                 goto fail;
311
312         err = xenbus_switch_state(dev, XenbusStateInitWait);
313         if (err)
314                 goto fail;
315
316         return 0;
317
318 fail:
319         DPRINTK("failed");
320         blkback_remove(dev);
321         return err;
322 }
323
324
325 /**
326  * Callback received when the hotplug scripts have placed the physical-device
327  * node.  Read it and the mode node, and create a vbd.  If the frontend is
328  * ready, connect.
329  */
330 static void backend_changed(struct xenbus_watch *watch,
331                             const char **vec, unsigned int len)
332 {
333         int err;
334         unsigned major;
335         unsigned minor;
336         struct backend_info *be
337                 = container_of(watch, struct backend_info, backend_watch);
338         struct xenbus_device *dev = be->dev;
339         int cdrom = 0;
340         char *device_type;
341
342         DPRINTK("");
343
344         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
345                            &major, &minor);
346         if (XENBUS_EXIST_ERR(err)) {
347                 /* Since this watch will fire once immediately after it is
348                    registered, we expect this.  Ignore it, and wait for the
349                    hotplug scripts. */
350                 return;
351         }
352         if (err != 2) {
353                 xenbus_dev_fatal(dev, err, "reading physical-device");
354                 return;
355         }
356
357         if ((be->major || be->minor) &&
358             ((be->major != major) || (be->minor != minor))) {
359                 pr_warning("blkback: changing physical device (from %x:%x to"
360                            " %x:%x) not supported\n", be->major, be->minor,
361                            major, minor);
362                 return;
363         }
364
365         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
366         if (IS_ERR(be->mode)) {
367                 err = PTR_ERR(be->mode);
368                 be->mode = NULL;
369                 xenbus_dev_fatal(dev, err, "reading mode");
370                 return;
371         }
372
373         device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
374         if (!IS_ERR(device_type)) {
375                 cdrom = strcmp(device_type, "cdrom") == 0;
376                 kfree(device_type);
377         }
378
379         if (be->major == 0 && be->minor == 0) {
380                 /* Front end dir is a number, which is used as the handle. */
381
382                 char *p = strrchr(dev->otherend, '/') + 1;
383                 long handle = simple_strtoul(p, NULL, 0);
384
385                 be->major = major;
386                 be->minor = minor;
387
388                 err = vbd_create(be->blkif, handle, major, minor,
389                                  FMODE_READ
390                                  | (strchr(be->mode, 'w') ? FMODE_WRITE : 0)
391                                  | (strchr(be->mode, '!') ? 0 : FMODE_EXCL),
392                                  cdrom);
393                 switch (err) {
394                 case -ENOMEDIUM:
395                         if (be->blkif->vbd.type
396                             & (VDISK_CDROM | VDISK_REMOVABLE))
397                 case 0:
398                                 break;
399                 default:
400                         be->major = be->minor = 0;
401                         xenbus_dev_fatal(dev, err, "creating vbd structure");
402                         return;
403                 }
404
405                 err = xenvbd_sysfs_addif(dev);
406                 if (err) {
407                         vbd_free(&be->blkif->vbd);
408                         be->major = be->minor = 0;
409                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
410                         return;
411                 }
412
413                 /* We're potentially connected now */
414                 update_blkif_status(be->blkif);
415
416                 /* Add watch for cdrom media status if necessay */
417                 cdrom_add_media_watch(be);
418         }
419 }
420
421
422 /**
423  * Callback received when the frontend's state changes.
424  */
425 static void frontend_changed(struct xenbus_device *dev,
426                              enum xenbus_state frontend_state)
427 {
428         struct backend_info *be = dev_get_drvdata(&dev->dev);
429         int err;
430
431         DPRINTK("%s", xenbus_strstate(frontend_state));
432
433         switch (frontend_state) {
434         case XenbusStateInitialising:
435                 if (dev->state == XenbusStateClosed) {
436                         pr_info("%s: %s: prepare for reconnect\n",
437                                 __FUNCTION__, dev->nodename);
438                         xenbus_switch_state(dev, XenbusStateInitWait);
439                 }
440                 break;
441
442         case XenbusStateInitialised:
443         case XenbusStateConnected:
444                 /* Ensure we connect even when two watches fire in 
445                    close successsion and we miss the intermediate value 
446                    of frontend_state. */
447                 if (dev->state == XenbusStateConnected)
448                         break;
449
450                 /* Enforce precondition before potential leak point.
451                  * blkif_disconnect() is idempotent.
452                  */
453                 blkif_disconnect(be->blkif);
454
455                 err = connect_ring(be);
456                 if (err)
457                         break;
458                 if (be->blkif->vbd.bdev)
459                         update_blkif_status(be->blkif);
460                 break;
461
462         case XenbusStateClosing:
463         case XenbusStateClosed:
464                 blkif_disconnect(be->blkif);
465                 xenbus_switch_state(dev, frontend_state);
466                 if (frontend_state != XenbusStateClosed ||
467                     xenbus_dev_is_online(dev))
468                         break;
469                 /* fall through if not online */
470         case XenbusStateUnknown:
471                 /* implies blkif_disconnect() via blkback_remove() */
472                 device_unregister(&dev->dev);
473                 break;
474
475         default:
476                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
477                                  frontend_state);
478                 break;
479         }
480 }
481
482
483 /* ** Connection ** */
484
485
486 /**
487  * Write the physical details regarding the block device to the store, and
488  * switch to Connected state.
489  */
490 static void connect(struct backend_info *be)
491 {
492         struct xenbus_transaction xbt;
493         int err;
494         struct xenbus_device *dev = be->dev;
495
496         DPRINTK("%s", dev->otherend);
497
498         /* Supply the information about the device the frontend needs */
499 again:
500         err = xenbus_transaction_start(&xbt);
501         if (err) {
502                 xenbus_dev_fatal(dev, err, "starting transaction");
503                 return;
504         }
505
506         blkback_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
507         blkback_barrier(xbt, be, be->blkif->vbd.flush_support);
508         blkback_discard(xbt, be);
509
510         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
511                             vbd_size(&be->blkif->vbd));
512         if (err) {
513                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
514                                  dev->nodename);
515                 goto abort;
516         }
517
518         /* FIXME: use a typename instead */
519         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
520                             be->blkif->vbd.type);
521         if (err) {
522                 xenbus_dev_fatal(dev, err, "writing %s/info",
523                                  dev->nodename);
524                 goto abort;
525         }
526         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
527                             vbd_secsize(&be->blkif->vbd));
528         if (err) {
529                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
530                                  dev->nodename);
531                 goto abort;
532         }
533
534         err = xenbus_transaction_end(xbt, 0);
535         if (err == -EAGAIN)
536                 goto again;
537         if (err)
538                 xenbus_dev_fatal(dev, err, "ending transaction");
539
540         err = xenbus_switch_state(dev, XenbusStateConnected);
541         if (err)
542                 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
543                                  dev->nodename);
544
545         return;
546  abort:
547         xenbus_transaction_end(xbt, 1);
548 }
549
550
551 static int connect_ring(struct backend_info *be)
552 {
553         struct xenbus_device *dev = be->dev;
554         unsigned int ring_ref, evtchn;
555         char *protocol;
556         int err;
557
558         DPRINTK("%s", dev->otherend);
559
560         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref,
561                             "event-channel", "%u", &evtchn, NULL);
562         if (err) {
563                 xenbus_dev_fatal(dev, err,
564                                  "reading %s/ring-ref and event-channel",
565                                  dev->otherend);
566                 return err;
567         }
568
569         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
570         protocol = xenbus_read(XBT_NIL, dev->otherend, "protocol", NULL);
571         if (IS_ERR(protocol)) {
572                 protocol = NULL;
573                 be->blkif->blk_protocol = xen_guest_blkif_protocol(be->blkif->domid);
574 #ifndef CONFIG_X86_32
575         } else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) {
576                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
577 #endif
578 #ifndef CONFIG_X86_64
579         } else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) {
580                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
581 #endif
582         } else if (0 != strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) {
583                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
584                 kfree(protocol);
585                 return -1;
586         }
587         pr_info("blkback: ring-ref %u, event-channel %u, protocol %d (%s)\n",
588                 ring_ref, evtchn, be->blkif->blk_protocol,
589                 protocol ?: "unspecified");
590         kfree(protocol);
591
592         /* Map the shared frame, irq etc. */
593         err = blkif_map(be->blkif, ring_ref, evtchn);
594         if (err) {
595                 xenbus_dev_fatal(dev, err, "mapping ring-ref %u port %u",
596                                  ring_ref, evtchn);
597                 return err;
598         }
599
600         return 0;
601 }
602
603
604 /* ** Driver Registration ** */
605
606
607 static const struct xenbus_device_id blkback_ids[] = {
608         { "vbd" },
609         { "" }
610 };
611
612 static DEFINE_XENBUS_DRIVER(blkback, ,
613         .probe = blkback_probe,
614         .remove = blkback_remove,
615         .otherend_changed = frontend_changed
616 );
617
618
619 void blkif_xenbus_init(void)
620 {
621         WARN_ON(xenbus_register_backend(&blkback_driver));
622 }