From acd8f51fc654fdcd8c5b220d743063bedf61704b Mon Sep 17 00:00:00 2001 From: Roger Pau Monne Date: Thu, 21 May 2015 19:06:09 +0200 Subject: [PATCH] blkback: add support for hotplug scripts Hotplug scripts are needed in order to use fancy disk configurations in xl, like iSCSI disks. The job of hotplug scripts is to locally attach the disk and present it to blkback as a block device or a regular file. This change introduces a new xenstore node in the blkback hierarchy, called "path". This is a straigh replacement for the "params" node, which was used before. Hotplug scripts will need to read the "params" node, perform whatever actions are necessary and then write the "path" node. The hotplug script is also in charge of detaching the disk once the domain has been shutdown. Sponsored by: Citrix Systems R&D --- sys/dev/xen/blkback/blkback.c | 196 +++++++++++++++++++++++++----------------- 1 file changed, 118 insertions(+), 78 deletions(-) diff --git a/sys/dev/xen/blkback/blkback.c b/sys/dev/xen/blkback/blkback.c index d352242..6b97740 100644 --- a/sys/dev/xen/blkback/blkback.c +++ b/sys/dev/xen/blkback/blkback.c @@ -802,6 +802,9 @@ struct xbb_softc { /** How many times we have run out of request structures */ uint64_t request_shortages; + + /** Watch to wait for hotplug script execution */ + struct xs_watch hotplug_watch; }; /*---------------------------- Request Processing ----------------------------*/ @@ -3399,7 +3402,7 @@ xbb_connect(struct xbb_softc *xbb) { int error; - if (xenbus_get_state(xbb->dev) == XenbusStateConnected) + if (xenbus_get_state(xbb->dev) != XenbusStateInitialised) return; if (xbb_collect_frontend_info(xbb) != 0) @@ -3676,6 +3679,106 @@ xbb_setup_sysctl(struct xbb_softc *xbb) "communication channel pages (negotiated)"); } +static void +xbb_attach_disk(struct xs_watch *watch, const char **vec, unsigned int len) +{ + device_t dev; + struct xbb_softc *xbb; + int error; + + dev = (device_t) watch->callback_data; + xbb = device_get_softc(dev); + + error = xs_gather(XST_NIL, xenbus_get_node(dev), "path", NULL, + &xbb->dev_name, NULL); + if (error != 0) + return; + + xs_unregister_watch(watch); + free(watch->node, M_XENBLOCKBACK); + + /* Collect physical device information. */ + error = xs_gather(XST_NIL, xenbus_get_otherend_path(xbb->dev), + "device-type", NULL, &xbb->dev_type, + NULL); + if (error != 0) + xbb->dev_type = NULL; + + error = xs_gather(XST_NIL, xenbus_get_node(dev), + "mode", NULL, &xbb->dev_mode, + NULL); + if (error != 0) { + xbb_attach_failed(xbb, error, "reading backend fields at %s", + xenbus_get_node(dev)); + return; + } + + /* Parse fopen style mode flags. */ + if (strchr(xbb->dev_mode, 'w') == NULL) + xbb->flags |= XBBF_READ_ONLY; + + /* + * Verify the physical device is present and can support + * the desired I/O mode. + */ + error = xbb_open_backend(xbb); + if (error != 0) { + xbb_attach_failed(xbb, error, "Unable to open %s", + xbb->dev_name); + return; + } + + /* Use devstat(9) for recording statistics. */ + xbb->xbb_stats = devstat_new_entry("xbb", device_get_unit(xbb->dev), + xbb->sector_size, + DEVSTAT_ALL_SUPPORTED, + DEVSTAT_TYPE_DIRECT + | DEVSTAT_TYPE_IF_OTHER, + DEVSTAT_PRIORITY_OTHER); + + xbb->xbb_stats_in = devstat_new_entry("xbbi", device_get_unit(xbb->dev), + xbb->sector_size, + DEVSTAT_ALL_SUPPORTED, + DEVSTAT_TYPE_DIRECT + | DEVSTAT_TYPE_IF_OTHER, + DEVSTAT_PRIORITY_OTHER); + /* + * Setup sysctl variables. + */ + xbb_setup_sysctl(xbb); + + /* + * Create a taskqueue for doing work that must occur from a + * thread context. + */ + xbb->io_taskqueue = taskqueue_create_fast(device_get_nameunit(dev), + M_NOWAIT, + taskqueue_thread_enqueue, + /*contxt*/&xbb->io_taskqueue); + if (xbb->io_taskqueue == NULL) { + xbb_attach_failed(xbb, error, "Unable to create taskqueue"); + return; + } + + taskqueue_start_threads(&xbb->io_taskqueue, + /*num threads*/1, + /*priority*/PWAIT, + /*thread name*/ + "%s taskq", device_get_nameunit(dev)); + + /* Update hot-plug status to satisfy xend. */ + error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev), + "hotplug-status", "connected"); + if (error) { + xbb_attach_failed(xbb, error, "writing %s/hotplug-status", + xenbus_get_node(xbb->dev)); + return; + } + + /* Tell the front end that we are ready to connect. */ + xenbus_set_state(dev, XenbusStateInitialised); +} + /** * Attach to a XenBus device that has been claimed by our probe routine. * @@ -3689,6 +3792,7 @@ xbb_attach(device_t dev) struct xbb_softc *xbb; int error; u_int max_ring_page_order; + struct sbuf *watch_path; DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); @@ -3770,88 +3874,24 @@ xbb_attach(device_t dev) return (error); } - /* Collect physical device information. */ - error = xs_gather(XST_NIL, xenbus_get_otherend_path(xbb->dev), - "device-type", NULL, &xbb->dev_type, - NULL); - if (error != 0) - xbb->dev_type = NULL; - - error = xs_gather(XST_NIL, xenbus_get_node(dev), - "mode", NULL, &xbb->dev_mode, - "params", NULL, &xbb->dev_name, - NULL); - if (error != 0) { - xbb_attach_failed(xbb, error, "reading backend fields at %s", - xenbus_get_node(dev)); - return (ENXIO); - } - - /* Parse fopen style mode flags. */ - if (strchr(xbb->dev_mode, 'w') == NULL) - xbb->flags |= XBBF_READ_ONLY; - /* - * Verify the physical device is present and can support - * the desired I/O mode. - */ - DROP_GIANT(); - error = xbb_open_backend(xbb); - PICKUP_GIANT(); + * We need to wait for hotplug script execution before + * moving forward. + */ + watch_path = xs_join(xenbus_get_node(xbb->dev), "path"); + xbb->hotplug_watch.callback_data = (uintptr_t)dev; + xbb->hotplug_watch.callback = xbb_attach_disk; + xbb->hotplug_watch.node = strdup(sbuf_data(watch_path), M_XENBLOCKBACK); + sbuf_delete(watch_path); + error = xs_register_watch(&xbb->hotplug_watch); if (error != 0) { - xbb_attach_failed(xbb, error, "Unable to open %s", - xbb->dev_name); - return (ENXIO); - } - - /* Use devstat(9) for recording statistics. */ - xbb->xbb_stats = devstat_new_entry("xbb", device_get_unit(xbb->dev), - xbb->sector_size, - DEVSTAT_ALL_SUPPORTED, - DEVSTAT_TYPE_DIRECT - | DEVSTAT_TYPE_IF_OTHER, - DEVSTAT_PRIORITY_OTHER); - - xbb->xbb_stats_in = devstat_new_entry("xbbi", device_get_unit(xbb->dev), - xbb->sector_size, - DEVSTAT_ALL_SUPPORTED, - DEVSTAT_TYPE_DIRECT - | DEVSTAT_TYPE_IF_OTHER, - DEVSTAT_PRIORITY_OTHER); - /* - * Setup sysctl variables. - */ - xbb_setup_sysctl(xbb); - - /* - * Create a taskqueue for doing work that must occur from a - * thread context. - */ - xbb->io_taskqueue = taskqueue_create_fast(device_get_nameunit(dev), - M_NOWAIT, - taskqueue_thread_enqueue, - /*contxt*/&xbb->io_taskqueue); - if (xbb->io_taskqueue == NULL) { - xbb_attach_failed(xbb, error, "Unable to create taskqueue"); - return (ENOMEM); - } - - taskqueue_start_threads(&xbb->io_taskqueue, - /*num threads*/1, - /*priority*/PWAIT, - /*thread name*/ - "%s taskq", device_get_nameunit(dev)); - - /* Update hot-plug status to satisfy xend. */ - error = xs_printf(XST_NIL, xenbus_get_node(xbb->dev), - "hotplug-status", "connected"); - if (error) { - xbb_attach_failed(xbb, error, "writing %s/hotplug-status", - xenbus_get_node(xbb->dev)); + xbb_attach_failed(xbb, error, "failed to create watch on %s", + xbb->hotplug_watch.node); + free(xbb->hotplug_watch.node, M_XENBLOCKBACK); return (error); } - /* Tell the front end that we are ready to connect. */ + /* Tell the toolstack blkback has attached. */ xenbus_set_state(dev, XenbusStateInitWait); return (0); -- 1.9.5 (Apple Git-50.3)