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