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 *
313151060acSTejun Heo * This function creates the character device and sets up all the
314151060acSTejun Heo * required data structures for it. Please read the comment at the
315151060acSTejun Heo * top of this file for high level overview.
316151060acSTejun Heo */
cuse_process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)317fcee216bSMax Reitz static void cuse_process_init_reply(struct fuse_mount *fm,
318b50ef7c5SMiklos Szeredi struct fuse_args *args, int error)
319151060acSTejun Heo {
320fcee216bSMax Reitz struct fuse_conn *fc = fm->fc;
321b50ef7c5SMiklos Szeredi struct cuse_init_args *ia = container_of(args, typeof(*ia), ap.args);
322b50ef7c5SMiklos Szeredi struct fuse_args_pages *ap = &ia->ap;
32330783587SDavid Herrmann struct cuse_conn *cc = fc_to_cc(fc), *pos;
324b50ef7c5SMiklos Szeredi struct cuse_init_out *arg = &ia->out;
325b50ef7c5SMiklos Szeredi struct page *page = ap->pages[0];
326151060acSTejun Heo struct cuse_devinfo devinfo = { };
327151060acSTejun Heo struct device *dev;
328151060acSTejun Heo struct cdev *cdev;
329151060acSTejun Heo dev_t devt;
33030783587SDavid Herrmann int rc, i;
331151060acSTejun Heo
332b50ef7c5SMiklos Szeredi if (error || arg->major != FUSE_KERNEL_VERSION || arg->minor < 11)
333151060acSTejun Heo goto err;
334151060acSTejun Heo
335151060acSTejun Heo fc->minor = arg->minor;
336151060acSTejun Heo fc->max_read = max_t(unsigned, arg->max_read, 4096);
337151060acSTejun Heo fc->max_write = max_t(unsigned, arg->max_write, 4096);
338151060acSTejun Heo
339151060acSTejun Heo /* parse init reply */
340151060acSTejun Heo cc->unrestricted_ioctl = arg->flags & CUSE_UNRESTRICTED_IOCTL;
341151060acSTejun Heo
342b50ef7c5SMiklos Szeredi rc = cuse_parse_devinfo(page_address(page), ap->args.out_args[1].size,
343151060acSTejun Heo &devinfo);
344151060acSTejun Heo if (rc)
345151060acSTejun Heo goto err;
346151060acSTejun Heo
347151060acSTejun Heo /* determine and reserve devt */
348151060acSTejun Heo devt = MKDEV(arg->dev_major, arg->dev_minor);
349151060acSTejun Heo if (!MAJOR(devt))
350151060acSTejun Heo rc = alloc_chrdev_region(&devt, MINOR(devt), 1, devinfo.name);
351151060acSTejun Heo else
352151060acSTejun Heo rc = register_chrdev_region(devt, 1, devinfo.name);
353151060acSTejun Heo if (rc) {
354f2294482SKirill Smelkov pr_err("failed to register chrdev region\n");
355151060acSTejun Heo goto err;
356151060acSTejun Heo }
357151060acSTejun Heo
358151060acSTejun Heo /* devt determined, create device */
359151060acSTejun Heo rc = -ENOMEM;
360151060acSTejun Heo dev = kzalloc(sizeof(*dev), GFP_KERNEL);
361151060acSTejun Heo if (!dev)
362151060acSTejun Heo goto err_region;
363151060acSTejun Heo
364151060acSTejun Heo device_initialize(dev);
365151060acSTejun Heo dev_set_uevent_suppress(dev, 1);
366151060acSTejun Heo dev->class = cuse_class;
367151060acSTejun Heo dev->devt = devt;
368151060acSTejun Heo dev->release = cuse_gendev_release;
369151060acSTejun Heo dev_set_drvdata(dev, cc);
370151060acSTejun Heo dev_set_name(dev, "%s", devinfo.name);
371151060acSTejun Heo
37230783587SDavid Herrmann mutex_lock(&cuse_lock);
37330783587SDavid Herrmann
37430783587SDavid Herrmann /* make sure the device-name is unique */
37530783587SDavid Herrmann for (i = 0; i < CUSE_CONNTBL_LEN; ++i) {
37630783587SDavid Herrmann list_for_each_entry(pos, &cuse_conntbl[i], list)
37730783587SDavid Herrmann if (!strcmp(dev_name(pos->dev), dev_name(dev)))
37830783587SDavid Herrmann goto err_unlock;
37930783587SDavid Herrmann }
38030783587SDavid Herrmann
381151060acSTejun Heo rc = device_add(dev);
382151060acSTejun Heo if (rc)
38330783587SDavid Herrmann goto err_unlock;
384151060acSTejun Heo
385151060acSTejun Heo /* register cdev */
386151060acSTejun Heo rc = -ENOMEM;
387151060acSTejun Heo cdev = cdev_alloc();
388151060acSTejun Heo if (!cdev)
38930783587SDavid Herrmann goto err_unlock;
390151060acSTejun Heo
391151060acSTejun Heo cdev->owner = THIS_MODULE;
392151060acSTejun Heo cdev->ops = &cuse_frontend_fops;
393151060acSTejun Heo
394151060acSTejun Heo rc = cdev_add(cdev, devt, 1);
395151060acSTejun Heo if (rc)
396151060acSTejun Heo goto err_cdev;
397151060acSTejun Heo
398151060acSTejun Heo cc->dev = dev;
399151060acSTejun Heo cc->cdev = cdev;
400151060acSTejun Heo
401151060acSTejun Heo /* make the device available */
402151060acSTejun Heo list_add(&cc->list, cuse_conntbl_head(devt));
4038ce03fd7SDavid Herrmann mutex_unlock(&cuse_lock);
404151060acSTejun Heo
405151060acSTejun Heo /* announce device availability */
406151060acSTejun Heo dev_set_uevent_suppress(dev, 0);
407151060acSTejun Heo kobject_uevent(&dev->kobj, KOBJ_ADD);
408151060acSTejun Heo out:
409b50ef7c5SMiklos Szeredi kfree(ia);
410151060acSTejun Heo __free_page(page);
411151060acSTejun Heo return;
412151060acSTejun Heo
413151060acSTejun Heo err_cdev:
414151060acSTejun Heo cdev_del(cdev);
41530783587SDavid Herrmann err_unlock:
41630783587SDavid Herrmann mutex_unlock(&cuse_lock);
417151060acSTejun Heo put_device(dev);
418151060acSTejun Heo err_region:
419151060acSTejun Heo unregister_chrdev_region(devt, 1);
420151060acSTejun Heo err:
421eb98e3bdSMiklos Szeredi fuse_abort_conn(fc);
422151060acSTejun Heo goto out;
423151060acSTejun Heo }
424151060acSTejun Heo
cuse_send_init(struct cuse_conn * cc)425151060acSTejun Heo static int cuse_send_init(struct cuse_conn *cc)
426151060acSTejun Heo {
427151060acSTejun Heo int rc;
428151060acSTejun Heo struct page *page;
429fcee216bSMax Reitz struct fuse_mount *fm = &cc->fm;
430b50ef7c5SMiklos Szeredi struct cuse_init_args *ia;
431b50ef7c5SMiklos Szeredi struct fuse_args_pages *ap;
432151060acSTejun Heo
433151060acSTejun Heo BUILD_BUG_ON(CUSE_INIT_INFO_MAX > PAGE_SIZE);
434151060acSTejun Heo
435151060acSTejun Heo rc = -ENOMEM;
436151060acSTejun Heo page = alloc_page(GFP_KERNEL | __GFP_ZERO);
437151060acSTejun Heo if (!page)
438b50ef7c5SMiklos Szeredi goto err;
439151060acSTejun Heo
440b50ef7c5SMiklos Szeredi ia = kzalloc(sizeof(*ia), GFP_KERNEL);
441b50ef7c5SMiklos Szeredi if (!ia)
44207d5f69bSMiklos Szeredi goto err_free_page;
44307d5f69bSMiklos Szeredi
444b50ef7c5SMiklos Szeredi ap = &ia->ap;
445b50ef7c5SMiklos Szeredi ia->in.major = FUSE_KERNEL_VERSION;
446b50ef7c5SMiklos Szeredi ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
447b50ef7c5SMiklos Szeredi ia->in.flags |= CUSE_UNRESTRICTED_IOCTL;
448b50ef7c5SMiklos Szeredi ap->args.opcode = CUSE_INIT;
449b50ef7c5SMiklos Szeredi ap->args.in_numargs = 1;
450b50ef7c5SMiklos Szeredi ap->args.in_args[0].size = sizeof(ia->in);
451b50ef7c5SMiklos Szeredi ap->args.in_args[0].value = &ia->in;
452b50ef7c5SMiklos Szeredi ap->args.out_numargs = 2;
453b50ef7c5SMiklos Szeredi ap->args.out_args[0].size = sizeof(ia->out);
454b50ef7c5SMiklos Szeredi ap->args.out_args[0].value = &ia->out;
455b50ef7c5SMiklos Szeredi ap->args.out_args[1].size = CUSE_INIT_INFO_MAX;
456cabdb4faSzhengbin ap->args.out_argvar = true;
457cabdb4faSzhengbin ap->args.out_pages = true;
458b50ef7c5SMiklos Szeredi ap->num_pages = 1;
459b50ef7c5SMiklos Szeredi ap->pages = &ia->page;
460b50ef7c5SMiklos Szeredi ap->descs = &ia->desc;
461b50ef7c5SMiklos Szeredi ia->page = page;
462b50ef7c5SMiklos Szeredi ia->desc.length = ap->args.out_args[1].size;
463b50ef7c5SMiklos Szeredi ap->args.end = cuse_process_init_reply;
464151060acSTejun Heo
465fcee216bSMax Reitz rc = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
466b50ef7c5SMiklos Szeredi if (rc) {
467b50ef7c5SMiklos Szeredi kfree(ia);
46807d5f69bSMiklos Szeredi err_free_page:
46907d5f69bSMiklos Szeredi __free_page(page);
470b50ef7c5SMiklos Szeredi }
471151060acSTejun Heo err:
472151060acSTejun Heo return rc;
473151060acSTejun Heo }
474151060acSTejun Heo
cuse_fc_release(struct fuse_conn * fc)475151060acSTejun Heo static void cuse_fc_release(struct fuse_conn *fc)
476151060acSTejun Heo {
477*535e9bd0SAl Viro kfree(fc_to_cc(fc));
478151060acSTejun Heo }
479151060acSTejun Heo
480151060acSTejun Heo /**
481151060acSTejun Heo * cuse_channel_open - open method for /dev/cuse
482151060acSTejun Heo * @inode: inode for /dev/cuse
483151060acSTejun Heo * @file: file struct being opened
484151060acSTejun Heo *
485151060acSTejun Heo * Userland CUSE server can create a CUSE device by opening /dev/cuse
4868272f4c9SPaul Bolle * and replying to the initialization request kernel sends. This
487151060acSTejun Heo * function is responsible for handling CUSE device initialization.
488151060acSTejun Heo * Because the fd opened by this function is used during
489151060acSTejun Heo * initialization, this function only creates cuse_conn and sends
490151060acSTejun Heo * init. The rest is delegated to a kthread.
491151060acSTejun Heo *
492151060acSTejun Heo * RETURNS:
493151060acSTejun Heo * 0 on success, -errno on failure.
494151060acSTejun Heo */
cuse_channel_open(struct inode * inode,struct file * file)495151060acSTejun Heo static int cuse_channel_open(struct inode *inode, struct file *file)
496151060acSTejun Heo {
497cc080e9eSMiklos Szeredi struct fuse_dev *fud;
498151060acSTejun Heo struct cuse_conn *cc;
499151060acSTejun Heo int rc;
500151060acSTejun Heo
501151060acSTejun Heo /* set up cuse_conn */
502151060acSTejun Heo cc = kzalloc(sizeof(*cc), GFP_KERNEL);
503151060acSTejun Heo if (!cc)
504151060acSTejun Heo return -ENOMEM;
505151060acSTejun Heo
5068cb08329SEric W. Biederman /*
5078cb08329SEric W. Biederman * Limit the cuse channel to requests that can
5088cb08329SEric W. Biederman * be represented in file->f_cred->user_ns.
5098cb08329SEric W. Biederman */
510fcee216bSMax Reitz fuse_conn_init(&cc->fc, &cc->fm, file->f_cred->user_ns,
511fcee216bSMax Reitz &fuse_dev_fiq_ops, NULL);
512151060acSTejun Heo
5133c9c1433SMiklos Szeredi cc->fc.release = cuse_fc_release;
5140cd1eb9aSVivek Goyal fud = fuse_dev_alloc_install(&cc->fc);
5153c9c1433SMiklos Szeredi fuse_conn_put(&cc->fc);
5163c9c1433SMiklos Szeredi if (!fud)
517cc080e9eSMiklos Szeredi return -ENOMEM;
518cc080e9eSMiklos Szeredi
519151060acSTejun Heo INIT_LIST_HEAD(&cc->list);
520151060acSTejun Heo
521796523fbSMaxim Patlasov cc->fc.initialized = 1;
522151060acSTejun Heo rc = cuse_send_init(cc);
523151060acSTejun Heo if (rc) {
524cc080e9eSMiklos Szeredi fuse_dev_free(fud);
525151060acSTejun Heo return rc;
526151060acSTejun Heo }
527cc080e9eSMiklos Szeredi file->private_data = fud;
528151060acSTejun Heo
529151060acSTejun Heo return 0;
530151060acSTejun Heo }
531151060acSTejun Heo
532151060acSTejun Heo /**
533151060acSTejun Heo * cuse_channel_release - release method for /dev/cuse
534151060acSTejun Heo * @inode: inode for /dev/cuse
535151060acSTejun Heo * @file: file struct being closed
536151060acSTejun Heo *
537151060acSTejun Heo * Disconnect the channel, deregister CUSE device and initiate
538151060acSTejun Heo * destruction by putting the default reference.
539151060acSTejun Heo *
540151060acSTejun Heo * RETURNS:
541151060acSTejun Heo * 0 on success, -errno on failure.
542151060acSTejun Heo */
cuse_channel_release(struct inode * inode,struct file * file)543151060acSTejun Heo static int cuse_channel_release(struct inode *inode, struct file *file)
544151060acSTejun Heo {
545cc080e9eSMiklos Szeredi struct fuse_dev *fud = file->private_data;
546cc080e9eSMiklos Szeredi struct cuse_conn *cc = fc_to_cc(fud->fc);
547151060acSTejun Heo
548151060acSTejun Heo /* remove from the conntbl, no more access from this point on */
5498ce03fd7SDavid Herrmann mutex_lock(&cuse_lock);
550151060acSTejun Heo list_del_init(&cc->list);
5518ce03fd7SDavid Herrmann mutex_unlock(&cuse_lock);
552151060acSTejun Heo
553151060acSTejun Heo /* remove device */
554151060acSTejun Heo if (cc->dev)
555151060acSTejun Heo device_unregister(cc->dev);
556151060acSTejun Heo if (cc->cdev) {
557151060acSTejun Heo unregister_chrdev_region(cc->cdev->dev, 1);
558151060acSTejun Heo cdev_del(cc->cdev);
559151060acSTejun Heo }
560151060acSTejun Heo
561e2283a73Sye xingchen return fuse_dev_release(inode, file);
562151060acSTejun Heo }
563151060acSTejun Heo
564151060acSTejun Heo static struct file_operations cuse_channel_fops; /* initialized during init */
565151060acSTejun Heo
566151060acSTejun Heo
567151060acSTejun Heo /**************************************************************************
568151060acSTejun Heo * Misc stuff and module initializatiion
569151060acSTejun Heo *
570151060acSTejun Heo * CUSE exports the same set of attributes to sysfs as fusectl.
571151060acSTejun Heo */
572151060acSTejun Heo
cuse_class_waiting_show(struct device * dev,struct device_attribute * attr,char * buf)573151060acSTejun Heo static ssize_t cuse_class_waiting_show(struct device *dev,
574151060acSTejun Heo struct device_attribute *attr, char *buf)
575151060acSTejun Heo {
576151060acSTejun Heo struct cuse_conn *cc = dev_get_drvdata(dev);
577151060acSTejun Heo
578151060acSTejun Heo return sprintf(buf, "%d\n", atomic_read(&cc->fc.num_waiting));
579151060acSTejun Heo }
58058f86cc8SRusty Russell static DEVICE_ATTR(waiting, 0400, cuse_class_waiting_show, NULL);
581151060acSTejun Heo
cuse_class_abort_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)582151060acSTejun Heo static ssize_t cuse_class_abort_store(struct device *dev,
583151060acSTejun Heo struct device_attribute *attr,
584151060acSTejun Heo const char *buf, size_t count)
585151060acSTejun Heo {
586151060acSTejun Heo struct cuse_conn *cc = dev_get_drvdata(dev);
587151060acSTejun Heo
588eb98e3bdSMiklos Szeredi fuse_abort_conn(&cc->fc);
589151060acSTejun Heo return count;
590151060acSTejun Heo }
59158f86cc8SRusty Russell static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store);
592151060acSTejun Heo
5934183fb95SGreg Kroah-Hartman static struct attribute *cuse_class_dev_attrs[] = {
5944183fb95SGreg Kroah-Hartman &dev_attr_waiting.attr,
5954183fb95SGreg Kroah-Hartman &dev_attr_abort.attr,
5964183fb95SGreg Kroah-Hartman NULL,
597151060acSTejun Heo };
5984183fb95SGreg Kroah-Hartman ATTRIBUTE_GROUPS(cuse_class_dev);
599151060acSTejun Heo
600151060acSTejun Heo static struct miscdevice cuse_miscdev = {
601cb2ffb26STom Gundersen .minor = CUSE_MINOR,
602151060acSTejun Heo .name = "cuse",
603151060acSTejun Heo .fops = &cuse_channel_fops,
604151060acSTejun Heo };
605151060acSTejun Heo
606cb2ffb26STom Gundersen MODULE_ALIAS_MISCDEV(CUSE_MINOR);
607cb2ffb26STom Gundersen MODULE_ALIAS("devname:cuse");
608cb2ffb26STom Gundersen
cuse_init(void)609151060acSTejun Heo static int __init cuse_init(void)
610151060acSTejun Heo {
611151060acSTejun Heo int i, rc;
612151060acSTejun Heo
613151060acSTejun Heo /* init conntbl */
614151060acSTejun Heo for (i = 0; i < CUSE_CONNTBL_LEN; i++)
615151060acSTejun Heo INIT_LIST_HEAD(&cuse_conntbl[i]);
616151060acSTejun Heo
617151060acSTejun Heo /* inherit and extend fuse_dev_operations */
618151060acSTejun Heo cuse_channel_fops = fuse_dev_operations;
619151060acSTejun Heo cuse_channel_fops.owner = THIS_MODULE;
620151060acSTejun Heo cuse_channel_fops.open = cuse_channel_open;
621151060acSTejun Heo cuse_channel_fops.release = cuse_channel_release;
6228217673dSMiklos Szeredi /* CUSE is not prepared for FUSE_DEV_IOC_CLONE */
6238217673dSMiklos Szeredi cuse_channel_fops.unlocked_ioctl = NULL;
624151060acSTejun Heo
6251aaba11dSGreg Kroah-Hartman cuse_class = class_create("cuse");
626151060acSTejun Heo if (IS_ERR(cuse_class))
627151060acSTejun Heo return PTR_ERR(cuse_class);
628151060acSTejun Heo
6294183fb95SGreg Kroah-Hartman cuse_class->dev_groups = cuse_class_dev_groups;
630151060acSTejun Heo
631151060acSTejun Heo rc = misc_register(&cuse_miscdev);
632151060acSTejun Heo if (rc) {
633151060acSTejun Heo class_destroy(cuse_class);
634151060acSTejun Heo return rc;
635151060acSTejun Heo }
636151060acSTejun Heo
637151060acSTejun Heo return 0;
638151060acSTejun Heo }
639151060acSTejun Heo
cuse_exit(void)640151060acSTejun Heo static void __exit cuse_exit(void)
641151060acSTejun Heo {
642151060acSTejun Heo misc_deregister(&cuse_miscdev);
643151060acSTejun Heo class_destroy(cuse_class);
644151060acSTejun Heo }
645151060acSTejun Heo
646151060acSTejun Heo module_init(cuse_init);
647151060acSTejun Heo module_exit(cuse_exit);
648151060acSTejun Heo
649151060acSTejun Heo MODULE_AUTHOR("Tejun Heo <tj@kernel.org>");
650151060acSTejun Heo MODULE_DESCRIPTION("Character device in Userspace");
651151060acSTejun Heo MODULE_LICENSE("GPL");
652