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