blob: 46cf4048e60ed3793ba048f0c0f1233726fdc5e9 [file] [log] [blame]
Alex Zeffertt1107ba82009-01-07 18:07:11 -08001/*
2 * Driver giving user-space access to the kernel's xenbus connection
3 * to xenstore.
4 *
5 * Copyright (c) 2005, Christian Limpach
6 * Copyright (c) 2005, Rusty Russell, IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 *
32 * Changes:
33 * 2008-10-07 Alex Zeffertt Replaced /proc/xen/xenbus with xenfs filesystem
34 * and /proc/xen compatibility mount point.
35 * Turned xenfs into a loadable module.
36 */
37
38#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/uio.h>
41#include <linux/notifier.h>
42#include <linux/wait.h>
43#include <linux/fs.h>
44#include <linux/poll.h>
45#include <linux/mutex.h>
46#include <linux/spinlock.h>
47#include <linux/mount.h>
48#include <linux/pagemap.h>
49#include <linux/uaccess.h>
50#include <linux/init.h>
51#include <linux/namei.h>
52#include <linux/string.h>
53
54#include "xenfs.h"
55#include "../xenbus/xenbus_comms.h"
56
57#include <xen/xenbus.h>
58#include <asm/xen/hypervisor.h>
59
60/*
61 * An element of a list of outstanding transactions, for which we're
62 * still waiting a reply.
63 */
64struct xenbus_transaction_holder {
65 struct list_head list;
66 struct xenbus_transaction handle;
67};
68
69/*
70 * A buffer of data on the queue.
71 */
72struct read_buffer {
73 struct list_head list;
74 unsigned int cons;
75 unsigned int len;
76 char msg[];
77};
78
79struct xenbus_file_priv {
80 /*
81 * msgbuffer_mutex is held while partial requests are built up
82 * and complete requests are acted on. It therefore protects
83 * the "transactions" and "watches" lists, and the partial
84 * request length and buffer.
85 *
86 * reply_mutex protects the reply being built up to return to
87 * usermode. It nests inside msgbuffer_mutex but may be held
88 * alone during a watch callback.
89 */
90 struct mutex msgbuffer_mutex;
91
92 /* In-progress transactions */
93 struct list_head transactions;
94
95 /* Active watches. */
96 struct list_head watches;
97
98 /* Partial request. */
99 unsigned int len;
100 union {
101 struct xsd_sockmsg msg;
102 char buffer[PAGE_SIZE];
103 } u;
104
105 /* Response queue. */
106 struct mutex reply_mutex;
107 struct list_head read_buffers;
108 wait_queue_head_t read_waitq;
109
110};
111
112/* Read out any raw xenbus messages queued up. */
113static ssize_t xenbus_file_read(struct file *filp,
114 char __user *ubuf,
115 size_t len, loff_t *ppos)
116{
117 struct xenbus_file_priv *u = filp->private_data;
118 struct read_buffer *rb;
119 unsigned i;
120 int ret;
121
122 mutex_lock(&u->reply_mutex);
123 while (list_empty(&u->read_buffers)) {
124 mutex_unlock(&u->reply_mutex);
Paolo Bonzini6280f192010-06-23 18:30:15 +0200125 if (filp->f_flags & O_NONBLOCK)
126 return -EAGAIN;
127
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800128 ret = wait_event_interruptible(u->read_waitq,
129 !list_empty(&u->read_buffers));
130 if (ret)
131 return ret;
132 mutex_lock(&u->reply_mutex);
133 }
134
135 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
136 i = 0;
137 while (i < len) {
138 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
139
140 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
141
142 i += sz - ret;
143 rb->cons += sz - ret;
144
Jeremy Fitzhardingefb27cfb2010-08-25 12:19:53 -0700145 if (ret != 0) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800146 if (i == 0)
147 i = -EFAULT;
148 goto out;
149 }
150
151 /* Clear out buffer if it has been consumed */
152 if (rb->cons == rb->len) {
153 list_del(&rb->list);
154 kfree(rb);
155 if (list_empty(&u->read_buffers))
156 break;
157 rb = list_entry(u->read_buffers.next,
158 struct read_buffer, list);
159 }
160 }
161
162out:
163 mutex_unlock(&u->reply_mutex);
164 return i;
165}
166
167/*
168 * Add a buffer to the queue. Caller must hold the appropriate lock
169 * if the queue is not local. (Commonly the caller will build up
170 * multiple queued buffers on a temporary local list, and then add it
171 * to the appropriate list under lock once all the buffers have een
172 * successfully allocated.)
173 */
174static int queue_reply(struct list_head *queue, const void *data, size_t len)
175{
176 struct read_buffer *rb;
177
178 if (len == 0)
179 return 0;
180
181 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
182 if (rb == NULL)
183 return -ENOMEM;
184
185 rb->cons = 0;
186 rb->len = len;
187
188 memcpy(rb->msg, data, len);
189
190 list_add_tail(&rb->list, queue);
191 return 0;
192}
193
194/*
195 * Free all the read_buffer s on a list.
196 * Caller must have sole reference to list.
197 */
198static void queue_cleanup(struct list_head *list)
199{
200 struct read_buffer *rb;
201
202 while (!list_empty(list)) {
203 rb = list_entry(list->next, struct read_buffer, list);
204 list_del(list->next);
205 kfree(rb);
206 }
207}
208
209struct watch_adapter {
210 struct list_head list;
211 struct xenbus_watch watch;
212 struct xenbus_file_priv *dev_data;
213 char *token;
214};
215
216static void free_watch_adapter(struct watch_adapter *watch)
217{
218 kfree(watch->watch.node);
219 kfree(watch->token);
220 kfree(watch);
221}
222
223static struct watch_adapter *alloc_watch_adapter(const char *path,
224 const char *token)
225{
226 struct watch_adapter *watch;
227
228 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
229 if (watch == NULL)
230 goto out_fail;
231
232 watch->watch.node = kstrdup(path, GFP_KERNEL);
233 if (watch->watch.node == NULL)
234 goto out_free;
235
236 watch->token = kstrdup(token, GFP_KERNEL);
237 if (watch->token == NULL)
238 goto out_free;
239
240 return watch;
241
242out_free:
243 free_watch_adapter(watch);
244
245out_fail:
246 return NULL;
247}
248
249static void watch_fired(struct xenbus_watch *watch,
250 const char **vec,
251 unsigned int len)
252{
253 struct watch_adapter *adap;
254 struct xsd_sockmsg hdr;
255 const char *path, *token;
256 int path_len, tok_len, body_len, data_len = 0;
257 int ret;
258 LIST_HEAD(staging_q);
259
260 adap = container_of(watch, struct watch_adapter, watch);
261
262 path = vec[XS_WATCH_PATH];
263 token = adap->token;
264
265 path_len = strlen(path) + 1;
266 tok_len = strlen(token) + 1;
267 if (len > 2)
268 data_len = vec[len] - vec[2] + 1;
269 body_len = path_len + tok_len + data_len;
270
271 hdr.type = XS_WATCH_EVENT;
272 hdr.len = body_len;
273
274 mutex_lock(&adap->dev_data->reply_mutex);
275
276 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
277 if (!ret)
278 ret = queue_reply(&staging_q, path, path_len);
279 if (!ret)
280 ret = queue_reply(&staging_q, token, tok_len);
281 if (!ret && len > 2)
282 ret = queue_reply(&staging_q, vec[2], data_len);
283
284 if (!ret) {
285 /* success: pass reply list onto watcher */
286 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
287 wake_up(&adap->dev_data->read_waitq);
288 } else
289 queue_cleanup(&staging_q);
290
291 mutex_unlock(&adap->dev_data->reply_mutex);
292}
293
294static int xenbus_write_transaction(unsigned msg_type,
295 struct xenbus_file_priv *u)
296{
Ian Campbelle88a0fa2009-01-24 08:22:47 +0000297 int rc;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800298 void *reply;
299 struct xenbus_transaction_holder *trans = NULL;
300 LIST_HEAD(staging_q);
301
302 if (msg_type == XS_TRANSACTION_START) {
303 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
304 if (!trans) {
305 rc = -ENOMEM;
306 goto out;
307 }
308 }
309
310 reply = xenbus_dev_request_and_reply(&u->u.msg);
311 if (IS_ERR(reply)) {
312 kfree(trans);
313 rc = PTR_ERR(reply);
314 goto out;
315 }
316
317 if (msg_type == XS_TRANSACTION_START) {
318 trans->handle.id = simple_strtoul(reply, NULL, 0);
319
320 list_add(&trans->list, &u->transactions);
321 } else if (msg_type == XS_TRANSACTION_END) {
322 list_for_each_entry(trans, &u->transactions, list)
323 if (trans->handle.id == u->u.msg.tx_id)
324 break;
325 BUG_ON(&trans->list == &u->transactions);
326 list_del(&trans->list);
327
328 kfree(trans);
329 }
330
331 mutex_lock(&u->reply_mutex);
Ian Campbelle88a0fa2009-01-24 08:22:47 +0000332 rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
333 if (!rc)
334 rc = queue_reply(&staging_q, reply, u->u.msg.len);
335 if (!rc) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800336 list_splice_tail(&staging_q, &u->read_buffers);
337 wake_up(&u->read_waitq);
338 } else {
339 queue_cleanup(&staging_q);
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800340 }
341 mutex_unlock(&u->reply_mutex);
342
343 kfree(reply);
344
345out:
346 return rc;
347}
348
349static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
350{
351 struct watch_adapter *watch, *tmp_watch;
352 char *path, *token;
353 int err, rc;
354 LIST_HEAD(staging_q);
355
356 path = u->u.buffer + sizeof(u->u.msg);
357 token = memchr(path, 0, u->u.msg.len);
358 if (token == NULL) {
359 rc = -EILSEQ;
360 goto out;
361 }
362 token++;
363
364 if (msg_type == XS_WATCH) {
365 watch = alloc_watch_adapter(path, token);
366 if (watch == NULL) {
367 rc = -ENOMEM;
368 goto out;
369 }
370
371 watch->watch.callback = watch_fired;
372 watch->dev_data = u;
373
374 err = register_xenbus_watch(&watch->watch);
375 if (err) {
376 free_watch_adapter(watch);
377 rc = err;
378 goto out;
379 }
380 list_add(&watch->list, &u->watches);
381 } else {
382 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
383 if (!strcmp(watch->token, token) &&
384 !strcmp(watch->watch.node, path)) {
385 unregister_xenbus_watch(&watch->watch);
386 list_del(&watch->list);
387 free_watch_adapter(watch);
388 break;
389 }
390 }
391 }
392
393 /* Success. Synthesize a reply to say all is OK. */
394 {
395 struct {
396 struct xsd_sockmsg hdr;
397 char body[3];
398 } __packed reply = {
399 {
400 .type = msg_type,
401 .len = sizeof(reply.body)
402 },
403 "OK"
404 };
405
406 mutex_lock(&u->reply_mutex);
407 rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
408 mutex_unlock(&u->reply_mutex);
409 }
410
411out:
412 return rc;
413}
414
415static ssize_t xenbus_file_write(struct file *filp,
416 const char __user *ubuf,
417 size_t len, loff_t *ppos)
418{
419 struct xenbus_file_priv *u = filp->private_data;
420 uint32_t msg_type;
421 int rc = len;
422 int ret;
423 LIST_HEAD(staging_q);
424
425 /*
426 * We're expecting usermode to be writing properly formed
427 * xenbus messages. If they write an incomplete message we
428 * buffer it up. Once it is complete, we act on it.
429 */
430
431 /*
432 * Make sure concurrent writers can't stomp all over each
433 * other's messages and make a mess of our partial message
434 * buffer. We don't make any attemppt to stop multiple
435 * writers from making a mess of each other's incomplete
436 * messages; we're just trying to guarantee our own internal
437 * consistency and make sure that single writes are handled
438 * atomically.
439 */
440 mutex_lock(&u->msgbuffer_mutex);
441
442 /* Get this out of the way early to avoid confusion */
443 if (len == 0)
444 goto out;
445
446 /* Can't write a xenbus message larger we can buffer */
447 if ((len + u->len) > sizeof(u->u.buffer)) {
448 /* On error, dump existing buffer */
449 u->len = 0;
450 rc = -EINVAL;
451 goto out;
452 }
453
454 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
455
Jeremy Fitzhardingefb27cfb2010-08-25 12:19:53 -0700456 if (ret != 0) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800457 rc = -EFAULT;
458 goto out;
459 }
460
461 /* Deal with a partial copy. */
462 len -= ret;
463 rc = len;
464
465 u->len += len;
466
467 /* Return if we haven't got a full message yet */
468 if (u->len < sizeof(u->u.msg))
469 goto out; /* not even the header yet */
470
471 /* If we're expecting a message that's larger than we can
472 possibly send, dump what we have and return an error. */
473 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
474 rc = -E2BIG;
475 u->len = 0;
476 goto out;
477 }
478
479 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
480 goto out; /* incomplete data portion */
481
482 /*
483 * OK, now we have a complete message. Do something with it.
484 */
485
486 msg_type = u->u.msg.type;
487
488 switch (msg_type) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800489 case XS_WATCH:
490 case XS_UNWATCH:
491 /* (Un)Ask for some path to be watched for changes */
492 ret = xenbus_write_watch(msg_type, u);
493 break;
494
495 default:
Diego Ongaro6d6df2e2010-09-01 09:18:54 -0700496 /* Send out a transaction */
497 ret = xenbus_write_transaction(msg_type, u);
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800498 break;
499 }
500 if (ret != 0)
501 rc = ret;
502
503 /* Buffered message consumed */
504 u->len = 0;
505
506 out:
507 mutex_unlock(&u->msgbuffer_mutex);
508 return rc;
509}
510
511static int xenbus_file_open(struct inode *inode, struct file *filp)
512{
513 struct xenbus_file_priv *u;
514
515 if (xen_store_evtchn == 0)
516 return -ENOENT;
517
518 nonseekable_open(inode, filp);
519
520 u = kzalloc(sizeof(*u), GFP_KERNEL);
521 if (u == NULL)
522 return -ENOMEM;
523
524 INIT_LIST_HEAD(&u->transactions);
525 INIT_LIST_HEAD(&u->watches);
526 INIT_LIST_HEAD(&u->read_buffers);
527 init_waitqueue_head(&u->read_waitq);
528
529 mutex_init(&u->reply_mutex);
530 mutex_init(&u->msgbuffer_mutex);
531
532 filp->private_data = u;
533
534 return 0;
535}
536
537static int xenbus_file_release(struct inode *inode, struct file *filp)
538{
539 struct xenbus_file_priv *u = filp->private_data;
540 struct xenbus_transaction_holder *trans, *tmp;
541 struct watch_adapter *watch, *tmp_watch;
542
543 /*
544 * No need for locking here because there are no other users,
545 * by definition.
546 */
547
548 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
549 xenbus_transaction_end(trans->handle, 1);
550 list_del(&trans->list);
551 kfree(trans);
552 }
553
554 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
555 unregister_xenbus_watch(&watch->watch);
556 list_del(&watch->list);
557 free_watch_adapter(watch);
558 }
559
560 kfree(u);
561
562 return 0;
563}
564
565static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
566{
567 struct xenbus_file_priv *u = file->private_data;
568
569 poll_wait(file, &u->read_waitq, wait);
570 if (!list_empty(&u->read_buffers))
571 return POLLIN | POLLRDNORM;
572 return 0;
573}
574
575const struct file_operations xenbus_file_ops = {
576 .read = xenbus_file_read,
577 .write = xenbus_file_write,
578 .open = xenbus_file_open,
579 .release = xenbus_file_release,
580 .poll = xenbus_file_poll,
581};