Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / xenbus / xenbus_dev.c
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  */
34
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
43 #include "xenbus_comms.h"
44
45 #include <asm/uaccess.h>
46 #include <asm/hypervisor.h>
47 #include <xen/xenbus.h>
48 #include <xen/xen_proc.h>
49 #include <asm/hypervisor.h>
50
51 struct xenbus_dev_transaction {
52         struct list_head list;
53         struct xenbus_transaction handle;
54 };
55
56 struct xenbus_dev_data {
57         /* In-progress transaction. */
58         struct list_head transactions;
59
60         /* Active watches. */
61         struct list_head watches;
62
63         /* Partial request. */
64         unsigned int len;
65         union {
66                 struct xsd_sockmsg msg;
67                 char buffer[PAGE_SIZE];
68         } u;
69
70         /* Response queue. */
71 #define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
72         char read_buffer[PAGE_SIZE];
73         unsigned int read_cons, read_prod;
74         wait_queue_head_t read_waitq;
75
76         struct mutex reply_mutex;
77 };
78
79 static struct proc_dir_entry *xenbus_dev_intf;
80
81 static ssize_t xenbus_dev_read(struct file *filp,
82                                char __user *ubuf,
83                                size_t len, loff_t *ppos)
84 {
85         struct xenbus_dev_data *u = filp->private_data;
86         int i;
87
88         if (wait_event_interruptible(u->read_waitq,
89                                      u->read_prod != u->read_cons))
90                 return -EINTR;
91
92         for (i = 0; i < len; i++) {
93                 if (u->read_cons == u->read_prod)
94                         break;
95                 put_user(u->read_buffer[MASK_READ_IDX(u->read_cons)], ubuf+i);
96                 u->read_cons++;
97         }
98
99         return i;
100 }
101
102 static void queue_reply(struct xenbus_dev_data *u,
103                         char *data, unsigned int len)
104 {
105         int i;
106
107         mutex_lock(&u->reply_mutex);
108
109         for (i = 0; i < len; i++, u->read_prod++)
110                 u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];
111
112         BUG_ON((u->read_prod - u->read_cons) > sizeof(u->read_buffer));
113
114         mutex_unlock(&u->reply_mutex);
115
116         wake_up(&u->read_waitq);
117 }
118
119 struct watch_adapter
120 {
121         struct list_head list;
122         struct xenbus_watch watch;
123         struct xenbus_dev_data *dev_data;
124         char *token;
125 };
126
127 static void free_watch_adapter (struct watch_adapter *watch)
128 {
129         kfree(watch->watch.node);
130         kfree(watch->token);
131         kfree(watch);
132 }
133
134 static void watch_fired(struct xenbus_watch *watch,
135                         const char **vec,
136                         unsigned int len)
137 {
138         struct watch_adapter *adap =
139             container_of(watch, struct watch_adapter, watch);
140         struct xsd_sockmsg hdr;
141         const char *path, *token;
142         int path_len, tok_len, body_len;
143
144         path = vec[XS_WATCH_PATH];
145         token = adap->token;
146
147         path_len = strlen(path) + 1;
148         tok_len = strlen(token) + 1;
149         body_len = path_len + tok_len;
150
151         hdr.type = XS_WATCH_EVENT;
152         hdr.len = body_len;
153         
154         queue_reply(adap->dev_data, (char *)&hdr, sizeof(hdr));
155         queue_reply(adap->dev_data, (char *)path, path_len);
156         queue_reply(adap->dev_data, (char *)token, tok_len);
157 }
158
159 static LIST_HEAD(watch_list);
160
161 static ssize_t xenbus_dev_write(struct file *filp,
162                                 const char __user *ubuf,
163                                 size_t len, loff_t *ppos)
164 {
165         struct xenbus_dev_data *u = filp->private_data;
166         struct xenbus_dev_transaction *trans = NULL;
167         uint32_t msg_type;
168         void *reply;
169         char *path, *token;
170         struct watch_adapter *watch, *tmp_watch;
171         int err;
172
173         if ((len + u->len) > sizeof(u->u.buffer))
174                 return -EINVAL;
175
176         if (copy_from_user(u->u.buffer + u->len, ubuf, len) != 0)
177                 return -EFAULT;
178
179         u->len += len;
180         if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
181                 return len;
182
183         msg_type = u->u.msg.type;
184
185         switch (msg_type) {
186         case XS_TRANSACTION_START:
187         case XS_TRANSACTION_END:
188         case XS_DIRECTORY:
189         case XS_READ:
190         case XS_GET_PERMS:
191         case XS_RELEASE:
192         case XS_GET_DOMAIN_PATH:
193         case XS_WRITE:
194         case XS_MKDIR:
195         case XS_RM:
196         case XS_SET_PERMS:
197                 if (msg_type == XS_TRANSACTION_START) {
198                         trans = kmalloc(sizeof(*trans), GFP_KERNEL);
199                         if (!trans)
200                                 return -ENOMEM;
201                 }
202
203                 reply = xenbus_dev_request_and_reply(&u->u.msg);
204                 if (IS_ERR(reply)) {
205                         kfree(trans);
206                         return PTR_ERR(reply);
207                 }
208
209                 if (msg_type == XS_TRANSACTION_START) {
210                         trans->handle.id = simple_strtoul(reply, NULL, 0);
211                         list_add(&trans->list, &u->transactions);
212                 } else if (msg_type == XS_TRANSACTION_END) {
213                         list_for_each_entry(trans, &u->transactions, list)
214                                 if (trans->handle.id == u->u.msg.tx_id)
215                                         break;
216                         BUG_ON(&trans->list == &u->transactions);
217                         list_del(&trans->list);
218                         kfree(trans);
219                 }
220                 queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
221                 queue_reply(u, (char *)reply, u->u.msg.len);
222                 kfree(reply);
223                 break;
224
225         case XS_WATCH:
226         case XS_UNWATCH:
227                 path = u->u.buffer + sizeof(u->u.msg);
228                 token = memchr(path, 0, u->u.msg.len);
229                 if (token == NULL)
230                         return -EILSEQ;
231                 token++;
232
233                 if (msg_type == XS_WATCH) {
234                         static const char * XS_WATCH_RESP = "OK";
235                         struct xsd_sockmsg hdr;
236
237                         watch = kmalloc(sizeof(*watch), GFP_KERNEL);
238                         watch->watch.node = kmalloc(strlen(path)+1,
239                                                     GFP_KERNEL);
240                         strcpy((char *)watch->watch.node, path);
241                         watch->watch.callback = watch_fired;
242                         watch->token = kmalloc(strlen(token)+1, GFP_KERNEL);
243                         strcpy(watch->token, token);
244                         watch->dev_data = u;
245
246                         err = register_xenbus_watch(&watch->watch);
247                         if (err) {
248                                 free_watch_adapter(watch);
249                                 return err;
250                         }
251                         
252                         list_add(&watch->list, &u->watches);
253
254                         hdr.type = XS_WATCH;
255                         hdr.len = strlen(XS_WATCH_RESP) + 1;
256                         queue_reply(u, (char *)&hdr, sizeof(hdr));
257                         queue_reply(u, (char *)XS_WATCH_RESP, hdr.len);
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                                         break;
264                                 {
265                                         unregister_xenbus_watch(&watch->watch);
266                                         list_del(&watch->list);
267                                         free_watch_adapter(watch);
268                                         break;
269                                 }
270                         }
271                 }
272
273                 break;
274
275         default:
276                 return -EINVAL;
277         }
278
279         u->len = 0;
280         return len;
281 }
282
283 static int xenbus_dev_open(struct inode *inode, struct file *filp)
284 {
285         struct xenbus_dev_data *u;
286
287         if (xen_store_evtchn == 0)
288                 return -ENOENT;
289
290         nonseekable_open(inode, filp);
291
292         u = kzalloc(sizeof(*u), GFP_KERNEL);
293         if (u == NULL)
294                 return -ENOMEM;
295
296         INIT_LIST_HEAD(&u->transactions);
297         INIT_LIST_HEAD(&u->watches);
298         init_waitqueue_head(&u->read_waitq);
299
300         mutex_init(&u->reply_mutex);
301
302         filp->private_data = u;
303
304         return 0;
305 }
306
307 static int xenbus_dev_release(struct inode *inode, struct file *filp)
308 {
309         struct xenbus_dev_data *u = filp->private_data;
310         struct xenbus_dev_transaction *trans, *tmp;
311         struct watch_adapter *watch, *tmp_watch;
312
313         list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
314                 xenbus_transaction_end(trans->handle, 1);
315                 list_del(&trans->list);
316                 kfree(trans);
317         }
318
319         list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
320                 unregister_xenbus_watch(&watch->watch);
321                 list_del(&watch->list);
322                 free_watch_adapter(watch);
323         }
324
325         kfree(u);
326
327         return 0;
328 }
329
330 static unsigned int xenbus_dev_poll(struct file *file, poll_table *wait)
331 {
332         struct xenbus_dev_data *u = file->private_data;
333
334         poll_wait(file, &u->read_waitq, wait);
335         if (u->read_cons != u->read_prod)
336                 return POLLIN | POLLRDNORM;
337         return 0;
338 }
339
340 static struct file_operations xenbus_dev_file_ops = {
341         .read = xenbus_dev_read,
342         .write = xenbus_dev_write,
343         .open = xenbus_dev_open,
344         .release = xenbus_dev_release,
345         .poll = xenbus_dev_poll,
346 };
347
348 int __init
349 xenbus_dev_init(void)
350 {
351         xenbus_dev_intf = create_xen_proc_entry("xenbus", 0400);
352         if (xenbus_dev_intf)
353                 xenbus_dev_intf->proc_fops = &xenbus_dev_file_ops;
354
355         return 0;
356 }