Merge branch 'for-3.2/drivers' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / drivers / block / 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 */
16
17 #include <stdarg.h>
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <xen/events.h>
21 #include <xen/grant_table.h>
22 #include "common.h"
23
24 struct backend_info {
25         struct xenbus_device    *dev;
26         struct xen_blkif        *blkif;
27         struct xenbus_watch     backend_watch;
28         unsigned                major;
29         unsigned                minor;
30         char                    *mode;
31 };
32
33 static struct kmem_cache *xen_blkif_cachep;
34 static void connect(struct backend_info *);
35 static int connect_ring(struct backend_info *);
36 static void backend_changed(struct xenbus_watch *, const char **,
37                             unsigned int);
38
39 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
40 {
41         return be->dev;
42 }
43
44 static int blkback_name(struct xen_blkif *blkif, char *buf)
45 {
46         char *devpath, *devname;
47         struct xenbus_device *dev = blkif->be->dev;
48
49         devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
50         if (IS_ERR(devpath))
51                 return PTR_ERR(devpath);
52
53         devname = strstr(devpath, "/dev/");
54         if (devname != NULL)
55                 devname += strlen("/dev/");
56         else
57                 devname  = devpath;
58
59         snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
60         kfree(devpath);
61
62         return 0;
63 }
64
65 static void xen_update_blkif_status(struct xen_blkif *blkif)
66 {
67         int err;
68         char name[TASK_COMM_LEN];
69
70         /* Not ready to connect? */
71         if (!blkif->irq || !blkif->vbd.bdev)
72                 return;
73
74         /* Already connected? */
75         if (blkif->be->dev->state == XenbusStateConnected)
76                 return;
77
78         /* Attempt to connect: exit if we fail to. */
79         connect(blkif->be);
80         if (blkif->be->dev->state != XenbusStateConnected)
81                 return;
82
83         err = blkback_name(blkif, name);
84         if (err) {
85                 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
86                 return;
87         }
88
89         err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
90         if (err) {
91                 xenbus_dev_error(blkif->be->dev, err, "block flush");
92                 return;
93         }
94         invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
95
96         blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name);
97         if (IS_ERR(blkif->xenblkd)) {
98                 err = PTR_ERR(blkif->xenblkd);
99                 blkif->xenblkd = NULL;
100                 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
101         }
102 }
103
104 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
105 {
106         struct xen_blkif *blkif;
107
108         blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL);
109         if (!blkif)
110                 return ERR_PTR(-ENOMEM);
111
112         memset(blkif, 0, sizeof(*blkif));
113         blkif->domid = domid;
114         spin_lock_init(&blkif->blk_ring_lock);
115         atomic_set(&blkif->refcnt, 1);
116         init_waitqueue_head(&blkif->wq);
117         init_completion(&blkif->drain_complete);
118         atomic_set(&blkif->drain, 0);
119         blkif->st_print = jiffies;
120         init_waitqueue_head(&blkif->waiting_to_free);
121
122         return blkif;
123 }
124
125 static int map_frontend_page(struct xen_blkif *blkif, unsigned long shared_page)
126 {
127         struct gnttab_map_grant_ref op;
128
129         gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
130                           GNTMAP_host_map, shared_page, blkif->domid);
131
132         if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
133                 BUG();
134
135         if (op.status) {
136                 DPRINTK("Grant table operation failure !\n");
137                 return op.status;
138         }
139
140         blkif->shmem_ref = shared_page;
141         blkif->shmem_handle = op.handle;
142
143         return 0;
144 }
145
146 static void unmap_frontend_page(struct xen_blkif *blkif)
147 {
148         struct gnttab_unmap_grant_ref op;
149
150         gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
151                             GNTMAP_host_map, blkif->shmem_handle);
152
153         if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
154                 BUG();
155 }
156
157 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
158                          unsigned int evtchn)
159 {
160         int err;
161
162         /* Already connected through? */
163         if (blkif->irq)
164                 return 0;
165
166         blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE);
167         if (!blkif->blk_ring_area)
168                 return -ENOMEM;
169
170         err = map_frontend_page(blkif, shared_page);
171         if (err) {
172                 free_vm_area(blkif->blk_ring_area);
173                 return err;
174         }
175
176         switch (blkif->blk_protocol) {
177         case BLKIF_PROTOCOL_NATIVE:
178         {
179                 struct blkif_sring *sring;
180                 sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
181                 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
182                 break;
183         }
184         case BLKIF_PROTOCOL_X86_32:
185         {
186                 struct blkif_x86_32_sring *sring_x86_32;
187                 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
188                 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
189                 break;
190         }
191         case BLKIF_PROTOCOL_X86_64:
192         {
193                 struct blkif_x86_64_sring *sring_x86_64;
194                 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
195                 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
196                 break;
197         }
198         default:
199                 BUG();
200         }
201
202         err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
203                                                     xen_blkif_be_int, 0,
204                                                     "blkif-backend", blkif);
205         if (err < 0) {
206                 unmap_frontend_page(blkif);
207                 free_vm_area(blkif->blk_ring_area);
208                 blkif->blk_rings.common.sring = NULL;
209                 return err;
210         }
211         blkif->irq = err;
212
213         return 0;
214 }
215
216 static void xen_blkif_disconnect(struct xen_blkif *blkif)
217 {
218         if (blkif->xenblkd) {
219                 kthread_stop(blkif->xenblkd);
220                 blkif->xenblkd = NULL;
221         }
222
223         atomic_dec(&blkif->refcnt);
224         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
225         atomic_inc(&blkif->refcnt);
226
227         if (blkif->irq) {
228                 unbind_from_irqhandler(blkif->irq, blkif);
229                 blkif->irq = 0;
230         }
231
232         if (blkif->blk_rings.common.sring) {
233                 unmap_frontend_page(blkif);
234                 free_vm_area(blkif->blk_ring_area);
235                 blkif->blk_rings.common.sring = NULL;
236         }
237 }
238
239 void xen_blkif_free(struct xen_blkif *blkif)
240 {
241         if (!atomic_dec_and_test(&blkif->refcnt))
242                 BUG();
243         kmem_cache_free(xen_blkif_cachep, blkif);
244 }
245
246 int __init xen_blkif_interface_init(void)
247 {
248         xen_blkif_cachep = kmem_cache_create("blkif_cache",
249                                              sizeof(struct xen_blkif),
250                                              0, 0, NULL);
251         if (!xen_blkif_cachep)
252                 return -ENOMEM;
253
254         return 0;
255 }
256
257 /*
258  *  sysfs interface for VBD I/O requests
259  */
260
261 #define VBD_SHOW(name, format, args...)                                 \
262         static ssize_t show_##name(struct device *_dev,                 \
263                                    struct device_attribute *attr,       \
264                                    char *buf)                           \
265         {                                                               \
266                 struct xenbus_device *dev = to_xenbus_device(_dev);     \
267                 struct backend_info *be = dev_get_drvdata(&dev->dev);   \
268                                                                         \
269                 return sprintf(buf, format, ##args);                    \
270         }                                                               \
271         static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
272
273 VBD_SHOW(oo_req,  "%d\n", be->blkif->st_oo_req);
274 VBD_SHOW(rd_req,  "%d\n", be->blkif->st_rd_req);
275 VBD_SHOW(wr_req,  "%d\n", be->blkif->st_wr_req);
276 VBD_SHOW(f_req,  "%d\n", be->blkif->st_f_req);
277 VBD_SHOW(ds_req,  "%d\n", be->blkif->st_ds_req);
278 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
279 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
280
281 static struct attribute *xen_vbdstat_attrs[] = {
282         &dev_attr_oo_req.attr,
283         &dev_attr_rd_req.attr,
284         &dev_attr_wr_req.attr,
285         &dev_attr_f_req.attr,
286         &dev_attr_ds_req.attr,
287         &dev_attr_rd_sect.attr,
288         &dev_attr_wr_sect.attr,
289         NULL
290 };
291
292 static struct attribute_group xen_vbdstat_group = {
293         .name = "statistics",
294         .attrs = xen_vbdstat_attrs,
295 };
296
297 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
298 VBD_SHOW(mode, "%s\n", be->mode);
299
300 int xenvbd_sysfs_addif(struct xenbus_device *dev)
301 {
302         int error;
303
304         error = device_create_file(&dev->dev, &dev_attr_physical_device);
305         if (error)
306                 goto fail1;
307
308         error = device_create_file(&dev->dev, &dev_attr_mode);
309         if (error)
310                 goto fail2;
311
312         error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
313         if (error)
314                 goto fail3;
315
316         return 0;
317
318 fail3:  sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
319 fail2:  device_remove_file(&dev->dev, &dev_attr_mode);
320 fail1:  device_remove_file(&dev->dev, &dev_attr_physical_device);
321         return error;
322 }
323
324 void xenvbd_sysfs_delif(struct xenbus_device *dev)
325 {
326         sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
327         device_remove_file(&dev->dev, &dev_attr_mode);
328         device_remove_file(&dev->dev, &dev_attr_physical_device);
329 }
330
331
332 static void xen_vbd_free(struct xen_vbd *vbd)
333 {
334         if (vbd->bdev)
335                 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
336         vbd->bdev = NULL;
337 }
338
339 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
340                           unsigned major, unsigned minor, int readonly,
341                           int cdrom)
342 {
343         struct xen_vbd *vbd;
344         struct block_device *bdev;
345         struct request_queue *q;
346
347         vbd = &blkif->vbd;
348         vbd->handle   = handle;
349         vbd->readonly = readonly;
350         vbd->type     = 0;
351
352         vbd->pdevice  = MKDEV(major, minor);
353
354         bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
355                                  FMODE_READ : FMODE_WRITE, NULL);
356
357         if (IS_ERR(bdev)) {
358                 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
359                         vbd->pdevice);
360                 return -ENOENT;
361         }
362
363         vbd->bdev = bdev;
364         if (vbd->bdev->bd_disk == NULL) {
365                 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
366                         vbd->pdevice);
367                 xen_vbd_free(vbd);
368                 return -ENOENT;
369         }
370         vbd->size = vbd_sz(vbd);
371
372         if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
373                 vbd->type |= VDISK_CDROM;
374         if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
375                 vbd->type |= VDISK_REMOVABLE;
376
377         q = bdev_get_queue(bdev);
378         if (q && q->flush_flags)
379                 vbd->flush_support = true;
380
381         DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
382                 handle, blkif->domid);
383         return 0;
384 }
385 static int xen_blkbk_remove(struct xenbus_device *dev)
386 {
387         struct backend_info *be = dev_get_drvdata(&dev->dev);
388
389         DPRINTK("");
390
391         if (be->major || be->minor)
392                 xenvbd_sysfs_delif(dev);
393
394         if (be->backend_watch.node) {
395                 unregister_xenbus_watch(&be->backend_watch);
396                 kfree(be->backend_watch.node);
397                 be->backend_watch.node = NULL;
398         }
399
400         if (be->blkif) {
401                 xen_blkif_disconnect(be->blkif);
402                 xen_vbd_free(&be->blkif->vbd);
403                 xen_blkif_free(be->blkif);
404                 be->blkif = NULL;
405         }
406
407         kfree(be);
408         dev_set_drvdata(&dev->dev, NULL);
409         return 0;
410 }
411
412 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
413                               struct backend_info *be, int state)
414 {
415         struct xenbus_device *dev = be->dev;
416         int err;
417
418         err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
419                             "%d", state);
420         if (err)
421                 xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
422
423         return err;
424 }
425
426 int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
427 {
428         struct xenbus_device *dev = be->dev;
429         struct xen_blkif *blkif = be->blkif;
430         char *type;
431         int err;
432         int state = 0;
433
434         type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL);
435         if (!IS_ERR(type)) {
436                 if (strncmp(type, "file", 4) == 0) {
437                         state = 1;
438                         blkif->blk_backend_type = BLKIF_BACKEND_FILE;
439                 }
440                 if (strncmp(type, "phy", 3) == 0) {
441                         struct block_device *bdev = be->blkif->vbd.bdev;
442                         struct request_queue *q = bdev_get_queue(bdev);
443                         if (blk_queue_discard(q)) {
444                                 err = xenbus_printf(xbt, dev->nodename,
445                                         "discard-granularity", "%u",
446                                         q->limits.discard_granularity);
447                                 if (err) {
448                                         xenbus_dev_fatal(dev, err,
449                                                 "writing discard-granularity");
450                                         goto kfree;
451                                 }
452                                 err = xenbus_printf(xbt, dev->nodename,
453                                         "discard-alignment", "%u",
454                                         q->limits.discard_alignment);
455                                 if (err) {
456                                         xenbus_dev_fatal(dev, err,
457                                                 "writing discard-alignment");
458                                         goto kfree;
459                                 }
460                                 state = 1;
461                                 blkif->blk_backend_type = BLKIF_BACKEND_PHY;
462                         }
463                 }
464         } else {
465                 err = PTR_ERR(type);
466                 xenbus_dev_fatal(dev, err, "reading type");
467                 goto out;
468         }
469
470         err = xenbus_printf(xbt, dev->nodename, "feature-discard",
471                             "%d", state);
472         if (err)
473                 xenbus_dev_fatal(dev, err, "writing feature-discard");
474 kfree:
475         kfree(type);
476 out:
477         return err;
478 }
479 int xen_blkbk_barrier(struct xenbus_transaction xbt,
480                       struct backend_info *be, int state)
481 {
482         struct xenbus_device *dev = be->dev;
483         int err;
484
485         err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
486                             "%d", state);
487         if (err)
488                 xenbus_dev_fatal(dev, err, "writing feature-barrier");
489
490         return err;
491 }
492
493 /*
494  * Entry point to this code when a new device is created.  Allocate the basic
495  * structures, and watch the store waiting for the hotplug scripts to tell us
496  * the device's physical major and minor numbers.  Switch to InitWait.
497  */
498 static int xen_blkbk_probe(struct xenbus_device *dev,
499                            const struct xenbus_device_id *id)
500 {
501         int err;
502         struct backend_info *be = kzalloc(sizeof(struct backend_info),
503                                           GFP_KERNEL);
504         if (!be) {
505                 xenbus_dev_fatal(dev, -ENOMEM,
506                                  "allocating backend structure");
507                 return -ENOMEM;
508         }
509         be->dev = dev;
510         dev_set_drvdata(&dev->dev, be);
511
512         be->blkif = xen_blkif_alloc(dev->otherend_id);
513         if (IS_ERR(be->blkif)) {
514                 err = PTR_ERR(be->blkif);
515                 be->blkif = NULL;
516                 xenbus_dev_fatal(dev, err, "creating block interface");
517                 goto fail;
518         }
519
520         /* setup back pointer */
521         be->blkif->be = be;
522
523         err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
524                                    "%s/%s", dev->nodename, "physical-device");
525         if (err)
526                 goto fail;
527
528         err = xenbus_switch_state(dev, XenbusStateInitWait);
529         if (err)
530                 goto fail;
531
532         return 0;
533
534 fail:
535         DPRINTK("failed");
536         xen_blkbk_remove(dev);
537         return err;
538 }
539
540
541 /*
542  * Callback received when the hotplug scripts have placed the physical-device
543  * node.  Read it and the mode node, and create a vbd.  If the frontend is
544  * ready, connect.
545  */
546 static void backend_changed(struct xenbus_watch *watch,
547                             const char **vec, unsigned int len)
548 {
549         int err;
550         unsigned major;
551         unsigned minor;
552         struct backend_info *be
553                 = container_of(watch, struct backend_info, backend_watch);
554         struct xenbus_device *dev = be->dev;
555         int cdrom = 0;
556         char *device_type;
557
558         DPRINTK("");
559
560         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
561                            &major, &minor);
562         if (XENBUS_EXIST_ERR(err)) {
563                 /*
564                  * Since this watch will fire once immediately after it is
565                  * registered, we expect this.  Ignore it, and wait for the
566                  * hotplug scripts.
567                  */
568                 return;
569         }
570         if (err != 2) {
571                 xenbus_dev_fatal(dev, err, "reading physical-device");
572                 return;
573         }
574
575         if ((be->major || be->minor) &&
576             ((be->major != major) || (be->minor != minor))) {
577                 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
578                         be->major, be->minor, major, minor);
579                 return;
580         }
581
582         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
583         if (IS_ERR(be->mode)) {
584                 err = PTR_ERR(be->mode);
585                 be->mode = NULL;
586                 xenbus_dev_fatal(dev, err, "reading mode");
587                 return;
588         }
589
590         device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
591         if (!IS_ERR(device_type)) {
592                 cdrom = strcmp(device_type, "cdrom") == 0;
593                 kfree(device_type);
594         }
595
596         if (be->major == 0 && be->minor == 0) {
597                 /* Front end dir is a number, which is used as the handle. */
598
599                 char *p = strrchr(dev->otherend, '/') + 1;
600                 long handle;
601                 err = strict_strtoul(p, 0, &handle);
602                 if (err)
603                         return;
604
605                 be->major = major;
606                 be->minor = minor;
607
608                 err = xen_vbd_create(be->blkif, handle, major, minor,
609                                  (NULL == strchr(be->mode, 'w')), cdrom);
610                 if (err) {
611                         be->major = 0;
612                         be->minor = 0;
613                         xenbus_dev_fatal(dev, err, "creating vbd structure");
614                         return;
615                 }
616
617                 err = xenvbd_sysfs_addif(dev);
618                 if (err) {
619                         xen_vbd_free(&be->blkif->vbd);
620                         be->major = 0;
621                         be->minor = 0;
622                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
623                         return;
624                 }
625
626                 /* We're potentially connected now */
627                 xen_update_blkif_status(be->blkif);
628         }
629 }
630
631
632 /*
633  * Callback received when the frontend's state changes.
634  */
635 static void frontend_changed(struct xenbus_device *dev,
636                              enum xenbus_state frontend_state)
637 {
638         struct backend_info *be = dev_get_drvdata(&dev->dev);
639         int err;
640
641         DPRINTK("%s", xenbus_strstate(frontend_state));
642
643         switch (frontend_state) {
644         case XenbusStateInitialising:
645                 if (dev->state == XenbusStateClosed) {
646                         pr_info(DRV_PFX "%s: prepare for reconnect\n",
647                                 dev->nodename);
648                         xenbus_switch_state(dev, XenbusStateInitWait);
649                 }
650                 break;
651
652         case XenbusStateInitialised:
653         case XenbusStateConnected:
654                 /*
655                  * Ensure we connect even when two watches fire in
656                  * close successsion and we miss the intermediate value
657                  * of frontend_state.
658                  */
659                 if (dev->state == XenbusStateConnected)
660                         break;
661
662                 /*
663                  * Enforce precondition before potential leak point.
664                  * xen_blkif_disconnect() is idempotent.
665                  */
666                 xen_blkif_disconnect(be->blkif);
667
668                 err = connect_ring(be);
669                 if (err)
670                         break;
671                 xen_update_blkif_status(be->blkif);
672                 break;
673
674         case XenbusStateClosing:
675                 xenbus_switch_state(dev, XenbusStateClosing);
676                 break;
677
678         case XenbusStateClosed:
679                 xen_blkif_disconnect(be->blkif);
680                 xenbus_switch_state(dev, XenbusStateClosed);
681                 if (xenbus_dev_is_online(dev))
682                         break;
683                 /* fall through if not online */
684         case XenbusStateUnknown:
685                 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
686                 device_unregister(&dev->dev);
687                 break;
688
689         default:
690                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
691                                  frontend_state);
692                 break;
693         }
694 }
695
696
697 /* ** Connection ** */
698
699
700 /*
701  * Write the physical details regarding the block device to the store, and
702  * switch to Connected state.
703  */
704 static void connect(struct backend_info *be)
705 {
706         struct xenbus_transaction xbt;
707         int err;
708         struct xenbus_device *dev = be->dev;
709
710         DPRINTK("%s", dev->otherend);
711
712         /* Supply the information about the device the frontend needs */
713 again:
714         err = xenbus_transaction_start(&xbt);
715         if (err) {
716                 xenbus_dev_fatal(dev, err, "starting transaction");
717                 return;
718         }
719
720         err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
721         if (err)
722                 goto abort;
723
724         err = xen_blkbk_discard(xbt, be);
725
726         /* If we can't advertise it is OK. */
727         err = xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
728
729         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
730                             (unsigned long long)vbd_sz(&be->blkif->vbd));
731         if (err) {
732                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
733                                  dev->nodename);
734                 goto abort;
735         }
736
737         /* FIXME: use a typename instead */
738         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
739                             be->blkif->vbd.type |
740                             (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
741         if (err) {
742                 xenbus_dev_fatal(dev, err, "writing %s/info",
743                                  dev->nodename);
744                 goto abort;
745         }
746         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
747                             (unsigned long)
748                             bdev_logical_block_size(be->blkif->vbd.bdev));
749         if (err) {
750                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
751                                  dev->nodename);
752                 goto abort;
753         }
754
755         err = xenbus_transaction_end(xbt, 0);
756         if (err == -EAGAIN)
757                 goto again;
758         if (err)
759                 xenbus_dev_fatal(dev, err, "ending transaction");
760
761         err = xenbus_switch_state(dev, XenbusStateConnected);
762         if (err)
763                 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
764                                  dev->nodename);
765
766         return;
767  abort:
768         xenbus_transaction_end(xbt, 1);
769 }
770
771
772 static int connect_ring(struct backend_info *be)
773 {
774         struct xenbus_device *dev = be->dev;
775         unsigned long ring_ref;
776         unsigned int evtchn;
777         char protocol[64] = "";
778         int err;
779
780         DPRINTK("%s", dev->otherend);
781
782         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
783                             &ring_ref, "event-channel", "%u", &evtchn, NULL);
784         if (err) {
785                 xenbus_dev_fatal(dev, err,
786                                  "reading %s/ring-ref and event-channel",
787                                  dev->otherend);
788                 return err;
789         }
790
791         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
792         err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
793                             "%63s", protocol, NULL);
794         if (err)
795                 strcpy(protocol, "unspecified, assuming native");
796         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
797                 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
798         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
799                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
800         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
801                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
802         else {
803                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
804                 return -1;
805         }
806         pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n",
807                 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
808
809         /* Map the shared frame, irq etc. */
810         err = xen_blkif_map(be->blkif, ring_ref, evtchn);
811         if (err) {
812                 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
813                                  ring_ref, evtchn);
814                 return err;
815         }
816
817         return 0;
818 }
819
820
821 /* ** Driver Registration ** */
822
823
824 static const struct xenbus_device_id xen_blkbk_ids[] = {
825         { "vbd" },
826         { "" }
827 };
828
829
830 static struct xenbus_driver xen_blkbk = {
831         .name = "vbd",
832         .owner = THIS_MODULE,
833         .ids = xen_blkbk_ids,
834         .probe = xen_blkbk_probe,
835         .remove = xen_blkbk_remove,
836         .otherend_changed = frontend_changed
837 };
838
839
840 int xen_blkif_xenbus_init(void)
841 {
842         return xenbus_register_backend(&xen_blkbk);
843 }