1/*
2 * CUSE: Character device in Userspace
3 *
4 * Copyright (C) 2008-2009  SUSE Linux Products GmbH
5 * Copyright (C) 2008-2009  Tejun Heo <tj@kernel.org>
6 *
7 * This file is released under the GPLv2.
8 *
9 * CUSE enables character devices to be implemented from userland much
10 * like FUSE allows filesystems.  On initialization /dev/cuse is
11 * created.  By opening the file and replying to the CUSE_INIT request
12 * userland CUSE server can create a character device.  After that the
13 * operation is very similar to FUSE.
14 *
15 * A CUSE instance involves the following objects.
16 *
17 * cuse_conn	: contains fuse_conn and serves as bonding structure
18 * channel	: file handle connected to the userland CUSE server
19 * cdev		: the implemented character device
20 * dev		: generic device for cdev
21 *
22 * Note that 'channel' is what 'dev' is in FUSE.  As CUSE deals with
23 * devices, it's called 'channel' to reduce confusion.
24 *
25 * channel determines when the character device dies.  When channel is
26 * closed, everything begins to destruct.  The cuse_conn is taken off
27 * the lookup table preventing further access from cdev, cdev and
28 * generic device are removed and the base reference of cuse_conn is
29 * put.
30 *
31 * On each open, the matching cuse_conn is looked up and if found an
32 * additional reference is taken which is released when the file is
33 * closed.
34 */
35
36#include <linux/fuse.h>
37#include <linux/cdev.h>
38#include <linux/device.h>
39#include <linux/file.h>
40#include <linux/fs.h>
41#include <linux/aio.h>
42#include <linux/kdev_t.h>
43#include <linux/kthread.h>
44#include <linux/list.h>
45#include <linux/magic.h>
46#include <linux/miscdevice.h>
47#include <linux/mutex.h>
48#include <linux/slab.h>
49#include <linux/stat.h>
50#include <linux/module.h>
51
52#include "fuse_i.h"
53
54#define CUSE_CONNTBL_LEN	64
55
56struct cuse_conn {
57	struct list_head	list;	/* linked on cuse_conntbl */
58	struct fuse_conn	fc;	/* fuse connection */
59	struct cdev		*cdev;	/* associated character device */
60	struct device		*dev;	/* device representing @cdev */
61
62	/* init parameters, set once during initialization */
63	bool			unrestricted_ioctl;
64};
65
66static DEFINE_MUTEX(cuse_lock);		/* protects registration */
67static struct list_head cuse_conntbl[CUSE_CONNTBL_LEN];
68static struct class *cuse_class;
69
70static struct cuse_conn *fc_to_cc(struct fuse_conn *fc)
71{
72	return container_of(fc, struct cuse_conn, fc);
73}
74
75static struct list_head *cuse_conntbl_head(dev_t devt)
76{
77	return &cuse_conntbl[(MAJOR(devt) + MINOR(devt)) % CUSE_CONNTBL_LEN];
78}
79
80
81/**************************************************************************
82 * CUSE frontend operations
83 *
84 * These are file operations for the character device.
85 *
86 * On open, CUSE opens a file from the FUSE mnt and stores it to
87 * private_data of the open file.  All other ops call FUSE ops on the
88 * FUSE file.
89 */
90
91static ssize_t cuse_read(struct file *file, char __user *buf, size_t count,
92			 loff_t *ppos)
93{
94	loff_t pos = 0;
95	struct iovec iov = { .iov_base = buf, .iov_len = count };
96	struct fuse_io_priv io = { .async = 0, .file = file };
97	struct iov_iter ii;
98	iov_iter_init(&ii, READ, &iov, 1, count);
99
100	return fuse_direct_io(&io, &ii, &pos, FUSE_DIO_CUSE);
101}
102
103static ssize_t cuse_write(struct file *file, const char __user *buf,
104			  size_t count, loff_t *ppos)
105{
106	loff_t pos = 0;
107	struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
108	struct fuse_io_priv io = { .async = 0, .file = file };
109	struct iov_iter ii;
110	iov_iter_init(&ii, WRITE, &iov, 1, count);
111
112	/*
113	 * No locking or generic_write_checks(), the server is
114	 * responsible for locking and sanity checks.
115	 */
116	return fuse_direct_io(&io, &ii, &pos,
117			      FUSE_DIO_WRITE | FUSE_DIO_CUSE);
118}
119
120static int cuse_open(struct inode *inode, struct file *file)
121{
122	dev_t devt = inode->i_cdev->dev;
123	struct cuse_conn *cc = NULL, *pos;
124	int rc;
125
126	/* look up and get the connection */
127	mutex_lock(&cuse_lock);
128	list_for_each_entry(pos, cuse_conntbl_head(devt), list)
129		if (pos->dev->devt == devt) {
130			fuse_conn_get(&pos->fc);
131			cc = pos;
132			break;
133		}
134	mutex_unlock(&cuse_lock);
135
136	/* dead? */
137	if (!cc)
138		return -ENODEV;
139
140	/*
141	 * Generic permission check is already done against the chrdev
142	 * file, proceed to open.
143	 */
144	rc = fuse_do_open(&cc->fc, 0, file, 0);
145	if (rc)
146		fuse_conn_put(&cc->fc);
147	return rc;
148}
149
150static int cuse_release(struct inode *inode, struct file *file)
151{
152	struct fuse_file *ff = file->private_data;
153	struct fuse_conn *fc = ff->fc;
154
155	fuse_sync_release(ff, file->f_flags);
156	fuse_conn_put(fc);
157
158	return 0;
159}
160
161static long cuse_file_ioctl(struct file *file, unsigned int cmd,
162			    unsigned long arg)
163{
164	struct fuse_file *ff = file->private_data;
165	struct cuse_conn *cc = fc_to_cc(ff->fc);
166	unsigned int flags = 0;
167
168	if (cc->unrestricted_ioctl)
169		flags |= FUSE_IOCTL_UNRESTRICTED;
170
171	return fuse_do_ioctl(file, cmd, arg, flags);
172}
173
174static long cuse_file_compat_ioctl(struct file *file, unsigned int cmd,
175				   unsigned long arg)
176{
177	struct fuse_file *ff = file->private_data;
178	struct cuse_conn *cc = fc_to_cc(ff->fc);
179	unsigned int flags = FUSE_IOCTL_COMPAT;
180
181	if (cc->unrestricted_ioctl)
182		flags |= FUSE_IOCTL_UNRESTRICTED;
183
184	return fuse_do_ioctl(file, cmd, arg, flags);
185}
186
187static const struct file_operations cuse_frontend_fops = {
188	.owner			= THIS_MODULE,
189	.read			= cuse_read,
190	.write			= cuse_write,
191	.open			= cuse_open,
192	.release		= cuse_release,
193	.unlocked_ioctl		= cuse_file_ioctl,
194	.compat_ioctl		= cuse_file_compat_ioctl,
195	.poll			= fuse_file_poll,
196	.llseek		= noop_llseek,
197};
198
199
200/**************************************************************************
201 * CUSE channel initialization and destruction
202 */
203
204struct cuse_devinfo {
205	const char		*name;
206};
207
208/**
209 * cuse_parse_one - parse one key=value pair
210 * @pp: i/o parameter for the current position
211 * @end: points to one past the end of the packed string
212 * @keyp: out parameter for key
213 * @valp: out parameter for value
214 *
215 * *@pp points to packed strings - "key0=val0\0key1=val1\0" which ends
216 * at @end - 1.  This function parses one pair and set *@keyp to the
217 * start of the key and *@valp to the start of the value.  Note that
218 * the original string is modified such that the key string is
219 * terminated with '\0'.  *@pp is updated to point to the next string.
220 *
221 * RETURNS:
222 * 1 on successful parse, 0 on EOF, -errno on failure.
223 */
224static int cuse_parse_one(char **pp, char *end, char **keyp, char **valp)
225{
226	char *p = *pp;
227	char *key, *val;
228
229	while (p < end && *p == '\0')
230		p++;
231	if (p == end)
232		return 0;
233
234	if (end[-1] != '\0') {
235		printk(KERN_ERR "CUSE: info not properly terminated\n");
236		return -EINVAL;
237	}
238
239	key = val = p;
240	p += strlen(p);
241
242	if (valp) {
243		strsep(&val, "=");
244		if (!val)
245			val = key + strlen(key);
246		key = strstrip(key);
247		val = strstrip(val);
248	} else
249		key = strstrip(key);
250
251	if (!strlen(key)) {
252		printk(KERN_ERR "CUSE: zero length info key specified\n");
253		return -EINVAL;
254	}
255
256	*pp = p;
257	*keyp = key;
258	if (valp)
259		*valp = val;
260
261	return 1;
262}
263
264/**
265 * cuse_parse_dev_info - parse device info
266 * @p: device info string
267 * @len: length of device info string
268 * @devinfo: out parameter for parsed device info
269 *
270 * Parse @p to extract device info and store it into @devinfo.  String
271 * pointed to by @p is modified by parsing and @devinfo points into
272 * them, so @p shouldn't be freed while @devinfo is in use.
273 *
274 * RETURNS:
275 * 0 on success, -errno on failure.
276 */
277static int cuse_parse_devinfo(char *p, size_t len, struct cuse_devinfo *devinfo)
278{
279	char *end = p + len;
280	char *uninitialized_var(key), *uninitialized_var(val);
281	int rc;
282
283	while (true) {
284		rc = cuse_parse_one(&p, end, &key, &val);
285		if (rc < 0)
286			return rc;
287		if (!rc)
288			break;
289		if (strcmp(key, "DEVNAME") == 0)
290			devinfo->name = val;
291		else
292			printk(KERN_WARNING "CUSE: unknown device info \"%s\"\n",
293			       key);
294	}
295
296	if (!devinfo->name || !strlen(devinfo->name)) {
297		printk(KERN_ERR "CUSE: DEVNAME unspecified\n");
298		return -EINVAL;
299	}
300
301	return 0;
302}
303
304static void cuse_gendev_release(struct device *dev)
305{
306	kfree(dev);
307}
308
309/**
310 * cuse_process_init_reply - finish initializing CUSE channel
311 *
312 * This function creates the character device and sets up all the
313 * required data structures for it.  Please read the comment at the
314 * top of this file for high level overview.
315 */
316static void cuse_process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
317{
318	struct cuse_conn *cc = fc_to_cc(fc), *pos;
319	struct cuse_init_out *arg = req->out.args[0].value;
320	struct page *page = req->pages[0];
321	struct cuse_devinfo devinfo = { };
322	struct device *dev;
323	struct cdev *cdev;
324	dev_t devt;
325	int rc, i;
326
327	if (req->out.h.error ||
328	    arg->major != FUSE_KERNEL_VERSION || arg->minor < 11) {
329		goto err;
330	}
331
332	fc->minor = arg->minor;
333	fc->max_read = max_t(unsigned, arg->max_read, 4096);
334	fc->max_write = max_t(unsigned, arg->max_write, 4096);
335
336	/* parse init reply */
337	cc->unrestricted_ioctl = arg->flags & CUSE_UNRESTRICTED_IOCTL;
338
339	rc = cuse_parse_devinfo(page_address(page), req->out.args[1].size,
340				&devinfo);
341	if (rc)
342		goto err;
343
344	/* determine and reserve devt */
345	devt = MKDEV(arg->dev_major, arg->dev_minor);
346	if (!MAJOR(devt))
347		rc = alloc_chrdev_region(&devt, MINOR(devt), 1, devinfo.name);
348	else
349		rc = register_chrdev_region(devt, 1, devinfo.name);
350	if (rc) {
351		printk(KERN_ERR "CUSE: failed to register chrdev region\n");
352		goto err;
353	}
354
355	/* devt determined, create device */
356	rc = -ENOMEM;
357	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
358	if (!dev)
359		goto err_region;
360
361	device_initialize(dev);
362	dev_set_uevent_suppress(dev, 1);
363	dev->class = cuse_class;
364	dev->devt = devt;
365	dev->release = cuse_gendev_release;
366	dev_set_drvdata(dev, cc);
367	dev_set_name(dev, "%s", devinfo.name);
368
369	mutex_lock(&cuse_lock);
370
371	/* make sure the device-name is unique */
372	for (i = 0; i < CUSE_CONNTBL_LEN; ++i) {
373		list_for_each_entry(pos, &cuse_conntbl[i], list)
374			if (!strcmp(dev_name(pos->dev), dev_name(dev)))
375				goto err_unlock;
376	}
377
378	rc = device_add(dev);
379	if (rc)
380		goto err_unlock;
381
382	/* register cdev */
383	rc = -ENOMEM;
384	cdev = cdev_alloc();
385	if (!cdev)
386		goto err_unlock;
387
388	cdev->owner = THIS_MODULE;
389	cdev->ops = &cuse_frontend_fops;
390
391	rc = cdev_add(cdev, devt, 1);
392	if (rc)
393		goto err_cdev;
394
395	cc->dev = dev;
396	cc->cdev = cdev;
397
398	/* make the device available */
399	list_add(&cc->list, cuse_conntbl_head(devt));
400	mutex_unlock(&cuse_lock);
401
402	/* announce device availability */
403	dev_set_uevent_suppress(dev, 0);
404	kobject_uevent(&dev->kobj, KOBJ_ADD);
405out:
406	kfree(arg);
407	__free_page(page);
408	return;
409
410err_cdev:
411	cdev_del(cdev);
412err_unlock:
413	mutex_unlock(&cuse_lock);
414	put_device(dev);
415err_region:
416	unregister_chrdev_region(devt, 1);
417err:
418	fuse_conn_kill(fc);
419	goto out;
420}
421
422static int cuse_send_init(struct cuse_conn *cc)
423{
424	int rc;
425	struct fuse_req *req;
426	struct page *page;
427	struct fuse_conn *fc = &cc->fc;
428	struct cuse_init_in *arg;
429	void *outarg;
430
431	BUILD_BUG_ON(CUSE_INIT_INFO_MAX > PAGE_SIZE);
432
433	req = fuse_get_req_for_background(fc, 1);
434	if (IS_ERR(req)) {
435		rc = PTR_ERR(req);
436		goto err;
437	}
438
439	rc = -ENOMEM;
440	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
441	if (!page)
442		goto err_put_req;
443
444	outarg = kzalloc(sizeof(struct cuse_init_out), GFP_KERNEL);
445	if (!outarg)
446		goto err_free_page;
447
448	arg = &req->misc.cuse_init_in;
449	arg->major = FUSE_KERNEL_VERSION;
450	arg->minor = FUSE_KERNEL_MINOR_VERSION;
451	arg->flags |= CUSE_UNRESTRICTED_IOCTL;
452	req->in.h.opcode = CUSE_INIT;
453	req->in.numargs = 1;
454	req->in.args[0].size = sizeof(struct cuse_init_in);
455	req->in.args[0].value = arg;
456	req->out.numargs = 2;
457	req->out.args[0].size = sizeof(struct cuse_init_out);
458	req->out.args[0].value = outarg;
459	req->out.args[1].size = CUSE_INIT_INFO_MAX;
460	req->out.argvar = 1;
461	req->out.argpages = 1;
462	req->pages[0] = page;
463	req->page_descs[0].length = req->out.args[1].size;
464	req->num_pages = 1;
465	req->end = cuse_process_init_reply;
466	fuse_request_send_background(fc, req);
467
468	return 0;
469
470err_free_page:
471	__free_page(page);
472err_put_req:
473	fuse_put_request(fc, req);
474err:
475	return rc;
476}
477
478static void cuse_fc_release(struct fuse_conn *fc)
479{
480	struct cuse_conn *cc = fc_to_cc(fc);
481	kfree_rcu(cc, fc.rcu);
482}
483
484/**
485 * cuse_channel_open - open method for /dev/cuse
486 * @inode: inode for /dev/cuse
487 * @file: file struct being opened
488 *
489 * Userland CUSE server can create a CUSE device by opening /dev/cuse
490 * and replying to the initialization request kernel sends.  This
491 * function is responsible for handling CUSE device initialization.
492 * Because the fd opened by this function is used during
493 * initialization, this function only creates cuse_conn and sends
494 * init.  The rest is delegated to a kthread.
495 *
496 * RETURNS:
497 * 0 on success, -errno on failure.
498 */
499static int cuse_channel_open(struct inode *inode, struct file *file)
500{
501	struct cuse_conn *cc;
502	int rc;
503
504	/* set up cuse_conn */
505	cc = kzalloc(sizeof(*cc), GFP_KERNEL);
506	if (!cc)
507		return -ENOMEM;
508
509	fuse_conn_init(&cc->fc);
510
511	INIT_LIST_HEAD(&cc->list);
512	cc->fc.release = cuse_fc_release;
513
514	cc->fc.connected = 1;
515	cc->fc.initialized = 1;
516	rc = cuse_send_init(cc);
517	if (rc) {
518		fuse_conn_put(&cc->fc);
519		return rc;
520	}
521	file->private_data = &cc->fc;	/* channel owns base reference to cc */
522
523	return 0;
524}
525
526/**
527 * cuse_channel_release - release method for /dev/cuse
528 * @inode: inode for /dev/cuse
529 * @file: file struct being closed
530 *
531 * Disconnect the channel, deregister CUSE device and initiate
532 * destruction by putting the default reference.
533 *
534 * RETURNS:
535 * 0 on success, -errno on failure.
536 */
537static int cuse_channel_release(struct inode *inode, struct file *file)
538{
539	struct cuse_conn *cc = fc_to_cc(file->private_data);
540	int rc;
541
542	/* remove from the conntbl, no more access from this point on */
543	mutex_lock(&cuse_lock);
544	list_del_init(&cc->list);
545	mutex_unlock(&cuse_lock);
546
547	/* remove device */
548	if (cc->dev)
549		device_unregister(cc->dev);
550	if (cc->cdev) {
551		unregister_chrdev_region(cc->cdev->dev, 1);
552		cdev_del(cc->cdev);
553	}
554
555	rc = fuse_dev_release(inode, file);	/* puts the base reference */
556
557	return rc;
558}
559
560static struct file_operations cuse_channel_fops; /* initialized during init */
561
562
563/**************************************************************************
564 * Misc stuff and module initializatiion
565 *
566 * CUSE exports the same set of attributes to sysfs as fusectl.
567 */
568
569static ssize_t cuse_class_waiting_show(struct device *dev,
570				       struct device_attribute *attr, char *buf)
571{
572	struct cuse_conn *cc = dev_get_drvdata(dev);
573
574	return sprintf(buf, "%d\n", atomic_read(&cc->fc.num_waiting));
575}
576static DEVICE_ATTR(waiting, 0400, cuse_class_waiting_show, NULL);
577
578static ssize_t cuse_class_abort_store(struct device *dev,
579				      struct device_attribute *attr,
580				      const char *buf, size_t count)
581{
582	struct cuse_conn *cc = dev_get_drvdata(dev);
583
584	fuse_abort_conn(&cc->fc);
585	return count;
586}
587static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store);
588
589static struct attribute *cuse_class_dev_attrs[] = {
590	&dev_attr_waiting.attr,
591	&dev_attr_abort.attr,
592	NULL,
593};
594ATTRIBUTE_GROUPS(cuse_class_dev);
595
596static struct miscdevice cuse_miscdev = {
597	.minor		= CUSE_MINOR,
598	.name		= "cuse",
599	.fops		= &cuse_channel_fops,
600};
601
602MODULE_ALIAS_MISCDEV(CUSE_MINOR);
603MODULE_ALIAS("devname:cuse");
604
605static int __init cuse_init(void)
606{
607	int i, rc;
608
609	/* init conntbl */
610	for (i = 0; i < CUSE_CONNTBL_LEN; i++)
611		INIT_LIST_HEAD(&cuse_conntbl[i]);
612
613	/* inherit and extend fuse_dev_operations */
614	cuse_channel_fops		= fuse_dev_operations;
615	cuse_channel_fops.owner		= THIS_MODULE;
616	cuse_channel_fops.open		= cuse_channel_open;
617	cuse_channel_fops.release	= cuse_channel_release;
618
619	cuse_class = class_create(THIS_MODULE, "cuse");
620	if (IS_ERR(cuse_class))
621		return PTR_ERR(cuse_class);
622
623	cuse_class->dev_groups = cuse_class_dev_groups;
624
625	rc = misc_register(&cuse_miscdev);
626	if (rc) {
627		class_destroy(cuse_class);
628		return rc;
629	}
630
631	return 0;
632}
633
634static void __exit cuse_exit(void)
635{
636	misc_deregister(&cuse_miscdev);
637	class_destroy(cuse_class);
638}
639
640module_init(cuse_init);
641module_exit(cuse_exit);
642
643MODULE_AUTHOR("Tejun Heo <tj@kernel.org>");
644MODULE_DESCRIPTION("Character device in Userspace");
645MODULE_LICENSE("GPL");
646