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)
30 struct xenbus_device
*dev
;
31 struct xen_blkif
*blkif
;
32 struct xenbus_watch backend_watch
;
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 **,
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
)
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
);
70 return PTR_ERR(devpath
);
72 devname
= strstr(devpath
, "/dev/");
74 devname
+= strlen("/dev/");
78 snprintf(buf
, BLKBACK_NAME_LEN
, "blkback.%d.%s", blkif
->domid
, devname
);
84 static void xen_update_blkif_status(struct xen_blkif
*blkif
)
87 char name
[BLKBACK_NAME_LEN
];
89 /* Not ready to connect? */
90 if (!blkif
->irq
|| !blkif
->vbd
.bdev
)
93 /* Already connected? */
94 if (blkif
->be
->dev
->state
== XenbusStateConnected
)
97 /* Attempt to connect: exit if we fail to. */
99 if (blkif
->be
->dev
->state
!= XenbusStateConnected
)
102 err
= blkback_name(blkif
, name
);
104 xenbus_dev_error(blkif
->be
->dev
, err
, "get blkback dev name");
108 err
= filemap_write_and_wait(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
110 xenbus_dev_error(blkif
->be
->dev
, err
, "block flush");
113 invalidate_inode_pages2(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
115 blkif
->xenblkd
= kthread_run(xen_blkif_schedule
, blkif
, "%s", name
);
116 if (IS_ERR(blkif
->xenblkd
)) {
117 err
= PTR_ERR(blkif
->xenblkd
);
118 blkif
->xenblkd
= NULL
;
119 xenbus_dev_error(blkif
->be
->dev
, err
, "start xenblkd");
124 static struct xen_blkif
*xen_blkif_alloc(domid_t domid
)
126 struct xen_blkif
*blkif
;
127 struct pending_req
*req
, *n
;
130 BUILD_BUG_ON(MAX_INDIRECT_PAGES
> BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST
);
132 blkif
= kmem_cache_zalloc(xen_blkif_cachep
, GFP_KERNEL
);
134 return ERR_PTR(-ENOMEM
);
136 blkif
->domid
= domid
;
137 spin_lock_init(&blkif
->blk_ring_lock
);
138 atomic_set(&blkif
->refcnt
, 1);
139 init_waitqueue_head(&blkif
->wq
);
140 init_completion(&blkif
->drain_complete
);
141 atomic_set(&blkif
->drain
, 0);
142 blkif
->st_print
= jiffies
;
143 blkif
->persistent_gnts
.rb_node
= NULL
;
144 spin_lock_init(&blkif
->free_pages_lock
);
145 INIT_LIST_HEAD(&blkif
->free_pages
);
146 INIT_LIST_HEAD(&blkif
->persistent_purge_list
);
147 blkif
->free_pages_num
= 0;
148 atomic_set(&blkif
->persistent_gnt_in_use
, 0);
149 atomic_set(&blkif
->inflight
, 0);
150 INIT_WORK(&blkif
->persistent_purge_work
, xen_blkbk_unmap_purged_grants
);
152 INIT_LIST_HEAD(&blkif
->pending_free
);
153 INIT_WORK(&blkif
->free_work
, xen_blkif_deferred_free
);
155 for (i
= 0; i
< XEN_BLKIF_REQS
; i
++) {
156 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
159 list_add_tail(&req
->free_list
,
160 &blkif
->pending_free
);
161 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
162 req
->segments
[j
] = kzalloc(sizeof(*req
->segments
[0]),
164 if (!req
->segments
[j
])
167 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
168 req
->indirect_pages
[j
] = kzalloc(sizeof(*req
->indirect_pages
[0]),
170 if (!req
->indirect_pages
[j
])
174 spin_lock_init(&blkif
->pending_free_lock
);
175 init_waitqueue_head(&blkif
->pending_free_wq
);
176 init_waitqueue_head(&blkif
->shutdown_wq
);
181 list_for_each_entry_safe(req
, n
, &blkif
->pending_free
, free_list
) {
182 list_del(&req
->free_list
);
183 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
184 if (!req
->segments
[j
])
186 kfree(req
->segments
[j
]);
188 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
189 if (!req
->indirect_pages
[j
])
191 kfree(req
->indirect_pages
[j
]);
196 kmem_cache_free(xen_blkif_cachep
, blkif
);
198 return ERR_PTR(-ENOMEM
);
201 static int xen_blkif_map(struct xen_blkif
*blkif
, grant_ref_t gref
,
206 /* Already connected through? */
210 err
= xenbus_map_ring_valloc(blkif
->be
->dev
, &gref
, 1,
215 switch (blkif
->blk_protocol
) {
216 case BLKIF_PROTOCOL_NATIVE
:
218 struct blkif_sring
*sring
;
219 sring
= (struct blkif_sring
*)blkif
->blk_ring
;
220 BACK_RING_INIT(&blkif
->blk_rings
.native
, sring
, PAGE_SIZE
);
223 case BLKIF_PROTOCOL_X86_32
:
225 struct blkif_x86_32_sring
*sring_x86_32
;
226 sring_x86_32
= (struct blkif_x86_32_sring
*)blkif
->blk_ring
;
227 BACK_RING_INIT(&blkif
->blk_rings
.x86_32
, sring_x86_32
, PAGE_SIZE
);
230 case BLKIF_PROTOCOL_X86_64
:
232 struct blkif_x86_64_sring
*sring_x86_64
;
233 sring_x86_64
= (struct blkif_x86_64_sring
*)blkif
->blk_ring
;
234 BACK_RING_INIT(&blkif
->blk_rings
.x86_64
, sring_x86_64
, PAGE_SIZE
);
241 err
= bind_interdomain_evtchn_to_irqhandler(blkif
->domid
, evtchn
,
243 "blkif-backend", blkif
);
245 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
246 blkif
->blk_rings
.common
.sring
= NULL
;
254 static int xen_blkif_disconnect(struct xen_blkif
*blkif
)
256 if (blkif
->xenblkd
) {
257 kthread_stop(blkif
->xenblkd
);
258 wake_up(&blkif
->shutdown_wq
);
259 blkif
->xenblkd
= NULL
;
262 /* The above kthread_stop() guarantees that at this point we
263 * don't have any discard_io or other_io requests. So, checking
264 * for inflight IO is enough.
266 if (atomic_read(&blkif
->inflight
) > 0)
270 unbind_from_irqhandler(blkif
->irq
, blkif
);
274 if (blkif
->blk_rings
.common
.sring
) {
275 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
276 blkif
->blk_rings
.common
.sring
= NULL
;
279 /* Remove all persistent grants and the cache of ballooned pages. */
280 xen_blkbk_free_caches(blkif
);
285 static void xen_blkif_free(struct xen_blkif
*blkif
)
287 struct pending_req
*req
, *n
;
290 xen_blkif_disconnect(blkif
);
291 xen_vbd_free(&blkif
->vbd
);
293 /* Make sure everything is drained before shutting down */
294 BUG_ON(blkif
->persistent_gnt_c
!= 0);
295 BUG_ON(atomic_read(&blkif
->persistent_gnt_in_use
) != 0);
296 BUG_ON(blkif
->free_pages_num
!= 0);
297 BUG_ON(!list_empty(&blkif
->persistent_purge_list
));
298 BUG_ON(!list_empty(&blkif
->free_pages
));
299 BUG_ON(!RB_EMPTY_ROOT(&blkif
->persistent_gnts
));
301 /* Check that there is no request in use */
302 list_for_each_entry_safe(req
, n
, &blkif
->pending_free
, free_list
) {
303 list_del(&req
->free_list
);
305 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++)
306 kfree(req
->segments
[j
]);
308 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++)
309 kfree(req
->indirect_pages
[j
]);
315 WARN_ON(i
!= XEN_BLKIF_REQS
);
317 kmem_cache_free(xen_blkif_cachep
, blkif
);
320 int __init
xen_blkif_interface_init(void)
322 xen_blkif_cachep
= kmem_cache_create("blkif_cache",
323 sizeof(struct xen_blkif
),
325 if (!xen_blkif_cachep
)
332 * sysfs interface for VBD I/O requests
335 #define VBD_SHOW(name, format, args...) \
336 static ssize_t show_##name(struct device *_dev, \
337 struct device_attribute *attr, \
340 struct xenbus_device *dev = to_xenbus_device(_dev); \
341 struct backend_info *be = dev_get_drvdata(&dev->dev); \
343 return sprintf(buf, format, ##args); \
345 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
347 VBD_SHOW(oo_req
, "%llu\n", be
->blkif
->st_oo_req
);
348 VBD_SHOW(rd_req
, "%llu\n", be
->blkif
->st_rd_req
);
349 VBD_SHOW(wr_req
, "%llu\n", be
->blkif
->st_wr_req
);
350 VBD_SHOW(f_req
, "%llu\n", be
->blkif
->st_f_req
);
351 VBD_SHOW(ds_req
, "%llu\n", be
->blkif
->st_ds_req
);
352 VBD_SHOW(rd_sect
, "%llu\n", be
->blkif
->st_rd_sect
);
353 VBD_SHOW(wr_sect
, "%llu\n", be
->blkif
->st_wr_sect
);
355 static struct attribute
*xen_vbdstat_attrs
[] = {
356 &dev_attr_oo_req
.attr
,
357 &dev_attr_rd_req
.attr
,
358 &dev_attr_wr_req
.attr
,
359 &dev_attr_f_req
.attr
,
360 &dev_attr_ds_req
.attr
,
361 &dev_attr_rd_sect
.attr
,
362 &dev_attr_wr_sect
.attr
,
366 static struct attribute_group xen_vbdstat_group
= {
367 .name
= "statistics",
368 .attrs
= xen_vbdstat_attrs
,
371 VBD_SHOW(physical_device
, "%x:%x\n", be
->major
, be
->minor
);
372 VBD_SHOW(mode
, "%s\n", be
->mode
);
374 static int xenvbd_sysfs_addif(struct xenbus_device
*dev
)
378 error
= device_create_file(&dev
->dev
, &dev_attr_physical_device
);
382 error
= device_create_file(&dev
->dev
, &dev_attr_mode
);
386 error
= sysfs_create_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
392 fail3
: sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
393 fail2
: device_remove_file(&dev
->dev
, &dev_attr_mode
);
394 fail1
: device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
398 static void xenvbd_sysfs_delif(struct xenbus_device
*dev
)
400 sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
401 device_remove_file(&dev
->dev
, &dev_attr_mode
);
402 device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
406 static void xen_vbd_free(struct xen_vbd
*vbd
)
409 blkdev_put(vbd
->bdev
, vbd
->readonly
? FMODE_READ
: FMODE_WRITE
);
413 static int xen_vbd_create(struct xen_blkif
*blkif
, blkif_vdev_t handle
,
414 unsigned major
, unsigned minor
, int readonly
,
418 struct block_device
*bdev
;
419 struct request_queue
*q
;
422 vbd
->handle
= handle
;
423 vbd
->readonly
= readonly
;
426 vbd
->pdevice
= MKDEV(major
, minor
);
428 bdev
= blkdev_get_by_dev(vbd
->pdevice
, vbd
->readonly
?
429 FMODE_READ
: FMODE_WRITE
, NULL
);
432 pr_warn("xen_vbd_create: device %08x could not be opened\n",
438 if (vbd
->bdev
->bd_disk
== NULL
) {
439 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
444 vbd
->size
= vbd_sz(vbd
);
446 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_CD
|| cdrom
)
447 vbd
->type
|= VDISK_CDROM
;
448 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_REMOVABLE
)
449 vbd
->type
|= VDISK_REMOVABLE
;
451 q
= bdev_get_queue(bdev
);
452 if (q
&& q
->flush_flags
)
453 vbd
->flush_support
= true;
455 if (q
&& blk_queue_secdiscard(q
))
456 vbd
->discard_secure
= true;
458 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
459 handle
, blkif
->domid
);
462 static int xen_blkbk_remove(struct xenbus_device
*dev
)
464 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
466 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
468 if (be
->major
|| be
->minor
)
469 xenvbd_sysfs_delif(dev
);
471 if (be
->backend_watch
.node
) {
472 unregister_xenbus_watch(&be
->backend_watch
);
473 kfree(be
->backend_watch
.node
);
474 be
->backend_watch
.node
= NULL
;
477 dev_set_drvdata(&dev
->dev
, NULL
);
480 xen_blkif_disconnect(be
->blkif
);
481 xen_blkif_put(be
->blkif
);
489 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
490 struct backend_info
*be
, int state
)
492 struct xenbus_device
*dev
= be
->dev
;
495 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-flush-cache",
498 dev_warn(&dev
->dev
, "writing feature-flush-cache (%d)", err
);
503 static void xen_blkbk_discard(struct xenbus_transaction xbt
, struct backend_info
*be
)
505 struct xenbus_device
*dev
= be
->dev
;
506 struct xen_blkif
*blkif
= be
->blkif
;
508 int state
= 0, discard_enable
;
509 struct block_device
*bdev
= be
->blkif
->vbd
.bdev
;
510 struct request_queue
*q
= bdev_get_queue(bdev
);
512 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "discard-enable", "%d",
514 if (err
== 1 && !discard_enable
)
517 if (blk_queue_discard(q
)) {
518 err
= xenbus_printf(xbt
, dev
->nodename
,
519 "discard-granularity", "%u",
520 q
->limits
.discard_granularity
);
522 dev_warn(&dev
->dev
, "writing discard-granularity (%d)", err
);
525 err
= xenbus_printf(xbt
, dev
->nodename
,
526 "discard-alignment", "%u",
527 q
->limits
.discard_alignment
);
529 dev_warn(&dev
->dev
, "writing discard-alignment (%d)", err
);
534 err
= xenbus_printf(xbt
, dev
->nodename
,
535 "discard-secure", "%d",
536 blkif
->vbd
.discard_secure
);
538 dev_warn(&dev
->dev
, "writing discard-secure (%d)", err
);
542 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-discard",
545 dev_warn(&dev
->dev
, "writing feature-discard (%d)", err
);
547 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
548 struct backend_info
*be
, int state
)
550 struct xenbus_device
*dev
= be
->dev
;
553 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-barrier",
556 dev_warn(&dev
->dev
, "writing feature-barrier (%d)", err
);
562 * Entry point to this code when a new device is created. Allocate the basic
563 * structures, and watch the store waiting for the hotplug scripts to tell us
564 * the device's physical major and minor numbers. Switch to InitWait.
566 static int xen_blkbk_probe(struct xenbus_device
*dev
,
567 const struct xenbus_device_id
*id
)
570 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
573 /* match the pr_debug in xen_blkbk_remove */
574 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
577 xenbus_dev_fatal(dev
, -ENOMEM
,
578 "allocating backend structure");
582 dev_set_drvdata(&dev
->dev
, be
);
584 be
->blkif
= xen_blkif_alloc(dev
->otherend_id
);
585 if (IS_ERR(be
->blkif
)) {
586 err
= PTR_ERR(be
->blkif
);
588 xenbus_dev_fatal(dev
, err
, "creating block interface");
592 /* setup back pointer */
595 err
= xenbus_watch_pathfmt(dev
, &be
->backend_watch
, backend_changed
,
596 "%s/%s", dev
->nodename
, "physical-device");
600 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
607 pr_warn("%s failed\n", __func__
);
608 xen_blkbk_remove(dev
);
614 * Callback received when the hotplug scripts have placed the physical-device
615 * node. Read it and the mode node, and create a vbd. If the frontend is
618 static void backend_changed(struct xenbus_watch
*watch
,
619 const char **vec
, unsigned int len
)
624 struct backend_info
*be
625 = container_of(watch
, struct backend_info
, backend_watch
);
626 struct xenbus_device
*dev
= be
->dev
;
628 unsigned long handle
;
631 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
633 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "physical-device", "%x:%x",
635 if (XENBUS_EXIST_ERR(err
)) {
637 * Since this watch will fire once immediately after it is
638 * registered, we expect this. Ignore it, and wait for the
644 xenbus_dev_fatal(dev
, err
, "reading physical-device");
648 if (be
->major
| be
->minor
) {
649 if (be
->major
!= major
|| be
->minor
!= minor
)
650 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
651 be
->major
, be
->minor
, major
, minor
);
655 be
->mode
= xenbus_read(XBT_NIL
, dev
->nodename
, "mode", NULL
);
656 if (IS_ERR(be
->mode
)) {
657 err
= PTR_ERR(be
->mode
);
659 xenbus_dev_fatal(dev
, err
, "reading mode");
663 device_type
= xenbus_read(XBT_NIL
, dev
->otherend
, "device-type", NULL
);
664 if (!IS_ERR(device_type
)) {
665 cdrom
= strcmp(device_type
, "cdrom") == 0;
669 /* Front end dir is a number, which is used as the handle. */
670 err
= kstrtoul(strrchr(dev
->otherend
, '/') + 1, 0, &handle
);
677 err
= xen_vbd_create(be
->blkif
, handle
, major
, minor
,
678 !strchr(be
->mode
, 'w'), cdrom
);
681 xenbus_dev_fatal(dev
, err
, "creating vbd structure");
683 err
= xenvbd_sysfs_addif(dev
);
685 xen_vbd_free(&be
->blkif
->vbd
);
686 xenbus_dev_fatal(dev
, err
, "creating sysfs entries");
696 /* We're potentially connected now */
697 xen_update_blkif_status(be
->blkif
);
703 * Callback received when the frontend's state changes.
705 static void frontend_changed(struct xenbus_device
*dev
,
706 enum xenbus_state frontend_state
)
708 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
711 pr_debug("%s %p %s\n", __func__
, dev
, xenbus_strstate(frontend_state
));
713 switch (frontend_state
) {
714 case XenbusStateInitialising
:
715 if (dev
->state
== XenbusStateClosed
) {
716 pr_info("%s: prepare for reconnect\n", dev
->nodename
);
717 xenbus_switch_state(dev
, XenbusStateInitWait
);
721 case XenbusStateInitialised
:
722 case XenbusStateConnected
:
724 * Ensure we connect even when two watches fire in
725 * close succession and we miss the intermediate value
728 if (dev
->state
== XenbusStateConnected
)
732 * Enforce precondition before potential leak point.
733 * xen_blkif_disconnect() is idempotent.
735 err
= xen_blkif_disconnect(be
->blkif
);
737 xenbus_dev_fatal(dev
, err
, "pending I/O");
741 err
= connect_ring(be
);
744 xen_update_blkif_status(be
->blkif
);
747 case XenbusStateClosing
:
748 xenbus_switch_state(dev
, XenbusStateClosing
);
751 case XenbusStateClosed
:
752 xen_blkif_disconnect(be
->blkif
);
753 xenbus_switch_state(dev
, XenbusStateClosed
);
754 if (xenbus_dev_is_online(dev
))
756 /* fall through if not online */
757 case XenbusStateUnknown
:
758 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
759 device_unregister(&dev
->dev
);
763 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
770 /* ** Connection ** */
774 * Write the physical details regarding the block device to the store, and
775 * switch to Connected state.
777 static void connect(struct backend_info
*be
)
779 struct xenbus_transaction xbt
;
781 struct xenbus_device
*dev
= be
->dev
;
783 pr_debug("%s %s\n", __func__
, dev
->otherend
);
785 /* Supply the information about the device the frontend needs */
787 err
= xenbus_transaction_start(&xbt
);
789 xenbus_dev_fatal(dev
, err
, "starting transaction");
793 /* If we can't advertise it is OK. */
794 xen_blkbk_flush_diskcache(xbt
, be
, be
->blkif
->vbd
.flush_support
);
796 xen_blkbk_discard(xbt
, be
);
798 xen_blkbk_barrier(xbt
, be
, be
->blkif
->vbd
.flush_support
);
800 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-persistent", "%u", 1);
802 xenbus_dev_fatal(dev
, err
, "writing %s/feature-persistent",
806 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-max-indirect-segments", "%u",
807 MAX_INDIRECT_SEGMENTS
);
809 dev_warn(&dev
->dev
, "writing %s/feature-max-indirect-segments (%d)",
812 err
= xenbus_printf(xbt
, dev
->nodename
, "sectors", "%llu",
813 (unsigned long long)vbd_sz(&be
->blkif
->vbd
));
815 xenbus_dev_fatal(dev
, err
, "writing %s/sectors",
820 /* FIXME: use a typename instead */
821 err
= xenbus_printf(xbt
, dev
->nodename
, "info", "%u",
822 be
->blkif
->vbd
.type
|
823 (be
->blkif
->vbd
.readonly
? VDISK_READONLY
: 0));
825 xenbus_dev_fatal(dev
, err
, "writing %s/info",
829 err
= xenbus_printf(xbt
, dev
->nodename
, "sector-size", "%lu",
831 bdev_logical_block_size(be
->blkif
->vbd
.bdev
));
833 xenbus_dev_fatal(dev
, err
, "writing %s/sector-size",
837 err
= xenbus_printf(xbt
, dev
->nodename
, "physical-sector-size", "%u",
838 bdev_physical_block_size(be
->blkif
->vbd
.bdev
));
840 xenbus_dev_error(dev
, err
, "writing %s/physical-sector-size",
843 err
= xenbus_transaction_end(xbt
, 0);
847 xenbus_dev_fatal(dev
, err
, "ending transaction");
849 err
= xenbus_switch_state(dev
, XenbusStateConnected
);
851 xenbus_dev_fatal(dev
, err
, "%s: switching to Connected state",
856 xenbus_transaction_end(xbt
, 1);
860 static int connect_ring(struct backend_info
*be
)
862 struct xenbus_device
*dev
= be
->dev
;
863 unsigned long ring_ref
;
865 unsigned int pers_grants
;
866 char protocol
[64] = "";
869 pr_debug("%s %s\n", __func__
, dev
->otherend
);
871 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "ring-ref", "%lu",
872 &ring_ref
, "event-channel", "%u", &evtchn
, NULL
);
874 xenbus_dev_fatal(dev
, err
,
875 "reading %s/ring-ref and event-channel",
880 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_DEFAULT
;
881 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "protocol",
882 "%63s", protocol
, NULL
);
884 strcpy(protocol
, "unspecified, assuming default");
885 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_NATIVE
))
886 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
887 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_32
))
888 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_32
;
889 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_64
))
890 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_64
;
892 xenbus_dev_fatal(dev
, err
, "unknown fe protocol %s", protocol
);
895 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
896 "feature-persistent", "%u",
901 be
->blkif
->vbd
.feature_gnt_persistent
= pers_grants
;
902 be
->blkif
->vbd
.overflow_max_grants
= 0;
904 pr_info("ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
905 ring_ref
, evtchn
, be
->blkif
->blk_protocol
, protocol
,
906 pers_grants
? "persistent grants" : "");
908 /* Map the shared frame, irq etc. */
909 err
= xen_blkif_map(be
->blkif
, ring_ref
, evtchn
);
911 xenbus_dev_fatal(dev
, err
, "mapping ring-ref %lu port %u",
919 static const struct xenbus_device_id xen_blkbk_ids
[] = {
924 static struct xenbus_driver xen_blkbk_driver
= {
925 .ids
= xen_blkbk_ids
,
926 .probe
= xen_blkbk_probe
,
927 .remove
= xen_blkbk_remove
,
928 .otherend_changed
= frontend_changed
931 int xen_blkif_xenbus_init(void)
933 return xenbus_register_backend(&xen_blkbk_driver
);