blob: 8c0a359ab4a8620ae8343294acd77d6be4c8deb6 [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
Joe Perches283c0972013-06-28 03:21:41 -070038#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
Alex Zeffertt1107ba82009-01-07 18:07:11 -080040#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>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040048#include <linux/sched.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080049#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bastian Blank2fb36832011-12-10 19:29:47 +010057#include <linux/miscdevice.h>
Paul Gortmakerab1241a2016-02-21 19:06:06 -050058#include <linux/init.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080059
Bastian Blank2fb36832011-12-10 19:29:47 +010060#include "xenbus_comms.h"
Alex Zeffertt1107ba82009-01-07 18:07:11 -080061
62#include <xen/xenbus.h>
Konrad Rzeszutek Wilkb79d2ff2011-12-19 15:08:15 -050063#include <xen/xen.h>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080064#include <asm/xen/hypervisor.h>
65
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;
Ian Campbell50bf7372012-01-04 11:39:51 +0000108 char buffer[XENSTORE_PAYLOAD_MAX];
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800109 } 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);
Daniel De Graaf78081212010-09-08 18:10:42 -0400129again:
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800130 while (list_empty(&u->read_buffers)) {
131 mutex_unlock(&u->reply_mutex);
Paolo Bonzini6280f192010-06-23 18:30:15 +0200132 if (filp->f_flags & O_NONBLOCK)
133 return -EAGAIN;
134
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800135 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
Jeremy Fitzhardingefb27cfb2010-08-25 12:19:53 -0700152 if (ret != 0) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800153 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 }
Daniel De Graaf78081212010-09-08 18:10:42 -0400168 if (i == 0)
169 goto again;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800170
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;
189
190 rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
191 if (rb == NULL)
192 return -ENOMEM;
193
194 rb->cons = 0;
195 rb->len = len;
196
197 memcpy(rb->msg, data, len);
198
199 list_add_tail(&rb->list, queue);
200 return 0;
201}
202
203/*
204 * Free all the read_buffer s on a list.
205 * Caller must have sole reference to list.
206 */
207static void queue_cleanup(struct list_head *list)
208{
209 struct read_buffer *rb;
210
211 while (!list_empty(list)) {
212 rb = list_entry(list->next, struct read_buffer, list);
213 list_del(list->next);
214 kfree(rb);
215 }
216}
217
218struct watch_adapter {
219 struct list_head list;
220 struct xenbus_watch watch;
221 struct xenbus_file_priv *dev_data;
222 char *token;
223};
224
225static void free_watch_adapter(struct watch_adapter *watch)
226{
227 kfree(watch->watch.node);
228 kfree(watch->token);
229 kfree(watch);
230}
231
232static struct watch_adapter *alloc_watch_adapter(const char *path,
233 const char *token)
234{
235 struct watch_adapter *watch;
236
237 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
238 if (watch == NULL)
239 goto out_fail;
240
241 watch->watch.node = kstrdup(path, GFP_KERNEL);
242 if (watch->watch.node == NULL)
243 goto out_free;
244
245 watch->token = kstrdup(token, GFP_KERNEL);
246 if (watch->token == NULL)
247 goto out_free;
248
249 return watch;
250
251out_free:
252 free_watch_adapter(watch);
253
254out_fail:
255 return NULL;
256}
257
258static void watch_fired(struct xenbus_watch *watch,
259 const char **vec,
260 unsigned int len)
261{
262 struct watch_adapter *adap;
263 struct xsd_sockmsg hdr;
264 const char *path, *token;
265 int path_len, tok_len, body_len, data_len = 0;
266 int ret;
267 LIST_HEAD(staging_q);
268
269 adap = container_of(watch, struct watch_adapter, watch);
270
271 path = vec[XS_WATCH_PATH];
272 token = adap->token;
273
274 path_len = strlen(path) + 1;
275 tok_len = strlen(token) + 1;
276 if (len > 2)
277 data_len = vec[len] - vec[2] + 1;
278 body_len = path_len + tok_len + data_len;
279
280 hdr.type = XS_WATCH_EVENT;
281 hdr.len = body_len;
282
283 mutex_lock(&adap->dev_data->reply_mutex);
284
285 ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
286 if (!ret)
287 ret = queue_reply(&staging_q, path, path_len);
288 if (!ret)
289 ret = queue_reply(&staging_q, token, tok_len);
290 if (!ret && len > 2)
291 ret = queue_reply(&staging_q, vec[2], data_len);
292
293 if (!ret) {
294 /* success: pass reply list onto watcher */
295 list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
296 wake_up(&adap->dev_data->read_waitq);
297 } else
298 queue_cleanup(&staging_q);
299
300 mutex_unlock(&adap->dev_data->reply_mutex);
301}
302
303static int xenbus_write_transaction(unsigned msg_type,
304 struct xenbus_file_priv *u)
305{
Ian Campbelle88a0fa2009-01-24 08:22:47 +0000306 int rc;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800307 void *reply;
308 struct xenbus_transaction_holder *trans = NULL;
309 LIST_HEAD(staging_q);
310
311 if (msg_type == XS_TRANSACTION_START) {
312 trans = kmalloc(sizeof(*trans), GFP_KERNEL);
313 if (!trans) {
314 rc = -ENOMEM;
315 goto out;
316 }
317 }
318
319 reply = xenbus_dev_request_and_reply(&u->u.msg);
320 if (IS_ERR(reply)) {
321 kfree(trans);
322 rc = PTR_ERR(reply);
323 goto out;
324 }
325
326 if (msg_type == XS_TRANSACTION_START) {
Jennifer Herberta2e75bc22015-02-05 14:45:40 +0000327 if (u->u.msg.type == XS_ERROR)
328 kfree(trans);
329 else {
330 trans->handle.id = simple_strtoul(reply, NULL, 0);
331 list_add(&trans->list, &u->transactions);
332 }
333 } else if (u->u.msg.type == XS_TRANSACTION_END) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800334 list_for_each_entry(trans, &u->transactions, list)
335 if (trans->handle.id == u->u.msg.tx_id)
336 break;
337 BUG_ON(&trans->list == &u->transactions);
338 list_del(&trans->list);
339
340 kfree(trans);
341 }
342
343 mutex_lock(&u->reply_mutex);
Ian Campbelle88a0fa2009-01-24 08:22:47 +0000344 rc = queue_reply(&staging_q, &u->u.msg, sizeof(u->u.msg));
345 if (!rc)
346 rc = queue_reply(&staging_q, reply, u->u.msg.len);
347 if (!rc) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800348 list_splice_tail(&staging_q, &u->read_buffers);
349 wake_up(&u->read_waitq);
350 } else {
351 queue_cleanup(&staging_q);
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800352 }
353 mutex_unlock(&u->reply_mutex);
354
355 kfree(reply);
356
357out:
358 return rc;
359}
360
361static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
362{
363 struct watch_adapter *watch, *tmp_watch;
364 char *path, *token;
365 int err, rc;
366 LIST_HEAD(staging_q);
367
368 path = u->u.buffer + sizeof(u->u.msg);
369 token = memchr(path, 0, u->u.msg.len);
370 if (token == NULL) {
371 rc = -EILSEQ;
372 goto out;
373 }
374 token++;
Jan Beulicha43a5cc2012-01-24 13:52:42 +0000375 if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
376 rc = -EILSEQ;
377 goto out;
378 }
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800379
380 if (msg_type == XS_WATCH) {
381 watch = alloc_watch_adapter(path, token);
382 if (watch == NULL) {
383 rc = -ENOMEM;
384 goto out;
385 }
386
387 watch->watch.callback = watch_fired;
388 watch->dev_data = u;
389
390 err = register_xenbus_watch(&watch->watch);
391 if (err) {
392 free_watch_adapter(watch);
393 rc = err;
394 goto out;
395 }
396 list_add(&watch->list, &u->watches);
397 } else {
398 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
399 if (!strcmp(watch->token, token) &&
400 !strcmp(watch->watch.node, path)) {
401 unregister_xenbus_watch(&watch->watch);
402 list_del(&watch->list);
403 free_watch_adapter(watch);
404 break;
405 }
406 }
407 }
408
409 /* Success. Synthesize a reply to say all is OK. */
410 {
411 struct {
412 struct xsd_sockmsg hdr;
413 char body[3];
414 } __packed reply = {
415 {
416 .type = msg_type,
417 .len = sizeof(reply.body)
418 },
419 "OK"
420 };
421
422 mutex_lock(&u->reply_mutex);
423 rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
Daniel De Graaf76ce7612010-09-07 11:42:18 -0400424 wake_up(&u->read_waitq);
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800425 mutex_unlock(&u->reply_mutex);
426 }
427
428out:
429 return rc;
430}
431
432static ssize_t xenbus_file_write(struct file *filp,
433 const char __user *ubuf,
434 size_t len, loff_t *ppos)
435{
436 struct xenbus_file_priv *u = filp->private_data;
437 uint32_t msg_type;
438 int rc = len;
439 int ret;
440 LIST_HEAD(staging_q);
441
442 /*
443 * We're expecting usermode to be writing properly formed
444 * xenbus messages. If they write an incomplete message we
445 * buffer it up. Once it is complete, we act on it.
446 */
447
448 /*
449 * Make sure concurrent writers can't stomp all over each
450 * other's messages and make a mess of our partial message
451 * buffer. We don't make any attemppt to stop multiple
452 * writers from making a mess of each other's incomplete
453 * messages; we're just trying to guarantee our own internal
454 * consistency and make sure that single writes are handled
455 * atomically.
456 */
457 mutex_lock(&u->msgbuffer_mutex);
458
459 /* Get this out of the way early to avoid confusion */
460 if (len == 0)
461 goto out;
462
463 /* Can't write a xenbus message larger we can buffer */
Jan Beulich1bcaba512012-10-17 13:14:09 -0400464 if (len > sizeof(u->u.buffer) - u->len) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800465 /* On error, dump existing buffer */
466 u->len = 0;
467 rc = -EINVAL;
468 goto out;
469 }
470
471 ret = copy_from_user(u->u.buffer + u->len, ubuf, len);
472
Jeremy Fitzhardingefb27cfb2010-08-25 12:19:53 -0700473 if (ret != 0) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800474 rc = -EFAULT;
475 goto out;
476 }
477
478 /* Deal with a partial copy. */
479 len -= ret;
480 rc = len;
481
482 u->len += len;
483
484 /* Return if we haven't got a full message yet */
485 if (u->len < sizeof(u->u.msg))
486 goto out; /* not even the header yet */
487
488 /* If we're expecting a message that's larger than we can
489 possibly send, dump what we have and return an error. */
490 if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
491 rc = -E2BIG;
492 u->len = 0;
493 goto out;
494 }
495
496 if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
497 goto out; /* incomplete data portion */
498
499 /*
500 * OK, now we have a complete message. Do something with it.
501 */
502
503 msg_type = u->u.msg.type;
504
505 switch (msg_type) {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800506 case XS_WATCH:
507 case XS_UNWATCH:
508 /* (Un)Ask for some path to be watched for changes */
509 ret = xenbus_write_watch(msg_type, u);
510 break;
511
512 default:
Diego Ongaro6d6df2e2010-09-01 09:18:54 -0700513 /* Send out a transaction */
514 ret = xenbus_write_transaction(msg_type, u);
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800515 break;
516 }
517 if (ret != 0)
518 rc = ret;
519
520 /* Buffered message consumed */
521 u->len = 0;
522
523 out:
524 mutex_unlock(&u->msgbuffer_mutex);
525 return rc;
526}
527
528static int xenbus_file_open(struct inode *inode, struct file *filp)
529{
530 struct xenbus_file_priv *u;
531
532 if (xen_store_evtchn == 0)
533 return -ENOENT;
534
535 nonseekable_open(inode, filp);
536
537 u = kzalloc(sizeof(*u), GFP_KERNEL);
538 if (u == NULL)
539 return -ENOMEM;
540
541 INIT_LIST_HEAD(&u->transactions);
542 INIT_LIST_HEAD(&u->watches);
543 INIT_LIST_HEAD(&u->read_buffers);
544 init_waitqueue_head(&u->read_waitq);
545
546 mutex_init(&u->reply_mutex);
547 mutex_init(&u->msgbuffer_mutex);
548
549 filp->private_data = u;
550
551 return 0;
552}
553
554static int xenbus_file_release(struct inode *inode, struct file *filp)
555{
556 struct xenbus_file_priv *u = filp->private_data;
557 struct xenbus_transaction_holder *trans, *tmp;
558 struct watch_adapter *watch, *tmp_watch;
Daniel De Graaf6a5b3be2010-12-20 14:56:09 -0800559 struct read_buffer *rb, *tmp_rb;
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800560
561 /*
562 * No need for locking here because there are no other users,
563 * by definition.
564 */
565
566 list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
567 xenbus_transaction_end(trans->handle, 1);
568 list_del(&trans->list);
569 kfree(trans);
570 }
571
572 list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
573 unregister_xenbus_watch(&watch->watch);
574 list_del(&watch->list);
575 free_watch_adapter(watch);
576 }
577
Daniel De Graaf6a5b3be2010-12-20 14:56:09 -0800578 list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
579 list_del(&rb->list);
580 kfree(rb);
581 }
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800582 kfree(u);
583
584 return 0;
585}
586
587static unsigned int xenbus_file_poll(struct file *file, poll_table *wait)
588{
589 struct xenbus_file_priv *u = file->private_data;
590
591 poll_wait(file, &u->read_waitq, wait);
592 if (!list_empty(&u->read_buffers))
593 return POLLIN | POLLRDNORM;
594 return 0;
595}
596
Bastian Blank2fb36832011-12-10 19:29:47 +0100597const struct file_operations xen_xenbus_fops = {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800598 .read = xenbus_file_read,
599 .write = xenbus_file_write,
600 .open = xenbus_file_open,
601 .release = xenbus_file_release,
602 .poll = xenbus_file_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200603 .llseek = no_llseek,
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800604};
Bastian Blank2fb36832011-12-10 19:29:47 +0100605EXPORT_SYMBOL_GPL(xen_xenbus_fops);
606
607static struct miscdevice xenbus_dev = {
608 .minor = MISC_DYNAMIC_MINOR,
609 .name = "xen/xenbus",
610 .fops = &xen_xenbus_fops,
611};
612
613static int __init xenbus_init(void)
614{
615 int err;
616
617 if (!xen_domain())
618 return -ENODEV;
619
620 err = misc_register(&xenbus_dev);
621 if (err)
Joe Perches283c0972013-06-28 03:21:41 -0700622 pr_err("Could not register xenbus frontend device\n");
Bastian Blank2fb36832011-12-10 19:29:47 +0100623 return err;
624}
Paul Gortmakerab1241a2016-02-21 19:06:06 -0500625device_initcall(xenbus_init);