]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/xen/xenbus/xenbus_dev_frontend.c
xen: clean up xenbus internal headers
[mirror_ubuntu-artful-kernel.git] / drivers / xen / xenbus / xenbus_dev_frontend.c
CommitLineData
1107ba88
AZ
1/*
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
283c0972
JP
38#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
1107ba88
AZ
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/uio.h>
43#include <linux/notifier.h>
44#include <linux/wait.h>
45#include <linux/fs.h>
46#include <linux/poll.h>
47#include <linux/mutex.h>
a99bbaf5 48#include <linux/sched.h>
1107ba88
AZ
49#include <linux/spinlock.h>
50#include <linux/mount.h>
51#include <linux/pagemap.h>
52#include <linux/uaccess.h>
53#include <linux/init.h>
54#include <linux/namei.h>
55#include <linux/string.h>
5a0e3ad6 56#include <linux/slab.h>
2fb3683e 57#include <linux/miscdevice.h>
ab1241a1 58#include <linux/init.h>
1107ba88 59
1107ba88 60#include <xen/xenbus.h>
b79d2ff9 61#include <xen/xen.h>
1107ba88
AZ
62#include <asm/xen/hypervisor.h>
63
332f791d
JG
64#include "xenbus.h"
65
1107ba88
AZ
66/*
67 * An element of a list of outstanding transactions, for which we're
68 * still waiting a reply.
69 */
70struct xenbus_transaction_holder {
71 struct list_head list;
72 struct xenbus_transaction handle;
73};
74
75/*
76 * A buffer of data on the queue.
77 */
78struct read_buffer {
79 struct list_head list;
80 unsigned int cons;
81 unsigned int len;
82 char msg[];
83};
84
85struct xenbus_file_priv {
86 /*
87 * msgbuffer_mutex is held while partial requests are built up
88 * and complete requests are acted on. It therefore protects
89 * the "transactions" and "watches" lists, and the partial
90 * request length and buffer.
91 *
92 * reply_mutex protects the reply being built up to return to
93 * usermode. It nests inside msgbuffer_mutex but may be held
94 * alone during a watch callback.
95 */
96 struct mutex msgbuffer_mutex;
97
98 /* In-progress transactions */
99 struct list_head transactions;
100
101 /* Active watches. */
102 struct list_head watches;
103
104 /* Partial request. */
105 unsigned int len;
106 union {
107 struct xsd_sockmsg msg;
50bf7379 108 char buffer[XENSTORE_PAYLOAD_MAX];
1107ba88
AZ
109 } u;
110
111 /* Response queue. */
112 struct mutex reply_mutex;
113 struct list_head read_buffers;
114 wait_queue_head_t read_waitq;
115
116};
117
118/* Read out any raw xenbus messages queued up. */
119static ssize_t xenbus_file_read(struct file *filp,
120 char __user *ubuf,
121 size_t len, loff_t *ppos)
122{
123 struct xenbus_file_priv *u = filp->private_data;
124 struct read_buffer *rb;
125 unsigned i;
126 int ret;
127
128 mutex_lock(&u->reply_mutex);
7808121b 129again:
1107ba88
AZ
130 while (list_empty(&u->read_buffers)) {
131 mutex_unlock(&u->reply_mutex);
6280f190
PB
132 if (filp->f_flags & O_NONBLOCK)
133 return -EAGAIN;
134
1107ba88
AZ
135 ret = wait_event_interruptible(u->read_waitq,
136 !list_empty(&u->read_buffers));
137 if (ret)
138 return ret;
139 mutex_lock(&u->reply_mutex);
140 }
141
142 rb = list_entry(u->read_buffers.next, struct read_buffer, list);
143 i = 0;
144 while (i < len) {
145 unsigned sz = min((unsigned)len - i, rb->len - rb->cons);
146
147 ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);
148
149 i += sz - ret;
150 rb->cons += sz - ret;
151
fb27cfbc 152 if (ret != 0) {
1107ba88
AZ
153 if (i == 0)
154 i = -EFAULT;
155 goto out;
156 }
157
158 /* Clear out buffer if it has been consumed */
159 if (rb->cons == rb->len) {
160 list_del(&rb->list);
161 kfree(rb);
162 if (list_empty(&u->read_buffers))
163 break;
164 rb = list_entry(u->read_buffers.next,
165 struct read_buffer, list);
166 }
167 }
7808121b
DDG
168 if (i == 0)
169 goto again;
1107ba88
AZ
170
171out:
172 mutex_unlock(&u->reply_mutex);
173 return i;
174}
175
176/*
177 * Add a buffer to the queue. Caller must hold the appropriate lock
178 * if the queue is not local. (Commonly the caller will build up
179 * multiple queued buffers on a temporary local list, and then add it
180 * to the appropriate list under lock once all the buffers have een
181 * successfully allocated.)
182 */
183static int queue_reply(struct list_head *queue, const void *data, size_t len)
184{
185 struct read_buffer *rb;
186
187 if (len == 0)
188 return 0;
85c0a87c
IY
189 if (len > XENSTORE_PAYLOAD_MAX)
190 return -EINVAL;
1107ba88
AZ
191
192 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
193 if (rb == NULL)
194 return -ENOMEM;
195
196 rb->cons = 0;
197 rb->len = len;
198
199 memcpy(rb->msg, data, len);
200
201 list_add_tail(&rb->list, queue);
202 return 0;
203}
204
205/*
206 * Free all the read_buffer s on a list.
207 * Caller must have sole reference to list.
208 */
209static void queue_cleanup(struct list_head *list)
210{
211 struct read_buffer *rb;
212
213 while (!list_empty(list)) {
214 rb = list_entry(list->next, struct read_buffer, list);
215 list_del(list->next);
216 kfree(rb);
217 }
218}
219
220struct watch_adapter {
221 struct list_head list;
222 struct xenbus_watch watch;
223 struct xenbus_file_priv *dev_data;
224 char *token;
225};
226
227static void free_watch_adapter(struct watch_adapter *watch)
228{
229 kfree(watch->watch.node);
230 kfree(watch->token);
231 kfree(watch);
232}
233
234static struct watch_adapter *alloc_watch_adapter(const char *path,
235 const char *token)
236{
237 struct watch_adapter *watch;
238
239 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
240 if (watch == NULL)
241 goto out_fail;
242
243 watch->watch.node = kstrdup(path, GFP_KERNEL);
244 if (watch->watch.node == NULL)
245 goto out_free;
246
247 watch->token = kstrdup(token, GFP_KERNEL);
248 if (watch->token == NULL)
249 goto out_free;
250
251 return watch;
252
253out_free:
254 free_watch_adapter(watch);
255
256out_fail:
257 return NULL;
258}
259
260static void watch_fired(struct xenbus_watch *watch,
261 const char **vec,
262 unsigned int len)
263{
264 struct watch_adapter *adap;
265 struct xsd_sockmsg hdr;
266 const char *path, *token;
267 int path_len, tok_len, body_len, data_len = 0;
268 int ret;
269 LIST_HEAD(staging_q);
270
271 adap = container_of(watch, struct watch_adapter, watch);
272
273 path = vec[XS_WATCH_PATH];
274 token = adap->token;
275
276 path_len = strlen(path) + 1;
277 tok_len = strlen(token) + 1;
278 if (len > 2)
279 data_len = vec[len] - vec[2] + 1;
280 body_len = path_len + tok_len + data_len;
281
282 hdr.type = XS_WATCH_EVENT;
283 hdr.len = body_len;
284
285 mutex_lock(&adap->dev_data->reply_mutex);
286
287 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
288 if (!ret)
289 ret = queue_reply(&staging_q, path, path_len);
290 if (!ret)
291 ret = queue_reply(&staging_q, token, tok_len);
292 if (!ret && len > 2)
293 ret = queue_reply(&staging_q, vec[2], data_len);
294
295 if (!ret) {
296 /* success: pass reply list onto watcher */
297 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
298 wake_up(&adap->dev_data->read_waitq);
299 } else
300 queue_cleanup(&staging_q);
301
302 mutex_unlock(&adap->dev_data->reply_mutex);
303}
304
9a6161fe
JG
305static int xenbus_command_reply(struct xenbus_file_priv *u,
306 unsigned int msg_type, const char *reply)
307{
308 struct {
309 struct xsd_sockmsg hdr;
310 const char body[16];
311 } msg;
312 int rc;
313
314 msg.hdr = u->u.msg;
315 msg.hdr.type = msg_type;
316 msg.hdr.len = strlen(reply) + 1;
317 if (msg.hdr.len > sizeof(msg.body))
318 return -E2BIG;
319
320 mutex_lock(&u->reply_mutex);
321 rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
322 wake_up(&u->read_waitq);
323 mutex_unlock(&u->reply_mutex);
324
325 return rc;
326}
327
1107ba88
AZ
328static int xenbus_write_transaction(unsigned msg_type,
329 struct xenbus_file_priv *u)
330{
e88a0faa 331 int rc;
1107ba88
AZ
332 void *reply;
333 struct xenbus_transaction_holder *trans = NULL;
334 LIST_HEAD(staging_q);
335
336 if (msg_type == XS_TRANSACTION_START) {
337 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
338 if (!trans) {
339 rc = -ENOMEM;
340 goto out;
341 }
639b0881 342 } else if (u->u.msg.tx_id != 0) {
0beef634
JB
343 list_for_each_entry(trans, &u->transactions, list)
344 if (trans->handle.id == u->u.msg.tx_id)
345 break;
346 if (&trans->list == &u->transactions)
9a6161fe 347 return xenbus_command_reply(u, XS_ERROR, "ENOENT");
1107ba88
AZ
348 }
349
350 reply = xenbus_dev_request_and_reply(&u->u.msg);
351 if (IS_ERR(reply)) {
0beef634
JB
352 if (msg_type == XS_TRANSACTION_START)
353 kfree(trans);
1107ba88
AZ
354 rc = PTR_ERR(reply);
355 goto out;
356 }
357
358 if (msg_type == XS_TRANSACTION_START) {
a2e75bc2
JH
359 if (u->u.msg.type == XS_ERROR)
360 kfree(trans);
361 else {
362 trans->handle.id = simple_strtoul(reply, NULL, 0);
363 list_add(&trans->list, &u->transactions);
364 }
365 } else if (u->u.msg.type == XS_TRANSACTION_END) {
1107ba88 366 list_del(&trans->list);
1107ba88
AZ
367 kfree(trans);
368 }
369
370 mutex_lock(&u->reply_mutex);
e88a0faa
IC
371 rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
372 if (!rc)
373 rc = queue_reply(&staging_q, reply, u->u.msg.len);
374 if (!rc) {
1107ba88
AZ
375 list_splice_tail(&staging_q, &u->read_buffers);
376 wake_up(&u->read_waitq);
377 } else {
378 queue_cleanup(&staging_q);
1107ba88
AZ
379 }
380 mutex_unlock(&u->reply_mutex);
381
382 kfree(reply);
383
384out:
385 return rc;
386}
387
388static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
389{
e1e5b3ff 390 struct watch_adapter *watch;
1107ba88
AZ
391 char *path, *token;
392 int err, rc;
393 LIST_HEAD(staging_q);
394
395 path = u->u.buffer + sizeof(u->u.msg);
396 token = memchr(path, 0, u->u.msg.len);
397 if (token == NULL) {
9a6161fe 398 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
1107ba88
AZ
399 goto out;
400 }
401 token++;
a43a5ccd 402 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
9a6161fe 403 rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
a43a5ccd
JB
404 goto out;
405 }
1107ba88
AZ
406
407 if (msg_type == XS_WATCH) {
408 watch = alloc_watch_adapter(path, token);
409 if (watch == NULL) {
410 rc = -ENOMEM;
411 goto out;
412 }
413
414 watch->watch.callback = watch_fired;
415 watch->dev_data = u;
416
417 err = register_xenbus_watch(&watch->watch);
418 if (err) {
419 free_watch_adapter(watch);
420 rc = err;
421 goto out;
422 }
423 list_add(&watch->list, &u->watches);
424 } else {
e1e5b3ff 425 list_for_each_entry(watch, &u->watches, list) {
1107ba88
AZ
426 if (!strcmp(watch->token, token) &&
427 !strcmp(watch->watch.node, path)) {
428 unregister_xenbus_watch(&watch->watch);
429 list_del(&watch->list);
430 free_watch_adapter(watch);
431 break;
432 }
433 }
434 }
435
436 /* Success. Synthesize a reply to say all is OK. */
9a6161fe 437 rc = xenbus_command_reply(u, msg_type, "OK");
1107ba88
AZ
438
439out:
440 return rc;
441}
442
443static ssize_t xenbus_file_write(struct file *filp,
444 const char __user *ubuf,
445 size_t len, loff_t *ppos)
446{
447 struct xenbus_file_priv *u = filp->private_data;
448 uint32_t msg_type;
449 int rc = len;
450 int ret;
451 LIST_HEAD(staging_q);
452
453 /*
454 * We're expecting usermode to be writing properly formed
455 * xenbus messages. If they write an incomplete message we
456 * buffer it up. Once it is complete, we act on it.
457 */
458
459 /*
460 * Make sure concurrent writers can't stomp all over each
461 * other's messages and make a mess of our partial message
462 * buffer. We don't make any attemppt to stop multiple
463 * writers from making a mess of each other's incomplete
464 * messages; we're just trying to guarantee our own internal
465 * consistency and make sure that single writes are handled
466 * atomically.
467 */
468 mutex_lock(&u->msgbuffer_mutex);
469
470 /* Get this out of the way early to avoid confusion */
471 if (len == 0)
472 goto out;
473
474 /* Can't write a xenbus message larger we can buffer */
1bcaba51 475 if (len > sizeof(u->u.buffer) - u->len) {
1107ba88
AZ
476 /* On error, dump existing buffer */
477 u->len = 0;
478 rc = -EINVAL;
479 goto out;
480 }
481
482 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
483
fb27cfbc 484 if (ret != 0) {
1107ba88
AZ
485 rc = -EFAULT;
486 goto out;
487 }
488
489 /* Deal with a partial copy. */
490 len -= ret;
491 rc = len;
492
493 u->len += len;
494
495 /* Return if we haven't got a full message yet */
496 if (u->len < sizeof(u->u.msg))
497 goto out; /* not even the header yet */
498
499 /* If we're expecting a message that's larger than we can
500 possibly send, dump what we have and return an error. */
501 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
502 rc = -E2BIG;
503 u->len = 0;
504 goto out;
505 }
506
507 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
508 goto out; /* incomplete data portion */
509
510 /*
511 * OK, now we have a complete message. Do something with it.
512 */
513
514 msg_type = u->u.msg.type;
515
516 switch (msg_type) {
1107ba88
AZ
517 case XS_WATCH:
518 case XS_UNWATCH:
519 /* (Un)Ask for some path to be watched for changes */
520 ret = xenbus_write_watch(msg_type, u);
521 break;
522
523 default:
6d6df2e4
DO
524 /* Send out a transaction */
525 ret = xenbus_write_transaction(msg_type, u);
1107ba88
AZ
526 break;
527 }
528 if (ret != 0)
529 rc = ret;
530
531 /* Buffered message consumed */
532 u->len = 0;
533
534 out:
535 mutex_unlock(&u->msgbuffer_mutex);
536 return rc;
537}
538
539static int xenbus_file_open(struct inode *inode, struct file *filp)
540{
541 struct xenbus_file_priv *u;
542
543 if (xen_store_evtchn == 0)
544 return -ENOENT;
545
546 nonseekable_open(inode, filp);
547
581d21a2
DV
548 filp->f_mode &= ~FMODE_ATOMIC_POS; /* cdev-style semantics */
549
1107ba88
AZ
550 u = kzalloc(sizeof(*u), GFP_KERNEL);
551 if (u == NULL)
552 return -ENOMEM;
553
554 INIT_LIST_HEAD(&u->transactions);
555 INIT_LIST_HEAD(&u->watches);
556 INIT_LIST_HEAD(&u->read_buffers);
557 init_waitqueue_head(&u->read_waitq);
558
559 mutex_init(&u->reply_mutex);
560 mutex_init(&u->msgbuffer_mutex);
561
562 filp->private_data = u;
563
564 return 0;
565}
566
567static int xenbus_file_release(struct inode *inode, struct file *filp)
568{
569 struct xenbus_file_priv *u = filp->private_data;
570 struct xenbus_transaction_holder *trans, *tmp;
571 struct watch_adapter *watch, *tmp_watch;
6a5b3bef 572 struct read_buffer *rb, *tmp_rb;
1107ba88
AZ
573
574 /*
575 * No need for locking here because there are no other users,
576 * by definition.
577 */
578
579 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
580 xenbus_transaction_end(trans->handle, 1);
581 list_del(&trans->list);
582 kfree(trans);
583 }
584
585 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
586 unregister_xenbus_watch(&watch->watch);
587 list_del(&watch->list);
588 free_watch_adapter(watch);
589 }
590
6a5b3bef
DDG
591 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
592 list_del(&rb->list);
593 kfree(rb);
594 }
1107ba88
AZ
595 kfree(u);
596
597 return 0;
598}
599
600static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
601{
602 struct xenbus_file_priv *u = file->private_data;
603
604 poll_wait(file, &u->read_waitq, wait);
605 if (!list_empty(&u->read_buffers))
606 return POLLIN | POLLRDNORM;
607 return 0;
608}
609
2fb3683e 610const struct file_operations xen_xenbus_fops = {
1107ba88
AZ
611 .read = xenbus_file_read,
612 .write = xenbus_file_write,
613 .open = xenbus_file_open,
614 .release = xenbus_file_release,
615 .poll = xenbus_file_poll,
6038f373 616 .llseek = no_llseek,
1107ba88 617};
2fb3683e
BB
618EXPORT_SYMBOL_GPL(xen_xenbus_fops);
619
620static struct miscdevice xenbus_dev = {
621 .minor = MISC_DYNAMIC_MINOR,
622 .name = "xen/xenbus",
623 .fops = &xen_xenbus_fops,
624};
625
626static int __init xenbus_init(void)
627{
628 int err;
629
630 if (!xen_domain())
631 return -ENODEV;
632
633 err = misc_register(&xenbus_dev);
634 if (err)
283c0972 635 pr_err("Could not register xenbus frontend device\n");
2fb3683e
BB
636 return err;
637}
ab1241a1 638device_initcall(xenbus_init);