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