xenbus_dev_frontend.c 16.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Driver giving user-space access to the kernel's xenbus connection
 * to xenstore.
 *
 * Copyright (c) 2005, Christian Limpach
 * Copyright (c) 2005, Rusty Russell, IBM Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation; or, when distributed
 * separately from the Linux kernel or incorporated into other
 * software packages, subject to the following license:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Changes:
 * 2008-10-07  Alex Zeffertt    Replaced /proc/xen/xenbus with xenfs filesystem
 *                              and /proc/xen compatibility mount point.
 *                              Turned xenfs into a loadable module.
 */

38
39
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

40
41
42
43
44
45
46
47
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/uio.h>
#include <linux/notifier.h>
#include <linux/wait.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/mutex.h>
48
#include <linux/sched.h>
49
50
51
52
53
54
55
#include <linux/spinlock.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/namei.h>
#include <linux/string.h>
56
#include <linux/slab.h>
Bastian Blank's avatar
Bastian Blank committed
57
#include <linux/miscdevice.h>
58
#include <linux/workqueue.h>
59
60

#include <xen/xenbus.h>
61
#include <xen/xen.h>
62
63
#include <asm/xen/hypervisor.h>

64
65
#include "xenbus.h"

66
67
unsigned int xb_dev_generation_id;

68
69
70
71
72
73
74
/*
 * An element of a list of outstanding transactions, for which we're
 * still waiting a reply.
 */
struct xenbus_transaction_holder {
	struct list_head list;
	struct xenbus_transaction handle;
75
	unsigned int generation_id;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
};

/*
 * A buffer of data on the queue.
 */
struct read_buffer {
	struct list_head list;
	unsigned int cons;
	unsigned int len;
	char msg[];
};

struct xenbus_file_priv {
	/*
	 * msgbuffer_mutex is held while partial requests are built up
	 * and complete requests are acted on.  It therefore protects
	 * the "transactions" and "watches" lists, and the partial
	 * request length and buffer.
	 *
	 * reply_mutex protects the reply being built up to return to
	 * usermode.  It nests inside msgbuffer_mutex but may be held
	 * alone during a watch callback.
	 */
	struct mutex msgbuffer_mutex;

	/* In-progress transactions */
	struct list_head transactions;

	/* Active watches. */
	struct list_head watches;

	/* Partial request. */
	unsigned int len;
	union {
		struct xsd_sockmsg msg;
111
		char buffer[XENSTORE_PAYLOAD_MAX];
112
113
114
115
116
117
118
	} u;

	/* Response queue. */
	struct mutex reply_mutex;
	struct list_head read_buffers;
	wait_queue_head_t read_waitq;

119
	struct kref kref;
120
121

	struct work_struct wq;
122
123
124
125
126
127
128
129
130
131
132
133
134
};

/* Read out any raw xenbus messages queued up. */
static ssize_t xenbus_file_read(struct file *filp,
			       char __user *ubuf,
			       size_t len, loff_t *ppos)
{
	struct xenbus_file_priv *u = filp->private_data;
	struct read_buffer *rb;
	unsigned i;
	int ret;

	mutex_lock(&u->reply_mutex);
135
again:
136
137
	while (list_empty(&u->read_buffers)) {
		mutex_unlock(&u->reply_mutex);
138
139
140
		if (filp->f_flags & O_NONBLOCK)
			return -EAGAIN;

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
		ret = wait_event_interruptible(u->read_waitq,
					       !list_empty(&u->read_buffers));
		if (ret)
			return ret;
		mutex_lock(&u->reply_mutex);
	}

	rb = list_entry(u->read_buffers.next, struct read_buffer, list);
	i = 0;
	while (i < len) {
		unsigned sz = min((unsigned)len - i, rb->len - rb->cons);

		ret = copy_to_user(ubuf + i, &rb->msg[rb->cons], sz);

		i += sz - ret;
		rb->cons += sz - ret;

158
		if (ret != 0) {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
			if (i == 0)
				i = -EFAULT;
			goto out;
		}

		/* Clear out buffer if it has been consumed */
		if (rb->cons == rb->len) {
			list_del(&rb->list);
			kfree(rb);
			if (list_empty(&u->read_buffers))
				break;
			rb = list_entry(u->read_buffers.next,
					struct read_buffer, list);
		}
	}
174
175
	if (i == 0)
		goto again;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

out:
	mutex_unlock(&u->reply_mutex);
	return i;
}

/*
 * Add a buffer to the queue.  Caller must hold the appropriate lock
 * if the queue is not local.  (Commonly the caller will build up
 * multiple queued buffers on a temporary local list, and then add it
 * to the appropriate list under lock once all the buffers have een
 * successfully allocated.)
 */
static int queue_reply(struct list_head *queue, const void *data, size_t len)
{
	struct read_buffer *rb;

	if (len == 0)
		return 0;
195
196
	if (len > XENSTORE_PAYLOAD_MAX)
		return -EINVAL;
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

	rb = kmalloc(sizeof(*rb) + len, GFP_KERNEL);
	if (rb == NULL)
		return -ENOMEM;

	rb->cons = 0;
	rb->len = len;

	memcpy(rb->msg, data, len);

	list_add_tail(&rb->list, queue);
	return 0;
}

/*
 * Free all the read_buffer s on a list.
 * Caller must have sole reference to list.
 */
static void queue_cleanup(struct list_head *list)
{
	struct read_buffer *rb;

	while (!list_empty(list)) {
		rb = list_entry(list->next, struct read_buffer, list);
		list_del(list->next);
		kfree(rb);
	}
}

struct watch_adapter {
	struct list_head list;
	struct xenbus_watch watch;
	struct xenbus_file_priv *dev_data;
	char *token;
};

static void free_watch_adapter(struct watch_adapter *watch)
{
	kfree(watch->watch.node);
	kfree(watch->token);
	kfree(watch);
}

static struct watch_adapter *alloc_watch_adapter(const char *path,
						 const char *token)
{
	struct watch_adapter *watch;

	watch = kzalloc(sizeof(*watch), GFP_KERNEL);
	if (watch == NULL)
		goto out_fail;

	watch->watch.node = kstrdup(path, GFP_KERNEL);
	if (watch->watch.node == NULL)
		goto out_free;

	watch->token = kstrdup(token, GFP_KERNEL);
	if (watch->token == NULL)
		goto out_free;

	return watch;

out_free:
	free_watch_adapter(watch);

out_fail:
	return NULL;
}

static void watch_fired(struct xenbus_watch *watch,
267
268
			const char *path,
			const char *token)
269
270
271
{
	struct watch_adapter *adap;
	struct xsd_sockmsg hdr;
272
273
	const char *token_caller;
	int path_len, tok_len, body_len;
274
275
276
277
278
	int ret;
	LIST_HEAD(staging_q);

	adap = container_of(watch, struct watch_adapter, watch);

279
	token_caller = adap->token;
280
281

	path_len = strlen(path) + 1;
282
283
	tok_len = strlen(token_caller) + 1;
	body_len = path_len + tok_len;
284
285
286
287
288
289
290
291
292
293

	hdr.type = XS_WATCH_EVENT;
	hdr.len = body_len;

	mutex_lock(&adap->dev_data->reply_mutex);

	ret = queue_reply(&staging_q, &hdr, sizeof(hdr));
	if (!ret)
		ret = queue_reply(&staging_q, path, path_len);
	if (!ret)
294
		ret = queue_reply(&staging_q, token_caller, tok_len);
295
296
297
298
299
300
301
302
303
304
305

	if (!ret) {
		/* success: pass reply list onto watcher */
		list_splice_tail(&staging_q, &adap->dev_data->read_buffers);
		wake_up(&adap->dev_data->read_waitq);
	} else
		queue_cleanup(&staging_q);

	mutex_unlock(&adap->dev_data->reply_mutex);
}

306
static void xenbus_worker(struct work_struct *wq)
307
308
309
310
311
312
{
	struct xenbus_file_priv *u;
	struct xenbus_transaction_holder *trans, *tmp;
	struct watch_adapter *watch, *tmp_watch;
	struct read_buffer *rb, *tmp_rb;

313
	u = container_of(wq, struct xenbus_file_priv, wq);
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

	/*
	 * No need for locking here because there are no other users,
	 * by definition.
	 */

	list_for_each_entry_safe(trans, tmp, &u->transactions, list) {
		xenbus_transaction_end(trans->handle, 1);
		list_del(&trans->list);
		kfree(trans);
	}

	list_for_each_entry_safe(watch, tmp_watch, &u->watches, list) {
		unregister_xenbus_watch(&watch->watch);
		list_del(&watch->list);
		free_watch_adapter(watch);
	}

	list_for_each_entry_safe(rb, tmp_rb, &u->read_buffers, list) {
		list_del(&rb->list);
		kfree(rb);
	}
	kfree(u);
}

339
340
341
342
343
344
345
346
347
348
349
350
static void xenbus_file_free(struct kref *kref)
{
	struct xenbus_file_priv *u;

	/*
	 * We might be called in xenbus_thread().
	 * Use workqueue to avoid deadlock.
	 */
	u = container_of(kref, struct xenbus_file_priv, kref);
	schedule_work(&u->wq);
}

351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
static struct xenbus_transaction_holder *xenbus_get_transaction(
	struct xenbus_file_priv *u, uint32_t tx_id)
{
	struct xenbus_transaction_holder *trans;

	list_for_each_entry(trans, &u->transactions, list)
		if (trans->handle.id == tx_id)
			return trans;

	return NULL;
}

void xenbus_dev_queue_reply(struct xb_req_data *req)
{
	struct xenbus_file_priv *u = req->par;
	struct xenbus_transaction_holder *trans = NULL;
	int rc;
	LIST_HEAD(staging_q);

	xs_request_exit(req);

	mutex_lock(&u->msgbuffer_mutex);

	if (req->type == XS_TRANSACTION_START) {
		trans = xenbus_get_transaction(u, 0);
		if (WARN_ON(!trans))
			goto out;
		if (req->msg.type == XS_ERROR) {
			list_del(&trans->list);
			kfree(trans);
		} else {
			rc = kstrtou32(req->body, 10, &trans->handle.id);
			if (WARN_ON(rc))
				goto out;
		}
386
	} else if (req->type == XS_TRANSACTION_END) {
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
		trans = xenbus_get_transaction(u, req->msg.tx_id);
		if (WARN_ON(!trans))
			goto out;
		list_del(&trans->list);
		kfree(trans);
	}

	mutex_unlock(&u->msgbuffer_mutex);

	mutex_lock(&u->reply_mutex);
	rc = queue_reply(&staging_q, &req->msg, sizeof(req->msg));
	if (!rc)
		rc = queue_reply(&staging_q, req->body, req->msg.len);
	if (!rc) {
		list_splice_tail(&staging_q, &u->read_buffers);
		wake_up(&u->read_waitq);
	} else {
		queue_cleanup(&staging_q);
	}
	mutex_unlock(&u->reply_mutex);

	kfree(req->body);
	kfree(req);

	kref_put(&u->kref, xenbus_file_free);

	return;

 out:
	mutex_unlock(&u->msgbuffer_mutex);
}

419
420
421
422
423
static int xenbus_command_reply(struct xenbus_file_priv *u,
				unsigned int msg_type, const char *reply)
{
	struct {
		struct xsd_sockmsg hdr;
424
		char body[16];
425
426
427
428
429
430
431
432
	} msg;
	int rc;

	msg.hdr = u->u.msg;
	msg.hdr.type = msg_type;
	msg.hdr.len = strlen(reply) + 1;
	if (msg.hdr.len > sizeof(msg.body))
		return -E2BIG;
433
	memcpy(&msg.body, reply, msg.hdr.len);
434
435
436
437
438
439

	mutex_lock(&u->reply_mutex);
	rc = queue_reply(&u->read_buffers, &msg, sizeof(msg.hdr) + msg.hdr.len);
	wake_up(&u->read_waitq);
	mutex_unlock(&u->reply_mutex);

440
441
442
	if (!rc)
		kref_put(&u->kref, xenbus_file_free);

443
444
445
	return rc;
}

446
447
448
static int xenbus_write_transaction(unsigned msg_type,
				    struct xenbus_file_priv *u)
{
449
	int rc;
450
	struct xenbus_transaction_holder *trans = NULL;
451
452
453
454
	struct {
		struct xsd_sockmsg hdr;
		char body[];
	} *msg = (void *)u->u.buffer;
455
456

	if (msg_type == XS_TRANSACTION_START) {
457
		trans = kzalloc(sizeof(*trans), GFP_KERNEL);
458
459
460
461
		if (!trans) {
			rc = -ENOMEM;
			goto out;
		}
462
		trans->generation_id = xb_dev_generation_id;
463
		list_add(&trans->list, &u->transactions);
464
465
	} else if (msg->hdr.tx_id != 0 &&
		   !xenbus_get_transaction(u, msg->hdr.tx_id))
466
		return xenbus_command_reply(u, XS_ERROR, "ENOENT");
467
468
469
470
	else if (msg_type == XS_TRANSACTION_END &&
		 !(msg->hdr.len == 2 &&
		   (!strcmp(msg->body, "T") || !strcmp(msg->body, "F"))))
		return xenbus_command_reply(u, XS_ERROR, "EINVAL");
471
472
473
474
475
476
477
478
479
480
481
482
483
484
	else if (msg_type == XS_TRANSACTION_END) {
		trans = xenbus_get_transaction(u, msg->hdr.tx_id);
		if (trans && trans->generation_id != xb_dev_generation_id) {
			list_del(&trans->list);
			kfree(trans);
			if (!strcmp(msg->body, "T"))
				return xenbus_command_reply(u, XS_ERROR,
							    "EAGAIN");
			else
				return xenbus_command_reply(u,
							    XS_TRANSACTION_END,
							    "OK");
		}
	}
485

486
	rc = xenbus_dev_request_and_reply(&msg->hdr, u);
487
488
	if (rc && trans) {
		list_del(&trans->list);
489
		kfree(trans);
490
	}
491
492
493
494
495
496
497

out:
	return rc;
}

static int xenbus_write_watch(unsigned msg_type, struct xenbus_file_priv *u)
{
Jan Beulich's avatar
Jan Beulich committed
498
	struct watch_adapter *watch;
499
500
501
502
503
504
	char *path, *token;
	int err, rc;

	path = u->u.buffer + sizeof(u->u.msg);
	token = memchr(path, 0, u->u.msg.len);
	if (token == NULL) {
505
		rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
506
507
508
		goto out;
	}
	token++;
509
	if (memchr(token, 0, u->u.msg.len - (token - path)) == NULL) {
510
		rc = xenbus_command_reply(u, XS_ERROR, "EINVAL");
511
512
		goto out;
	}
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531

	if (msg_type == XS_WATCH) {
		watch = alloc_watch_adapter(path, token);
		if (watch == NULL) {
			rc = -ENOMEM;
			goto out;
		}

		watch->watch.callback = watch_fired;
		watch->dev_data = u;

		err = register_xenbus_watch(&watch->watch);
		if (err) {
			free_watch_adapter(watch);
			rc = err;
			goto out;
		}
		list_add(&watch->list, &u->watches);
	} else {
Jan Beulich's avatar
Jan Beulich committed
532
		list_for_each_entry(watch, &u->watches, list) {
533
534
535
536
537
538
539
540
541
542
543
			if (!strcmp(watch->token, token) &&
			    !strcmp(watch->watch.node, path)) {
				unregister_xenbus_watch(&watch->watch);
				list_del(&watch->list);
				free_watch_adapter(watch);
				break;
			}
		}
	}

	/* Success.  Synthesize a reply to say all is OK. */
544
	rc = xenbus_command_reply(u, msg_type, "OK");
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

out:
	return rc;
}

static ssize_t xenbus_file_write(struct file *filp,
				const char __user *ubuf,
				size_t len, loff_t *ppos)
{
	struct xenbus_file_priv *u = filp->private_data;
	uint32_t msg_type;
	int rc = len;
	int ret;

	/*
	 * We're expecting usermode to be writing properly formed
	 * xenbus messages.  If they write an incomplete message we
	 * buffer it up.  Once it is complete, we act on it.
	 */

	/*
	 * Make sure concurrent writers can't stomp all over each
	 * other's messages and make a mess of our partial message
	 * buffer.  We don't make any attemppt to stop multiple
	 * writers from making a mess of each other's incomplete
	 * messages; we're just trying to guarantee our own internal
	 * consistency and make sure that single writes are handled
	 * atomically.
	 */
	mutex_lock(&u->msgbuffer_mutex);

	/* Get this out of the way early to avoid confusion */
	if (len == 0)
		goto out;

	/* Can't write a xenbus message larger we can buffer */
581
	if (len > sizeof(u->u.buffer) - u->len) {
582
583
584
585
586
587
588
589
		/* On error, dump existing buffer */
		u->len = 0;
		rc = -EINVAL;
		goto out;
	}

	ret = copy_from_user(u->u.buffer + u->len, ubuf, len);

590
	if (ret != 0) {
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
		rc = -EFAULT;
		goto out;
	}

	/* Deal with a partial copy. */
	len -= ret;
	rc = len;

	u->len += len;

	/* Return if we haven't got a full message yet */
	if (u->len < sizeof(u->u.msg))
		goto out;	/* not even the header yet */

	/* If we're expecting a message that's larger than we can
	   possibly send, dump what we have and return an error. */
	if ((sizeof(u->u.msg) + u->u.msg.len) > sizeof(u->u.buffer)) {
		rc = -E2BIG;
		u->len = 0;
		goto out;
	}

	if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
		goto out;	/* incomplete data portion */

	/*
	 * OK, now we have a complete message.  Do something with it.
	 */

620
621
	kref_get(&u->kref);

622
623
624
625
626
627
628
629
630
631
	msg_type = u->u.msg.type;

	switch (msg_type) {
	case XS_WATCH:
	case XS_UNWATCH:
		/* (Un)Ask for some path to be watched for changes */
		ret = xenbus_write_watch(msg_type, u);
		break;

	default:
632
633
		/* Send out a transaction */
		ret = xenbus_write_transaction(msg_type, u);
634
635
		break;
	}
636
	if (ret != 0) {
637
		rc = ret;
638
639
		kref_put(&u->kref, xenbus_file_free);
	}
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655

	/* Buffered message consumed */
	u->len = 0;

 out:
	mutex_unlock(&u->msgbuffer_mutex);
	return rc;
}

static int xenbus_file_open(struct inode *inode, struct file *filp)
{
	struct xenbus_file_priv *u;

	if (xen_store_evtchn == 0)
		return -ENOENT;

656
	stream_open(inode, filp);
657

658
659
660
661
	u = kzalloc(sizeof(*u), GFP_KERNEL);
	if (u == NULL)
		return -ENOMEM;

662
663
	kref_init(&u->kref);

664
665
666
667
	INIT_LIST_HEAD(&u->transactions);
	INIT_LIST_HEAD(&u->watches);
	INIT_LIST_HEAD(&u->read_buffers);
	init_waitqueue_head(&u->read_waitq);
668
	INIT_WORK(&u->wq, xenbus_worker);
669
670
671
672
673
674
675
676
677
678
679
680
681

	mutex_init(&u->reply_mutex);
	mutex_init(&u->msgbuffer_mutex);

	filp->private_data = u;

	return 0;
}

static int xenbus_file_release(struct inode *inode, struct file *filp)
{
	struct xenbus_file_priv *u = filp->private_data;

682
	kref_put(&u->kref, xenbus_file_free);
683
684
685
686

	return 0;
}

687
static __poll_t xenbus_file_poll(struct file *file, poll_table *wait)
688
689
690
691
692
{
	struct xenbus_file_priv *u = file->private_data;

	poll_wait(file, &u->read_waitq, wait);
	if (!list_empty(&u->read_buffers))
693
		return EPOLLIN | EPOLLRDNORM;
694
695
696
	return 0;
}

Bastian Blank's avatar
Bastian Blank committed
697
const struct file_operations xen_xenbus_fops = {
698
699
700
701
702
	.read = xenbus_file_read,
	.write = xenbus_file_write,
	.open = xenbus_file_open,
	.release = xenbus_file_release,
	.poll = xenbus_file_poll,
703
	.llseek = no_llseek,
704
};
Bastian Blank's avatar
Bastian Blank committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
EXPORT_SYMBOL_GPL(xen_xenbus_fops);

static struct miscdevice xenbus_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "xen/xenbus",
	.fops = &xen_xenbus_fops,
};

static int __init xenbus_init(void)
{
	int err;

	if (!xen_domain())
		return -ENODEV;

	err = misc_register(&xenbus_dev);
	if (err)
722
		pr_err("Could not register xenbus frontend device\n");
Bastian Blank's avatar
Bastian Blank committed
723
724
	return err;
}
725
device_initcall(xenbus_init);