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