- Update Xen patches to 3.3-rc5 and c/s 1157.
[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 int blkback_barrier(struct xenbus_transaction xbt,
217                     struct backend_info *be, int state)
218 {
219         struct xenbus_device *dev = be->dev;
220         int err;
221
222         err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
223                             "%d", state);
224         if (err)
225                 xenbus_dev_fatal(dev, err, "writing feature-barrier");
226
227         return err;
228 }
229
230 int blkback_flush_diskcache(struct xenbus_transaction xbt,
231                             struct backend_info *be, int state)
232 {
233         struct xenbus_device *dev = be->dev;
234         int err;
235
236         err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
237                             "%d", state);
238         if (err)
239                 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
240
241         return err;
242 }
243
244 static int xen_blkbk_discard(struct xenbus_transaction xbt,
245                              struct backend_info *be)
246 {
247         struct xenbus_device *dev = be->dev;
248         blkif_t *blkif = be->blkif;
249         char *type;
250         int err;
251         int state = 0;
252
253         type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL);
254         if (!IS_ERR(type)) {
255                 if (strncmp(type, "file", 4) == 0) {
256                         state = 1;
257                         blkif->blk_backend_type = BLKIF_BACKEND_FILE;
258                 }
259                 if (strncmp(type, "phy", 3) == 0) {
260                         struct request_queue *q;
261
262                         q = bdev_get_queue(blkif->vbd.bdev);
263                         if (blk_queue_discard(q)) {
264                                 err = xenbus_printf(xbt, dev->nodename,
265                                         "discard-granularity", "%u",
266                                         q->limits.discard_granularity);
267                                 if (err) {
268                                         xenbus_dev_fatal(dev, err,
269                                                 "writing discard-granularity");
270                                         goto kfree;
271                                 }
272                                 err = xenbus_printf(xbt, dev->nodename,
273                                         "discard-alignment", "%u",
274                                         q->limits.discard_alignment);
275                                 if (err) {
276                                         xenbus_dev_fatal(dev, err,
277                                                 "writing discard-alignment");
278                                         goto kfree;
279                                 }
280                                 state = 1;
281                                 blkif->blk_backend_type = BLKIF_BACKEND_PHY;
282                         }
283                         /* Optional. */
284                         err = xenbus_printf(xbt, dev->nodename,
285                                             "discard-secure", "%d",
286                                             blkif->vbd.discard_secure);
287                         if (err) {
288                                 xenbus_dev_fatal(dev, err,
289                                                  "writing discard-secure");
290                                 goto kfree;
291                         }
292                 }
293         } else {
294                 err = PTR_ERR(type);
295                 xenbus_dev_fatal(dev, err, "reading type");
296                 goto out;
297         }
298
299         err = xenbus_printf(xbt, dev->nodename, "feature-discard",
300                             "%d", state);
301         if (err)
302                 xenbus_dev_fatal(dev, err, "writing feature-discard");
303 kfree:
304         kfree(type);
305 out:
306         return err;
307 }
308
309 /**
310  * Entry point to this code when a new device is created.  Allocate the basic
311  * structures, and watch the store waiting for the hotplug scripts to tell us
312  * the device's physical major and minor numbers.  Switch to InitWait.
313  */
314 static int blkback_probe(struct xenbus_device *dev,
315                          const struct xenbus_device_id *id)
316 {
317         int err;
318         struct backend_info *be = kzalloc(sizeof(struct backend_info),
319                                           GFP_KERNEL);
320         if (!be) {
321                 xenbus_dev_fatal(dev, -ENOMEM,
322                                  "allocating backend structure");
323                 return -ENOMEM;
324         }
325         be->dev = dev;
326         dev_set_drvdata(&dev->dev, be);
327
328         be->blkif = blkif_alloc(dev->otherend_id);
329         if (IS_ERR(be->blkif)) {
330                 err = PTR_ERR(be->blkif);
331                 be->blkif = NULL;
332                 xenbus_dev_fatal(dev, err, "creating block interface");
333                 goto fail;
334         }
335
336         /* setup back pointer */
337         be->blkif->be = be;
338
339         err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
340                                  &be->backend_watch, backend_changed);
341         if (err)
342                 goto fail;
343
344         err = xenbus_switch_state(dev, XenbusStateInitWait);
345         if (err)
346                 goto fail;
347
348         return 0;
349
350 fail:
351         DPRINTK("failed");
352         blkback_remove(dev);
353         return err;
354 }
355
356
357 /**
358  * Callback received when the hotplug scripts have placed the physical-device
359  * node.  Read it and the mode node, and create a vbd.  If the frontend is
360  * ready, connect.
361  */
362 static void backend_changed(struct xenbus_watch *watch,
363                             const char **vec, unsigned int len)
364 {
365         int err;
366         unsigned major;
367         unsigned minor;
368         struct backend_info *be
369                 = container_of(watch, struct backend_info, backend_watch);
370         struct xenbus_device *dev = be->dev;
371         int cdrom = 0;
372         char *device_type;
373
374         DPRINTK("");
375
376         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
377                            &major, &minor);
378         if (XENBUS_EXIST_ERR(err)) {
379                 /* Since this watch will fire once immediately after it is
380                    registered, we expect this.  Ignore it, and wait for the
381                    hotplug scripts. */
382                 return;
383         }
384         if (err != 2) {
385                 xenbus_dev_fatal(dev, err, "reading physical-device");
386                 return;
387         }
388
389         if ((be->major || be->minor) &&
390             ((be->major != major) || (be->minor != minor))) {
391                 pr_warning("blkback: changing physical device (from %x:%x to"
392                            " %x:%x) not supported\n", be->major, be->minor,
393                            major, minor);
394                 return;
395         }
396
397         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
398         if (IS_ERR(be->mode)) {
399                 err = PTR_ERR(be->mode);
400                 be->mode = NULL;
401                 xenbus_dev_fatal(dev, err, "reading mode");
402                 return;
403         }
404
405         device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
406         if (!IS_ERR(device_type)) {
407                 cdrom = strcmp(device_type, "cdrom") == 0;
408                 kfree(device_type);
409         }
410
411         if (be->major == 0 && be->minor == 0) {
412                 /* Front end dir is a number, which is used as the handle. */
413
414                 char *p = strrchr(dev->otherend, '/') + 1;
415                 long handle = simple_strtoul(p, NULL, 0);
416
417                 be->major = major;
418                 be->minor = minor;
419
420                 err = vbd_create(be->blkif, handle, major, minor,
421                                  FMODE_READ
422                                  | (strchr(be->mode, 'w') ? FMODE_WRITE : 0)
423                                  | (strchr(be->mode, '!') ? 0 : FMODE_EXCL),
424                                  cdrom);
425                 switch (err) {
426                 case -ENOMEDIUM:
427                         if (be->blkif->vbd.type
428                             & (VDISK_CDROM | VDISK_REMOVABLE))
429                 case 0:
430                                 break;
431                 default:
432                         be->major = be->minor = 0;
433                         xenbus_dev_fatal(dev, err, "creating vbd structure");
434                         return;
435                 }
436
437                 err = xenvbd_sysfs_addif(dev);
438                 if (err) {
439                         vbd_free(&be->blkif->vbd);
440                         be->major = be->minor = 0;
441                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
442                         return;
443                 }
444
445                 /* We're potentially connected now */
446                 update_blkif_status(be->blkif);
447
448                 /* Add watch for cdrom media status if necessay */
449                 cdrom_add_media_watch(be);
450         }
451 }
452
453
454 /**
455  * Callback received when the frontend's state changes.
456  */
457 static void frontend_changed(struct xenbus_device *dev,
458                              enum xenbus_state frontend_state)
459 {
460         struct backend_info *be = dev_get_drvdata(&dev->dev);
461         int err;
462
463         DPRINTK("%s", xenbus_strstate(frontend_state));
464
465         switch (frontend_state) {
466         case XenbusStateInitialising:
467                 if (dev->state == XenbusStateClosed) {
468                         pr_info("%s: %s: prepare for reconnect\n",
469                                 __FUNCTION__, dev->nodename);
470                         xenbus_switch_state(dev, XenbusStateInitWait);
471                 }
472                 break;
473
474         case XenbusStateInitialised:
475         case XenbusStateConnected:
476                 /* Ensure we connect even when two watches fire in 
477                    close successsion and we miss the intermediate value 
478                    of frontend_state. */
479                 if (dev->state == XenbusStateConnected)
480                         break;
481
482                 /* Enforce precondition before potential leak point.
483                  * blkif_disconnect() is idempotent.
484                  */
485                 blkif_disconnect(be->blkif);
486
487                 err = connect_ring(be);
488                 if (err)
489                         break;
490                 if (be->blkif->vbd.bdev)
491                         update_blkif_status(be->blkif);
492                 break;
493
494         case XenbusStateClosing:
495                 blkif_disconnect(be->blkif);
496                 xenbus_switch_state(dev, XenbusStateClosing);
497                 break;
498
499         case XenbusStateClosed:
500                 xenbus_switch_state(dev, XenbusStateClosed);
501                 if (xenbus_dev_is_online(dev))
502                         break;
503                 /* fall through if not online */
504         case XenbusStateUnknown:
505                 /* implies blkif_disconnect() via blkback_remove() */
506                 device_unregister(&dev->dev);
507                 break;
508
509         default:
510                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
511                                  frontend_state);
512                 break;
513         }
514 }
515
516
517 /* ** Connection ** */
518
519
520 /**
521  * Write the physical details regarding the block device to the store, and
522  * switch to Connected state.
523  */
524 static void connect(struct backend_info *be)
525 {
526         struct xenbus_transaction xbt;
527         int err;
528         struct xenbus_device *dev = be->dev;
529
530         DPRINTK("%s", dev->otherend);
531
532         /* Supply the information about the device the frontend needs */
533 again:
534         err = xenbus_transaction_start(&xbt);
535         if (err) {
536                 xenbus_dev_fatal(dev, err, "starting transaction");
537                 return;
538         }
539
540         err = blkback_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
541         if (err)
542                 goto abort;
543
544         err = blkback_barrier(xbt, be, be->blkif->vbd.flush_support);
545         if (err)
546                 goto abort;
547
548         err = xen_blkbk_discard(xbt, be);
549
550         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
551                             vbd_size(&be->blkif->vbd));
552         if (err) {
553                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
554                                  dev->nodename);
555                 goto abort;
556         }
557
558         /* FIXME: use a typename instead */
559         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
560                             be->blkif->vbd.type);
561         if (err) {
562                 xenbus_dev_fatal(dev, err, "writing %s/info",
563                                  dev->nodename);
564                 goto abort;
565         }
566         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
567                             vbd_secsize(&be->blkif->vbd));
568         if (err) {
569                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
570                                  dev->nodename);
571                 goto abort;
572         }
573
574         err = xenbus_transaction_end(xbt, 0);
575         if (err == -EAGAIN)
576                 goto again;
577         if (err)
578                 xenbus_dev_fatal(dev, err, "ending transaction");
579
580         err = xenbus_switch_state(dev, XenbusStateConnected);
581         if (err)
582                 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
583                                  dev->nodename);
584
585         return;
586  abort:
587         xenbus_transaction_end(xbt, 1);
588 }
589
590
591 static int connect_ring(struct backend_info *be)
592 {
593         struct xenbus_device *dev = be->dev;
594         unsigned int ring_ref, evtchn;
595         char *protocol;
596         int err;
597
598         DPRINTK("%s", dev->otherend);
599
600         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref,
601                             "event-channel", "%u", &evtchn, NULL);
602         if (err) {
603                 xenbus_dev_fatal(dev, err,
604                                  "reading %s/ring-ref and event-channel",
605                                  dev->otherend);
606                 return err;
607         }
608
609         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
610         err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
611                             NULL, &protocol, NULL);
612         if (err) {
613                 protocol = NULL;
614                 be->blkif->blk_protocol = xen_guest_blkif_protocol(be->blkif->domid);
615         }
616         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
617                 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
618         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
619                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
620         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
621                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
622         else {
623                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
624                 kfree(protocol);
625                 return -1;
626         }
627         pr_info("blkback: ring-ref %u, event-channel %u, protocol %d (%s)\n",
628                 ring_ref, evtchn, be->blkif->blk_protocol,
629                 protocol ?: "unspecified");
630         kfree(protocol);
631
632         /* Map the shared frame, irq etc. */
633         err = blkif_map(be->blkif, ring_ref, evtchn);
634         if (err) {
635                 xenbus_dev_fatal(dev, err, "mapping ring-ref %u port %u",
636                                  ring_ref, evtchn);
637                 return err;
638         }
639
640         return 0;
641 }
642
643
644 /* ** Driver Registration ** */
645
646
647 static const struct xenbus_device_id blkback_ids[] = {
648         { "vbd" },
649         { "" }
650 };
651
652 static DEFINE_XENBUS_DRIVER(blkback, ,
653         .probe = blkback_probe,
654         .remove = blkback_remove,
655         .otherend_changed = frontend_changed
656 );
657
658
659 void blkif_xenbus_init(void)
660 {
661         WARN_ON(xenbus_register_backend(&blkback_driver));
662 }