xref: /linux/fs/fuse/cuse.c (revision 3a07362fab1653d3aca31a9155c8cc776138fd02)
155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2151060acSTejun Heo /*
3151060acSTejun Heo  * CUSE: Character device in Userspace
4151060acSTejun Heo  *
5151060acSTejun Heo  * Copyright (C) 2008-2009  SUSE Linux Products GmbH
6151060acSTejun Heo  * Copyright (C) 2008-2009  Tejun Heo <tj@kernel.org>
7151060acSTejun Heo  *
8151060acSTejun Heo  * CUSE enables character devices to be implemented from userland much
9151060acSTejun Heo  * like FUSE allows filesystems.  On initialization /dev/cuse is
10151060acSTejun Heo  * created.  By opening the file and replying to the CUSE_INIT request
11151060acSTejun Heo  * userland CUSE server can create a character device.  After that the
12151060acSTejun Heo  * operation is very similar to FUSE.
13151060acSTejun Heo  *
14151060acSTejun Heo  * A CUSE instance involves the following objects.
15151060acSTejun Heo  *
16151060acSTejun Heo  * cuse_conn	: contains fuse_conn and serves as bonding structure
17151060acSTejun Heo  * channel	: file handle connected to the userland CUSE server
18151060acSTejun Heo  * cdev		: the implemented character device
19151060acSTejun Heo  * dev		: generic device for cdev
20151060acSTejun Heo  *
21151060acSTejun Heo  * Note that 'channel' is what 'dev' is in FUSE.  As CUSE deals with
22151060acSTejun Heo  * devices, it's called 'channel' to reduce confusion.
23151060acSTejun Heo  *
24151060acSTejun Heo  * channel determines when the character device dies.  When channel is
25151060acSTejun Heo  * closed, everything begins to destruct.  The cuse_conn is taken off
26151060acSTejun Heo  * the lookup table preventing further access from cdev, cdev and
27151060acSTejun Heo  * generic device are removed and the base reference of cuse_conn is
28151060acSTejun Heo  * put.
29151060acSTejun Heo  *
30151060acSTejun Heo  * On each open, the matching cuse_conn is looked up and if found an
31151060acSTejun Heo  * additional reference is taken which is released when the file is
32151060acSTejun Heo  * closed.
33151060acSTejun Heo  */
34151060acSTejun Heo 
35f2294482SKirill Smelkov #define pr_fmt(fmt) "CUSE: " fmt
36f2294482SKirill Smelkov 
37151060acSTejun Heo #include <linux/fuse.h>
38151060acSTejun Heo #include <linux/cdev.h>
39151060acSTejun Heo #include <linux/device.h>
40151060acSTejun Heo #include <linux/file.h>
41151060acSTejun Heo #include <linux/fs.h>
42151060acSTejun Heo #include <linux/kdev_t.h>
43151060acSTejun Heo #include <linux/kthread.h>
44151060acSTejun Heo #include <linux/list.h>
45151060acSTejun Heo #include <linux/magic.h>
46151060acSTejun Heo #include <linux/miscdevice.h>
47151060acSTejun Heo #include <linux/mutex.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
49151060acSTejun Heo #include <linux/stat.h>
50143cb494SPaul Gortmaker #include <linux/module.h>
51e2e40f2cSChristoph Hellwig #include <linux/uio.h>
528cb08329SEric W. Biederman #include <linux/user_namespace.h>
53151060acSTejun Heo 
54151060acSTejun Heo #include "fuse_i.h"
55151060acSTejun Heo 
56151060acSTejun Heo #define CUSE_CONNTBL_LEN	64
57151060acSTejun Heo 
58151060acSTejun Heo struct cuse_conn {
59151060acSTejun Heo 	struct list_head	list;	/* linked on cuse_conntbl */
60fcee216bSMax Reitz 	struct fuse_mount	fm;	/* Dummy mount referencing fc */
61151060acSTejun Heo 	struct fuse_conn	fc;	/* fuse connection */
62151060acSTejun Heo 	struct cdev		*cdev;	/* associated character device */
63151060acSTejun Heo 	struct device		*dev;	/* device representing @cdev */
64151060acSTejun Heo 
65151060acSTejun Heo 	/* init parameters, set once during initialization */
66151060acSTejun Heo 	bool			unrestricted_ioctl;
67151060acSTejun Heo };
68151060acSTejun Heo 
698ce03fd7SDavid Herrmann static DEFINE_MUTEX(cuse_lock);		/* protects registration */
70151060acSTejun Heo static struct list_head cuse_conntbl[CUSE_CONNTBL_LEN];
71151060acSTejun Heo static struct class *cuse_class;
72151060acSTejun Heo 
fc_to_cc(struct fuse_conn * fc)73151060acSTejun Heo static struct cuse_conn *fc_to_cc(struct fuse_conn *fc)
74151060acSTejun Heo {
75151060acSTejun Heo 	return container_of(fc, struct cuse_conn, fc);
76151060acSTejun Heo }
77151060acSTejun Heo 
cuse_conntbl_head(dev_t devt)78151060acSTejun Heo static struct list_head *cuse_conntbl_head(dev_t devt)
79151060acSTejun Heo {
80151060acSTejun Heo 	return &cuse_conntbl[(MAJOR(devt) + MINOR(devt)) % CUSE_CONNTBL_LEN];
81151060acSTejun Heo }
82151060acSTejun Heo 
83151060acSTejun Heo 
84151060acSTejun Heo /**************************************************************************
85151060acSTejun Heo  * CUSE frontend operations
86151060acSTejun Heo  *
87151060acSTejun Heo  * These are file operations for the character device.
88151060acSTejun Heo  *
89151060acSTejun Heo  * On open, CUSE opens a file from the FUSE mnt and stores it to
90151060acSTejun Heo  * private_data of the open file.  All other ops call FUSE ops on the
91151060acSTejun Heo  * FUSE file.
92151060acSTejun Heo  */
93151060acSTejun Heo 
cuse_read_iter(struct kiocb * kiocb,struct iov_iter * to)94cfa86a74SAl Viro static ssize_t cuse_read_iter(struct kiocb *kiocb, struct iov_iter *to)
95151060acSTejun Heo {
96e1c0eecbSMiklos Szeredi 	struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(kiocb);
97151060acSTejun Heo 	loff_t pos = 0;
98151060acSTejun Heo 
99cfa86a74SAl Viro 	return fuse_direct_io(&io, to, &pos, FUSE_DIO_CUSE);
100151060acSTejun Heo }
101151060acSTejun Heo 
cuse_write_iter(struct kiocb * kiocb,struct iov_iter * from)102cfa86a74SAl Viro static ssize_t cuse_write_iter(struct kiocb *kiocb, struct iov_iter *from)
103151060acSTejun Heo {
104e1c0eecbSMiklos Szeredi 	struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(kiocb);
105151060acSTejun Heo 	loff_t pos = 0;
106151060acSTejun Heo 	/*
107151060acSTejun Heo 	 * No locking or generic_write_checks(), the server is
108151060acSTejun Heo 	 * responsible for locking and sanity checks.
109151060acSTejun Heo 	 */
110cfa86a74SAl Viro 	return fuse_direct_io(&io, from, &pos,
111ea8cd333SPavel Emelyanov 			      FUSE_DIO_WRITE | FUSE_DIO_CUSE);
112151060acSTejun Heo }
113151060acSTejun Heo 
cuse_open(struct inode * inode,struct file * file)114151060acSTejun Heo static int cuse_open(struct inode *inode, struct file *file)
115151060acSTejun Heo {
116151060acSTejun Heo 	dev_t devt = inode->i_cdev->dev;
117151060acSTejun Heo 	struct cuse_conn *cc = NULL, *pos;
118151060acSTejun Heo 	int rc;
119151060acSTejun Heo 
120151060acSTejun Heo 	/* look up and get the connection */
1218ce03fd7SDavid Herrmann 	mutex_lock(&cuse_lock);
122151060acSTejun Heo 	list_for_each_entry(pos, cuse_conntbl_head(devt), list)
123151060acSTejun Heo 		if (pos->dev->devt == devt) {
124151060acSTejun Heo 			fuse_conn_get(&pos->fc);
125151060acSTejun Heo 			cc = pos;
126151060acSTejun Heo 			break;
127151060acSTejun Heo 		}
1288ce03fd7SDavid Herrmann 	mutex_unlock(&cuse_lock);
129151060acSTejun Heo 
130151060acSTejun Heo 	/* dead? */
131151060acSTejun Heo 	if (!cc)
132151060acSTejun Heo 		return -ENODEV;
133151060acSTejun Heo 
134151060acSTejun Heo 	/*
135151060acSTejun Heo 	 * Generic permission check is already done against the chrdev
136151060acSTejun Heo 	 * file, proceed to open.
137151060acSTejun Heo 	 */
138fcee216bSMax Reitz 	rc = fuse_do_open(&cc->fm, 0, file, 0);
139151060acSTejun Heo 	if (rc)
140151060acSTejun Heo 		fuse_conn_put(&cc->fc);
141151060acSTejun Heo 	return rc;
142151060acSTejun Heo }
143151060acSTejun Heo 
cuse_release(struct inode * inode,struct file * file)144151060acSTejun Heo static int cuse_release(struct inode *inode, struct file *file)
145151060acSTejun Heo {
146151060acSTejun Heo 	struct fuse_file *ff = file->private_data;
147fcee216bSMax Reitz 	struct fuse_mount *fm = ff->fm;
148151060acSTejun Heo 
14956d250efSMiklos Szeredi 	fuse_sync_release(NULL, ff, file->f_flags);
150fcee216bSMax Reitz 	fuse_conn_put(fm->fc);
151151060acSTejun Heo 
152151060acSTejun Heo 	return 0;
153151060acSTejun Heo }
154151060acSTejun Heo 
cuse_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)155151060acSTejun Heo static long cuse_file_ioctl(struct file *file, unsigned int cmd,
156151060acSTejun Heo 			    unsigned long arg)
157151060acSTejun Heo {
158151060acSTejun Heo 	struct fuse_file *ff = file->private_data;
159fcee216bSMax Reitz 	struct cuse_conn *cc = fc_to_cc(ff->fm->fc);
160151060acSTejun Heo 	unsigned int flags = 0;
161151060acSTejun Heo 
162151060acSTejun Heo 	if (cc->unrestricted_ioctl)
163151060acSTejun Heo 		flags |= FUSE_IOCTL_UNRESTRICTED;
164151060acSTejun Heo 
165151060acSTejun Heo 	return fuse_do_ioctl(file, cmd, arg, flags);
166151060acSTejun Heo }
167151060acSTejun Heo 
cuse_file_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)168151060acSTejun Heo static long cuse_file_compat_ioctl(struct file *file, unsigned int cmd,
169151060acSTejun Heo 				   unsigned long arg)
170151060acSTejun Heo {
171151060acSTejun Heo 	struct fuse_file *ff = file->private_data;
172fcee216bSMax Reitz 	struct cuse_conn *cc = fc_to_cc(ff->fm->fc);
173151060acSTejun Heo 	unsigned int flags = FUSE_IOCTL_COMPAT;
174151060acSTejun Heo 
175151060acSTejun Heo 	if (cc->unrestricted_ioctl)
176151060acSTejun Heo 		flags |= FUSE_IOCTL_UNRESTRICTED;
177151060acSTejun Heo 
178151060acSTejun Heo 	return fuse_do_ioctl(file, cmd, arg, flags);
179151060acSTejun Heo }
180151060acSTejun Heo 
181151060acSTejun Heo static const struct file_operations cuse_frontend_fops = {
182151060acSTejun Heo 	.owner			= THIS_MODULE,
183cfa86a74SAl Viro 	.read_iter		= cuse_read_iter,
184cfa86a74SAl Viro 	.write_iter		= cuse_write_iter,
185151060acSTejun Heo 	.open			= cuse_open,
186151060acSTejun Heo 	.release		= cuse_release,
187151060acSTejun Heo 	.unlocked_ioctl		= cuse_file_ioctl,
188151060acSTejun Heo 	.compat_ioctl		= cuse_file_compat_ioctl,
189151060acSTejun Heo 	.poll			= fuse_file_poll,
1906038f373SArnd Bergmann 	.llseek		= noop_llseek,
191151060acSTejun Heo };
192151060acSTejun Heo 
193151060acSTejun Heo 
194151060acSTejun Heo /**************************************************************************
195151060acSTejun Heo  * CUSE channel initialization and destruction
196151060acSTejun Heo  */
197151060acSTejun Heo 
198151060acSTejun Heo struct cuse_devinfo {
199151060acSTejun Heo 	const char		*name;
200151060acSTejun Heo };
201151060acSTejun Heo 
202151060acSTejun Heo /**
203151060acSTejun Heo  * cuse_parse_one - parse one key=value pair
204151060acSTejun Heo  * @pp: i/o parameter for the current position
205151060acSTejun Heo  * @end: points to one past the end of the packed string
206151060acSTejun Heo  * @keyp: out parameter for key
207151060acSTejun Heo  * @valp: out parameter for value
208151060acSTejun Heo  *
209151060acSTejun Heo  * *@pp points to packed strings - "key0=val0\0key1=val1\0" which ends
210151060acSTejun Heo  * at @end - 1.  This function parses one pair and set *@keyp to the
211151060acSTejun Heo  * start of the key and *@valp to the start of the value.  Note that
212151060acSTejun Heo  * the original string is modified such that the key string is
213151060acSTejun Heo  * terminated with '\0'.  *@pp is updated to point to the next string.
214151060acSTejun Heo  *
215151060acSTejun Heo  * RETURNS:
216151060acSTejun Heo  * 1 on successful parse, 0 on EOF, -errno on failure.
217151060acSTejun Heo  */
cuse_parse_one(char ** pp,char * end,char ** keyp,char ** valp)218151060acSTejun Heo static int cuse_parse_one(char **pp, char *end, char **keyp, char **valp)
219151060acSTejun Heo {
220151060acSTejun Heo 	char *p = *pp;
221151060acSTejun Heo 	char *key, *val;
222151060acSTejun Heo 
223151060acSTejun Heo 	while (p < end && *p == '\0')
224151060acSTejun Heo 		p++;
225151060acSTejun Heo 	if (p == end)
226151060acSTejun Heo 		return 0;
227151060acSTejun Heo 
228151060acSTejun Heo 	if (end[-1] != '\0') {
229f2294482SKirill Smelkov 		pr_err("info not properly terminated\n");
230151060acSTejun Heo 		return -EINVAL;
231151060acSTejun Heo 	}
232151060acSTejun Heo 
233151060acSTejun Heo 	key = val = p;
234151060acSTejun Heo 	p += strlen(p);
235151060acSTejun Heo 
236151060acSTejun Heo 	if (valp) {
237151060acSTejun Heo 		strsep(&val, "=");
238151060acSTejun Heo 		if (!val)
239151060acSTejun Heo 			val = key + strlen(key);
240151060acSTejun Heo 		key = strstrip(key);
241151060acSTejun Heo 		val = strstrip(val);
242151060acSTejun Heo 	} else
243151060acSTejun Heo 		key = strstrip(key);
244151060acSTejun Heo 
245151060acSTejun Heo 	if (!strlen(key)) {
246f2294482SKirill Smelkov 		pr_err("zero length info key specified\n");
247151060acSTejun Heo 		return -EINVAL;
248151060acSTejun Heo 	}
249151060acSTejun Heo 
250151060acSTejun Heo 	*pp = p;
251151060acSTejun Heo 	*keyp = key;
252151060acSTejun Heo 	if (valp)
253151060acSTejun Heo 		*valp = val;
254151060acSTejun Heo 
255151060acSTejun Heo 	return 1;
256151060acSTejun Heo }
257151060acSTejun Heo 
258151060acSTejun Heo /**
25906bbb761SRandy Dunlap  * cuse_parse_devinfo - parse device info
260151060acSTejun Heo  * @p: device info string
261151060acSTejun Heo  * @len: length of device info string
262151060acSTejun Heo  * @devinfo: out parameter for parsed device info
263151060acSTejun Heo  *
264151060acSTejun Heo  * Parse @p to extract device info and store it into @devinfo.  String
265151060acSTejun Heo  * pointed to by @p is modified by parsing and @devinfo points into
266151060acSTejun Heo  * them, so @p shouldn't be freed while @devinfo is in use.
267151060acSTejun Heo  *
268151060acSTejun Heo  * RETURNS:
269151060acSTejun Heo  * 0 on success, -errno on failure.
270151060acSTejun Heo  */
cuse_parse_devinfo(char * p,size_t len,struct cuse_devinfo * devinfo)271151060acSTejun Heo static int cuse_parse_devinfo(char *p, size_t len, struct cuse_devinfo *devinfo)
272151060acSTejun Heo {
273151060acSTejun Heo 	char *end = p + len;
2743f649ab7SKees Cook 	char *key, *val;
275151060acSTejun Heo 	int rc;
276151060acSTejun Heo 
277151060acSTejun Heo 	while (true) {
278151060acSTejun Heo 		rc = cuse_parse_one(&p, end, &key, &val);
279151060acSTejun Heo 		if (rc < 0)
280151060acSTejun Heo 			return rc;
281151060acSTejun Heo 		if (!rc)
282151060acSTejun Heo 			break;
283151060acSTejun Heo 		if (strcmp(key, "DEVNAME") == 0)
284151060acSTejun Heo 			devinfo->name = val;
285151060acSTejun Heo 		else
286f2294482SKirill Smelkov 			pr_warn("unknown device info \"%s\"\n", key);
287151060acSTejun Heo 	}
288151060acSTejun Heo 
289151060acSTejun Heo 	if (!devinfo->name || !strlen(devinfo->name)) {
290f2294482SKirill Smelkov 		pr_err("DEVNAME unspecified\n");
291151060acSTejun Heo 		return -EINVAL;
292151060acSTejun Heo 	}
293151060acSTejun Heo 
294151060acSTejun Heo 	return 0;
295151060acSTejun Heo }
296151060acSTejun Heo 
cuse_gendev_release(struct device * dev)297151060acSTejun Heo static void cuse_gendev_release(struct device *dev)
298151060acSTejun Heo {
299151060acSTejun Heo 	kfree(dev);
300151060acSTejun Heo }
301151060acSTejun Heo 
302b50ef7c5SMiklos Szeredi struct cuse_init_args {
303b50ef7c5SMiklos Szeredi 	struct fuse_args_pages ap;
304b50ef7c5SMiklos Szeredi 	struct cuse_init_in in;
305b50ef7c5SMiklos Szeredi 	struct cuse_init_out out;
306b50ef7c5SMiklos Szeredi 	struct page *page;
307b50ef7c5SMiklos Szeredi 	struct fuse_page_desc desc;
308b50ef7c5SMiklos Szeredi };
309b50ef7c5SMiklos Szeredi 
310151060acSTejun Heo /**
311151060acSTejun Heo  * cuse_process_init_reply - finish initializing CUSE channel
312151060acSTejun Heo  *
313*09492cb4SYang Li  * @fm: The fuse mount information containing the CUSE connection.
314*09492cb4SYang Li  * @args: The arguments passed to the init reply.
315*09492cb4SYang Li  * @error: The error code signifying if any error occurred during the process.
316*09492cb4SYang Li  *
317151060acSTejun Heo  * This function creates the character device and sets up all the
318151060acSTejun Heo  * required data structures for it.  Please read the comment at the
319151060acSTejun Heo  * top of this file for high level overview.
320151060acSTejun Heo  */
cuse_process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)321fcee216bSMax Reitz static void cuse_process_init_reply(struct fuse_mount *fm,
322b50ef7c5SMiklos Szeredi 				    struct fuse_args *args, int error)
323151060acSTejun Heo {
324fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
325b50ef7c5SMiklos Szeredi 	struct cuse_init_args *ia = container_of(args, typeof(*ia), ap.args);
326b50ef7c5SMiklos Szeredi 	struct fuse_args_pages *ap = &ia->ap;
32730783587SDavid Herrmann 	struct cuse_conn *cc = fc_to_cc(fc), *pos;
328b50ef7c5SMiklos Szeredi 	struct cuse_init_out *arg = &ia->out;
329b50ef7c5SMiklos Szeredi 	struct page *page = ap->pages[0];
330151060acSTejun Heo 	struct cuse_devinfo devinfo = { };
331151060acSTejun Heo 	struct device *dev;
332151060acSTejun Heo 	struct cdev *cdev;
333151060acSTejun Heo 	dev_t devt;
33430783587SDavid Herrmann 	int rc, i;
335151060acSTejun Heo 
336b50ef7c5SMiklos Szeredi 	if (error || arg->major != FUSE_KERNEL_VERSION || arg->minor < 11)
337151060acSTejun Heo 		goto err;
338151060acSTejun Heo 
339151060acSTejun Heo 	fc->minor = arg->minor;
340151060acSTejun Heo 	fc->max_read = max_t(unsigned, arg->max_read, 4096);
341151060acSTejun Heo 	fc->max_write = max_t(unsigned, arg->max_write, 4096);
342151060acSTejun Heo 
343151060acSTejun Heo 	/* parse init reply */
344151060acSTejun Heo 	cc->unrestricted_ioctl = arg->flags & CUSE_UNRESTRICTED_IOCTL;
345151060acSTejun Heo 
346b50ef7c5SMiklos Szeredi 	rc = cuse_parse_devinfo(page_address(page), ap->args.out_args[1].size,
347151060acSTejun Heo 				&devinfo);
348151060acSTejun Heo 	if (rc)
349151060acSTejun Heo 		goto err;
350151060acSTejun Heo 
351151060acSTejun Heo 	/* determine and reserve devt */
352151060acSTejun Heo 	devt = MKDEV(arg->dev_major, arg->dev_minor);
353151060acSTejun Heo 	if (!MAJOR(devt))
354151060acSTejun Heo 		rc = alloc_chrdev_region(&devt, MINOR(devt), 1, devinfo.name);
355151060acSTejun Heo 	else
356151060acSTejun Heo 		rc = register_chrdev_region(devt, 1, devinfo.name);
357151060acSTejun Heo 	if (rc) {
358f2294482SKirill Smelkov 		pr_err("failed to register chrdev region\n");
359151060acSTejun Heo 		goto err;
360151060acSTejun Heo 	}
361151060acSTejun Heo 
362151060acSTejun Heo 	/* devt determined, create device */
363151060acSTejun Heo 	rc = -ENOMEM;
364151060acSTejun Heo 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
365151060acSTejun Heo 	if (!dev)
366151060acSTejun Heo 		goto err_region;
367151060acSTejun Heo 
368151060acSTejun Heo 	device_initialize(dev);
369151060acSTejun Heo 	dev_set_uevent_suppress(dev, 1);
370151060acSTejun Heo 	dev->class = cuse_class;
371151060acSTejun Heo 	dev->devt = devt;
372151060acSTejun Heo 	dev->release = cuse_gendev_release;
373151060acSTejun Heo 	dev_set_drvdata(dev, cc);
374151060acSTejun Heo 	dev_set_name(dev, "%s", devinfo.name);
375151060acSTejun Heo 
37630783587SDavid Herrmann 	mutex_lock(&cuse_lock);
37730783587SDavid Herrmann 
37830783587SDavid Herrmann 	/* make sure the device-name is unique */
37930783587SDavid Herrmann 	for (i = 0; i < CUSE_CONNTBL_LEN; ++i) {
38030783587SDavid Herrmann 		list_for_each_entry(pos, &cuse_conntbl[i], list)
38130783587SDavid Herrmann 			if (!strcmp(dev_name(pos->dev), dev_name(dev)))
38230783587SDavid Herrmann 				goto err_unlock;
38330783587SDavid Herrmann 	}
38430783587SDavid Herrmann 
385151060acSTejun Heo 	rc = device_add(dev);
386151060acSTejun Heo 	if (rc)
38730783587SDavid Herrmann 		goto err_unlock;
388151060acSTejun Heo 
389151060acSTejun Heo 	/* register cdev */
390151060acSTejun Heo 	rc = -ENOMEM;
391151060acSTejun Heo 	cdev = cdev_alloc();
392151060acSTejun Heo 	if (!cdev)
39330783587SDavid Herrmann 		goto err_unlock;
394151060acSTejun Heo 
395151060acSTejun Heo 	cdev->owner = THIS_MODULE;
396151060acSTejun Heo 	cdev->ops = &cuse_frontend_fops;
397151060acSTejun Heo 
398151060acSTejun Heo 	rc = cdev_add(cdev, devt, 1);
399151060acSTejun Heo 	if (rc)
400151060acSTejun Heo 		goto err_cdev;
401151060acSTejun Heo 
402151060acSTejun Heo 	cc->dev = dev;
403151060acSTejun Heo 	cc->cdev = cdev;
404151060acSTejun Heo 
405151060acSTejun Heo 	/* make the device available */
406151060acSTejun Heo 	list_add(&cc->list, cuse_conntbl_head(devt));
4078ce03fd7SDavid Herrmann 	mutex_unlock(&cuse_lock);
408151060acSTejun Heo 
409151060acSTejun Heo 	/* announce device availability */
410151060acSTejun Heo 	dev_set_uevent_suppress(dev, 0);
411151060acSTejun Heo 	kobject_uevent(&dev->kobj, KOBJ_ADD);
412151060acSTejun Heo out:
413b50ef7c5SMiklos Szeredi 	kfree(ia);
414151060acSTejun Heo 	__free_page(page);
415151060acSTejun Heo 	return;
416151060acSTejun Heo 
417151060acSTejun Heo err_cdev:
418151060acSTejun Heo 	cdev_del(cdev);
41930783587SDavid Herrmann err_unlock:
42030783587SDavid Herrmann 	mutex_unlock(&cuse_lock);
421151060acSTejun Heo 	put_device(dev);
422151060acSTejun Heo err_region:
423151060acSTejun Heo 	unregister_chrdev_region(devt, 1);
424151060acSTejun Heo err:
425eb98e3bdSMiklos Szeredi 	fuse_abort_conn(fc);
426151060acSTejun Heo 	goto out;
427151060acSTejun Heo }
428151060acSTejun Heo 
cuse_send_init(struct cuse_conn * cc)429151060acSTejun Heo static int cuse_send_init(struct cuse_conn *cc)
430151060acSTejun Heo {
431151060acSTejun Heo 	int rc;
432151060acSTejun Heo 	struct page *page;
433fcee216bSMax Reitz 	struct fuse_mount *fm = &cc->fm;
434b50ef7c5SMiklos Szeredi 	struct cuse_init_args *ia;
435b50ef7c5SMiklos Szeredi 	struct fuse_args_pages *ap;
436151060acSTejun Heo 
437151060acSTejun Heo 	BUILD_BUG_ON(CUSE_INIT_INFO_MAX > PAGE_SIZE);
438151060acSTejun Heo 
439151060acSTejun Heo 	rc = -ENOMEM;
440151060acSTejun Heo 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
441151060acSTejun Heo 	if (!page)
442b50ef7c5SMiklos Szeredi 		goto err;
443151060acSTejun Heo 
444b50ef7c5SMiklos Szeredi 	ia = kzalloc(sizeof(*ia), GFP_KERNEL);
445b50ef7c5SMiklos Szeredi 	if (!ia)
44607d5f69bSMiklos Szeredi 		goto err_free_page;
44707d5f69bSMiklos Szeredi 
448b50ef7c5SMiklos Szeredi 	ap = &ia->ap;
449b50ef7c5SMiklos Szeredi 	ia->in.major = FUSE_KERNEL_VERSION;
450b50ef7c5SMiklos Szeredi 	ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
451b50ef7c5SMiklos Szeredi 	ia->in.flags |= CUSE_UNRESTRICTED_IOCTL;
452b50ef7c5SMiklos Szeredi 	ap->args.opcode = CUSE_INIT;
453b50ef7c5SMiklos Szeredi 	ap->args.in_numargs = 1;
454b50ef7c5SMiklos Szeredi 	ap->args.in_args[0].size = sizeof(ia->in);
455b50ef7c5SMiklos Szeredi 	ap->args.in_args[0].value = &ia->in;
456b50ef7c5SMiklos Szeredi 	ap->args.out_numargs = 2;
457b50ef7c5SMiklos Szeredi 	ap->args.out_args[0].size = sizeof(ia->out);
458b50ef7c5SMiklos Szeredi 	ap->args.out_args[0].value = &ia->out;
459b50ef7c5SMiklos Szeredi 	ap->args.out_args[1].size = CUSE_INIT_INFO_MAX;
460cabdb4faSzhengbin 	ap->args.out_argvar = true;
461cabdb4faSzhengbin 	ap->args.out_pages = true;
462b50ef7c5SMiklos Szeredi 	ap->num_pages = 1;
463b50ef7c5SMiklos Szeredi 	ap->pages = &ia->page;
464b50ef7c5SMiklos Szeredi 	ap->descs = &ia->desc;
465b50ef7c5SMiklos Szeredi 	ia->page = page;
466b50ef7c5SMiklos Szeredi 	ia->desc.length = ap->args.out_args[1].size;
467b50ef7c5SMiklos Szeredi 	ap->args.end = cuse_process_init_reply;
468151060acSTejun Heo 
469fcee216bSMax Reitz 	rc = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
470b50ef7c5SMiklos Szeredi 	if (rc) {
471b50ef7c5SMiklos Szeredi 		kfree(ia);
47207d5f69bSMiklos Szeredi err_free_page:
47307d5f69bSMiklos Szeredi 		__free_page(page);
474b50ef7c5SMiklos Szeredi 	}
475151060acSTejun Heo err:
476151060acSTejun Heo 	return rc;
477151060acSTejun Heo }
478151060acSTejun Heo 
cuse_fc_release(struct fuse_conn * fc)479151060acSTejun Heo static void cuse_fc_release(struct fuse_conn *fc)
480151060acSTejun Heo {
481053fc4f7SAl Viro 	kfree(fc_to_cc(fc));
482151060acSTejun Heo }
483151060acSTejun Heo 
484151060acSTejun Heo /**
485151060acSTejun Heo  * cuse_channel_open - open method for /dev/cuse
486151060acSTejun Heo  * @inode: inode for /dev/cuse
487151060acSTejun Heo  * @file: file struct being opened
488151060acSTejun Heo  *
489151060acSTejun Heo  * Userland CUSE server can create a CUSE device by opening /dev/cuse
4908272f4c9SPaul Bolle  * and replying to the initialization request kernel sends.  This
491151060acSTejun Heo  * function is responsible for handling CUSE device initialization.
492151060acSTejun Heo  * Because the fd opened by this function is used during
493151060acSTejun Heo  * initialization, this function only creates cuse_conn and sends
494151060acSTejun Heo  * init.  The rest is delegated to a kthread.
495151060acSTejun Heo  *
496151060acSTejun Heo  * RETURNS:
497151060acSTejun Heo  * 0 on success, -errno on failure.
498151060acSTejun Heo  */
cuse_channel_open(struct inode * inode,struct file * file)499151060acSTejun Heo static int cuse_channel_open(struct inode *inode, struct file *file)
500151060acSTejun Heo {
501cc080e9eSMiklos Szeredi 	struct fuse_dev *fud;
502151060acSTejun Heo 	struct cuse_conn *cc;
503151060acSTejun Heo 	int rc;
504151060acSTejun Heo 
505151060acSTejun Heo 	/* set up cuse_conn */
506151060acSTejun Heo 	cc = kzalloc(sizeof(*cc), GFP_KERNEL);
507151060acSTejun Heo 	if (!cc)
508151060acSTejun Heo 		return -ENOMEM;
509151060acSTejun Heo 
5108cb08329SEric W. Biederman 	/*
5118cb08329SEric W. Biederman 	 * Limit the cuse channel to requests that can
5128cb08329SEric W. Biederman 	 * be represented in file->f_cred->user_ns.
5138cb08329SEric W. Biederman 	 */
514fcee216bSMax Reitz 	fuse_conn_init(&cc->fc, &cc->fm, file->f_cred->user_ns,
515fcee216bSMax Reitz 		       &fuse_dev_fiq_ops, NULL);
516151060acSTejun Heo 
5173c9c1433SMiklos Szeredi 	cc->fc.release = cuse_fc_release;
5180cd1eb9aSVivek Goyal 	fud = fuse_dev_alloc_install(&cc->fc);
5193c9c1433SMiklos Szeredi 	fuse_conn_put(&cc->fc);
5203c9c1433SMiklos Szeredi 	if (!fud)
521cc080e9eSMiklos Szeredi 		return -ENOMEM;
522cc080e9eSMiklos Szeredi 
523151060acSTejun Heo 	INIT_LIST_HEAD(&cc->list);
524151060acSTejun Heo 
525796523fbSMaxim Patlasov 	cc->fc.initialized = 1;
526151060acSTejun Heo 	rc = cuse_send_init(cc);
527151060acSTejun Heo 	if (rc) {
528cc080e9eSMiklos Szeredi 		fuse_dev_free(fud);
529151060acSTejun Heo 		return rc;
530151060acSTejun Heo 	}
531cc080e9eSMiklos Szeredi 	file->private_data = fud;
532151060acSTejun Heo 
533151060acSTejun Heo 	return 0;
534151060acSTejun Heo }
535151060acSTejun Heo 
536151060acSTejun Heo /**
537151060acSTejun Heo  * cuse_channel_release - release method for /dev/cuse
538151060acSTejun Heo  * @inode: inode for /dev/cuse
539151060acSTejun Heo  * @file: file struct being closed
540151060acSTejun Heo  *
541151060acSTejun Heo  * Disconnect the channel, deregister CUSE device and initiate
542151060acSTejun Heo  * destruction by putting the default reference.
543151060acSTejun Heo  *
544151060acSTejun Heo  * RETURNS:
545151060acSTejun Heo  * 0 on success, -errno on failure.
546151060acSTejun Heo  */
cuse_channel_release(struct inode * inode,struct file * file)547151060acSTejun Heo static int cuse_channel_release(struct inode *inode, struct file *file)
548151060acSTejun Heo {
549cc080e9eSMiklos Szeredi 	struct fuse_dev *fud = file->private_data;
550cc080e9eSMiklos Szeredi 	struct cuse_conn *cc = fc_to_cc(fud->fc);
551151060acSTejun Heo 
552151060acSTejun Heo 	/* remove from the conntbl, no more access from this point on */
5538ce03fd7SDavid Herrmann 	mutex_lock(&cuse_lock);
554151060acSTejun Heo 	list_del_init(&cc->list);
5558ce03fd7SDavid Herrmann 	mutex_unlock(&cuse_lock);
556151060acSTejun Heo 
557151060acSTejun Heo 	/* remove device */
558151060acSTejun Heo 	if (cc->dev)
559151060acSTejun Heo 		device_unregister(cc->dev);
560151060acSTejun Heo 	if (cc->cdev) {
561151060acSTejun Heo 		unregister_chrdev_region(cc->cdev->dev, 1);
562151060acSTejun Heo 		cdev_del(cc->cdev);
563151060acSTejun Heo 	}
564151060acSTejun Heo 
565e2283a73Sye xingchen 	return fuse_dev_release(inode, file);
566151060acSTejun Heo }
567151060acSTejun Heo 
568151060acSTejun Heo static struct file_operations cuse_channel_fops; /* initialized during init */
569151060acSTejun Heo 
570151060acSTejun Heo 
571151060acSTejun Heo /**************************************************************************
572151060acSTejun Heo  * Misc stuff and module initializatiion
573151060acSTejun Heo  *
574151060acSTejun Heo  * CUSE exports the same set of attributes to sysfs as fusectl.
575151060acSTejun Heo  */
576151060acSTejun Heo 
cuse_class_waiting_show(struct device * dev,struct device_attribute * attr,char * buf)577151060acSTejun Heo static ssize_t cuse_class_waiting_show(struct device *dev,
578151060acSTejun Heo 				       struct device_attribute *attr, char *buf)
579151060acSTejun Heo {
580151060acSTejun Heo 	struct cuse_conn *cc = dev_get_drvdata(dev);
581151060acSTejun Heo 
582151060acSTejun Heo 	return sprintf(buf, "%d\n", atomic_read(&cc->fc.num_waiting));
583151060acSTejun Heo }
58458f86cc8SRusty Russell static DEVICE_ATTR(waiting, 0400, cuse_class_waiting_show, NULL);
585151060acSTejun Heo 
cuse_class_abort_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)586151060acSTejun Heo static ssize_t cuse_class_abort_store(struct device *dev,
587151060acSTejun Heo 				      struct device_attribute *attr,
588151060acSTejun Heo 				      const char *buf, size_t count)
589151060acSTejun Heo {
590151060acSTejun Heo 	struct cuse_conn *cc = dev_get_drvdata(dev);
591151060acSTejun Heo 
592eb98e3bdSMiklos Szeredi 	fuse_abort_conn(&cc->fc);
593151060acSTejun Heo 	return count;
594151060acSTejun Heo }
59558f86cc8SRusty Russell static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store);
596151060acSTejun Heo 
5974183fb95SGreg Kroah-Hartman static struct attribute *cuse_class_dev_attrs[] = {
5984183fb95SGreg Kroah-Hartman 	&dev_attr_waiting.attr,
5994183fb95SGreg Kroah-Hartman 	&dev_attr_abort.attr,
6004183fb95SGreg Kroah-Hartman 	NULL,
601151060acSTejun Heo };
6024183fb95SGreg Kroah-Hartman ATTRIBUTE_GROUPS(cuse_class_dev);
603151060acSTejun Heo 
604151060acSTejun Heo static struct miscdevice cuse_miscdev = {
605cb2ffb26STom Gundersen 	.minor		= CUSE_MINOR,
606151060acSTejun Heo 	.name		= "cuse",
607151060acSTejun Heo 	.fops		= &cuse_channel_fops,
608151060acSTejun Heo };
609151060acSTejun Heo 
610cb2ffb26STom Gundersen MODULE_ALIAS_MISCDEV(CUSE_MINOR);
611cb2ffb26STom Gundersen MODULE_ALIAS("devname:cuse");
612cb2ffb26STom Gundersen 
cuse_init(void)613151060acSTejun Heo static int __init cuse_init(void)
614151060acSTejun Heo {
615151060acSTejun Heo 	int i, rc;
616151060acSTejun Heo 
617151060acSTejun Heo 	/* init conntbl */
618151060acSTejun Heo 	for (i = 0; i < CUSE_CONNTBL_LEN; i++)
619151060acSTejun Heo 		INIT_LIST_HEAD(&cuse_conntbl[i]);
620151060acSTejun Heo 
621151060acSTejun Heo 	/* inherit and extend fuse_dev_operations */
622151060acSTejun Heo 	cuse_channel_fops		= fuse_dev_operations;
623151060acSTejun Heo 	cuse_channel_fops.owner		= THIS_MODULE;
624151060acSTejun Heo 	cuse_channel_fops.open		= cuse_channel_open;
625151060acSTejun Heo 	cuse_channel_fops.release	= cuse_channel_release;
6268217673dSMiklos Szeredi 	/* CUSE is not prepared for FUSE_DEV_IOC_CLONE */
6278217673dSMiklos Szeredi 	cuse_channel_fops.unlocked_ioctl	= NULL;
628151060acSTejun Heo 
6291aaba11dSGreg Kroah-Hartman 	cuse_class = class_create("cuse");
630151060acSTejun Heo 	if (IS_ERR(cuse_class))
631151060acSTejun Heo 		return PTR_ERR(cuse_class);
632151060acSTejun Heo 
6334183fb95SGreg Kroah-Hartman 	cuse_class->dev_groups = cuse_class_dev_groups;
634151060acSTejun Heo 
635151060acSTejun Heo 	rc = misc_register(&cuse_miscdev);
636151060acSTejun Heo 	if (rc) {
637151060acSTejun Heo 		class_destroy(cuse_class);
638151060acSTejun Heo 		return rc;
639151060acSTejun Heo 	}
640151060acSTejun Heo 
641151060acSTejun Heo 	return 0;
642151060acSTejun Heo }
643151060acSTejun Heo 
cuse_exit(void)644151060acSTejun Heo static void __exit cuse_exit(void)
645151060acSTejun Heo {
646151060acSTejun Heo 	misc_deregister(&cuse_miscdev);
647151060acSTejun Heo 	class_destroy(cuse_class);
648151060acSTejun Heo }
649151060acSTejun Heo 
650151060acSTejun Heo module_init(cuse_init);
651151060acSTejun Heo module_exit(cuse_exit);
652151060acSTejun Heo 
653151060acSTejun Heo MODULE_AUTHOR("Tejun Heo <tj@kernel.org>");
654151060acSTejun Heo MODULE_DESCRIPTION("Character device in Userspace");
655151060acSTejun Heo MODULE_LICENSE("GPL");
656