ia64/xen-unstable

changeset 6618:87ea297c1d3a

Add driver to use the kernel's xenbus connection from user-space.
The driver exports xs_talkv to user-space, allowing programs running in
the domain to use the kernel's xenbus connection. Intended use is driver
domain device configuration where the driver domain is not the domain
running xenstored.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Sat Sep 03 16:52:12 2005 +0000 (2005-09-03)
parents c7e4e2fc4f4a
children 8a228cbb69fe
files linux-2.6-xen-sparse/drivers/xen/xenbus/Makefile linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c linux-2.6-xen-sparse/include/asm-xen/linux-public/xenbus_dev.h
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/xenbus/Makefile	Sat Sep 03 16:32:53 2005 +0000
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/xenbus/Makefile	Sat Sep 03 16:52:12 2005 +0000
     1.3 @@ -4,3 +4,4 @@ xenbus-objs =
     1.4  xenbus-objs += xenbus_comms.o
     1.5  xenbus-objs += xenbus_xs.o
     1.6  xenbus-objs += xenbus_probe.o 
     1.7 +xenbus-objs += xenbus_dev.o 
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_dev.c	Sat Sep 03 16:52:12 2005 +0000
     2.3 @@ -0,0 +1,186 @@
     2.4 +/*
     2.5 + * xenbus_dev.c
     2.6 + * 
     2.7 + * Driver giving user-space access to the kernel's xenbus connection
     2.8 + * to xenstore.
     2.9 + * 
    2.10 + * Copyright (c) 2005, Christian Limpach
    2.11 + * 
    2.12 + * This file may be distributed separately from the Linux kernel, or
    2.13 + * incorporated into other software packages, subject to the following license:
    2.14 + * 
    2.15 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    2.16 + * of this source file (the "Software"), to deal in the Software without
    2.17 + * restriction, including without limitation the rights to use, copy, modify,
    2.18 + * merge, publish, distribute, sublicense, and/or sell copies of the Software,
    2.19 + * and to permit persons to whom the Software is furnished to do so, subject to
    2.20 + * the following conditions:
    2.21 + * 
    2.22 + * The above copyright notice and this permission notice shall be included in
    2.23 + * all copies or substantial portions of the Software.
    2.24 + * 
    2.25 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    2.26 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    2.27 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    2.28 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    2.29 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    2.30 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    2.31 + * IN THE SOFTWARE.
    2.32 + */
    2.33 +
    2.34 +#include <linux/config.h>
    2.35 +#include <linux/kernel.h>
    2.36 +#include <linux/errno.h>
    2.37 +#include <linux/uio.h>
    2.38 +#include <linux/notifier.h>
    2.39 +#include <linux/wait.h>
    2.40 +#include <linux/fs.h>
    2.41 +
    2.42 +#include "xenstored.h"
    2.43 +#include "xenbus_comms.h"
    2.44 +
    2.45 +#include <asm/uaccess.h>
    2.46 +#include <asm-xen/xenbus.h>
    2.47 +#include <asm-xen/linux-public/xenbus_dev.h>
    2.48 +#include <asm-xen/xen_proc.h>
    2.49 +
    2.50 +struct xenbus_dev_data {
    2.51 +	int in_transaction;
    2.52 +};
    2.53 +
    2.54 +static struct proc_dir_entry *xenbus_dev_intf;
    2.55 +
    2.56 +void *xs_talkv(enum xsd_sockmsg_type type, const struct kvec *iovec,
    2.57 +	       unsigned int num_vecs, unsigned int *len);
    2.58 +
    2.59 +static int xenbus_dev_talkv(struct xenbus_dev_data *u, unsigned long data)
    2.60 +{
    2.61 +	struct xenbus_dev_talkv xt;
    2.62 +	unsigned int len;
    2.63 +	void *resp, *base;
    2.64 +	struct kvec *iovec;
    2.65 +	int ret = -EFAULT, v = 0;
    2.66 +
    2.67 +	if (copy_from_user(&xt, (void *)data, sizeof(xt)))
    2.68 +		return -EFAULT;
    2.69 +
    2.70 +	iovec = kmalloc(xt.num_vecs * sizeof(struct kvec), GFP_KERNEL);
    2.71 +	if (iovec == NULL)
    2.72 +		return -ENOMEM;
    2.73 +
    2.74 +	if (copy_from_user(iovec, xt.iovec,
    2.75 +			   xt.num_vecs * sizeof(struct kvec)))
    2.76 +		goto out;
    2.77 +
    2.78 +	for (v = 0; v < xt.num_vecs; v++) {
    2.79 +		base = iovec[v].iov_base;
    2.80 +		iovec[v].iov_base = kmalloc(iovec[v].iov_len, GFP_KERNEL);
    2.81 +		if (iovec[v].iov_base == NULL ||
    2.82 +		    copy_from_user(iovec[v].iov_base, base, iovec[v].iov_len))
    2.83 +		{
    2.84 +			if (iovec[v].iov_base)
    2.85 +				kfree(iovec[v].iov_base);
    2.86 +			else
    2.87 +				ret = -ENOMEM;
    2.88 +			v--;
    2.89 +			goto out;
    2.90 +		}
    2.91 +	}
    2.92 +
    2.93 +	resp = xs_talkv(xt.type, iovec, xt.num_vecs, &len);
    2.94 +	if (IS_ERR(resp)) {
    2.95 +		ret = PTR_ERR(resp);
    2.96 +		goto out;
    2.97 +	}
    2.98 +
    2.99 +	switch (xt.type) {
   2.100 +	case XS_TRANSACTION_START:
   2.101 +		u->in_transaction = 1;
   2.102 +		break;
   2.103 +	case XS_TRANSACTION_END:
   2.104 +		u->in_transaction = 0;
   2.105 +		break;
   2.106 +	default:
   2.107 +		break;
   2.108 +	}
   2.109 +
   2.110 +	ret = len;
   2.111 +	if (len > xt.len)
   2.112 +		len = xt.len;
   2.113 +
   2.114 +	if (copy_to_user(xt.buf, resp, len))
   2.115 +		ret = -EFAULT;
   2.116 +
   2.117 +	kfree(resp);
   2.118 + out:
   2.119 +	while (v-- > 0)
   2.120 +		kfree(iovec[v].iov_base);
   2.121 +	kfree(iovec);
   2.122 +	return ret;
   2.123 +}
   2.124 +
   2.125 +static int xenbus_dev_ioctl(struct inode *inode, struct file *filp,
   2.126 +			    unsigned int cmd, unsigned long data)
   2.127 +{
   2.128 +	struct xenbus_dev_data *u = filp->private_data;
   2.129 +	int ret = -ENOSYS;
   2.130 +
   2.131 +	switch (cmd) {
   2.132 +	case IOCTL_XENBUS_DEV_TALKV:
   2.133 +		ret = xenbus_dev_talkv(u, data);
   2.134 +		break;
   2.135 +	default:
   2.136 +		ret = -EINVAL;
   2.137 +		break;
   2.138 +	}
   2.139 +	return ret;
   2.140 +}
   2.141 +
   2.142 +static int xenbus_dev_open(struct inode *inode, struct file *filp)
   2.143 +{
   2.144 +	struct xenbus_dev_data *u;
   2.145 +
   2.146 +	u = kmalloc(sizeof(*u), GFP_KERNEL);
   2.147 +	if (u == NULL)
   2.148 +		return -ENOMEM;
   2.149 +
   2.150 +	memset(u, 0, sizeof(*u));
   2.151 +
   2.152 +	filp->private_data = u;
   2.153 +
   2.154 +	down(&xenbus_lock);
   2.155 +
   2.156 +	return 0;
   2.157 +}
   2.158 +
   2.159 +static int xenbus_dev_release(struct inode *inode, struct file *filp)
   2.160 +{
   2.161 +	struct xenbus_dev_data *u = filp->private_data;
   2.162 +
   2.163 +	if (u->in_transaction)
   2.164 +		xenbus_transaction_end(1);
   2.165 +
   2.166 +	up(&xenbus_lock);
   2.167 +
   2.168 +	kfree(u);
   2.169 +
   2.170 +	return 0;
   2.171 +}
   2.172 +
   2.173 +static struct file_operations xenbus_dev_file_ops = {
   2.174 +	ioctl: xenbus_dev_ioctl,
   2.175 +	open: xenbus_dev_open,
   2.176 +	release: xenbus_dev_release
   2.177 +};
   2.178 +
   2.179 +static int __init
   2.180 +xenbus_dev_init(void)
   2.181 +{
   2.182 +	xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
   2.183 +	if (xenbus_dev_intf)
   2.184 +		xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
   2.185 +
   2.186 +	return 0;
   2.187 +}
   2.188 +
   2.189 +__initcall(xenbus_dev_init);
     3.1 --- a/linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c	Sat Sep 03 16:32:53 2005 +0000
     3.2 +++ b/linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c	Sat Sep 03 16:52:12 2005 +0000
     3.3 @@ -106,10 +106,10 @@ void xenbus_debug_write(const char *str,
     3.4  }
     3.5  
     3.6  /* Send message to xs, get kmalloc'ed reply.  ERR_PTR() on error. */
     3.7 -static void *xs_talkv(enum xsd_sockmsg_type type,
     3.8 -		      const struct kvec *iovec,
     3.9 -		      unsigned int num_vecs,
    3.10 -		      unsigned int *len)
    3.11 +void *xs_talkv(enum xsd_sockmsg_type type,
    3.12 +	       const struct kvec *iovec,
    3.13 +	       unsigned int num_vecs,
    3.14 +	       unsigned int *len)
    3.15  {
    3.16  	struct xsd_sockmsg msg;
    3.17  	void *ret = NULL;
    3.18 @@ -557,7 +557,7 @@ static int watch_thread(void *unused)
    3.19  			BUG_ON(!w);
    3.20  			w->callback(w, node);
    3.21  			kfree(node);
    3.22 -		} else
    3.23 +		} else if (node)
    3.24  			printk(KERN_WARNING "XENBUS xs_read_watch: %li\n",
    3.25  			       PTR_ERR(node));
    3.26  		up(&xenbus_lock);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/linux-public/xenbus_dev.h	Sat Sep 03 16:52:12 2005 +0000
     4.3 @@ -0,0 +1,47 @@
     4.4 +/*
     4.5 + * xenbus_dev.h
     4.6 + * 
     4.7 + * Copyright (c) 2005, Christian Limpach
     4.8 + * 
     4.9 + * This file may be distributed separately from the Linux kernel, or
    4.10 + * incorporated into other software packages, subject to the following license:
    4.11 + * 
    4.12 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    4.13 + * of this source file (the "Software"), to deal in the Software without
    4.14 + * restriction, including without limitation the rights to use, copy, modify,
    4.15 + * merge, publish, distribute, sublicense, and/or sell copies of the Software,
    4.16 + * and to permit persons to whom the Software is furnished to do so, subject to
    4.17 + * the following conditions:
    4.18 + * 
    4.19 + * The above copyright notice and this permission notice shall be included in
    4.20 + * all copies or substantial portions of the Software.
    4.21 + * 
    4.22 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    4.23 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    4.24 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    4.25 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    4.26 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    4.27 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    4.28 + * IN THE SOFTWARE.
    4.29 + */
    4.30 +
    4.31 +#ifndef _XENBUS_DEV_H_
    4.32 +#define _XENBUS_DEV_H_
    4.33 +
    4.34 +struct xenbus_dev_talkv {
    4.35 +	enum xsd_sockmsg_type type;
    4.36 +	const struct kvec *iovec;
    4.37 +	unsigned int num_vecs;
    4.38 +	char *buf;
    4.39 +	unsigned int len;
    4.40 +};
    4.41 +
    4.42 +/*
    4.43 + * @cmd: IOCTL_XENBUS_DEV_TALKV
    4.44 + * @arg: struct xenbus_dev_talkv
    4.45 + * Return: 0 on success, error code on failure.
    4.46 + */
    4.47 +#define	IOCTL_XENBUS_DEV_TALKV \
    4.48 +	_IOC(_IOC_NONE, 'X', 0, sizeof(struct xenbus_dev_talkv))
    4.49 +
    4.50 +#endif /* _XENBUS_DEV_H_ */