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