ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_dev.c @ 874:68d582b0ad05

xenbus: allow any xenbus command over /proc/xen/xenbus.

Signed-off-by: Diego Ongaro <diego.ongaro@citrix.com>
Signed-off-by: Alex Zeffertt <alex.zeffertt@eu.citrix.com>
Signed-off-by: Keir Fraser <keir.fraser@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 19 14:28:48 2009 +0100 (2009-05-19)
parents 88e01408ec0d
children bd7e30b58d12
line source
1 /*
2 * xenbus_dev.c
3 *
4 * Driver giving user-space access to the kernel's xenbus connection
5 * to xenstore.
6 *
7 * Copyright (c) 2005, Christian Limpach
8 * Copyright (c) 2005, Rusty Russell, IBM Corporation
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/uio.h>
38 #include <linux/notifier.h>
39 #include <linux/wait.h>
40 #include <linux/fs.h>
41 #include <linux/poll.h>
42 #include <linux/mutex.h>
44 #include "xenbus_comms.h"
46 #include <asm/uaccess.h>
47 #include <asm/hypervisor.h>
48 #include <xen/xenbus.h>
49 #include <xen/xen_proc.h>
50 #include <asm/hypervisor.h>
52 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
53 #include <xen/platform-compat.h>
54 #endif
56 struct xenbus_dev_transaction {
57 struct list_head list;
58 struct xenbus_transaction handle;
59 };
61 struct read_buffer {
62 struct list_head list;
63 unsigned int cons;
64 unsigned int len;
65 char msg[];
66 };
68 struct xenbus_dev_data {
69 /* In-progress transaction. */
70 struct list_head transactions;
72 /* Active watches. */
73 struct list_head watches;
75 /* Partial request. */
76 unsigned int len;
77 union {
78 struct xsd_sockmsg msg;
79 char buffer[PAGE_SIZE];
80 } u;
82 /* Response queue. */
83 struct list_head read_buffers;
84 wait_queue_head_t read_waitq;
86 struct mutex reply_mutex;
87 };
89 static struct proc_dir_entry *xenbus_dev_intf;
91 static ssize_t xenbus_dev_read(struct file *filp,
92 char __user *ubuf,
93 size_t len, loff_t *ppos)
94 {
95 struct xenbus_dev_data *u = filp->private_data;
96 struct read_buffer *rb;
97 int i, ret;
99 mutex_lock(&u->reply_mutex);
100 while (list_empty(&u->read_buffers)) {
101 mutex_unlock(&u->reply_mutex);
102 ret = wait_event_interruptible(u->read_waitq,
103 !list_empty(&u->read_buffers));
104 if (ret)
105 return ret;
106 mutex_lock(&u->reply_mutex);
107 }
109 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
110 for (i = 0; i < len;) {
111 put_user(rb->msg[rb->cons], ubuf + i);
112 i++;
113 rb->cons++;
114 if (rb->cons == rb->len) {
115 list_del(&rb->list);
116 kfree(rb);
117 if (list_empty(&u->read_buffers))
118 break;
119 rb = list_entry(u->read_buffers.next,
120 struct read_buffer, list);
121 }
122 }
123 mutex_unlock(&u->reply_mutex);
125 return i;
126 }
128 static void queue_reply(struct xenbus_dev_data *u,
129 char *data, unsigned int len)
130 {
131 struct read_buffer *rb;
133 if (len == 0)
134 return;
136 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
137 BUG_ON(rb == NULL);
139 rb->cons = 0;
140 rb->len = len;
142 memcpy(rb->msg, data, len);
144 list_add_tail(&rb->list, &u->read_buffers);
146 wake_up(&u->read_waitq);
147 }
149 struct watch_adapter
150 {
151 struct list_head list;
152 struct xenbus_watch watch;
153 struct xenbus_dev_data *dev_data;
154 char *token;
155 };
157 static void free_watch_adapter (struct watch_adapter *watch)
158 {
159 kfree(watch->watch.node);
160 kfree(watch->token);
161 kfree(watch);
162 }
164 static void watch_fired(struct xenbus_watch *watch,
165 const char **vec,
166 unsigned int len)
167 {
168 struct watch_adapter *adap =
169 container_of(watch, struct watch_adapter, watch);
170 struct xsd_sockmsg hdr;
171 const char *path, *token;
172 int path_len, tok_len, body_len, data_len = 0;
174 path = vec[XS_WATCH_PATH];
175 token = adap->token;
177 path_len = strlen(path) + 1;
178 tok_len = strlen(token) + 1;
179 if (len > 2)
180 data_len = vec[len] - vec[2] + 1;
181 body_len = path_len + tok_len + data_len;
183 hdr.type = XS_WATCH_EVENT;
184 hdr.len = body_len;
186 mutex_lock(&adap->dev_data->reply_mutex);
187 queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
188 queue_reply(adap->dev_data, (char *)path, path_len);
189 queue_reply(adap->dev_data, (char *)token, tok_len);
190 if (len > 2)
191 queue_reply(adap->dev_data, (char *)vec[2], data_len);
192 mutex_unlock(&adap->dev_data->reply_mutex);
193 }
195 static LIST_HEAD(watch_list);
197 static ssize_t xenbus_dev_write(struct file *filp,
198 const char __user *ubuf,
199 size_t len, loff_t *ppos)
200 {
201 struct xenbus_dev_data *u = filp->private_data;
202 struct xenbus_dev_transaction *trans = NULL;
203 uint32_t msg_type;
204 void *reply;
205 char *path, *token;
206 struct watch_adapter *watch, *tmp_watch;
207 int err, rc = len;
209 if ((len + u->len) > sizeof(u->u.buffer)) {
210 rc = -EINVAL;
211 goto out;
212 }
214 if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0) {
215 rc = -EFAULT;
216 goto out;
217 }
219 u->len += len;
220 if ((u->len < sizeof(u->u.msg)) ||
221 (u->len < (sizeof(u->u.msg) + u->u.msg.len)))
222 return rc;
224 msg_type = u->u.msg.type;
226 switch (msg_type) {
227 case XS_WATCH:
228 case XS_UNWATCH: {
229 static const char *XS_RESP = "OK";
230 struct xsd_sockmsg hdr;
232 path = u->u.buffer + sizeof(u->u.msg);
233 token = memchr(path, 0, u->u.msg.len);
234 if (token == NULL) {
235 rc = -EILSEQ;
236 goto out;
237 }
238 token++;
240 if (msg_type == XS_WATCH) {
241 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
242 watch->watch.node = kmalloc(strlen(path)+1,
243 GFP_KERNEL);
244 strcpy((char *)watch->watch.node, path);
245 watch->watch.callback = watch_fired;
246 watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
247 strcpy(watch->token, token);
248 watch->dev_data = u;
250 err = register_xenbus_watch(&watch->watch);
251 if (err) {
252 free_watch_adapter(watch);
253 rc = err;
254 goto out;
255 }
257 list_add(&watch->list, &u->watches);
258 } else {
259 list_for_each_entry_safe(watch, tmp_watch,
260 &u->watches, list) {
261 if (!strcmp(watch->token, token) &&
262 !strcmp(watch->watch.node, path))
263 {
264 unregister_xenbus_watch(&watch->watch);
265 list_del(&watch->list);
266 free_watch_adapter(watch);
267 break;
268 }
269 }
270 }
272 hdr.type = msg_type;
273 hdr.len = strlen(XS_RESP) + 1;
274 mutex_lock(&u->reply_mutex);
275 queue_reply(u, (char *)&hdr, sizeof(hdr));
276 queue_reply(u, (char *)XS_RESP, hdr.len);
277 mutex_unlock(&u->reply_mutex);
278 break;
279 }
281 default:
282 if (msg_type == XS_TRANSACTION_START) {
283 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
284 if (!trans) {
285 rc = -ENOMEM;
286 goto out;
287 }
288 }
290 reply = xenbus_dev_request_and_reply(&u->u.msg);
291 if (IS_ERR(reply)) {
292 kfree(trans);
293 rc = PTR_ERR(reply);
294 goto out;
295 }
297 if (msg_type == XS_TRANSACTION_START) {
298 trans->handle.id = simple_strtoul(reply, NULL, 0);
299 list_add(&trans->list, &u->transactions);
300 } else if (msg_type == XS_TRANSACTION_END) {
301 list_for_each_entry(trans, &u->transactions, list)
302 if (trans->handle.id == u->u.msg.tx_id)
303 break;
304 BUG_ON(&trans->list == &u->transactions);
305 list_del(&trans->list);
306 kfree(trans);
307 }
308 mutex_lock(&u->reply_mutex);
309 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
310 queue_reply(u, (char *)reply, u->u.msg.len);
311 mutex_unlock(&u->reply_mutex);
312 kfree(reply);
313 break;
314 }
316 out:
317 u->len = 0;
318 return rc;
319 }
321 static int xenbus_dev_open(struct inode *inode, struct file *filp)
322 {
323 struct xenbus_dev_data *u;
325 if (xen_store_evtchn == 0)
326 return -ENOENT;
328 nonseekable_open(inode, filp);
330 u = kzalloc(sizeof(*u), GFP_KERNEL);
331 if (u == NULL)
332 return -ENOMEM;
334 INIT_LIST_HEAD(&u->transactions);
335 INIT_LIST_HEAD(&u->watches);
336 INIT_LIST_HEAD(&u->read_buffers);
337 init_waitqueue_head(&u->read_waitq);
339 mutex_init(&u->reply_mutex);
341 filp->private_data = u;
343 return 0;
344 }
346 static int xenbus_dev_release(struct inode *inode, struct file *filp)
347 {
348 struct xenbus_dev_data *u = filp->private_data;
349 struct xenbus_dev_transaction *trans, *tmp;
350 struct watch_adapter *watch, *tmp_watch;
352 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
353 xenbus_transaction_end(trans->handle, 1);
354 list_del(&trans->list);
355 kfree(trans);
356 }
358 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
359 unregister_xenbus_watch(&watch->watch);
360 list_del(&watch->list);
361 free_watch_adapter(watch);
362 }
364 kfree(u);
366 return 0;
367 }
369 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
370 {
371 struct xenbus_dev_data *u = file->private_data;
373 poll_wait(file, &u->read_waitq, wait);
374 if (!list_empty(&u->read_buffers))
375 return POLLIN | POLLRDNORM;
376 return 0;
377 }
379 static const struct file_operations xenbus_dev_file_ops = {
380 .read = xenbus_dev_read,
381 .write = xenbus_dev_write,
382 .open = xenbus_dev_open,
383 .release = xenbus_dev_release,
384 .poll = xenbus_dev_poll,
385 };
387 int xenbus_dev_init(void)
388 {
389 xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
390 if (xenbus_dev_intf)
391 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
393 return 0;
394 }