]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/fuse/inode.c
[PATCH] FUSE: tighten check for processes allowed access
[mirror_ubuntu-hirsute-kernel.git] / fs / fuse / inode.c
CommitLineData
d8a5ba45
MS
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/file.h>
14#include <linux/mount.h>
15#include <linux/seq_file.h>
16#include <linux/init.h>
17#include <linux/module.h>
d8a5ba45
MS
18#include <linux/parser.h>
19#include <linux/statfs.h>
20
21MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22MODULE_DESCRIPTION("Filesystem in Userspace");
23MODULE_LICENSE("GPL");
24
25spinlock_t fuse_lock;
26static kmem_cache_t *fuse_inode_cachep;
d8a5ba45
MS
27
28#define FUSE_SUPER_MAGIC 0x65735546
29
30struct fuse_mount_data {
31 int fd;
32 unsigned rootmode;
33 unsigned user_id;
87729a55 34 unsigned group_id;
1e9a4ed9 35 unsigned flags;
db50b96c 36 unsigned max_read;
d8a5ba45
MS
37};
38
39static struct inode *fuse_alloc_inode(struct super_block *sb)
40{
41 struct inode *inode;
42 struct fuse_inode *fi;
43
44 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
45 if (!inode)
46 return NULL;
47
48 fi = get_fuse_inode(inode);
49 fi->i_time = jiffies - 1;
50 fi->nodeid = 0;
9e6268db 51 fi->nlookup = 0;
e5e5558e
MS
52 fi->forget_req = fuse_request_alloc();
53 if (!fi->forget_req) {
54 kmem_cache_free(fuse_inode_cachep, inode);
55 return NULL;
56 }
d8a5ba45
MS
57
58 return inode;
59}
60
61static void fuse_destroy_inode(struct inode *inode)
62{
e5e5558e
MS
63 struct fuse_inode *fi = get_fuse_inode(inode);
64 if (fi->forget_req)
65 fuse_request_free(fi->forget_req);
d8a5ba45
MS
66 kmem_cache_free(fuse_inode_cachep, inode);
67}
68
69static void fuse_read_inode(struct inode *inode)
70{
71 /* No op */
72}
73
e5e5558e 74void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
9e6268db 75 unsigned long nodeid, u64 nlookup)
e5e5558e
MS
76{
77 struct fuse_forget_in *inarg = &req->misc.forget_in;
9e6268db 78 inarg->nlookup = nlookup;
e5e5558e
MS
79 req->in.h.opcode = FUSE_FORGET;
80 req->in.h.nodeid = nodeid;
81 req->in.numargs = 1;
82 req->in.args[0].size = sizeof(struct fuse_forget_in);
83 req->in.args[0].value = inarg;
84 request_send_noreply(fc, req);
85}
86
d8a5ba45
MS
87static void fuse_clear_inode(struct inode *inode)
88{
1e9a4ed9
MS
89 if (inode->i_sb->s_flags & MS_ACTIVE) {
90 struct fuse_conn *fc = get_fuse_conn(inode);
e5e5558e 91 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 92 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
e5e5558e
MS
93 fi->forget_req = NULL;
94 }
d8a5ba45
MS
95}
96
97void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
98{
99 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
100 invalidate_inode_pages(inode->i_mapping);
101
102 inode->i_ino = attr->ino;
103 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
104 inode->i_nlink = attr->nlink;
105 inode->i_uid = attr->uid;
106 inode->i_gid = attr->gid;
107 i_size_write(inode, attr->size);
108 inode->i_blksize = PAGE_CACHE_SIZE;
109 inode->i_blocks = attr->blocks;
110 inode->i_atime.tv_sec = attr->atime;
111 inode->i_atime.tv_nsec = attr->atimensec;
112 inode->i_mtime.tv_sec = attr->mtime;
113 inode->i_mtime.tv_nsec = attr->mtimensec;
114 inode->i_ctime.tv_sec = attr->ctime;
115 inode->i_ctime.tv_nsec = attr->ctimensec;
116}
117
118static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
119{
120 inode->i_mode = attr->mode & S_IFMT;
121 i_size_write(inode, attr->size);
e5e5558e
MS
122 if (S_ISREG(inode->i_mode)) {
123 fuse_init_common(inode);
b6aeaded 124 fuse_init_file_inode(inode);
e5e5558e
MS
125 } else if (S_ISDIR(inode->i_mode))
126 fuse_init_dir(inode);
127 else if (S_ISLNK(inode->i_mode))
128 fuse_init_symlink(inode);
129 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
130 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
131 fuse_init_common(inode);
132 init_special_inode(inode, inode->i_mode,
133 new_decode_dev(attr->rdev));
134 } else {
135 /* Don't let user create weird files */
136 inode->i_mode = S_IFREG;
137 fuse_init_common(inode);
b6aeaded 138 fuse_init_file_inode(inode);
e5e5558e 139 }
d8a5ba45
MS
140}
141
142static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
143{
144 unsigned long nodeid = *(unsigned long *) _nodeidp;
145 if (get_node_id(inode) == nodeid)
146 return 1;
147 else
148 return 0;
149}
150
151static int fuse_inode_set(struct inode *inode, void *_nodeidp)
152{
153 unsigned long nodeid = *(unsigned long *) _nodeidp;
154 get_fuse_inode(inode)->nodeid = nodeid;
155 return 0;
156}
157
158struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
9e6268db 159 int generation, struct fuse_attr *attr)
d8a5ba45
MS
160{
161 struct inode *inode;
9e6268db 162 struct fuse_inode *fi;
d8a5ba45
MS
163 struct fuse_conn *fc = get_fuse_conn_super(sb);
164 int retried = 0;
165
166 retry:
167 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
168 if (!inode)
169 return NULL;
170
171 if ((inode->i_state & I_NEW)) {
172 inode->i_generation = generation;
173 inode->i_data.backing_dev_info = &fc->bdi;
174 fuse_init_inode(inode, attr);
175 unlock_new_inode(inode);
176 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
177 BUG_ON(retried);
178 /* Inode has changed type, any I/O on the old should fail */
179 make_bad_inode(inode);
180 iput(inode);
181 retried = 1;
182 goto retry;
183 }
184
9e6268db
MS
185 fi = get_fuse_inode(inode);
186 fi->nlookup ++;
d8a5ba45 187 fuse_change_attributes(inode, attr);
d8a5ba45
MS
188 return inode;
189}
190
191static void fuse_put_super(struct super_block *sb)
192{
193 struct fuse_conn *fc = get_fuse_conn_super(sb);
194
1e9a4ed9
MS
195 down_write(&fc->sbput_sem);
196 while (!list_empty(&fc->background))
197 fuse_release_background(list_entry(fc->background.next,
198 struct fuse_req, bg_entry));
199
d8a5ba45 200 spin_lock(&fuse_lock);
1e9a4ed9 201 fc->mounted = 0;
d8a5ba45 202 fc->user_id = 0;
87729a55 203 fc->group_id = 0;
1e9a4ed9 204 fc->flags = 0;
334f485d
MS
205 /* Flush all readers on this fs */
206 wake_up_all(&fc->waitq);
1e9a4ed9 207 up_write(&fc->sbput_sem);
d8a5ba45 208 fuse_release_conn(fc);
d8a5ba45
MS
209 spin_unlock(&fuse_lock);
210}
211
e5e5558e
MS
212static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
213{
214 stbuf->f_type = FUSE_SUPER_MAGIC;
215 stbuf->f_bsize = attr->bsize;
216 stbuf->f_blocks = attr->blocks;
217 stbuf->f_bfree = attr->bfree;
218 stbuf->f_bavail = attr->bavail;
219 stbuf->f_files = attr->files;
220 stbuf->f_ffree = attr->ffree;
221 stbuf->f_namelen = attr->namelen;
222 /* fsid is left zero */
223}
224
225static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
226{
227 struct fuse_conn *fc = get_fuse_conn_super(sb);
228 struct fuse_req *req;
229 struct fuse_statfs_out outarg;
230 int err;
231
232 req = fuse_get_request(fc);
233 if (!req)
234 return -ERESTARTSYS;
235
236 req->in.numargs = 0;
237 req->in.h.opcode = FUSE_STATFS;
238 req->out.numargs = 1;
239 req->out.args[0].size = sizeof(outarg);
240 req->out.args[0].value = &outarg;
241 request_send(fc, req);
242 err = req->out.h.error;
243 if (!err)
244 convert_fuse_statfs(buf, &outarg.st);
245 fuse_put_request(fc, req);
246 return err;
247}
248
d8a5ba45
MS
249enum {
250 OPT_FD,
251 OPT_ROOTMODE,
252 OPT_USER_ID,
87729a55 253 OPT_GROUP_ID,
d8a5ba45
MS
254 OPT_DEFAULT_PERMISSIONS,
255 OPT_ALLOW_OTHER,
d8a5ba45 256 OPT_KERNEL_CACHE,
db50b96c 257 OPT_MAX_READ,
d8a5ba45
MS
258 OPT_ERR
259};
260
261static match_table_t tokens = {
262 {OPT_FD, "fd=%u"},
263 {OPT_ROOTMODE, "rootmode=%o"},
264 {OPT_USER_ID, "user_id=%u"},
87729a55 265 {OPT_GROUP_ID, "group_id=%u"},
d8a5ba45
MS
266 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
267 {OPT_ALLOW_OTHER, "allow_other"},
d8a5ba45 268 {OPT_KERNEL_CACHE, "kernel_cache"},
db50b96c 269 {OPT_MAX_READ, "max_read=%u"},
d8a5ba45
MS
270 {OPT_ERR, NULL}
271};
272
273static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
274{
275 char *p;
276 memset(d, 0, sizeof(struct fuse_mount_data));
277 d->fd = -1;
db50b96c 278 d->max_read = ~0;
d8a5ba45
MS
279
280 while ((p = strsep(&opt, ",")) != NULL) {
281 int token;
282 int value;
283 substring_t args[MAX_OPT_ARGS];
284 if (!*p)
285 continue;
286
287 token = match_token(p, tokens, args);
288 switch (token) {
289 case OPT_FD:
290 if (match_int(&args[0], &value))
291 return 0;
292 d->fd = value;
293 break;
294
295 case OPT_ROOTMODE:
296 if (match_octal(&args[0], &value))
297 return 0;
298 d->rootmode = value;
299 break;
300
301 case OPT_USER_ID:
302 if (match_int(&args[0], &value))
303 return 0;
304 d->user_id = value;
305 break;
306
87729a55
MS
307 case OPT_GROUP_ID:
308 if (match_int(&args[0], &value))
309 return 0;
310 d->group_id = value;
311 break;
312
1e9a4ed9
MS
313 case OPT_DEFAULT_PERMISSIONS:
314 d->flags |= FUSE_DEFAULT_PERMISSIONS;
315 break;
316
317 case OPT_ALLOW_OTHER:
318 d->flags |= FUSE_ALLOW_OTHER;
319 break;
320
321 case OPT_KERNEL_CACHE:
322 d->flags |= FUSE_KERNEL_CACHE;
323 break;
324
db50b96c
MS
325 case OPT_MAX_READ:
326 if (match_int(&args[0], &value))
327 return 0;
328 d->max_read = value;
329 break;
330
d8a5ba45
MS
331 default:
332 return 0;
333 }
334 }
335 if (d->fd == -1)
336 return 0;
337
338 return 1;
339}
340
341static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
342{
343 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
344
345 seq_printf(m, ",user_id=%u", fc->user_id);
87729a55 346 seq_printf(m, ",group_id=%u", fc->group_id);
1e9a4ed9
MS
347 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
348 seq_puts(m, ",default_permissions");
349 if (fc->flags & FUSE_ALLOW_OTHER)
350 seq_puts(m, ",allow_other");
351 if (fc->flags & FUSE_KERNEL_CACHE)
352 seq_puts(m, ",kernel_cache");
db50b96c
MS
353 if (fc->max_read != ~0)
354 seq_printf(m, ",max_read=%u", fc->max_read);
d8a5ba45
MS
355 return 0;
356}
357
334f485d 358static void free_conn(struct fuse_conn *fc)
d8a5ba45 359{
334f485d
MS
360 while (!list_empty(&fc->unused_list)) {
361 struct fuse_req *req;
362 req = list_entry(fc->unused_list.next, struct fuse_req, list);
363 list_del(&req->list);
364 fuse_request_free(req);
365 }
d8a5ba45
MS
366 kfree(fc);
367}
368
334f485d
MS
369/* Must be called with the fuse lock held */
370void fuse_release_conn(struct fuse_conn *fc)
371{
1e9a4ed9
MS
372 fc->count--;
373 if (!fc->count)
334f485d
MS
374 free_conn(fc);
375}
376
d8a5ba45
MS
377static struct fuse_conn *new_conn(void)
378{
379 struct fuse_conn *fc;
380
381 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
382 if (fc != NULL) {
334f485d 383 int i;
d8a5ba45 384 memset(fc, 0, sizeof(*fc));
334f485d
MS
385 init_waitqueue_head(&fc->waitq);
386 INIT_LIST_HEAD(&fc->pending);
387 INIT_LIST_HEAD(&fc->processing);
388 INIT_LIST_HEAD(&fc->unused_list);
1e9a4ed9 389 INIT_LIST_HEAD(&fc->background);
334f485d 390 sema_init(&fc->outstanding_sem, 0);
1e9a4ed9 391 init_rwsem(&fc->sbput_sem);
334f485d
MS
392 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
393 struct fuse_req *req = fuse_request_alloc();
394 if (!req) {
395 free_conn(fc);
396 return NULL;
397 }
398 list_add(&req->list, &fc->unused_list);
399 }
d8a5ba45
MS
400 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
401 fc->bdi.unplug_io_fn = default_unplug_io_fn;
334f485d 402 fc->reqctr = 0;
d8a5ba45
MS
403 }
404 return fc;
405}
406
407static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
408{
409 struct fuse_conn *fc;
410
334f485d
MS
411 if (file->f_op != &fuse_dev_operations)
412 return ERR_PTR(-EINVAL);
d8a5ba45
MS
413 fc = new_conn();
414 if (fc == NULL)
334f485d 415 return ERR_PTR(-ENOMEM);
d8a5ba45 416 spin_lock(&fuse_lock);
334f485d
MS
417 if (file->private_data) {
418 free_conn(fc);
419 fc = ERR_PTR(-EINVAL);
420 } else {
421 file->private_data = fc;
1e9a4ed9
MS
422 *get_fuse_conn_super_p(sb) = fc;
423 fc->mounted = 1;
424 fc->connected = 1;
425 fc->count = 2;
334f485d 426 }
d8a5ba45
MS
427 spin_unlock(&fuse_lock);
428 return fc;
429}
430
431static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
432{
433 struct fuse_attr attr;
434 memset(&attr, 0, sizeof(attr));
435
436 attr.mode = mode;
437 attr.ino = FUSE_ROOT_ID;
9e6268db 438 return fuse_iget(sb, 1, 0, &attr);
d8a5ba45
MS
439}
440
441static struct super_operations fuse_super_operations = {
442 .alloc_inode = fuse_alloc_inode,
443 .destroy_inode = fuse_destroy_inode,
444 .read_inode = fuse_read_inode,
445 .clear_inode = fuse_clear_inode,
446 .put_super = fuse_put_super,
e5e5558e 447 .statfs = fuse_statfs,
d8a5ba45
MS
448 .show_options = fuse_show_options,
449};
450
d8a5ba45
MS
451static int fuse_fill_super(struct super_block *sb, void *data, int silent)
452{
453 struct fuse_conn *fc;
454 struct inode *root;
455 struct fuse_mount_data d;
456 struct file *file;
457 int err;
458
459 if (!parse_fuse_opt((char *) data, &d))
460 return -EINVAL;
461
462 sb->s_blocksize = PAGE_CACHE_SIZE;
463 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
464 sb->s_magic = FUSE_SUPER_MAGIC;
465 sb->s_op = &fuse_super_operations;
466 sb->s_maxbytes = MAX_LFS_FILESIZE;
467
468 file = fget(d.fd);
469 if (!file)
470 return -EINVAL;
471
472 fc = get_conn(file, sb);
473 fput(file);
334f485d
MS
474 if (IS_ERR(fc))
475 return PTR_ERR(fc);
d8a5ba45 476
1e9a4ed9 477 fc->flags = d.flags;
d8a5ba45 478 fc->user_id = d.user_id;
87729a55 479 fc->group_id = d.group_id;
db50b96c
MS
480 fc->max_read = d.max_read;
481 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
482 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
d8a5ba45 483
d8a5ba45
MS
484 err = -ENOMEM;
485 root = get_root_inode(sb, d.rootmode);
486 if (root == NULL)
487 goto err;
488
489 sb->s_root = d_alloc_root(root);
490 if (!sb->s_root) {
491 iput(root);
492 goto err;
493 }
334f485d 494 fuse_send_init(fc);
d8a5ba45
MS
495 return 0;
496
497 err:
498 spin_lock(&fuse_lock);
d8a5ba45
MS
499 fuse_release_conn(fc);
500 spin_unlock(&fuse_lock);
d8a5ba45
MS
501 return err;
502}
503
504static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
505 int flags, const char *dev_name,
506 void *raw_data)
507{
508 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
509}
510
511static struct file_system_type fuse_fs_type = {
512 .owner = THIS_MODULE,
513 .name = "fuse",
514 .get_sb = fuse_get_sb,
515 .kill_sb = kill_anon_super,
516};
517
518static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
519 unsigned long flags)
520{
521 struct inode * inode = foo;
522
523 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
524 SLAB_CTOR_CONSTRUCTOR)
525 inode_init_once(inode);
526}
527
528static int __init fuse_fs_init(void)
529{
530 int err;
531
532 err = register_filesystem(&fuse_fs_type);
533 if (err)
534 printk("fuse: failed to register filesystem\n");
535 else {
536 fuse_inode_cachep = kmem_cache_create("fuse_inode",
537 sizeof(struct fuse_inode),
538 0, SLAB_HWCACHE_ALIGN,
539 fuse_inode_init_once, NULL);
540 if (!fuse_inode_cachep) {
541 unregister_filesystem(&fuse_fs_type);
542 err = -ENOMEM;
543 }
544 }
545
546 return err;
547}
548
549static void fuse_fs_cleanup(void)
550{
551 unregister_filesystem(&fuse_fs_type);
552 kmem_cache_destroy(fuse_inode_cachep);
553}
554
555static int __init fuse_init(void)
556{
557 int res;
558
559 printk("fuse init (API version %i.%i)\n",
560 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
561
562 spin_lock_init(&fuse_lock);
563 res = fuse_fs_init();
564 if (res)
565 goto err;
566
334f485d
MS
567 res = fuse_dev_init();
568 if (res)
569 goto err_fs_cleanup;
570
d8a5ba45
MS
571 return 0;
572
334f485d
MS
573 err_fs_cleanup:
574 fuse_fs_cleanup();
d8a5ba45
MS
575 err:
576 return res;
577}
578
579static void __exit fuse_exit(void)
580{
581 printk(KERN_DEBUG "fuse exit\n");
582
583 fuse_fs_cleanup();
334f485d 584 fuse_dev_cleanup();
d8a5ba45
MS
585}
586
587module_init(fuse_init);
588module_exit(fuse_exit);