ia64/xen-unstable

changeset 6277:b399599f2ff8

Switch block device setup/teardown over to xenbus/xenstore.
Remove device probing support over the device channel.
g/c all the control message code.
Simplify and cleanup internal function calling code (explicit arguments
instead of passing structs around).
Also includes:
Changed "readonly" to "read-only". "backend" is still not hyphenated, but
that seems to be fairly common usage.
Block backends use xenbus_dev_error, making debugging nicer.
The block front end is told explicitly what domid to use to talk to the
backend. There's speculation that fake domid-like tokens may be given
for cluster purposes in future, so deriving from the backend dir name is
not guaranteed to work. Backends still derive front-end ids, using a
xenbus helper (xenbus_uuid_to_domid).
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Aug 19 12:13:31 2005 +0000 (2005-08-19)
parents 2d7a9bae9814
children 73b23942d6f5
files linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Fri Aug 19 12:13:31 2005 +0000
     1.3 @@ -0,0 +1,309 @@
     1.4 +/*  Xenbus code for blkif backend
     1.5 +    Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.20 +*/
    1.21 +#include <stdarg.h>
    1.22 +#include <linux/module.h>
    1.23 +#include <asm-xen/xenbus.h>
    1.24 +#include "common.h"
    1.25 +
    1.26 +struct backend_info
    1.27 +{
    1.28 +	struct xenbus_device *dev;
    1.29 +
    1.30 +	/* our communications channel */
    1.31 +	blkif_t *blkif;
    1.32 +	struct vbd *vbd;
    1.33 +
    1.34 +	long int frontend_id;
    1.35 +	long int pdev;
    1.36 +	long int readonly;
    1.37 +
    1.38 +	/* watch back end for changes */
    1.39 +	struct xenbus_watch backend_watch;
    1.40 +
    1.41 +	/* watch front end for changes */
    1.42 +	struct xenbus_watch watch;
    1.43 +	char *frontpath;
    1.44 +};
    1.45 +
    1.46 +static int blkback_remove(struct xenbus_device *dev)
    1.47 +{
    1.48 +	struct backend_info *be = dev->data;
    1.49 +
    1.50 +	unregister_xenbus_watch(&be->watch);
    1.51 +	unregister_xenbus_watch(&be->backend_watch);
    1.52 +	vbd_free(be->blkif, be->vbd);
    1.53 +	blkif_put(be->blkif);
    1.54 +	kfree(be->frontpath);
    1.55 +	kfree(be);
    1.56 +	return 0;
    1.57 +}
    1.58 +
    1.59 +/* Front end tells us frame. */
    1.60 +static void frontend_changed(struct xenbus_watch *watch, const char *node)
    1.61 +{
    1.62 +	unsigned long sharedmfn;
    1.63 +	unsigned int evtchn;
    1.64 +	int err;
    1.65 +	struct backend_info *be
    1.66 +		= container_of(watch, struct backend_info, watch);
    1.67 +
    1.68 +	// printk("Got front end event on %s (%s)\n", node, be->frontpath);
    1.69 +
    1.70 +	if (vbd_is_active(be->vbd)) {
    1.71 +		/* If other end is gone, delete ourself. */
    1.72 +		if (!xenbus_exists(be->frontpath, "")) {
    1.73 +			// printk("Removing...\n");
    1.74 +			xenbus_rm(be->dev->nodename, "");
    1.75 +			device_unregister(&be->dev->dev);
    1.76 +		}
    1.77 +		return;
    1.78 +	}
    1.79 +
    1.80 +#ifndef CONFIG_XEN_BLKDEV_GRANT
    1.81 +	err = xenbus_gather(be->frontpath, "shared-frame", "%lu", &sharedmfn,
    1.82 +			    "event-channel", "%u", &evtchn, NULL);
    1.83 +	if (err) {
    1.84 +		xenbus_dev_error(be->dev, err, 
    1.85 +				 "reading %s/shared-frame and event-channel",
    1.86 +				 be->frontpath);
    1.87 +		return;
    1.88 +	}
    1.89 +#else
    1.90 +	err = xenbus_gather(be->frontpath, "grant-id", "%lu", &sharedmfn,
    1.91 +			    "event-channel", "%u", &evtchn, NULL);
    1.92 +	if (err) {
    1.93 +		xenbus_dev_error(be->dev, err, 
    1.94 +				 "reading %s/grant-id and event-channel",
    1.95 +				 be->frontpath);
    1.96 +		return;
    1.97 +	}
    1.98 +#endif
    1.99 +
   1.100 +	/* Domains must use same shared frame for all vbds. */
   1.101 +	if (be->blkif->status == CONNECTED &&
   1.102 +	    (evtchn != be->blkif->remote_evtchn ||
   1.103 +	     sharedmfn != be->blkif->shmem_frame)) {
   1.104 +		xenbus_dev_error(be->dev, err,
   1.105 +				 "Shared frame/evtchn %li/%u not same as"
   1.106 +				 " old %li/%u",
   1.107 +				 sharedmfn, evtchn,
   1.108 +				 be->blkif->shmem_frame,
   1.109 +				 be->blkif->remote_evtchn);
   1.110 +		return;
   1.111 +	}
   1.112 +
   1.113 +	/* Supply the information about the device the frontend needs */
   1.114 +	err = xenbus_transaction_start(be->dev->nodename);
   1.115 +	if (err) {
   1.116 +		xenbus_dev_error(be->dev, err, "starting transaction");
   1.117 +		return;
   1.118 +	}
   1.119 +
   1.120 +	err = xenbus_printf(be->dev->nodename, "sectors", "%lu",
   1.121 +			    vbd_size(be->vbd));
   1.122 +	if (err) {
   1.123 +		xenbus_dev_error(be->dev, err, "writing %s/sectors",
   1.124 +				 be->dev->nodename);
   1.125 +		goto abort;
   1.126 +	}
   1.127 +
   1.128 +	/* FIXME: use a typename instead */
   1.129 +	err = xenbus_printf(be->dev->nodename, "info", "%u",
   1.130 +			    vbd_info(be->vbd));
   1.131 +	if (err) {
   1.132 +		xenbus_dev_error(be->dev, err, "writing %s/info",
   1.133 +				 be->dev->nodename);
   1.134 +		goto abort;
   1.135 +	}
   1.136 +	err = xenbus_printf(be->dev->nodename, "sector-size", "%lu",
   1.137 +			    vbd_secsize(be->vbd));
   1.138 +	if (err) {
   1.139 +		xenbus_dev_error(be->dev, err, "writing %s/sector-size",
   1.140 +				 be->dev->nodename);
   1.141 +		goto abort;
   1.142 +	}
   1.143 +
   1.144 +	/* First vbd?  We need to map the shared frame, irq etc. */
   1.145 +	if (be->blkif->status != CONNECTED) {
   1.146 +		err = blkif_map(be->blkif, sharedmfn, evtchn);
   1.147 +		if (err) {
   1.148 +			xenbus_dev_error(be->dev, err,
   1.149 +					 "mapping shared-frame %lu port %u",
   1.150 +					 sharedmfn, evtchn);
   1.151 +			goto abort;
   1.152 +		}
   1.153 +	}
   1.154 +
   1.155 +	/* We're ready, activate. */
   1.156 +	vbd_activate(be->blkif, be->vbd);
   1.157 +
   1.158 +	xenbus_transaction_end(0);
   1.159 +	xenbus_dev_ok(be->dev);
   1.160 +
   1.161 +	return;
   1.162 +
   1.163 +abort:
   1.164 +	xenbus_transaction_end(1);
   1.165 +}
   1.166 +
   1.167 +/* 
   1.168 +   Setup supplies physical device.  
   1.169 +   We provide event channel and device details to front end.
   1.170 +   Frontend supplies shared frame and event channel.
   1.171 + */
   1.172 +static void backend_changed(struct xenbus_watch *watch, const char *node)
   1.173 +{
   1.174 +	int err;
   1.175 +	char *p;
   1.176 +	char *frontend;
   1.177 +	long int handle, pdev;
   1.178 +	struct backend_info *be
   1.179 +		= container_of(watch, struct backend_info, backend_watch);
   1.180 +	struct xenbus_device *dev = be->dev;
   1.181 +
   1.182 +	err = xenbus_scanf(dev->nodename, "frontend-id", "%li",
   1.183 +			   &be->frontend_id);
   1.184 +	if (err == -ENOENT || err == -ERANGE)
   1.185 +		goto out;
   1.186 +	if (err < 0) {
   1.187 +		xenbus_dev_error(dev, err, "Reading frontend-id");
   1.188 +		goto out;
   1.189 +	}
   1.190 +
   1.191 +	err = xenbus_scanf(dev->nodename, "physical-device", "%li", &pdev);
   1.192 +	if (err == -ENOENT || err == -ERANGE)
   1.193 +		goto out;
   1.194 +	if (err < 0) {
   1.195 +		xenbus_dev_error(dev, err, "Reading physical-device");
   1.196 +		goto out;
   1.197 +	}
   1.198 +	if (be->pdev && be->pdev != pdev) {
   1.199 +		printk(KERN_WARNING
   1.200 +		       "changing physical-device not supported\n");
   1.201 +		return;
   1.202 +	}
   1.203 +	be->pdev = pdev;
   1.204 +
   1.205 +	frontend = xenbus_read(dev->nodename, "frontend", NULL);
   1.206 +	if (IS_ERR(frontend))
   1.207 +		return;
   1.208 +	if (strlen(frontend) == 0) {
   1.209 +		kfree(frontend);
   1.210 +		return;
   1.211 +	}
   1.212 +
   1.213 +	/* If there's a read-only node, we're read only. */
   1.214 +	p = xenbus_read(dev->nodename, "read-only", NULL);
   1.215 +	if (!IS_ERR(p)) {
   1.216 +		be->readonly = 1;
   1.217 +		kfree(p);
   1.218 +	}
   1.219 +
   1.220 +	if (!be->frontpath || strcmp(frontend, be->frontpath)) {
   1.221 +		if (be->watch.node)
   1.222 +			unregister_xenbus_watch(&be->watch);
   1.223 +		if (be->frontpath)
   1.224 +			kfree(be->frontpath);
   1.225 +		be->frontpath = frontend;
   1.226 +		be->watch.node = be->frontpath;
   1.227 +		be->watch.callback = frontend_changed;
   1.228 +		err = register_xenbus_watch(&be->watch);
   1.229 +		if (err)
   1.230 +			goto out;
   1.231 +
   1.232 +		/* Front end dir is a number, which is used as the handle. */
   1.233 +		p = strrchr(be->frontpath, '/') + 1;
   1.234 +		handle = simple_strtoul(p, NULL, 0);
   1.235 +
   1.236 +		be->blkif = blkif_find(be->frontend_id);
   1.237 +		if (IS_ERR(be->blkif)) {
   1.238 +			err = PTR_ERR(be->blkif);
   1.239 +			be->blkif = NULL;
   1.240 +			goto free_watch;
   1.241 +		}
   1.242 +
   1.243 +		be->vbd = vbd_create(be->blkif, handle, be->pdev,
   1.244 +				     be->readonly);
   1.245 +		if (IS_ERR(be->vbd)) {
   1.246 +			err = PTR_ERR(be->vbd);
   1.247 +			blkif_put(be->blkif);
   1.248 +			be->blkif = NULL;
   1.249 +			be->vbd = NULL;
   1.250 +			goto free_watch;
   1.251 +		}
   1.252 +
   1.253 +		frontend_changed(&be->watch, be->frontpath);
   1.254 +	} else
   1.255 +		kfree(frontend);
   1.256 +
   1.257 +	return;
   1.258 +
   1.259 + free_watch:
   1.260 +	unregister_xenbus_watch(&be->watch);
   1.261 +	be->watch.node = NULL;
   1.262 +	kfree(be->frontpath);
   1.263 +	be->frontpath = NULL;
   1.264 + out:
   1.265 +	return;
   1.266 +}
   1.267 +
   1.268 +static int blkback_probe(struct xenbus_device *dev,
   1.269 +			 const struct xenbus_device_id *id)
   1.270 +{
   1.271 +	struct backend_info *be;
   1.272 +	int err;
   1.273 +
   1.274 +	be = kmalloc(sizeof(*be), GFP_KERNEL);
   1.275 +	if (!be)
   1.276 +		return -ENOMEM;
   1.277 +
   1.278 +	memset(be, 0, sizeof(*be));
   1.279 +
   1.280 +	be->dev = dev;
   1.281 +	be->backend_watch.node = dev->nodename;
   1.282 +	be->backend_watch.callback = backend_changed;
   1.283 +	err = register_xenbus_watch(&be->backend_watch);
   1.284 +	if (err)
   1.285 +		goto free_be;
   1.286 +
   1.287 +	dev->data = be;
   1.288 +
   1.289 +	backend_changed(&be->backend_watch, dev->nodename);
   1.290 +	return err;
   1.291 + free_be:
   1.292 +	kfree(be);
   1.293 +	return err;
   1.294 +}
   1.295 +
   1.296 +static struct xenbus_device_id blkback_ids[] = {
   1.297 +	{ "vbd" },
   1.298 +	{ "" }
   1.299 +};
   1.300 +
   1.301 +static struct xenbus_driver blkback = {
   1.302 +	.name = __stringify(KBUILD_MODNAME),
   1.303 +	.owner = THIS_MODULE,
   1.304 +	.ids = blkback_ids,
   1.305 +	.probe = blkback_probe,
   1.306 +	.remove = blkback_remove,
   1.307 +};
   1.308 +
   1.309 +void blkif_xenbus_init(void)
   1.310 +{
   1.311 +	xenbus_register_backend(&blkback);
   1.312 +}