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