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