1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 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/seq_file.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/fs_context.h>
19 #include <linux/fs_parser.h>
20 #include <linux/statfs.h>
21 #include <linux/random.h>
22 #include <linux/sched.h>
23 #include <linux/exportfs.h>
24 #include <linux/posix_acl.h>
25 #include <linux/pid_namespace.h>
26
27 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
28 MODULE_DESCRIPTION("Filesystem in Userspace");
29 MODULE_LICENSE("GPL");
30
31 static struct kmem_cache *fuse_inode_cachep;
32 struct list_head fuse_conn_list;
33 DEFINE_MUTEX(fuse_mutex);
34
35 static int set_global_limit(const char *val, const struct kernel_param *kp);
36
37 unsigned max_user_bgreq;
38 module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
39 &max_user_bgreq, 0644);
40 __MODULE_PARM_TYPE(max_user_bgreq, "uint");
41 MODULE_PARM_DESC(max_user_bgreq,
42 "Global limit for the maximum number of backgrounded requests an "
43 "unprivileged user can set");
44
45 unsigned max_user_congthresh;
46 module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
47 &max_user_congthresh, 0644);
48 __MODULE_PARM_TYPE(max_user_congthresh, "uint");
49 MODULE_PARM_DESC(max_user_congthresh,
50 "Global limit for the maximum congestion threshold an "
51 "unprivileged user can set");
52
53 #define FUSE_SUPER_MAGIC 0x65735546
54
55 #define FUSE_DEFAULT_BLKSIZE 512
56
57 /** Maximum number of outstanding background requests */
58 #define FUSE_DEFAULT_MAX_BACKGROUND 12
59
60 /** Congestion starts at 75% of maximum */
61 #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
62
63 #ifdef CONFIG_BLOCK
64 static struct file_system_type fuseblk_fs_type;
65 #endif
66
fuse_alloc_forget(void)67 struct fuse_forget_link *fuse_alloc_forget(void)
68 {
69 return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
70 }
71
fuse_alloc_inode(struct super_block * sb)72 static struct inode *fuse_alloc_inode(struct super_block *sb)
73 {
74 struct fuse_inode *fi;
75
76 fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
77 if (!fi)
78 return NULL;
79
80 fi->i_time = 0;
81 fi->inval_mask = 0;
82 fi->nodeid = 0;
83 fi->nlookup = 0;
84 fi->attr_version = 0;
85 fi->orig_ino = 0;
86 fi->state = 0;
87 mutex_init(&fi->mutex);
88 spin_lock_init(&fi->lock);
89 fi->forget = fuse_alloc_forget();
90 if (!fi->forget) {
91 kmem_cache_free(fuse_inode_cachep, fi);
92 return NULL;
93 }
94
95 return &fi->inode;
96 }
97
fuse_free_inode(struct inode * inode)98 static void fuse_free_inode(struct inode *inode)
99 {
100 struct fuse_inode *fi = get_fuse_inode(inode);
101
102 mutex_destroy(&fi->mutex);
103 kfree(fi->forget);
104 kmem_cache_free(fuse_inode_cachep, fi);
105 }
106
fuse_evict_inode(struct inode * inode)107 static void fuse_evict_inode(struct inode *inode)
108 {
109 struct fuse_inode *fi = get_fuse_inode(inode);
110
111 truncate_inode_pages_final(&inode->i_data);
112 clear_inode(inode);
113 if (inode->i_sb->s_flags & SB_ACTIVE) {
114 struct fuse_conn *fc = get_fuse_conn(inode);
115 fuse_queue_forget(fc, fi->forget, fi->nodeid, fi->nlookup);
116 fi->forget = NULL;
117 }
118 if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
119 WARN_ON(!list_empty(&fi->write_files));
120 WARN_ON(!list_empty(&fi->queued_writes));
121 }
122 }
123
fuse_reconfigure(struct fs_context * fc)124 static int fuse_reconfigure(struct fs_context *fc)
125 {
126 struct super_block *sb = fc->root->d_sb;
127
128 sync_filesystem(sb);
129 if (fc->sb_flags & SB_MANDLOCK)
130 return -EINVAL;
131
132 return 0;
133 }
134
135 /*
136 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
137 * so that it will fit.
138 */
fuse_squash_ino(u64 ino64)139 static ino_t fuse_squash_ino(u64 ino64)
140 {
141 ino_t ino = (ino_t) ino64;
142 if (sizeof(ino_t) < sizeof(u64))
143 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
144 return ino;
145 }
146
fuse_change_attributes_common(struct inode * inode,struct fuse_attr * attr,u64 attr_valid)147 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
148 u64 attr_valid)
149 {
150 struct fuse_conn *fc = get_fuse_conn(inode);
151 struct fuse_inode *fi = get_fuse_inode(inode);
152
153 lockdep_assert_held(&fi->lock);
154
155 fi->attr_version = atomic64_inc_return(&fc->attr_version);
156 fi->i_time = attr_valid;
157 WRITE_ONCE(fi->inval_mask, 0);
158
159 inode->i_ino = fuse_squash_ino(attr->ino);
160 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
161 set_nlink(inode, attr->nlink);
162 inode->i_uid = make_kuid(fc->user_ns, attr->uid);
163 inode->i_gid = make_kgid(fc->user_ns, attr->gid);
164 inode->i_blocks = attr->blocks;
165
166 /* Sanitize nsecs */
167 attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
168 attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
169 attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
170
171 inode->i_atime.tv_sec = attr->atime;
172 inode->i_atime.tv_nsec = attr->atimensec;
173 /* mtime from server may be stale due to local buffered write */
174 if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
175 inode->i_mtime.tv_sec = attr->mtime;
176 inode->i_mtime.tv_nsec = attr->mtimensec;
177 inode->i_ctime.tv_sec = attr->ctime;
178 inode->i_ctime.tv_nsec = attr->ctimensec;
179 }
180
181 if (attr->blksize != 0)
182 inode->i_blkbits = ilog2(attr->blksize);
183 else
184 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
185
186 /*
187 * Don't set the sticky bit in i_mode, unless we want the VFS
188 * to check permissions. This prevents failures due to the
189 * check in may_delete().
190 */
191 fi->orig_i_mode = inode->i_mode;
192 if (!fc->default_permissions)
193 inode->i_mode &= ~S_ISVTX;
194
195 fi->orig_ino = attr->ino;
196 }
197
fuse_change_attributes(struct inode * inode,struct fuse_attr * attr,u64 attr_valid,u64 attr_version)198 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
199 u64 attr_valid, u64 attr_version)
200 {
201 struct fuse_conn *fc = get_fuse_conn(inode);
202 struct fuse_inode *fi = get_fuse_inode(inode);
203 bool is_wb = fc->writeback_cache;
204 loff_t oldsize;
205 struct timespec64 old_mtime;
206
207 spin_lock(&fi->lock);
208 if ((attr_version != 0 && fi->attr_version > attr_version) ||
209 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
210 spin_unlock(&fi->lock);
211 return;
212 }
213
214 old_mtime = inode->i_mtime;
215 fuse_change_attributes_common(inode, attr, attr_valid);
216
217 oldsize = inode->i_size;
218 /*
219 * In case of writeback_cache enabled, the cached writes beyond EOF
220 * extend local i_size without keeping userspace server in sync. So,
221 * attr->size coming from server can be stale. We cannot trust it.
222 */
223 if (!is_wb || !S_ISREG(inode->i_mode))
224 i_size_write(inode, attr->size);
225 spin_unlock(&fi->lock);
226
227 if (!is_wb && S_ISREG(inode->i_mode)) {
228 bool inval = false;
229
230 if (oldsize != attr->size) {
231 truncate_pagecache(inode, attr->size);
232 if (!fc->explicit_inval_data)
233 inval = true;
234 } else if (fc->auto_inval_data) {
235 struct timespec64 new_mtime = {
236 .tv_sec = attr->mtime,
237 .tv_nsec = attr->mtimensec,
238 };
239
240 /*
241 * Auto inval mode also checks and invalidates if mtime
242 * has changed.
243 */
244 if (!timespec64_equal(&old_mtime, &new_mtime))
245 inval = true;
246 }
247
248 if (inval)
249 invalidate_inode_pages2(inode->i_mapping);
250 }
251 }
252
fuse_init_inode(struct inode * inode,struct fuse_attr * attr)253 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
254 {
255 inode->i_mode = attr->mode & S_IFMT;
256 inode->i_size = attr->size;
257 inode->i_mtime.tv_sec = attr->mtime;
258 inode->i_mtime.tv_nsec = attr->mtimensec;
259 inode->i_ctime.tv_sec = attr->ctime;
260 inode->i_ctime.tv_nsec = attr->ctimensec;
261 if (S_ISREG(inode->i_mode)) {
262 fuse_init_common(inode);
263 fuse_init_file_inode(inode);
264 } else if (S_ISDIR(inode->i_mode))
265 fuse_init_dir(inode);
266 else if (S_ISLNK(inode->i_mode))
267 fuse_init_symlink(inode);
268 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
269 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
270 fuse_init_common(inode);
271 init_special_inode(inode, inode->i_mode,
272 new_decode_dev(attr->rdev));
273 } else
274 BUG();
275 }
276
fuse_inode_eq(struct inode * inode,void * _nodeidp)277 int fuse_inode_eq(struct inode *inode, void *_nodeidp)
278 {
279 u64 nodeid = *(u64 *) _nodeidp;
280 if (get_node_id(inode) == nodeid)
281 return 1;
282 else
283 return 0;
284 }
285
fuse_inode_set(struct inode * inode,void * _nodeidp)286 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
287 {
288 u64 nodeid = *(u64 *) _nodeidp;
289 get_fuse_inode(inode)->nodeid = nodeid;
290 return 0;
291 }
292
fuse_iget(struct super_block * sb,u64 nodeid,int generation,struct fuse_attr * attr,u64 attr_valid,u64 attr_version)293 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
294 int generation, struct fuse_attr *attr,
295 u64 attr_valid, u64 attr_version)
296 {
297 struct inode *inode;
298 struct fuse_inode *fi;
299 struct fuse_conn *fc = get_fuse_conn_super(sb);
300
301 retry:
302 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
303 if (!inode)
304 return NULL;
305
306 if ((inode->i_state & I_NEW)) {
307 inode->i_flags |= S_NOATIME;
308 if (!fc->writeback_cache || !S_ISREG(attr->mode))
309 inode->i_flags |= S_NOCMTIME;
310 inode->i_generation = generation;
311 fuse_init_inode(inode, attr);
312 unlock_new_inode(inode);
313 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
314 /* Inode has changed type, any I/O on the old should fail */
315 fuse_make_bad(inode);
316 if (inode != d_inode(sb->s_root)) {
317 remove_inode_hash(inode);
318 iput(inode);
319 goto retry;
320 }
321 }
322
323 fi = get_fuse_inode(inode);
324 spin_lock(&fi->lock);
325 fi->nlookup++;
326 spin_unlock(&fi->lock);
327 fuse_change_attributes(inode, attr, attr_valid, attr_version);
328
329 return inode;
330 }
331
fuse_reverse_inval_inode(struct super_block * sb,u64 nodeid,loff_t offset,loff_t len)332 int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
333 loff_t offset, loff_t len)
334 {
335 struct fuse_conn *fc = get_fuse_conn_super(sb);
336 struct fuse_inode *fi;
337 struct inode *inode;
338 pgoff_t pg_start;
339 pgoff_t pg_end;
340
341 inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
342 if (!inode)
343 return -ENOENT;
344
345 fi = get_fuse_inode(inode);
346 spin_lock(&fi->lock);
347 fi->attr_version = atomic64_inc_return(&fc->attr_version);
348 spin_unlock(&fi->lock);
349
350 fuse_invalidate_attr(inode);
351 forget_all_cached_acls(inode);
352 if (offset >= 0) {
353 pg_start = offset >> PAGE_SHIFT;
354 if (len <= 0)
355 pg_end = -1;
356 else
357 pg_end = (offset + len - 1) >> PAGE_SHIFT;
358 invalidate_inode_pages2_range(inode->i_mapping,
359 pg_start, pg_end);
360 }
361 iput(inode);
362 return 0;
363 }
364
fuse_lock_inode(struct inode * inode)365 bool fuse_lock_inode(struct inode *inode)
366 {
367 bool locked = false;
368
369 if (!get_fuse_conn(inode)->parallel_dirops) {
370 mutex_lock(&get_fuse_inode(inode)->mutex);
371 locked = true;
372 }
373
374 return locked;
375 }
376
fuse_unlock_inode(struct inode * inode,bool locked)377 void fuse_unlock_inode(struct inode *inode, bool locked)
378 {
379 if (locked)
380 mutex_unlock(&get_fuse_inode(inode)->mutex);
381 }
382
fuse_umount_begin(struct super_block * sb)383 static void fuse_umount_begin(struct super_block *sb)
384 {
385 struct fuse_conn *fc = get_fuse_conn_super(sb);
386
387 if (!fc->no_force_umount)
388 fuse_abort_conn(fc);
389 }
390
fuse_send_destroy(struct fuse_conn * fc)391 static void fuse_send_destroy(struct fuse_conn *fc)
392 {
393 if (fc->conn_init) {
394 FUSE_ARGS(args);
395
396 args.opcode = FUSE_DESTROY;
397 args.force = true;
398 args.nocreds = true;
399 fuse_simple_request(fc, &args);
400 }
401 }
402
fuse_put_super(struct super_block * sb)403 static void fuse_put_super(struct super_block *sb)
404 {
405 struct fuse_conn *fc = get_fuse_conn_super(sb);
406
407 mutex_lock(&fuse_mutex);
408 list_del(&fc->entry);
409 fuse_ctl_remove_conn(fc);
410 mutex_unlock(&fuse_mutex);
411
412 fuse_conn_put(fc);
413 }
414
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)415 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
416 {
417 stbuf->f_type = FUSE_SUPER_MAGIC;
418 stbuf->f_bsize = attr->bsize;
419 stbuf->f_frsize = attr->frsize;
420 stbuf->f_blocks = attr->blocks;
421 stbuf->f_bfree = attr->bfree;
422 stbuf->f_bavail = attr->bavail;
423 stbuf->f_files = attr->files;
424 stbuf->f_ffree = attr->ffree;
425 stbuf->f_namelen = attr->namelen;
426 /* fsid is left zero */
427 }
428
fuse_statfs(struct dentry * dentry,struct kstatfs * buf)429 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
430 {
431 struct super_block *sb = dentry->d_sb;
432 struct fuse_conn *fc = get_fuse_conn_super(sb);
433 FUSE_ARGS(args);
434 struct fuse_statfs_out outarg;
435 int err;
436
437 if (!fuse_allow_current_process(fc)) {
438 buf->f_type = FUSE_SUPER_MAGIC;
439 return 0;
440 }
441
442 memset(&outarg, 0, sizeof(outarg));
443 args.in_numargs = 0;
444 args.opcode = FUSE_STATFS;
445 args.nodeid = get_node_id(d_inode(dentry));
446 args.out_numargs = 1;
447 args.out_args[0].size = sizeof(outarg);
448 args.out_args[0].value = &outarg;
449 err = fuse_simple_request(fc, &args);
450 if (!err)
451 convert_fuse_statfs(buf, &outarg.st);
452 return err;
453 }
454
455 enum {
456 OPT_SOURCE,
457 OPT_SUBTYPE,
458 OPT_FD,
459 OPT_ROOTMODE,
460 OPT_USER_ID,
461 OPT_GROUP_ID,
462 OPT_DEFAULT_PERMISSIONS,
463 OPT_ALLOW_OTHER,
464 OPT_MAX_READ,
465 OPT_BLKSIZE,
466 OPT_ERR
467 };
468
469 static const struct fs_parameter_spec fuse_param_specs[] = {
470 fsparam_string ("source", OPT_SOURCE),
471 fsparam_u32 ("fd", OPT_FD),
472 fsparam_u32oct ("rootmode", OPT_ROOTMODE),
473 fsparam_u32 ("user_id", OPT_USER_ID),
474 fsparam_u32 ("group_id", OPT_GROUP_ID),
475 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
476 fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
477 fsparam_u32 ("max_read", OPT_MAX_READ),
478 fsparam_u32 ("blksize", OPT_BLKSIZE),
479 fsparam_string ("subtype", OPT_SUBTYPE),
480 {}
481 };
482
483 static const struct fs_parameter_description fuse_fs_parameters = {
484 .name = "fuse",
485 .specs = fuse_param_specs,
486 };
487
fuse_parse_param(struct fs_context * fc,struct fs_parameter * param)488 static int fuse_parse_param(struct fs_context *fc, struct fs_parameter *param)
489 {
490 struct fs_parse_result result;
491 struct fuse_fs_context *ctx = fc->fs_private;
492 int opt;
493
494 /*
495 * Ignore options coming from mount(MS_REMOUNT) for backward
496 * compatibility.
497 */
498 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
499 return 0;
500
501 opt = fs_parse(fc, &fuse_fs_parameters, param, &result);
502 if (opt < 0)
503 return opt;
504
505 switch (opt) {
506 case OPT_SOURCE:
507 if (fc->source)
508 return invalf(fc, "fuse: Multiple sources specified");
509 fc->source = param->string;
510 param->string = NULL;
511 break;
512
513 case OPT_SUBTYPE:
514 if (ctx->subtype)
515 return invalf(fc, "fuse: Multiple subtypes specified");
516 ctx->subtype = param->string;
517 param->string = NULL;
518 return 0;
519
520 case OPT_FD:
521 ctx->fd = result.uint_32;
522 ctx->fd_present = 1;
523 break;
524
525 case OPT_ROOTMODE:
526 if (!fuse_valid_type(result.uint_32))
527 return invalf(fc, "fuse: Invalid rootmode");
528 ctx->rootmode = result.uint_32;
529 ctx->rootmode_present = 1;
530 break;
531
532 case OPT_USER_ID:
533 ctx->user_id = make_kuid(fc->user_ns, result.uint_32);
534 if (!uid_valid(ctx->user_id))
535 return invalf(fc, "fuse: Invalid user_id");
536 ctx->user_id_present = 1;
537 break;
538
539 case OPT_GROUP_ID:
540 ctx->group_id = make_kgid(fc->user_ns, result.uint_32);
541 if (!gid_valid(ctx->group_id))
542 return invalf(fc, "fuse: Invalid group_id");
543 ctx->group_id_present = 1;
544 break;
545
546 case OPT_DEFAULT_PERMISSIONS:
547 ctx->default_permissions = 1;
548 break;
549
550 case OPT_ALLOW_OTHER:
551 ctx->allow_other = 1;
552 break;
553
554 case OPT_MAX_READ:
555 ctx->max_read = result.uint_32;
556 break;
557
558 case OPT_BLKSIZE:
559 if (!ctx->is_bdev)
560 return invalf(fc, "fuse: blksize only supported for fuseblk");
561 ctx->blksize = result.uint_32;
562 break;
563
564 default:
565 return -EINVAL;
566 }
567
568 return 0;
569 }
570
fuse_free_fc(struct fs_context * fc)571 static void fuse_free_fc(struct fs_context *fc)
572 {
573 struct fuse_fs_context *ctx = fc->fs_private;
574
575 if (ctx) {
576 kfree(ctx->subtype);
577 kfree(ctx);
578 }
579 }
580
fuse_show_options(struct seq_file * m,struct dentry * root)581 static int fuse_show_options(struct seq_file *m, struct dentry *root)
582 {
583 struct super_block *sb = root->d_sb;
584 struct fuse_conn *fc = get_fuse_conn_super(sb);
585
586 if (fc->no_mount_options)
587 return 0;
588
589 seq_printf(m, ",user_id=%u", from_kuid_munged(fc->user_ns, fc->user_id));
590 seq_printf(m, ",group_id=%u", from_kgid_munged(fc->user_ns, fc->group_id));
591 if (fc->default_permissions)
592 seq_puts(m, ",default_permissions");
593 if (fc->allow_other)
594 seq_puts(m, ",allow_other");
595 if (fc->max_read != ~0)
596 seq_printf(m, ",max_read=%u", fc->max_read);
597 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
598 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
599 return 0;
600 }
601
fuse_iqueue_init(struct fuse_iqueue * fiq,const struct fuse_iqueue_ops * ops,void * priv)602 static void fuse_iqueue_init(struct fuse_iqueue *fiq,
603 const struct fuse_iqueue_ops *ops,
604 void *priv)
605 {
606 memset(fiq, 0, sizeof(struct fuse_iqueue));
607 spin_lock_init(&fiq->lock);
608 init_waitqueue_head(&fiq->waitq);
609 INIT_LIST_HEAD(&fiq->pending);
610 INIT_LIST_HEAD(&fiq->interrupts);
611 fiq->forget_list_tail = &fiq->forget_list_head;
612 fiq->connected = 1;
613 fiq->ops = ops;
614 fiq->priv = priv;
615 }
616
fuse_pqueue_init(struct fuse_pqueue * fpq)617 static void fuse_pqueue_init(struct fuse_pqueue *fpq)
618 {
619 unsigned int i;
620
621 spin_lock_init(&fpq->lock);
622 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
623 INIT_LIST_HEAD(&fpq->processing[i]);
624 INIT_LIST_HEAD(&fpq->io);
625 fpq->connected = 1;
626 }
627
fuse_conn_init(struct fuse_conn * fc,struct user_namespace * user_ns,const struct fuse_iqueue_ops * fiq_ops,void * fiq_priv)628 void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns,
629 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
630 {
631 memset(fc, 0, sizeof(*fc));
632 spin_lock_init(&fc->lock);
633 spin_lock_init(&fc->bg_lock);
634 init_rwsem(&fc->killsb);
635 refcount_set(&fc->count, 1);
636 atomic_set(&fc->dev_count, 1);
637 init_waitqueue_head(&fc->blocked_waitq);
638 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
639 INIT_LIST_HEAD(&fc->bg_queue);
640 INIT_LIST_HEAD(&fc->entry);
641 INIT_LIST_HEAD(&fc->devices);
642 atomic_set(&fc->num_waiting, 0);
643 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
644 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
645 atomic64_set(&fc->khctr, 0);
646 fc->polled_files = RB_ROOT;
647 fc->blocked = 0;
648 fc->initialized = 0;
649 fc->connected = 1;
650 atomic64_set(&fc->attr_version, 1);
651 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
652 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
653 fc->user_ns = get_user_ns(user_ns);
654 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
655 }
656 EXPORT_SYMBOL_GPL(fuse_conn_init);
657
fuse_conn_put(struct fuse_conn * fc)658 void fuse_conn_put(struct fuse_conn *fc)
659 {
660 if (refcount_dec_and_test(&fc->count)) {
661 struct fuse_iqueue *fiq = &fc->iq;
662
663 if (fiq->ops->release)
664 fiq->ops->release(fiq);
665 put_pid_ns(fc->pid_ns);
666 put_user_ns(fc->user_ns);
667 fc->release(fc);
668 }
669 }
670 EXPORT_SYMBOL_GPL(fuse_conn_put);
671
fuse_conn_get(struct fuse_conn * fc)672 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
673 {
674 refcount_inc(&fc->count);
675 return fc;
676 }
677 EXPORT_SYMBOL_GPL(fuse_conn_get);
678
fuse_get_root_inode(struct super_block * sb,unsigned mode)679 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
680 {
681 struct fuse_attr attr;
682 memset(&attr, 0, sizeof(attr));
683
684 attr.mode = mode;
685 attr.ino = FUSE_ROOT_ID;
686 attr.nlink = 1;
687 return fuse_iget(sb, 1, 0, &attr, 0, 0);
688 }
689
690 struct fuse_inode_handle {
691 u64 nodeid;
692 u32 generation;
693 };
694
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)695 static struct dentry *fuse_get_dentry(struct super_block *sb,
696 struct fuse_inode_handle *handle)
697 {
698 struct fuse_conn *fc = get_fuse_conn_super(sb);
699 struct inode *inode;
700 struct dentry *entry;
701 int err = -ESTALE;
702
703 if (handle->nodeid == 0)
704 goto out_err;
705
706 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
707 if (!inode) {
708 struct fuse_entry_out outarg;
709 const struct qstr name = QSTR_INIT(".", 1);
710
711 if (!fc->export_support)
712 goto out_err;
713
714 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
715 &inode);
716 if (err && err != -ENOENT)
717 goto out_err;
718 if (err || !inode) {
719 err = -ESTALE;
720 goto out_err;
721 }
722 err = -EIO;
723 if (get_node_id(inode) != handle->nodeid)
724 goto out_iput;
725 }
726 err = -ESTALE;
727 if (inode->i_generation != handle->generation)
728 goto out_iput;
729
730 entry = d_obtain_alias(inode);
731 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
732 fuse_invalidate_entry_cache(entry);
733
734 return entry;
735
736 out_iput:
737 iput(inode);
738 out_err:
739 return ERR_PTR(err);
740 }
741
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)742 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
743 struct inode *parent)
744 {
745 int len = parent ? 6 : 3;
746 u64 nodeid;
747 u32 generation;
748
749 if (*max_len < len) {
750 *max_len = len;
751 return FILEID_INVALID;
752 }
753
754 nodeid = get_fuse_inode(inode)->nodeid;
755 generation = inode->i_generation;
756
757 fh[0] = (u32)(nodeid >> 32);
758 fh[1] = (u32)(nodeid & 0xffffffff);
759 fh[2] = generation;
760
761 if (parent) {
762 nodeid = get_fuse_inode(parent)->nodeid;
763 generation = parent->i_generation;
764
765 fh[3] = (u32)(nodeid >> 32);
766 fh[4] = (u32)(nodeid & 0xffffffff);
767 fh[5] = generation;
768 }
769
770 *max_len = len;
771 return parent ? 0x82 : 0x81;
772 }
773
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)774 static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
775 struct fid *fid, int fh_len, int fh_type)
776 {
777 struct fuse_inode_handle handle;
778
779 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
780 return NULL;
781
782 handle.nodeid = (u64) fid->raw[0] << 32;
783 handle.nodeid |= (u64) fid->raw[1];
784 handle.generation = fid->raw[2];
785 return fuse_get_dentry(sb, &handle);
786 }
787
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)788 static struct dentry *fuse_fh_to_parent(struct super_block *sb,
789 struct fid *fid, int fh_len, int fh_type)
790 {
791 struct fuse_inode_handle parent;
792
793 if (fh_type != 0x82 || fh_len < 6)
794 return NULL;
795
796 parent.nodeid = (u64) fid->raw[3] << 32;
797 parent.nodeid |= (u64) fid->raw[4];
798 parent.generation = fid->raw[5];
799 return fuse_get_dentry(sb, &parent);
800 }
801
fuse_get_parent(struct dentry * child)802 static struct dentry *fuse_get_parent(struct dentry *child)
803 {
804 struct inode *child_inode = d_inode(child);
805 struct fuse_conn *fc = get_fuse_conn(child_inode);
806 struct inode *inode;
807 struct dentry *parent;
808 struct fuse_entry_out outarg;
809 const struct qstr name = QSTR_INIT("..", 2);
810 int err;
811
812 if (!fc->export_support)
813 return ERR_PTR(-ESTALE);
814
815 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
816 &name, &outarg, &inode);
817 if (err) {
818 if (err == -ENOENT)
819 return ERR_PTR(-ESTALE);
820 return ERR_PTR(err);
821 }
822
823 parent = d_obtain_alias(inode);
824 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
825 fuse_invalidate_entry_cache(parent);
826
827 return parent;
828 }
829
830 static const struct export_operations fuse_export_operations = {
831 .fh_to_dentry = fuse_fh_to_dentry,
832 .fh_to_parent = fuse_fh_to_parent,
833 .encode_fh = fuse_encode_fh,
834 .get_parent = fuse_get_parent,
835 };
836
837 static const struct super_operations fuse_super_operations = {
838 .alloc_inode = fuse_alloc_inode,
839 .free_inode = fuse_free_inode,
840 .evict_inode = fuse_evict_inode,
841 .write_inode = fuse_write_inode,
842 .drop_inode = generic_delete_inode,
843 .put_super = fuse_put_super,
844 .umount_begin = fuse_umount_begin,
845 .statfs = fuse_statfs,
846 .show_options = fuse_show_options,
847 };
848
sanitize_global_limit(unsigned * limit)849 static void sanitize_global_limit(unsigned *limit)
850 {
851 /*
852 * The default maximum number of async requests is calculated to consume
853 * 1/2^13 of the total memory, assuming 392 bytes per request.
854 */
855 if (*limit == 0)
856 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
857
858 if (*limit >= 1 << 16)
859 *limit = (1 << 16) - 1;
860 }
861
set_global_limit(const char * val,const struct kernel_param * kp)862 static int set_global_limit(const char *val, const struct kernel_param *kp)
863 {
864 int rv;
865
866 rv = param_set_uint(val, kp);
867 if (rv)
868 return rv;
869
870 sanitize_global_limit((unsigned *)kp->arg);
871
872 return 0;
873 }
874
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)875 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
876 {
877 int cap_sys_admin = capable(CAP_SYS_ADMIN);
878
879 if (arg->minor < 13)
880 return;
881
882 sanitize_global_limit(&max_user_bgreq);
883 sanitize_global_limit(&max_user_congthresh);
884
885 spin_lock(&fc->bg_lock);
886 if (arg->max_background) {
887 fc->max_background = arg->max_background;
888
889 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
890 fc->max_background = max_user_bgreq;
891 }
892 if (arg->congestion_threshold) {
893 fc->congestion_threshold = arg->congestion_threshold;
894
895 if (!cap_sys_admin &&
896 fc->congestion_threshold > max_user_congthresh)
897 fc->congestion_threshold = max_user_congthresh;
898 }
899 spin_unlock(&fc->bg_lock);
900 }
901
902 struct fuse_init_args {
903 struct fuse_args args;
904 struct fuse_init_in in;
905 struct fuse_init_out out;
906 };
907
process_init_reply(struct fuse_conn * fc,struct fuse_args * args,int error)908 static void process_init_reply(struct fuse_conn *fc, struct fuse_args *args,
909 int error)
910 {
911 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
912 struct fuse_init_out *arg = &ia->out;
913
914 if (error || arg->major != FUSE_KERNEL_VERSION)
915 fc->conn_error = 1;
916 else {
917 unsigned long ra_pages;
918
919 process_init_limits(fc, arg);
920
921 if (arg->minor >= 6) {
922 ra_pages = arg->max_readahead / PAGE_SIZE;
923 if (arg->flags & FUSE_ASYNC_READ)
924 fc->async_read = 1;
925 if (!(arg->flags & FUSE_POSIX_LOCKS))
926 fc->no_lock = 1;
927 if (arg->minor >= 17) {
928 if (!(arg->flags & FUSE_FLOCK_LOCKS))
929 fc->no_flock = 1;
930 } else {
931 if (!(arg->flags & FUSE_POSIX_LOCKS))
932 fc->no_flock = 1;
933 }
934 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
935 fc->atomic_o_trunc = 1;
936 if (arg->minor >= 9) {
937 /* LOOKUP has dependency on proto version */
938 if (arg->flags & FUSE_EXPORT_SUPPORT)
939 fc->export_support = 1;
940 }
941 if (arg->flags & FUSE_BIG_WRITES)
942 fc->big_writes = 1;
943 if (arg->flags & FUSE_DONT_MASK)
944 fc->dont_mask = 1;
945 if (arg->flags & FUSE_AUTO_INVAL_DATA)
946 fc->auto_inval_data = 1;
947 else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
948 fc->explicit_inval_data = 1;
949 if (arg->flags & FUSE_DO_READDIRPLUS) {
950 fc->do_readdirplus = 1;
951 if (arg->flags & FUSE_READDIRPLUS_AUTO)
952 fc->readdirplus_auto = 1;
953 }
954 if (arg->flags & FUSE_ASYNC_DIO)
955 fc->async_dio = 1;
956 if (arg->flags & FUSE_WRITEBACK_CACHE)
957 fc->writeback_cache = 1;
958 if (arg->flags & FUSE_PARALLEL_DIROPS)
959 fc->parallel_dirops = 1;
960 if (arg->flags & FUSE_HANDLE_KILLPRIV)
961 fc->handle_killpriv = 1;
962 if (arg->time_gran && arg->time_gran <= 1000000000)
963 fc->sb->s_time_gran = arg->time_gran;
964 if ((arg->flags & FUSE_POSIX_ACL)) {
965 fc->default_permissions = 1;
966 fc->posix_acl = 1;
967 fc->sb->s_xattr = fuse_acl_xattr_handlers;
968 }
969 if (arg->flags & FUSE_CACHE_SYMLINKS)
970 fc->cache_symlinks = 1;
971 if (arg->flags & FUSE_ABORT_ERROR)
972 fc->abort_err = 1;
973 if (arg->flags & FUSE_MAX_PAGES) {
974 fc->max_pages =
975 min_t(unsigned int, FUSE_MAX_MAX_PAGES,
976 max_t(unsigned int, arg->max_pages, 1));
977 }
978 } else {
979 ra_pages = fc->max_read / PAGE_SIZE;
980 fc->no_lock = 1;
981 fc->no_flock = 1;
982 }
983
984 fc->sb->s_bdi->ra_pages =
985 min(fc->sb->s_bdi->ra_pages, ra_pages);
986 fc->minor = arg->minor;
987 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
988 fc->max_write = max_t(unsigned, 4096, fc->max_write);
989 fc->conn_init = 1;
990 }
991 kfree(ia);
992
993 fuse_set_initialized(fc);
994 wake_up_all(&fc->blocked_waitq);
995 }
996
fuse_send_init(struct fuse_conn * fc)997 void fuse_send_init(struct fuse_conn *fc)
998 {
999 struct fuse_init_args *ia;
1000
1001 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1002
1003 ia->in.major = FUSE_KERNEL_VERSION;
1004 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1005 ia->in.max_readahead = fc->sb->s_bdi->ra_pages * PAGE_SIZE;
1006 ia->in.flags |=
1007 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1008 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1009 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1010 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1011 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1012 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1013 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1014 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1015 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA;
1016 ia->args.opcode = FUSE_INIT;
1017 ia->args.in_numargs = 1;
1018 ia->args.in_args[0].size = sizeof(ia->in);
1019 ia->args.in_args[0].value = &ia->in;
1020 ia->args.out_numargs = 1;
1021 /* Variable length argument used for backward compatibility
1022 with interface version < 7.5. Rest of init_out is zeroed
1023 by do_get_request(), so a short reply is not a problem */
1024 ia->args.out_argvar = 1;
1025 ia->args.out_args[0].size = sizeof(ia->out);
1026 ia->args.out_args[0].value = &ia->out;
1027 ia->args.force = true;
1028 ia->args.nocreds = true;
1029 ia->args.end = process_init_reply;
1030
1031 if (fuse_simple_background(fc, &ia->args, GFP_KERNEL) != 0)
1032 process_init_reply(fc, &ia->args, -ENOTCONN);
1033 }
1034 EXPORT_SYMBOL_GPL(fuse_send_init);
1035
fuse_free_conn(struct fuse_conn * fc)1036 void fuse_free_conn(struct fuse_conn *fc)
1037 {
1038 WARN_ON(!list_empty(&fc->devices));
1039 kfree_rcu(fc, rcu);
1040 }
1041 EXPORT_SYMBOL_GPL(fuse_free_conn);
1042
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1043 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1044 {
1045 int err;
1046 char *suffix = "";
1047
1048 if (sb->s_bdev) {
1049 suffix = "-fuseblk";
1050 /*
1051 * sb->s_bdi points to blkdev's bdi however we want to redirect
1052 * it to our private bdi...
1053 */
1054 bdi_put(sb->s_bdi);
1055 sb->s_bdi = &noop_backing_dev_info;
1056 }
1057 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1058 MINOR(fc->dev), suffix);
1059 if (err)
1060 return err;
1061
1062 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1063 /* fuse does it's own writeback accounting */
1064 sb->s_bdi->capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
1065
1066 /*
1067 * For a single fuse filesystem use max 1% of dirty +
1068 * writeback threshold.
1069 *
1070 * This gives about 1M of write buffer for memory maps on a
1071 * machine with 1G and 10% dirty_ratio, which should be more
1072 * than enough.
1073 *
1074 * Privileged users can raise it by writing to
1075 *
1076 * /sys/class/bdi/<bdi>/max_ratio
1077 */
1078 bdi_set_max_ratio(sb->s_bdi, 1);
1079
1080 return 0;
1081 }
1082
fuse_dev_alloc(void)1083 struct fuse_dev *fuse_dev_alloc(void)
1084 {
1085 struct fuse_dev *fud;
1086 struct list_head *pq;
1087
1088 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1089 if (!fud)
1090 return NULL;
1091
1092 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1093 if (!pq) {
1094 kfree(fud);
1095 return NULL;
1096 }
1097
1098 fud->pq.processing = pq;
1099 fuse_pqueue_init(&fud->pq);
1100
1101 return fud;
1102 }
1103 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1104
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1105 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1106 {
1107 fud->fc = fuse_conn_get(fc);
1108 spin_lock(&fc->lock);
1109 list_add_tail(&fud->entry, &fc->devices);
1110 spin_unlock(&fc->lock);
1111 }
1112 EXPORT_SYMBOL_GPL(fuse_dev_install);
1113
fuse_dev_alloc_install(struct fuse_conn * fc)1114 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1115 {
1116 struct fuse_dev *fud;
1117
1118 fud = fuse_dev_alloc();
1119 if (!fud)
1120 return NULL;
1121
1122 fuse_dev_install(fud, fc);
1123 return fud;
1124 }
1125 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1126
fuse_dev_free(struct fuse_dev * fud)1127 void fuse_dev_free(struct fuse_dev *fud)
1128 {
1129 struct fuse_conn *fc = fud->fc;
1130
1131 if (fc) {
1132 spin_lock(&fc->lock);
1133 list_del(&fud->entry);
1134 spin_unlock(&fc->lock);
1135
1136 fuse_conn_put(fc);
1137 }
1138 kfree(fud->pq.processing);
1139 kfree(fud);
1140 }
1141 EXPORT_SYMBOL_GPL(fuse_dev_free);
1142
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1143 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1144 {
1145 struct fuse_dev *fud;
1146 struct fuse_conn *fc = get_fuse_conn_super(sb);
1147 struct inode *root;
1148 struct dentry *root_dentry;
1149 int err;
1150
1151 err = -EINVAL;
1152 if (sb->s_flags & SB_MANDLOCK)
1153 goto err;
1154
1155 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1156
1157 if (ctx->is_bdev) {
1158 #ifdef CONFIG_BLOCK
1159 err = -EINVAL;
1160 if (!sb_set_blocksize(sb, ctx->blksize))
1161 goto err;
1162 #endif
1163 } else {
1164 sb->s_blocksize = PAGE_SIZE;
1165 sb->s_blocksize_bits = PAGE_SHIFT;
1166 }
1167
1168 sb->s_subtype = ctx->subtype;
1169 ctx->subtype = NULL;
1170 sb->s_magic = FUSE_SUPER_MAGIC;
1171 sb->s_op = &fuse_super_operations;
1172 sb->s_xattr = fuse_xattr_handlers;
1173 sb->s_maxbytes = MAX_LFS_FILESIZE;
1174 sb->s_time_gran = 1;
1175 sb->s_export_op = &fuse_export_operations;
1176 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1177 if (sb->s_user_ns != &init_user_ns)
1178 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1179
1180 /*
1181 * If we are not in the initial user namespace posix
1182 * acls must be translated.
1183 */
1184 if (sb->s_user_ns != &init_user_ns)
1185 sb->s_xattr = fuse_no_acl_xattr_handlers;
1186
1187 fud = fuse_dev_alloc_install(fc);
1188 if (!fud)
1189 goto err;
1190
1191 fc->dev = sb->s_dev;
1192 fc->sb = sb;
1193 err = fuse_bdi_init(fc, sb);
1194 if (err)
1195 goto err_dev_free;
1196
1197 /* Handle umasking inside the fuse code */
1198 if (sb->s_flags & SB_POSIXACL)
1199 fc->dont_mask = 1;
1200 sb->s_flags |= SB_POSIXACL;
1201
1202 fc->default_permissions = ctx->default_permissions;
1203 fc->allow_other = ctx->allow_other;
1204 fc->user_id = ctx->user_id;
1205 fc->group_id = ctx->group_id;
1206 fc->max_read = max_t(unsigned, 4096, ctx->max_read);
1207 fc->destroy = ctx->destroy;
1208 fc->no_control = ctx->no_control;
1209 fc->no_force_umount = ctx->no_force_umount;
1210 fc->no_mount_options = ctx->no_mount_options;
1211
1212 err = -ENOMEM;
1213 root = fuse_get_root_inode(sb, ctx->rootmode);
1214 sb->s_d_op = &fuse_root_dentry_operations;
1215 root_dentry = d_make_root(root);
1216 if (!root_dentry)
1217 goto err_dev_free;
1218 /* Root dentry doesn't have .d_revalidate */
1219 sb->s_d_op = &fuse_dentry_operations;
1220
1221 mutex_lock(&fuse_mutex);
1222 err = -EINVAL;
1223 if (*ctx->fudptr)
1224 goto err_unlock;
1225
1226 err = fuse_ctl_add_conn(fc);
1227 if (err)
1228 goto err_unlock;
1229
1230 list_add_tail(&fc->entry, &fuse_conn_list);
1231 sb->s_root = root_dentry;
1232 *ctx->fudptr = fud;
1233 mutex_unlock(&fuse_mutex);
1234 return 0;
1235
1236 err_unlock:
1237 mutex_unlock(&fuse_mutex);
1238 dput(root_dentry);
1239 err_dev_free:
1240 fuse_dev_free(fud);
1241 err:
1242 return err;
1243 }
1244 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1245
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1246 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1247 {
1248 struct fuse_fs_context *ctx = fsc->fs_private;
1249 struct file *file;
1250 int err;
1251 struct fuse_conn *fc;
1252
1253 err = -EINVAL;
1254 file = fget(ctx->fd);
1255 if (!file)
1256 goto err;
1257
1258 /*
1259 * Require mount to happen from the same user namespace which
1260 * opened /dev/fuse to prevent potential attacks.
1261 */
1262 if ((file->f_op != &fuse_dev_operations) ||
1263 (file->f_cred->user_ns != sb->s_user_ns))
1264 goto err_fput;
1265 ctx->fudptr = &file->private_data;
1266
1267 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1268 err = -ENOMEM;
1269 if (!fc)
1270 goto err_fput;
1271
1272 fuse_conn_init(fc, sb->s_user_ns, &fuse_dev_fiq_ops, NULL);
1273 fc->release = fuse_free_conn;
1274 sb->s_fs_info = fc;
1275
1276 err = fuse_fill_super_common(sb, ctx);
1277 if (err)
1278 goto err_put_conn;
1279 /*
1280 * atomic_dec_and_test() in fput() provides the necessary
1281 * memory barrier for file->private_data to be visible on all
1282 * CPUs after this
1283 */
1284 fput(file);
1285 fuse_send_init(get_fuse_conn_super(sb));
1286 return 0;
1287
1288 err_put_conn:
1289 fuse_conn_put(fc);
1290 sb->s_fs_info = NULL;
1291 err_fput:
1292 fput(file);
1293 err:
1294 return err;
1295 }
1296
fuse_get_tree(struct fs_context * fc)1297 static int fuse_get_tree(struct fs_context *fc)
1298 {
1299 struct fuse_fs_context *ctx = fc->fs_private;
1300
1301 if (!ctx->fd_present || !ctx->rootmode_present ||
1302 !ctx->user_id_present || !ctx->group_id_present)
1303 return -EINVAL;
1304
1305 #ifdef CONFIG_BLOCK
1306 if (ctx->is_bdev)
1307 return get_tree_bdev(fc, fuse_fill_super);
1308 #endif
1309
1310 return get_tree_nodev(fc, fuse_fill_super);
1311 }
1312
1313 static const struct fs_context_operations fuse_context_ops = {
1314 .free = fuse_free_fc,
1315 .parse_param = fuse_parse_param,
1316 .reconfigure = fuse_reconfigure,
1317 .get_tree = fuse_get_tree,
1318 };
1319
1320 /*
1321 * Set up the filesystem mount context.
1322 */
fuse_init_fs_context(struct fs_context * fc)1323 static int fuse_init_fs_context(struct fs_context *fc)
1324 {
1325 struct fuse_fs_context *ctx;
1326
1327 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1328 if (!ctx)
1329 return -ENOMEM;
1330
1331 ctx->max_read = ~0;
1332 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1333
1334 #ifdef CONFIG_BLOCK
1335 if (fc->fs_type == &fuseblk_fs_type) {
1336 ctx->is_bdev = true;
1337 ctx->destroy = true;
1338 }
1339 #endif
1340
1341 fc->fs_private = ctx;
1342 fc->ops = &fuse_context_ops;
1343 return 0;
1344 }
1345
fuse_sb_destroy(struct super_block * sb)1346 static void fuse_sb_destroy(struct super_block *sb)
1347 {
1348 struct fuse_conn *fc = get_fuse_conn_super(sb);
1349
1350 if (fc) {
1351 if (fc->destroy)
1352 fuse_send_destroy(fc);
1353
1354 fuse_abort_conn(fc);
1355 fuse_wait_aborted(fc);
1356
1357 down_write(&fc->killsb);
1358 fc->sb = NULL;
1359 up_write(&fc->killsb);
1360 }
1361 }
1362
fuse_kill_sb_anon(struct super_block * sb)1363 void fuse_kill_sb_anon(struct super_block *sb)
1364 {
1365 fuse_sb_destroy(sb);
1366 kill_anon_super(sb);
1367 }
1368 EXPORT_SYMBOL_GPL(fuse_kill_sb_anon);
1369
1370 static struct file_system_type fuse_fs_type = {
1371 .owner = THIS_MODULE,
1372 .name = "fuse",
1373 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
1374 .init_fs_context = fuse_init_fs_context,
1375 .parameters = &fuse_fs_parameters,
1376 .kill_sb = fuse_kill_sb_anon,
1377 };
1378 MODULE_ALIAS_FS("fuse");
1379
1380 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)1381 static void fuse_kill_sb_blk(struct super_block *sb)
1382 {
1383 fuse_sb_destroy(sb);
1384 kill_block_super(sb);
1385 }
1386
1387 static struct file_system_type fuseblk_fs_type = {
1388 .owner = THIS_MODULE,
1389 .name = "fuseblk",
1390 .init_fs_context = fuse_init_fs_context,
1391 .parameters = &fuse_fs_parameters,
1392 .kill_sb = fuse_kill_sb_blk,
1393 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
1394 };
1395 MODULE_ALIAS_FS("fuseblk");
1396
register_fuseblk(void)1397 static inline int register_fuseblk(void)
1398 {
1399 return register_filesystem(&fuseblk_fs_type);
1400 }
1401
unregister_fuseblk(void)1402 static inline void unregister_fuseblk(void)
1403 {
1404 unregister_filesystem(&fuseblk_fs_type);
1405 }
1406 #else
register_fuseblk(void)1407 static inline int register_fuseblk(void)
1408 {
1409 return 0;
1410 }
1411
unregister_fuseblk(void)1412 static inline void unregister_fuseblk(void)
1413 {
1414 }
1415 #endif
1416
fuse_inode_init_once(void * foo)1417 static void fuse_inode_init_once(void *foo)
1418 {
1419 struct inode *inode = foo;
1420
1421 inode_init_once(inode);
1422 }
1423
fuse_fs_init(void)1424 static int __init fuse_fs_init(void)
1425 {
1426 int err;
1427
1428 fuse_inode_cachep = kmem_cache_create("fuse_inode",
1429 sizeof(struct fuse_inode), 0,
1430 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
1431 fuse_inode_init_once);
1432 err = -ENOMEM;
1433 if (!fuse_inode_cachep)
1434 goto out;
1435
1436 err = register_fuseblk();
1437 if (err)
1438 goto out2;
1439
1440 err = register_filesystem(&fuse_fs_type);
1441 if (err)
1442 goto out3;
1443
1444 return 0;
1445
1446 out3:
1447 unregister_fuseblk();
1448 out2:
1449 kmem_cache_destroy(fuse_inode_cachep);
1450 out:
1451 return err;
1452 }
1453
fuse_fs_cleanup(void)1454 static void fuse_fs_cleanup(void)
1455 {
1456 unregister_filesystem(&fuse_fs_type);
1457 unregister_fuseblk();
1458
1459 /*
1460 * Make sure all delayed rcu free inodes are flushed before we
1461 * destroy cache.
1462 */
1463 rcu_barrier();
1464 kmem_cache_destroy(fuse_inode_cachep);
1465 }
1466
1467 static struct kobject *fuse_kobj;
1468
fuse_sysfs_init(void)1469 static int fuse_sysfs_init(void)
1470 {
1471 int err;
1472
1473 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
1474 if (!fuse_kobj) {
1475 err = -ENOMEM;
1476 goto out_err;
1477 }
1478
1479 err = sysfs_create_mount_point(fuse_kobj, "connections");
1480 if (err)
1481 goto out_fuse_unregister;
1482
1483 return 0;
1484
1485 out_fuse_unregister:
1486 kobject_put(fuse_kobj);
1487 out_err:
1488 return err;
1489 }
1490
fuse_sysfs_cleanup(void)1491 static void fuse_sysfs_cleanup(void)
1492 {
1493 sysfs_remove_mount_point(fuse_kobj, "connections");
1494 kobject_put(fuse_kobj);
1495 }
1496
fuse_init(void)1497 static int __init fuse_init(void)
1498 {
1499 int res;
1500
1501 pr_info("init (API version %i.%i)\n",
1502 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1503
1504 INIT_LIST_HEAD(&fuse_conn_list);
1505 res = fuse_fs_init();
1506 if (res)
1507 goto err;
1508
1509 res = fuse_dev_init();
1510 if (res)
1511 goto err_fs_cleanup;
1512
1513 res = fuse_sysfs_init();
1514 if (res)
1515 goto err_dev_cleanup;
1516
1517 res = fuse_ctl_init();
1518 if (res)
1519 goto err_sysfs_cleanup;
1520
1521 sanitize_global_limit(&max_user_bgreq);
1522 sanitize_global_limit(&max_user_congthresh);
1523
1524 return 0;
1525
1526 err_sysfs_cleanup:
1527 fuse_sysfs_cleanup();
1528 err_dev_cleanup:
1529 fuse_dev_cleanup();
1530 err_fs_cleanup:
1531 fuse_fs_cleanup();
1532 err:
1533 return res;
1534 }
1535
fuse_exit(void)1536 static void __exit fuse_exit(void)
1537 {
1538 pr_debug("exit\n");
1539
1540 fuse_ctl_cleanup();
1541 fuse_sysfs_cleanup();
1542 fuse_fs_cleanup();
1543 fuse_dev_cleanup();
1544 }
1545
1546 module_init(fuse_init);
1547 module_exit(fuse_exit);
1548