Linux 4.19.133
[linux/fpc-iii.git] / drivers / block / xen-blkback / xenbus.c
blob25c41ce070a7feb607eadabb84e52d5bda506fd2
1 /* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
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.
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.
17 #define pr_fmt(fmt) "xen-blkback: " fmt
19 #include <stdarg.h>
20 #include <linux/module.h>
21 #include <linux/kthread.h>
22 #include <xen/events.h>
23 #include <xen/grant_table.h>
24 #include "common.h"
26 /* On the XenBus the max length of 'ring-ref%u'. */
27 #define RINGREF_NAME_LEN (20)
29 struct backend_info {
30 struct xenbus_device *dev;
31 struct xen_blkif *blkif;
32 struct xenbus_watch backend_watch;
33 unsigned major;
34 unsigned minor;
35 char *mode;
38 static struct kmem_cache *xen_blkif_cachep;
39 static void connect(struct backend_info *);
40 static int connect_ring(struct backend_info *);
41 static void backend_changed(struct xenbus_watch *, const char *,
42 const char *);
43 static void xen_blkif_free(struct xen_blkif *blkif);
44 static void xen_vbd_free(struct xen_vbd *vbd);
46 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
48 return be->dev;
52 * The last request could free the device from softirq context and
53 * xen_blkif_free() can sleep.
55 static void xen_blkif_deferred_free(struct work_struct *work)
57 struct xen_blkif *blkif;
59 blkif = container_of(work, struct xen_blkif, free_work);
60 xen_blkif_free(blkif);
63 static int blkback_name(struct xen_blkif *blkif, char *buf)
65 char *devpath, *devname;
66 struct xenbus_device *dev = blkif->be->dev;
68 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
69 if (IS_ERR(devpath))
70 return PTR_ERR(devpath);
72 devname = strstr(devpath, "/dev/");
73 if (devname != NULL)
74 devname += strlen("/dev/");
75 else
76 devname = devpath;
78 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
79 kfree(devpath);
81 return 0;
84 static void xen_update_blkif_status(struct xen_blkif *blkif)
86 int err;
87 char name[TASK_COMM_LEN];
88 struct xen_blkif_ring *ring;
89 int i;
91 /* Not ready to connect? */
92 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
93 return;
95 /* Already connected? */
96 if (blkif->be->dev->state == XenbusStateConnected)
97 return;
99 /* Attempt to connect: exit if we fail to. */
100 connect(blkif->be);
101 if (blkif->be->dev->state != XenbusStateConnected)
102 return;
104 err = blkback_name(blkif, name);
105 if (err) {
106 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
107 return;
110 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
111 if (err) {
112 xenbus_dev_error(blkif->be->dev, err, "block flush");
113 return;
115 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
117 for (i = 0; i < blkif->nr_rings; i++) {
118 ring = &blkif->rings[i];
119 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
120 if (IS_ERR(ring->xenblkd)) {
121 err = PTR_ERR(ring->xenblkd);
122 ring->xenblkd = NULL;
123 xenbus_dev_fatal(blkif->be->dev, err,
124 "start %s-%d xenblkd", name, i);
125 goto out;
128 return;
130 out:
131 while (--i >= 0) {
132 ring = &blkif->rings[i];
133 kthread_stop(ring->xenblkd);
135 return;
138 static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
140 unsigned int r;
142 blkif->rings = kcalloc(blkif->nr_rings, sizeof(struct xen_blkif_ring),
143 GFP_KERNEL);
144 if (!blkif->rings)
145 return -ENOMEM;
147 for (r = 0; r < blkif->nr_rings; r++) {
148 struct xen_blkif_ring *ring = &blkif->rings[r];
150 spin_lock_init(&ring->blk_ring_lock);
151 init_waitqueue_head(&ring->wq);
152 INIT_LIST_HEAD(&ring->pending_free);
153 INIT_LIST_HEAD(&ring->persistent_purge_list);
154 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
155 spin_lock_init(&ring->free_pages_lock);
156 INIT_LIST_HEAD(&ring->free_pages);
158 spin_lock_init(&ring->pending_free_lock);
159 init_waitqueue_head(&ring->pending_free_wq);
160 init_waitqueue_head(&ring->shutdown_wq);
161 ring->blkif = blkif;
162 ring->st_print = jiffies;
163 ring->active = true;
166 return 0;
169 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
171 struct xen_blkif *blkif;
173 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
175 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
176 if (!blkif)
177 return ERR_PTR(-ENOMEM);
179 blkif->domid = domid;
180 atomic_set(&blkif->refcnt, 1);
181 init_completion(&blkif->drain_complete);
184 * Because freeing back to the cache may be deferred, it is not
185 * safe to unload the module (and hence destroy the cache) until
186 * this has completed. To prevent premature unloading, take an
187 * extra module reference here and release only when the object
188 * has been freed back to the cache.
190 __module_get(THIS_MODULE);
191 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
193 return blkif;
196 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
197 unsigned int nr_grefs, unsigned int evtchn)
199 int err;
200 struct xen_blkif *blkif = ring->blkif;
202 /* Already connected through? */
203 if (ring->irq)
204 return 0;
206 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
207 &ring->blk_ring);
208 if (err < 0)
209 return err;
211 switch (blkif->blk_protocol) {
212 case BLKIF_PROTOCOL_NATIVE:
214 struct blkif_sring *sring;
215 sring = (struct blkif_sring *)ring->blk_ring;
216 BACK_RING_INIT(&ring->blk_rings.native, sring,
217 XEN_PAGE_SIZE * nr_grefs);
218 break;
220 case BLKIF_PROTOCOL_X86_32:
222 struct blkif_x86_32_sring *sring_x86_32;
223 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
224 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
225 XEN_PAGE_SIZE * nr_grefs);
226 break;
228 case BLKIF_PROTOCOL_X86_64:
230 struct blkif_x86_64_sring *sring_x86_64;
231 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
232 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
233 XEN_PAGE_SIZE * nr_grefs);
234 break;
236 default:
237 BUG();
240 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
241 xen_blkif_be_int, 0,
242 "blkif-backend", ring);
243 if (err < 0) {
244 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
245 ring->blk_rings.common.sring = NULL;
246 return err;
248 ring->irq = err;
250 return 0;
253 static int xen_blkif_disconnect(struct xen_blkif *blkif)
255 struct pending_req *req, *n;
256 unsigned int j, r;
257 bool busy = false;
259 for (r = 0; r < blkif->nr_rings; r++) {
260 struct xen_blkif_ring *ring = &blkif->rings[r];
261 unsigned int i = 0;
263 if (!ring->active)
264 continue;
266 if (ring->xenblkd) {
267 kthread_stop(ring->xenblkd);
268 wake_up(&ring->shutdown_wq);
271 /* The above kthread_stop() guarantees that at this point we
272 * don't have any discard_io or other_io requests. So, checking
273 * for inflight IO is enough.
275 if (atomic_read(&ring->inflight) > 0) {
276 busy = true;
277 continue;
280 if (ring->irq) {
281 unbind_from_irqhandler(ring->irq, ring);
282 ring->irq = 0;
285 if (ring->blk_rings.common.sring) {
286 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
287 ring->blk_rings.common.sring = NULL;
290 /* Remove all persistent grants and the cache of ballooned pages. */
291 xen_blkbk_free_caches(ring);
293 /* Check that there is no request in use */
294 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
295 list_del(&req->free_list);
297 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
298 kfree(req->segments[j]);
300 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
301 kfree(req->indirect_pages[j]);
303 kfree(req);
304 i++;
307 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
308 BUG_ON(!list_empty(&ring->persistent_purge_list));
309 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
310 BUG_ON(!list_empty(&ring->free_pages));
311 BUG_ON(ring->free_pages_num != 0);
312 BUG_ON(ring->persistent_gnt_c != 0);
313 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
314 ring->active = false;
316 if (busy)
317 return -EBUSY;
319 blkif->nr_ring_pages = 0;
321 * blkif->rings was allocated in connect_ring, so we should free it in
322 * here.
324 kfree(blkif->rings);
325 blkif->rings = NULL;
326 blkif->nr_rings = 0;
328 return 0;
331 static void xen_blkif_free(struct xen_blkif *blkif)
333 WARN_ON(xen_blkif_disconnect(blkif));
334 xen_vbd_free(&blkif->vbd);
335 kfree(blkif->be->mode);
336 kfree(blkif->be);
338 /* Make sure everything is drained before shutting down */
339 kmem_cache_free(xen_blkif_cachep, blkif);
340 module_put(THIS_MODULE);
343 int __init xen_blkif_interface_init(void)
345 xen_blkif_cachep = kmem_cache_create("blkif_cache",
346 sizeof(struct xen_blkif),
347 0, 0, NULL);
348 if (!xen_blkif_cachep)
349 return -ENOMEM;
351 return 0;
355 * sysfs interface for VBD I/O requests
358 #define VBD_SHOW_ALLRING(name, format) \
359 static ssize_t show_##name(struct device *_dev, \
360 struct device_attribute *attr, \
361 char *buf) \
363 struct xenbus_device *dev = to_xenbus_device(_dev); \
364 struct backend_info *be = dev_get_drvdata(&dev->dev); \
365 struct xen_blkif *blkif = be->blkif; \
366 unsigned int i; \
367 unsigned long long result = 0; \
369 if (!blkif->rings) \
370 goto out; \
372 for (i = 0; i < blkif->nr_rings; i++) { \
373 struct xen_blkif_ring *ring = &blkif->rings[i]; \
375 result += ring->st_##name; \
378 out: \
379 return sprintf(buf, format, result); \
381 static DEVICE_ATTR(name, 0444, show_##name, NULL)
383 VBD_SHOW_ALLRING(oo_req, "%llu\n");
384 VBD_SHOW_ALLRING(rd_req, "%llu\n");
385 VBD_SHOW_ALLRING(wr_req, "%llu\n");
386 VBD_SHOW_ALLRING(f_req, "%llu\n");
387 VBD_SHOW_ALLRING(ds_req, "%llu\n");
388 VBD_SHOW_ALLRING(rd_sect, "%llu\n");
389 VBD_SHOW_ALLRING(wr_sect, "%llu\n");
391 static struct attribute *xen_vbdstat_attrs[] = {
392 &dev_attr_oo_req.attr,
393 &dev_attr_rd_req.attr,
394 &dev_attr_wr_req.attr,
395 &dev_attr_f_req.attr,
396 &dev_attr_ds_req.attr,
397 &dev_attr_rd_sect.attr,
398 &dev_attr_wr_sect.attr,
399 NULL
402 static const struct attribute_group xen_vbdstat_group = {
403 .name = "statistics",
404 .attrs = xen_vbdstat_attrs,
407 #define VBD_SHOW(name, format, args...) \
408 static ssize_t show_##name(struct device *_dev, \
409 struct device_attribute *attr, \
410 char *buf) \
412 struct xenbus_device *dev = to_xenbus_device(_dev); \
413 struct backend_info *be = dev_get_drvdata(&dev->dev); \
415 return sprintf(buf, format, ##args); \
417 static DEVICE_ATTR(name, 0444, show_##name, NULL)
419 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
420 VBD_SHOW(mode, "%s\n", be->mode);
422 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
424 int error;
426 error = device_create_file(&dev->dev, &dev_attr_physical_device);
427 if (error)
428 goto fail1;
430 error = device_create_file(&dev->dev, &dev_attr_mode);
431 if (error)
432 goto fail2;
434 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
435 if (error)
436 goto fail3;
438 return 0;
440 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
441 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
442 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
443 return error;
446 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
448 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
449 device_remove_file(&dev->dev, &dev_attr_mode);
450 device_remove_file(&dev->dev, &dev_attr_physical_device);
454 static void xen_vbd_free(struct xen_vbd *vbd)
456 if (vbd->bdev)
457 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
458 vbd->bdev = NULL;
461 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
462 unsigned major, unsigned minor, int readonly,
463 int cdrom)
465 struct xen_vbd *vbd;
466 struct block_device *bdev;
467 struct request_queue *q;
469 vbd = &blkif->vbd;
470 vbd->handle = handle;
471 vbd->readonly = readonly;
472 vbd->type = 0;
474 vbd->pdevice = MKDEV(major, minor);
476 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
477 FMODE_READ : FMODE_WRITE, NULL);
479 if (IS_ERR(bdev)) {
480 pr_warn("xen_vbd_create: device %08x could not be opened\n",
481 vbd->pdevice);
482 return -ENOENT;
485 vbd->bdev = bdev;
486 if (vbd->bdev->bd_disk == NULL) {
487 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
488 vbd->pdevice);
489 xen_vbd_free(vbd);
490 return -ENOENT;
492 vbd->size = vbd_sz(vbd);
494 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
495 vbd->type |= VDISK_CDROM;
496 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
497 vbd->type |= VDISK_REMOVABLE;
499 q = bdev_get_queue(bdev);
500 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
501 vbd->flush_support = true;
503 if (q && blk_queue_secure_erase(q))
504 vbd->discard_secure = true;
506 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
507 handle, blkif->domid);
508 return 0;
510 static int xen_blkbk_remove(struct xenbus_device *dev)
512 struct backend_info *be = dev_get_drvdata(&dev->dev);
514 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
516 if (be->major || be->minor)
517 xenvbd_sysfs_delif(dev);
519 if (be->backend_watch.node) {
520 unregister_xenbus_watch(&be->backend_watch);
521 kfree(be->backend_watch.node);
522 be->backend_watch.node = NULL;
525 dev_set_drvdata(&dev->dev, NULL);
527 if (be->blkif) {
528 xen_blkif_disconnect(be->blkif);
530 /* Put the reference we set in xen_blkif_alloc(). */
531 xen_blkif_put(be->blkif);
534 return 0;
537 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
538 struct backend_info *be, int state)
540 struct xenbus_device *dev = be->dev;
541 int err;
543 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
544 "%d", state);
545 if (err)
546 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
548 return err;
551 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
553 struct xenbus_device *dev = be->dev;
554 struct xen_blkif *blkif = be->blkif;
555 int err;
556 int state = 0;
557 struct block_device *bdev = be->blkif->vbd.bdev;
558 struct request_queue *q = bdev_get_queue(bdev);
560 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
561 return;
563 if (blk_queue_discard(q)) {
564 err = xenbus_printf(xbt, dev->nodename,
565 "discard-granularity", "%u",
566 q->limits.discard_granularity);
567 if (err) {
568 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
569 return;
571 err = xenbus_printf(xbt, dev->nodename,
572 "discard-alignment", "%u",
573 q->limits.discard_alignment);
574 if (err) {
575 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
576 return;
578 state = 1;
579 /* Optional. */
580 err = xenbus_printf(xbt, dev->nodename,
581 "discard-secure", "%d",
582 blkif->vbd.discard_secure);
583 if (err) {
584 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
585 return;
588 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
589 "%d", state);
590 if (err)
591 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
593 int xen_blkbk_barrier(struct xenbus_transaction xbt,
594 struct backend_info *be, int state)
596 struct xenbus_device *dev = be->dev;
597 int err;
599 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
600 "%d", state);
601 if (err)
602 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
604 return err;
608 * Entry point to this code when a new device is created. Allocate the basic
609 * structures, and watch the store waiting for the hotplug scripts to tell us
610 * the device's physical major and minor numbers. Switch to InitWait.
612 static int xen_blkbk_probe(struct xenbus_device *dev,
613 const struct xenbus_device_id *id)
615 int err;
616 struct backend_info *be = kzalloc(sizeof(struct backend_info),
617 GFP_KERNEL);
619 /* match the pr_debug in xen_blkbk_remove */
620 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
622 if (!be) {
623 xenbus_dev_fatal(dev, -ENOMEM,
624 "allocating backend structure");
625 return -ENOMEM;
627 be->dev = dev;
628 dev_set_drvdata(&dev->dev, be);
630 be->blkif = xen_blkif_alloc(dev->otherend_id);
631 if (IS_ERR(be->blkif)) {
632 err = PTR_ERR(be->blkif);
633 be->blkif = NULL;
634 xenbus_dev_fatal(dev, err, "creating block interface");
635 goto fail;
638 err = xenbus_printf(XBT_NIL, dev->nodename,
639 "feature-max-indirect-segments", "%u",
640 MAX_INDIRECT_SEGMENTS);
641 if (err)
642 dev_warn(&dev->dev,
643 "writing %s/feature-max-indirect-segments (%d)",
644 dev->nodename, err);
646 /* Multi-queue: advertise how many queues are supported by us.*/
647 err = xenbus_printf(XBT_NIL, dev->nodename,
648 "multi-queue-max-queues", "%u", xenblk_max_queues);
649 if (err)
650 pr_warn("Error writing multi-queue-max-queues\n");
652 /* setup back pointer */
653 be->blkif->be = be;
655 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
656 "%s/%s", dev->nodename, "physical-device");
657 if (err)
658 goto fail;
660 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
661 xen_blkif_max_ring_order);
662 if (err)
663 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
665 err = xenbus_switch_state(dev, XenbusStateInitWait);
666 if (err)
667 goto fail;
669 return 0;
671 fail:
672 pr_warn("%s failed\n", __func__);
673 xen_blkbk_remove(dev);
674 return err;
679 * Callback received when the hotplug scripts have placed the physical-device
680 * node. Read it and the mode node, and create a vbd. If the frontend is
681 * ready, connect.
683 static void backend_changed(struct xenbus_watch *watch,
684 const char *path, const char *token)
686 int err;
687 unsigned major;
688 unsigned minor;
689 struct backend_info *be
690 = container_of(watch, struct backend_info, backend_watch);
691 struct xenbus_device *dev = be->dev;
692 int cdrom = 0;
693 unsigned long handle;
694 char *device_type;
696 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
698 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
699 &major, &minor);
700 if (XENBUS_EXIST_ERR(err)) {
702 * Since this watch will fire once immediately after it is
703 * registered, we expect this. Ignore it, and wait for the
704 * hotplug scripts.
706 return;
708 if (err != 2) {
709 xenbus_dev_fatal(dev, err, "reading physical-device");
710 return;
713 if (be->major | be->minor) {
714 if (be->major != major || be->minor != minor)
715 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
716 be->major, be->minor, major, minor);
717 return;
720 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
721 if (IS_ERR(be->mode)) {
722 err = PTR_ERR(be->mode);
723 be->mode = NULL;
724 xenbus_dev_fatal(dev, err, "reading mode");
725 return;
728 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
729 if (!IS_ERR(device_type)) {
730 cdrom = strcmp(device_type, "cdrom") == 0;
731 kfree(device_type);
734 /* Front end dir is a number, which is used as the handle. */
735 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
736 if (err) {
737 kfree(be->mode);
738 be->mode = NULL;
739 return;
742 be->major = major;
743 be->minor = minor;
745 err = xen_vbd_create(be->blkif, handle, major, minor,
746 !strchr(be->mode, 'w'), cdrom);
748 if (err)
749 xenbus_dev_fatal(dev, err, "creating vbd structure");
750 else {
751 err = xenvbd_sysfs_addif(dev);
752 if (err) {
753 xen_vbd_free(&be->blkif->vbd);
754 xenbus_dev_fatal(dev, err, "creating sysfs entries");
758 if (err) {
759 kfree(be->mode);
760 be->mode = NULL;
761 be->major = 0;
762 be->minor = 0;
763 } else {
764 /* We're potentially connected now */
765 xen_update_blkif_status(be->blkif);
771 * Callback received when the frontend's state changes.
773 static void frontend_changed(struct xenbus_device *dev,
774 enum xenbus_state frontend_state)
776 struct backend_info *be = dev_get_drvdata(&dev->dev);
777 int err;
779 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
781 switch (frontend_state) {
782 case XenbusStateInitialising:
783 if (dev->state == XenbusStateClosed) {
784 pr_info("%s: prepare for reconnect\n", dev->nodename);
785 xenbus_switch_state(dev, XenbusStateInitWait);
787 break;
789 case XenbusStateInitialised:
790 case XenbusStateConnected:
792 * Ensure we connect even when two watches fire in
793 * close succession and we miss the intermediate value
794 * of frontend_state.
796 if (dev->state == XenbusStateConnected)
797 break;
800 * Enforce precondition before potential leak point.
801 * xen_blkif_disconnect() is idempotent.
803 err = xen_blkif_disconnect(be->blkif);
804 if (err) {
805 xenbus_dev_fatal(dev, err, "pending I/O");
806 break;
809 err = connect_ring(be);
810 if (err) {
812 * Clean up so that memory resources can be used by
813 * other devices. connect_ring reported already error.
815 xen_blkif_disconnect(be->blkif);
816 break;
818 xen_update_blkif_status(be->blkif);
819 break;
821 case XenbusStateClosing:
822 xenbus_switch_state(dev, XenbusStateClosing);
823 break;
825 case XenbusStateClosed:
826 xen_blkif_disconnect(be->blkif);
827 xenbus_switch_state(dev, XenbusStateClosed);
828 if (xenbus_dev_is_online(dev))
829 break;
830 /* fall through */
831 /* if not online */
832 case XenbusStateUnknown:
833 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
834 device_unregister(&dev->dev);
835 break;
837 default:
838 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
839 frontend_state);
840 break;
845 /* ** Connection ** */
849 * Write the physical details regarding the block device to the store, and
850 * switch to Connected state.
852 static void connect(struct backend_info *be)
854 struct xenbus_transaction xbt;
855 int err;
856 struct xenbus_device *dev = be->dev;
858 pr_debug("%s %s\n", __func__, dev->otherend);
860 /* Supply the information about the device the frontend needs */
861 again:
862 err = xenbus_transaction_start(&xbt);
863 if (err) {
864 xenbus_dev_fatal(dev, err, "starting transaction");
865 return;
868 /* If we can't advertise it is OK. */
869 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
871 xen_blkbk_discard(xbt, be);
873 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
875 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
876 if (err) {
877 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
878 dev->nodename);
879 goto abort;
882 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
883 (unsigned long long)vbd_sz(&be->blkif->vbd));
884 if (err) {
885 xenbus_dev_fatal(dev, err, "writing %s/sectors",
886 dev->nodename);
887 goto abort;
890 /* FIXME: use a typename instead */
891 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
892 be->blkif->vbd.type |
893 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
894 if (err) {
895 xenbus_dev_fatal(dev, err, "writing %s/info",
896 dev->nodename);
897 goto abort;
899 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
900 (unsigned long)
901 bdev_logical_block_size(be->blkif->vbd.bdev));
902 if (err) {
903 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
904 dev->nodename);
905 goto abort;
907 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
908 bdev_physical_block_size(be->blkif->vbd.bdev));
909 if (err)
910 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
911 dev->nodename);
913 err = xenbus_transaction_end(xbt, 0);
914 if (err == -EAGAIN)
915 goto again;
916 if (err)
917 xenbus_dev_fatal(dev, err, "ending transaction");
919 err = xenbus_switch_state(dev, XenbusStateConnected);
920 if (err)
921 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
922 dev->nodename);
924 return;
925 abort:
926 xenbus_transaction_end(xbt, 1);
930 * Each ring may have multi pages, depends on "ring-page-order".
932 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
934 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
935 struct pending_req *req, *n;
936 int err, i, j;
937 struct xen_blkif *blkif = ring->blkif;
938 struct xenbus_device *dev = blkif->be->dev;
939 unsigned int ring_page_order, nr_grefs, evtchn;
941 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
942 &evtchn);
943 if (err != 1) {
944 err = -EINVAL;
945 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
946 return err;
949 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
950 &ring_page_order);
951 if (err != 1) {
952 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
953 if (err != 1) {
954 err = -EINVAL;
955 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
956 return err;
958 nr_grefs = 1;
959 } else {
960 unsigned int i;
962 if (ring_page_order > xen_blkif_max_ring_order) {
963 err = -EINVAL;
964 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
965 dir, ring_page_order,
966 xen_blkif_max_ring_order);
967 return err;
970 nr_grefs = 1 << ring_page_order;
971 for (i = 0; i < nr_grefs; i++) {
972 char ring_ref_name[RINGREF_NAME_LEN];
974 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
975 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
976 "%u", &ring_ref[i]);
977 if (err != 1) {
978 err = -EINVAL;
979 xenbus_dev_fatal(dev, err, "reading %s/%s",
980 dir, ring_ref_name);
981 return err;
985 blkif->nr_ring_pages = nr_grefs;
987 err = -ENOMEM;
988 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
989 req = kzalloc(sizeof(*req), GFP_KERNEL);
990 if (!req)
991 goto fail;
992 list_add_tail(&req->free_list, &ring->pending_free);
993 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
994 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
995 if (!req->segments[j])
996 goto fail;
998 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
999 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
1000 GFP_KERNEL);
1001 if (!req->indirect_pages[j])
1002 goto fail;
1006 /* Map the shared frame, irq etc. */
1007 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
1008 if (err) {
1009 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
1010 goto fail;
1013 return 0;
1015 fail:
1016 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
1017 list_del(&req->free_list);
1018 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1019 if (!req->segments[j])
1020 break;
1021 kfree(req->segments[j]);
1023 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1024 if (!req->indirect_pages[j])
1025 break;
1026 kfree(req->indirect_pages[j]);
1028 kfree(req);
1030 return err;
1033 static int connect_ring(struct backend_info *be)
1035 struct xenbus_device *dev = be->dev;
1036 unsigned int pers_grants;
1037 char protocol[64] = "";
1038 int err, i;
1039 char *xspath;
1040 size_t xspathsize;
1041 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
1042 unsigned int requested_num_queues = 0;
1044 pr_debug("%s %s\n", __func__, dev->otherend);
1046 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
1047 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1048 "%63s", protocol);
1049 if (err <= 0)
1050 strcpy(protocol, "unspecified, assuming default");
1051 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1052 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1053 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1054 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1055 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1056 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1057 else {
1058 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
1059 return -ENOSYS;
1061 pers_grants = xenbus_read_unsigned(dev->otherend, "feature-persistent",
1063 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1064 be->blkif->vbd.overflow_max_grants = 0;
1067 * Read the number of hardware queues from frontend.
1069 requested_num_queues = xenbus_read_unsigned(dev->otherend,
1070 "multi-queue-num-queues",
1072 if (requested_num_queues > xenblk_max_queues
1073 || requested_num_queues == 0) {
1074 /* Buggy or malicious guest. */
1075 xenbus_dev_fatal(dev, err,
1076 "guest requested %u queues, exceeding the maximum of %u.",
1077 requested_num_queues, xenblk_max_queues);
1078 return -ENOSYS;
1080 be->blkif->nr_rings = requested_num_queues;
1081 if (xen_blkif_alloc_rings(be->blkif))
1082 return -ENOMEM;
1084 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1085 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1086 pers_grants ? "persistent grants" : "");
1088 if (be->blkif->nr_rings == 1)
1089 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1090 else {
1091 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1092 xspath = kmalloc(xspathsize, GFP_KERNEL);
1093 if (!xspath) {
1094 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1095 return -ENOMEM;
1098 for (i = 0; i < be->blkif->nr_rings; i++) {
1099 memset(xspath, 0, xspathsize);
1100 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1101 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1102 if (err) {
1103 kfree(xspath);
1104 return err;
1107 kfree(xspath);
1109 return 0;
1112 static const struct xenbus_device_id xen_blkbk_ids[] = {
1113 { "vbd" },
1114 { "" }
1117 static struct xenbus_driver xen_blkbk_driver = {
1118 .ids = xen_blkbk_ids,
1119 .probe = xen_blkbk_probe,
1120 .remove = xen_blkbk_remove,
1121 .otherend_changed = frontend_changed
1124 int xen_blkif_xenbus_init(void)
1126 return xenbus_register_backend(&xen_blkbk_driver);