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
20 #include <linux/module.h>
21 #include <linux/kthread.h>
22 #include <xen/events.h>
23 #include <xen/grant_table.h>
26 /* Enlarge the array size in order to fully show blkback name. */
27 #define BLKBACK_NAME_LEN (20)
28 #define RINGREF_NAME_LEN (20)
31 struct xenbus_device
*dev
;
32 struct xen_blkif
*blkif
;
33 struct xenbus_watch backend_watch
;
39 static struct kmem_cache
*xen_blkif_cachep
;
40 static void connect(struct backend_info
*);
41 static int connect_ring(struct backend_info
*);
42 static void backend_changed(struct xenbus_watch
*, const char **,
44 static void xen_blkif_free(struct xen_blkif
*blkif
);
45 static void xen_vbd_free(struct xen_vbd
*vbd
);
47 struct xenbus_device
*xen_blkbk_xenbus(struct backend_info
*be
)
53 * The last request could free the device from softirq context and
54 * xen_blkif_free() can sleep.
56 static void xen_blkif_deferred_free(struct work_struct
*work
)
58 struct xen_blkif
*blkif
;
60 blkif
= container_of(work
, struct xen_blkif
, free_work
);
61 xen_blkif_free(blkif
);
64 static int blkback_name(struct xen_blkif
*blkif
, char *buf
)
66 char *devpath
, *devname
;
67 struct xenbus_device
*dev
= blkif
->be
->dev
;
69 devpath
= xenbus_read(XBT_NIL
, dev
->nodename
, "dev", NULL
);
71 return PTR_ERR(devpath
);
73 devname
= strstr(devpath
, "/dev/");
75 devname
+= strlen("/dev/");
79 snprintf(buf
, BLKBACK_NAME_LEN
, "blkback.%d.%s", blkif
->domid
, devname
);
85 static void xen_update_blkif_status(struct xen_blkif
*blkif
)
88 char name
[BLKBACK_NAME_LEN
];
90 /* Not ready to connect? */
91 if (!blkif
->irq
|| !blkif
->vbd
.bdev
)
94 /* Already connected? */
95 if (blkif
->be
->dev
->state
== XenbusStateConnected
)
98 /* Attempt to connect: exit if we fail to. */
100 if (blkif
->be
->dev
->state
!= XenbusStateConnected
)
103 err
= blkback_name(blkif
, name
);
105 xenbus_dev_error(blkif
->be
->dev
, err
, "get blkback dev name");
109 err
= filemap_write_and_wait(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
111 xenbus_dev_error(blkif
->be
->dev
, err
, "block flush");
114 invalidate_inode_pages2(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
116 blkif
->xenblkd
= kthread_run(xen_blkif_schedule
, blkif
, "%s", name
);
117 if (IS_ERR(blkif
->xenblkd
)) {
118 err
= PTR_ERR(blkif
->xenblkd
);
119 blkif
->xenblkd
= NULL
;
120 xenbus_dev_error(blkif
->be
->dev
, err
, "start xenblkd");
125 static struct xen_blkif
*xen_blkif_alloc(domid_t domid
)
127 struct xen_blkif
*blkif
;
129 BUILD_BUG_ON(MAX_INDIRECT_PAGES
> BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST
);
131 blkif
= kmem_cache_zalloc(xen_blkif_cachep
, GFP_KERNEL
);
133 return ERR_PTR(-ENOMEM
);
135 blkif
->domid
= domid
;
136 spin_lock_init(&blkif
->blk_ring_lock
);
137 atomic_set(&blkif
->refcnt
, 1);
138 init_waitqueue_head(&blkif
->wq
);
139 init_completion(&blkif
->drain_complete
);
140 atomic_set(&blkif
->drain
, 0);
141 blkif
->st_print
= jiffies
;
142 blkif
->persistent_gnts
.rb_node
= NULL
;
143 spin_lock_init(&blkif
->free_pages_lock
);
144 INIT_LIST_HEAD(&blkif
->free_pages
);
145 INIT_LIST_HEAD(&blkif
->persistent_purge_list
);
146 blkif
->free_pages_num
= 0;
147 atomic_set(&blkif
->persistent_gnt_in_use
, 0);
148 atomic_set(&blkif
->inflight
, 0);
149 INIT_WORK(&blkif
->persistent_purge_work
, xen_blkbk_unmap_purged_grants
);
151 INIT_LIST_HEAD(&blkif
->pending_free
);
152 INIT_WORK(&blkif
->free_work
, xen_blkif_deferred_free
);
153 spin_lock_init(&blkif
->pending_free_lock
);
154 init_waitqueue_head(&blkif
->pending_free_wq
);
155 init_waitqueue_head(&blkif
->shutdown_wq
);
160 static int xen_blkif_map(struct xen_blkif
*blkif
, grant_ref_t
*gref
,
161 unsigned int nr_grefs
, unsigned int evtchn
)
165 /* Already connected through? */
169 err
= xenbus_map_ring_valloc(blkif
->be
->dev
, gref
, nr_grefs
,
174 switch (blkif
->blk_protocol
) {
175 case BLKIF_PROTOCOL_NATIVE
:
177 struct blkif_sring
*sring
;
178 sring
= (struct blkif_sring
*)blkif
->blk_ring
;
179 BACK_RING_INIT(&blkif
->blk_rings
.native
, sring
, PAGE_SIZE
* nr_grefs
);
182 case BLKIF_PROTOCOL_X86_32
:
184 struct blkif_x86_32_sring
*sring_x86_32
;
185 sring_x86_32
= (struct blkif_x86_32_sring
*)blkif
->blk_ring
;
186 BACK_RING_INIT(&blkif
->blk_rings
.x86_32
, sring_x86_32
, PAGE_SIZE
* nr_grefs
);
189 case BLKIF_PROTOCOL_X86_64
:
191 struct blkif_x86_64_sring
*sring_x86_64
;
192 sring_x86_64
= (struct blkif_x86_64_sring
*)blkif
->blk_ring
;
193 BACK_RING_INIT(&blkif
->blk_rings
.x86_64
, sring_x86_64
, PAGE_SIZE
* nr_grefs
);
200 err
= bind_interdomain_evtchn_to_irqhandler(blkif
->domid
, evtchn
,
202 "blkif-backend", blkif
);
204 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
205 blkif
->blk_rings
.common
.sring
= NULL
;
213 static int xen_blkif_disconnect(struct xen_blkif
*blkif
)
215 if (blkif
->xenblkd
) {
216 kthread_stop(blkif
->xenblkd
);
217 wake_up(&blkif
->shutdown_wq
);
218 blkif
->xenblkd
= NULL
;
221 /* The above kthread_stop() guarantees that at this point we
222 * don't have any discard_io or other_io requests. So, checking
223 * for inflight IO is enough.
225 if (atomic_read(&blkif
->inflight
) > 0)
229 unbind_from_irqhandler(blkif
->irq
, blkif
);
233 if (blkif
->blk_rings
.common
.sring
) {
234 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
235 blkif
->blk_rings
.common
.sring
= NULL
;
238 /* Remove all persistent grants and the cache of ballooned pages. */
239 xen_blkbk_free_caches(blkif
);
244 static void xen_blkif_free(struct xen_blkif
*blkif
)
246 struct pending_req
*req
, *n
;
249 xen_blkif_disconnect(blkif
);
250 xen_vbd_free(&blkif
->vbd
);
252 /* Make sure everything is drained before shutting down */
253 BUG_ON(blkif
->persistent_gnt_c
!= 0);
254 BUG_ON(atomic_read(&blkif
->persistent_gnt_in_use
) != 0);
255 BUG_ON(blkif
->free_pages_num
!= 0);
256 BUG_ON(!list_empty(&blkif
->persistent_purge_list
));
257 BUG_ON(!list_empty(&blkif
->free_pages
));
258 BUG_ON(!RB_EMPTY_ROOT(&blkif
->persistent_gnts
));
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
);
264 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++)
265 kfree(req
->segments
[j
]);
267 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++)
268 kfree(req
->indirect_pages
[j
]);
274 WARN_ON(i
!= (XEN_BLKIF_REQS_PER_PAGE
* blkif
->nr_ring_pages
));
276 kmem_cache_free(xen_blkif_cachep
, blkif
);
279 int __init
xen_blkif_interface_init(void)
281 xen_blkif_cachep
= kmem_cache_create("blkif_cache",
282 sizeof(struct xen_blkif
),
284 if (!xen_blkif_cachep
)
291 * sysfs interface for VBD I/O requests
294 #define VBD_SHOW(name, format, args...) \
295 static ssize_t show_##name(struct device *_dev, \
296 struct device_attribute *attr, \
299 struct xenbus_device *dev = to_xenbus_device(_dev); \
300 struct backend_info *be = dev_get_drvdata(&dev->dev); \
302 return sprintf(buf, format, ##args); \
304 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
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
);
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
,
325 static struct attribute_group xen_vbdstat_group
= {
326 .name
= "statistics",
327 .attrs
= xen_vbdstat_attrs
,
330 VBD_SHOW(physical_device
, "%x:%x\n", be
->major
, be
->minor
);
331 VBD_SHOW(mode
, "%s\n", be
->mode
);
333 static int xenvbd_sysfs_addif(struct xenbus_device
*dev
)
337 error
= device_create_file(&dev
->dev
, &dev_attr_physical_device
);
341 error
= device_create_file(&dev
->dev
, &dev_attr_mode
);
345 error
= sysfs_create_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
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
);
357 static void xenvbd_sysfs_delif(struct xenbus_device
*dev
)
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
);
365 static void xen_vbd_free(struct xen_vbd
*vbd
)
368 blkdev_put(vbd
->bdev
, vbd
->readonly
? FMODE_READ
: FMODE_WRITE
);
372 static int xen_vbd_create(struct xen_blkif
*blkif
, blkif_vdev_t handle
,
373 unsigned major
, unsigned minor
, int readonly
,
377 struct block_device
*bdev
;
378 struct request_queue
*q
;
381 vbd
->handle
= handle
;
382 vbd
->readonly
= readonly
;
385 vbd
->pdevice
= MKDEV(major
, minor
);
387 bdev
= blkdev_get_by_dev(vbd
->pdevice
, vbd
->readonly
?
388 FMODE_READ
: FMODE_WRITE
, NULL
);
391 pr_warn("xen_vbd_create: device %08x could not be opened\n",
397 if (vbd
->bdev
->bd_disk
== NULL
) {
398 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
403 vbd
->size
= vbd_sz(vbd
);
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
;
410 q
= bdev_get_queue(bdev
);
411 if (q
&& q
->flush_flags
)
412 vbd
->flush_support
= true;
414 if (q
&& blk_queue_secdiscard(q
))
415 vbd
->discard_secure
= true;
417 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
418 handle
, blkif
->domid
);
421 static int xen_blkbk_remove(struct xenbus_device
*dev
)
423 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
425 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
427 if (be
->major
|| be
->minor
)
428 xenvbd_sysfs_delif(dev
);
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
;
436 dev_set_drvdata(&dev
->dev
, NULL
);
439 xen_blkif_disconnect(be
->blkif
);
440 xen_blkif_put(be
->blkif
);
448 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
449 struct backend_info
*be
, int state
)
451 struct xenbus_device
*dev
= be
->dev
;
454 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-flush-cache",
457 dev_warn(&dev
->dev
, "writing feature-flush-cache (%d)", err
);
462 static void xen_blkbk_discard(struct xenbus_transaction xbt
, struct backend_info
*be
)
464 struct xenbus_device
*dev
= be
->dev
;
465 struct xen_blkif
*blkif
= be
->blkif
;
467 int state
= 0, discard_enable
;
468 struct block_device
*bdev
= be
->blkif
->vbd
.bdev
;
469 struct request_queue
*q
= bdev_get_queue(bdev
);
471 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "discard-enable", "%d",
473 if (err
== 1 && !discard_enable
)
476 if (blk_queue_discard(q
)) {
477 err
= xenbus_printf(xbt
, dev
->nodename
,
478 "discard-granularity", "%u",
479 q
->limits
.discard_granularity
);
481 dev_warn(&dev
->dev
, "writing discard-granularity (%d)", err
);
484 err
= xenbus_printf(xbt
, dev
->nodename
,
485 "discard-alignment", "%u",
486 q
->limits
.discard_alignment
);
488 dev_warn(&dev
->dev
, "writing discard-alignment (%d)", err
);
493 err
= xenbus_printf(xbt
, dev
->nodename
,
494 "discard-secure", "%d",
495 blkif
->vbd
.discard_secure
);
497 dev_warn(&dev
->dev
, "writing discard-secure (%d)", err
);
501 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-discard",
504 dev_warn(&dev
->dev
, "writing feature-discard (%d)", err
);
506 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
507 struct backend_info
*be
, int state
)
509 struct xenbus_device
*dev
= be
->dev
;
512 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-barrier",
515 dev_warn(&dev
->dev
, "writing feature-barrier (%d)", err
);
521 * Entry point to this code when a new device is created. Allocate the basic
522 * structures, and watch the store waiting for the hotplug scripts to tell us
523 * the device's physical major and minor numbers. Switch to InitWait.
525 static int xen_blkbk_probe(struct xenbus_device
*dev
,
526 const struct xenbus_device_id
*id
)
529 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
532 /* match the pr_debug in xen_blkbk_remove */
533 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
536 xenbus_dev_fatal(dev
, -ENOMEM
,
537 "allocating backend structure");
541 dev_set_drvdata(&dev
->dev
, be
);
543 be
->blkif
= xen_blkif_alloc(dev
->otherend_id
);
544 if (IS_ERR(be
->blkif
)) {
545 err
= PTR_ERR(be
->blkif
);
547 xenbus_dev_fatal(dev
, err
, "creating block interface");
551 /* setup back pointer */
554 err
= xenbus_watch_pathfmt(dev
, &be
->backend_watch
, backend_changed
,
555 "%s/%s", dev
->nodename
, "physical-device");
559 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "max-ring-page-order", "%u",
560 xen_blkif_max_ring_order
);
562 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__
);
564 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
571 pr_warn("%s failed\n", __func__
);
572 xen_blkbk_remove(dev
);
578 * Callback received when the hotplug scripts have placed the physical-device
579 * node. Read it and the mode node, and create a vbd. If the frontend is
582 static void backend_changed(struct xenbus_watch
*watch
,
583 const char **vec
, unsigned int len
)
588 struct backend_info
*be
589 = container_of(watch
, struct backend_info
, backend_watch
);
590 struct xenbus_device
*dev
= be
->dev
;
592 unsigned long handle
;
595 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
597 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "physical-device", "%x:%x",
599 if (XENBUS_EXIST_ERR(err
)) {
601 * Since this watch will fire once immediately after it is
602 * registered, we expect this. Ignore it, and wait for the
608 xenbus_dev_fatal(dev
, err
, "reading physical-device");
612 if (be
->major
| be
->minor
) {
613 if (be
->major
!= major
|| be
->minor
!= minor
)
614 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
615 be
->major
, be
->minor
, major
, minor
);
619 be
->mode
= xenbus_read(XBT_NIL
, dev
->nodename
, "mode", NULL
);
620 if (IS_ERR(be
->mode
)) {
621 err
= PTR_ERR(be
->mode
);
623 xenbus_dev_fatal(dev
, err
, "reading mode");
627 device_type
= xenbus_read(XBT_NIL
, dev
->otherend
, "device-type", NULL
);
628 if (!IS_ERR(device_type
)) {
629 cdrom
= strcmp(device_type
, "cdrom") == 0;
633 /* Front end dir is a number, which is used as the handle. */
634 err
= kstrtoul(strrchr(dev
->otherend
, '/') + 1, 0, &handle
);
641 err
= xen_vbd_create(be
->blkif
, handle
, major
, minor
,
642 !strchr(be
->mode
, 'w'), cdrom
);
645 xenbus_dev_fatal(dev
, err
, "creating vbd structure");
647 err
= xenvbd_sysfs_addif(dev
);
649 xen_vbd_free(&be
->blkif
->vbd
);
650 xenbus_dev_fatal(dev
, err
, "creating sysfs entries");
660 /* We're potentially connected now */
661 xen_update_blkif_status(be
->blkif
);
667 * Callback received when the frontend's state changes.
669 static void frontend_changed(struct xenbus_device
*dev
,
670 enum xenbus_state frontend_state
)
672 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
675 pr_debug("%s %p %s\n", __func__
, dev
, xenbus_strstate(frontend_state
));
677 switch (frontend_state
) {
678 case XenbusStateInitialising
:
679 if (dev
->state
== XenbusStateClosed
) {
680 pr_info("%s: prepare for reconnect\n", dev
->nodename
);
681 xenbus_switch_state(dev
, XenbusStateInitWait
);
685 case XenbusStateInitialised
:
686 case XenbusStateConnected
:
688 * Ensure we connect even when two watches fire in
689 * close succession and we miss the intermediate value
692 if (dev
->state
== XenbusStateConnected
)
696 * Enforce precondition before potential leak point.
697 * xen_blkif_disconnect() is idempotent.
699 err
= xen_blkif_disconnect(be
->blkif
);
701 xenbus_dev_fatal(dev
, err
, "pending I/O");
705 err
= connect_ring(be
);
708 xen_update_blkif_status(be
->blkif
);
711 case XenbusStateClosing
:
712 xenbus_switch_state(dev
, XenbusStateClosing
);
715 case XenbusStateClosed
:
716 xen_blkif_disconnect(be
->blkif
);
717 xenbus_switch_state(dev
, XenbusStateClosed
);
718 if (xenbus_dev_is_online(dev
))
720 /* fall through if not online */
721 case XenbusStateUnknown
:
722 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
723 device_unregister(&dev
->dev
);
727 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
734 /* ** Connection ** */
738 * Write the physical details regarding the block device to the store, and
739 * switch to Connected state.
741 static void connect(struct backend_info
*be
)
743 struct xenbus_transaction xbt
;
745 struct xenbus_device
*dev
= be
->dev
;
747 pr_debug("%s %s\n", __func__
, dev
->otherend
);
749 /* Supply the information about the device the frontend needs */
751 err
= xenbus_transaction_start(&xbt
);
753 xenbus_dev_fatal(dev
, err
, "starting transaction");
757 /* If we can't advertise it is OK. */
758 xen_blkbk_flush_diskcache(xbt
, be
, be
->blkif
->vbd
.flush_support
);
760 xen_blkbk_discard(xbt
, be
);
762 xen_blkbk_barrier(xbt
, be
, be
->blkif
->vbd
.flush_support
);
764 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-persistent", "%u", 1);
766 xenbus_dev_fatal(dev
, err
, "writing %s/feature-persistent",
770 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-max-indirect-segments", "%u",
771 MAX_INDIRECT_SEGMENTS
);
773 dev_warn(&dev
->dev
, "writing %s/feature-max-indirect-segments (%d)",
776 err
= xenbus_printf(xbt
, dev
->nodename
, "sectors", "%llu",
777 (unsigned long long)vbd_sz(&be
->blkif
->vbd
));
779 xenbus_dev_fatal(dev
, err
, "writing %s/sectors",
784 /* FIXME: use a typename instead */
785 err
= xenbus_printf(xbt
, dev
->nodename
, "info", "%u",
786 be
->blkif
->vbd
.type
|
787 (be
->blkif
->vbd
.readonly
? VDISK_READONLY
: 0));
789 xenbus_dev_fatal(dev
, err
, "writing %s/info",
793 err
= xenbus_printf(xbt
, dev
->nodename
, "sector-size", "%lu",
795 bdev_logical_block_size(be
->blkif
->vbd
.bdev
));
797 xenbus_dev_fatal(dev
, err
, "writing %s/sector-size",
801 err
= xenbus_printf(xbt
, dev
->nodename
, "physical-sector-size", "%u",
802 bdev_physical_block_size(be
->blkif
->vbd
.bdev
));
804 xenbus_dev_error(dev
, err
, "writing %s/physical-sector-size",
807 err
= xenbus_transaction_end(xbt
, 0);
811 xenbus_dev_fatal(dev
, err
, "ending transaction");
813 err
= xenbus_switch_state(dev
, XenbusStateConnected
);
815 xenbus_dev_fatal(dev
, err
, "%s: switching to Connected state",
820 xenbus_transaction_end(xbt
, 1);
824 static int connect_ring(struct backend_info
*be
)
826 struct xenbus_device
*dev
= be
->dev
;
827 unsigned int ring_ref
[XENBUS_MAX_RING_PAGES
];
828 unsigned int evtchn
, nr_grefs
, ring_page_order
;
829 unsigned int pers_grants
;
830 char protocol
[64] = "";
831 struct pending_req
*req
, *n
;
834 pr_debug("%s %s\n", __func__
, dev
->otherend
);
836 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "event-channel", "%u",
840 xenbus_dev_fatal(dev
, err
, "reading %s/event-channel",
844 pr_info("event-channel %u\n", evtchn
);
846 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "ring-page-order", "%u",
849 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "ring-ref",
853 xenbus_dev_fatal(dev
, err
, "reading %s/ring-ref",
858 pr_info("%s:using single page: ring-ref %d\n", dev
->otherend
,
863 if (ring_page_order
> xen_blkif_max_ring_order
) {
865 xenbus_dev_fatal(dev
, err
, "%s/request %d ring page order exceed max:%d",
866 dev
->otherend
, ring_page_order
,
867 xen_blkif_max_ring_order
);
871 nr_grefs
= 1 << ring_page_order
;
872 for (i
= 0; i
< nr_grefs
; i
++) {
873 char ring_ref_name
[RINGREF_NAME_LEN
];
875 snprintf(ring_ref_name
, RINGREF_NAME_LEN
, "ring-ref%u", i
);
876 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, ring_ref_name
,
880 xenbus_dev_fatal(dev
, err
, "reading %s/%s",
881 dev
->otherend
, ring_ref_name
);
884 pr_info("ring-ref%u: %u\n", i
, ring_ref
[i
]);
888 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_DEFAULT
;
889 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "protocol",
890 "%63s", protocol
, NULL
);
892 strcpy(protocol
, "unspecified, assuming default");
893 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_NATIVE
))
894 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
895 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_32
))
896 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_32
;
897 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_64
))
898 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_64
;
900 xenbus_dev_fatal(dev
, err
, "unknown fe protocol %s", protocol
);
903 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
904 "feature-persistent", "%u",
909 be
->blkif
->vbd
.feature_gnt_persistent
= pers_grants
;
910 be
->blkif
->vbd
.overflow_max_grants
= 0;
911 be
->blkif
->nr_ring_pages
= nr_grefs
;
913 pr_info("ring-pages:%d, event-channel %d, protocol %d (%s) %s\n",
914 nr_grefs
, evtchn
, be
->blkif
->blk_protocol
, protocol
,
915 pers_grants
? "persistent grants" : "");
917 for (i
= 0; i
< nr_grefs
* XEN_BLKIF_REQS_PER_PAGE
; i
++) {
918 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
921 list_add_tail(&req
->free_list
, &be
->blkif
->pending_free
);
922 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
923 req
->segments
[j
] = kzalloc(sizeof(*req
->segments
[0]), GFP_KERNEL
);
924 if (!req
->segments
[j
])
927 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
928 req
->indirect_pages
[j
] = kzalloc(sizeof(*req
->indirect_pages
[0]),
930 if (!req
->indirect_pages
[j
])
935 /* Map the shared frame, irq etc. */
936 err
= xen_blkif_map(be
->blkif
, ring_ref
, nr_grefs
, evtchn
);
938 xenbus_dev_fatal(dev
, err
, "mapping ring-ref port %u", evtchn
);
945 list_for_each_entry_safe(req
, n
, &be
->blkif
->pending_free
, free_list
) {
946 list_del(&req
->free_list
);
947 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
948 if (!req
->segments
[j
])
950 kfree(req
->segments
[j
]);
952 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
953 if (!req
->indirect_pages
[j
])
955 kfree(req
->indirect_pages
[j
]);
962 static const struct xenbus_device_id xen_blkbk_ids
[] = {
967 static struct xenbus_driver xen_blkbk_driver
= {
968 .ids
= xen_blkbk_ids
,
969 .probe
= xen_blkbk_probe
,
970 .remove
= xen_blkbk_remove
,
971 .otherend_changed
= frontend_changed
974 int xen_blkif_xenbus_init(void)
976 return xenbus_register_backend(&xen_blkbk_driver
);