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 MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
31
32 static struct kmem_cache *fuse_inode_cachep;
33 struct list_head fuse_conn_list;
34 DEFINE_MUTEX(fuse_mutex);
35
36 static int set_global_limit(const char *val, const struct kernel_param *kp);
37
38 unsigned max_user_bgreq;
39 module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
40 &max_user_bgreq, 0644);
41 __MODULE_PARM_TYPE(max_user_bgreq, "uint");
42 MODULE_PARM_DESC(max_user_bgreq,
43 "Global limit for the maximum number of backgrounded requests an "
44 "unprivileged user can set");
45
46 unsigned max_user_congthresh;
47 module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
48 &max_user_congthresh, 0644);
49 __MODULE_PARM_TYPE(max_user_congthresh, "uint");
50 MODULE_PARM_DESC(max_user_congthresh,
51 "Global limit for the maximum congestion threshold an "
52 "unprivileged user can set");
53
54 #define FUSE_DEFAULT_BLKSIZE 512
55
56 /** Maximum number of outstanding background requests */
57 #define FUSE_DEFAULT_MAX_BACKGROUND 12
58
59 /** Congestion starts at 75% of maximum */
60 #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
61
62 #ifdef CONFIG_BLOCK
63 static struct file_system_type fuseblk_fs_type;
64 #endif
65
fuse_alloc_forget(void)66 struct fuse_forget_link *fuse_alloc_forget(void)
67 {
68 return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
69 }
70
fuse_alloc_submount_lookup(void)71 static struct fuse_submount_lookup *fuse_alloc_submount_lookup(void)
72 {
73 struct fuse_submount_lookup *sl;
74
75 sl = kzalloc(sizeof(struct fuse_submount_lookup), GFP_KERNEL_ACCOUNT);
76 if (!sl)
77 return NULL;
78 sl->forget = fuse_alloc_forget();
79 if (!sl->forget)
80 goto out_free;
81
82 return sl;
83
84 out_free:
85 kfree(sl);
86 return NULL;
87 }
88
fuse_alloc_inode(struct super_block * sb)89 static struct inode *fuse_alloc_inode(struct super_block *sb)
90 {
91 struct fuse_inode *fi;
92
93 fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
94 if (!fi)
95 return NULL;
96
97 fi->i_time = 0;
98 fi->inval_mask = 0;
99 #ifdef CONFIG_FUSE_BPF
100 fi->backing_inode = NULL;
101 fi->bpf = NULL;
102 #endif
103 fi->nodeid = 0;
104 fi->nlookup = 0;
105 fi->attr_version = 0;
106 fi->orig_ino = 0;
107 fi->state = 0;
108 fi->submount_lookup = NULL;
109 mutex_init(&fi->mutex);
110 spin_lock_init(&fi->lock);
111 fi->forget = fuse_alloc_forget();
112 if (!fi->forget)
113 goto out_free;
114
115 if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
116 goto out_free_forget;
117
118 return &fi->inode;
119
120 out_free_forget:
121 kfree(fi->forget);
122 out_free:
123 kmem_cache_free(fuse_inode_cachep, fi);
124 return NULL;
125 }
126
fuse_free_inode(struct inode * inode)127 static void fuse_free_inode(struct inode *inode)
128 {
129 struct fuse_inode *fi = get_fuse_inode(inode);
130
131 mutex_destroy(&fi->mutex);
132 kfree(fi->forget);
133 #ifdef CONFIG_FUSE_DAX
134 kfree(fi->dax);
135 #endif
136 #ifdef CONFIG_FUSE_BPF
137 if (fi->bpf)
138 bpf_prog_put(fi->bpf);
139 #endif
140 kmem_cache_free(fuse_inode_cachep, fi);
141 }
142
fuse_cleanup_submount_lookup(struct fuse_conn * fc,struct fuse_submount_lookup * sl)143 static void fuse_cleanup_submount_lookup(struct fuse_conn *fc,
144 struct fuse_submount_lookup *sl)
145 {
146 if (!refcount_dec_and_test(&sl->count))
147 return;
148
149 fuse_queue_forget(fc, sl->forget, sl->nodeid, 1);
150 sl->forget = NULL;
151 kfree(sl);
152 }
153
fuse_evict_inode(struct inode * inode)154 static void fuse_evict_inode(struct inode *inode)
155 {
156 struct fuse_inode *fi = get_fuse_inode(inode);
157
158 /* Will write inode on close/munmap and in all other dirtiers */
159 WARN_ON(inode->i_state & I_DIRTY_INODE);
160 truncate_inode_pages_final(&inode->i_data);
161 clear_inode(inode);
162 if (inode->i_sb->s_flags & SB_ACTIVE) {
163 struct fuse_conn *fc = get_fuse_conn(inode);
164
165 if (FUSE_IS_DAX(inode))
166 fuse_dax_inode_cleanup(inode);
167 if (fi->nlookup) {
168 fuse_queue_forget(fc, fi->forget, fi->nodeid,
169 fi->nlookup);
170 fi->forget = NULL;
171 }
172
173 if (fi->submount_lookup) {
174 fuse_cleanup_submount_lookup(fc, fi->submount_lookup);
175 fi->submount_lookup = NULL;
176 }
177 }
178 if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
179 WARN_ON(!list_empty(&fi->write_files));
180 WARN_ON(!list_empty(&fi->queued_writes));
181 }
182 }
183
184 #ifdef CONFIG_FUSE_BPF
fuse_destroy_inode(struct inode * inode)185 static void fuse_destroy_inode(struct inode *inode)
186 {
187 struct fuse_inode *fi = get_fuse_inode(inode);
188
189 iput(fi->backing_inode);
190 }
191 #endif
192
fuse_reconfigure(struct fs_context * fsc)193 static int fuse_reconfigure(struct fs_context *fsc)
194 {
195 struct super_block *sb = fsc->root->d_sb;
196
197 sync_filesystem(sb);
198 if (fsc->sb_flags & SB_MANDLOCK)
199 return -EINVAL;
200
201 return 0;
202 }
203
204 /*
205 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
206 * so that it will fit.
207 */
fuse_squash_ino(u64 ino64)208 static ino_t fuse_squash_ino(u64 ino64)
209 {
210 ino_t ino = (ino_t) ino64;
211 if (sizeof(ino_t) < sizeof(u64))
212 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
213 return ino;
214 }
215
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct inode * inode)216 static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
217 const struct inode *inode)
218 {
219 *attr = (struct fuse_attr){
220 .ino = inode->i_ino,
221 .size = inode->i_size,
222 .blocks = inode->i_blocks,
223 .atime = inode->i_atime.tv_sec,
224 .mtime = inode->i_mtime.tv_sec,
225 .ctime = inode->i_ctime.tv_sec,
226 .atimensec = inode->i_atime.tv_nsec,
227 .mtimensec = inode->i_mtime.tv_nsec,
228 .ctimensec = inode->i_ctime.tv_nsec,
229 .mode = inode->i_mode,
230 .nlink = inode->i_nlink,
231 .uid = inode->i_uid.val,
232 .gid = inode->i_gid.val,
233 .rdev = inode->i_rdev,
234 .blksize = 1u << inode->i_blkbits,
235 };
236 }
237
fuse_change_attributes_common(struct inode * inode,struct fuse_attr * attr,u64 attr_valid)238 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
239 u64 attr_valid)
240 {
241 struct fuse_conn *fc = get_fuse_conn(inode);
242 struct fuse_inode *fi = get_fuse_inode(inode);
243
244 lockdep_assert_held(&fi->lock);
245
246 fi->attr_version = atomic64_inc_return(&fc->attr_version);
247 fi->i_time = attr_valid;
248 WRITE_ONCE(fi->inval_mask, 0);
249
250 inode->i_ino = fuse_squash_ino(attr->ino);
251 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
252 set_nlink(inode, attr->nlink);
253 inode->i_uid = make_kuid(fc->user_ns, attr->uid);
254 inode->i_gid = make_kgid(fc->user_ns, attr->gid);
255 inode->i_blocks = attr->blocks;
256
257 /* Sanitize nsecs */
258 attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
259 attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
260 attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
261
262 inode->i_atime.tv_sec = attr->atime;
263 inode->i_atime.tv_nsec = attr->atimensec;
264 /* mtime from server may be stale due to local buffered write */
265 if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
266 inode->i_mtime.tv_sec = attr->mtime;
267 inode->i_mtime.tv_nsec = attr->mtimensec;
268 inode->i_ctime.tv_sec = attr->ctime;
269 inode->i_ctime.tv_nsec = attr->ctimensec;
270 }
271
272 if (attr->blksize != 0)
273 inode->i_blkbits = ilog2(attr->blksize);
274 else
275 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
276
277 /*
278 * Don't set the sticky bit in i_mode, unless we want the VFS
279 * to check permissions. This prevents failures due to the
280 * check in may_delete().
281 */
282 fi->orig_i_mode = inode->i_mode;
283 if (!fc->default_permissions)
284 inode->i_mode &= ~S_ISVTX;
285
286 fi->orig_ino = attr->ino;
287
288 /*
289 * We are refreshing inode data and it is possible that another
290 * client set suid/sgid or security.capability xattr. So clear
291 * S_NOSEC. Ideally, we could have cleared it only if suid/sgid
292 * was set or if security.capability xattr was set. But we don't
293 * know if security.capability has been set or not. So clear it
294 * anyway. Its less efficient but should be safe.
295 */
296 inode->i_flags &= ~S_NOSEC;
297 }
298
fuse_change_attributes(struct inode * inode,struct fuse_attr * attr,u64 attr_valid,u64 attr_version)299 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
300 u64 attr_valid, u64 attr_version)
301 {
302 struct fuse_conn *fc = get_fuse_conn(inode);
303 struct fuse_inode *fi = get_fuse_inode(inode);
304 bool is_wb = fc->writeback_cache;
305 loff_t oldsize;
306 struct timespec64 old_mtime;
307
308 spin_lock(&fi->lock);
309 if ((attr_version != 0 && fi->attr_version > attr_version) ||
310 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
311 spin_unlock(&fi->lock);
312 return;
313 }
314
315 old_mtime = inode->i_mtime;
316 fuse_change_attributes_common(inode, attr, attr_valid);
317
318 oldsize = inode->i_size;
319 /*
320 * In case of writeback_cache enabled, the cached writes beyond EOF
321 * extend local i_size without keeping userspace server in sync. So,
322 * attr->size coming from server can be stale. We cannot trust it.
323 */
324 if (!is_wb || !S_ISREG(inode->i_mode))
325 i_size_write(inode, attr->size);
326 spin_unlock(&fi->lock);
327
328 if (!is_wb && S_ISREG(inode->i_mode)) {
329 bool inval = false;
330
331 if (oldsize != attr->size) {
332 truncate_pagecache(inode, attr->size);
333 if (!fc->explicit_inval_data)
334 inval = true;
335 } else if (fc->auto_inval_data) {
336 struct timespec64 new_mtime = {
337 .tv_sec = attr->mtime,
338 .tv_nsec = attr->mtimensec,
339 };
340
341 /*
342 * Auto inval mode also checks and invalidates if mtime
343 * has changed.
344 */
345 if (!timespec64_equal(&old_mtime, &new_mtime))
346 inval = true;
347 }
348
349 if (inval)
350 invalidate_inode_pages2(inode->i_mapping);
351 }
352 }
353
fuse_init_submount_lookup(struct fuse_submount_lookup * sl,u64 nodeid)354 static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl,
355 u64 nodeid)
356 {
357 sl->nodeid = nodeid;
358 refcount_set(&sl->count, 1);
359 }
360
fuse_init_inode(struct inode * inode,struct fuse_attr * attr)361 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
362 {
363 inode->i_mode = attr->mode & S_IFMT;
364 inode->i_size = attr->size;
365 inode->i_mtime.tv_sec = attr->mtime;
366 inode->i_mtime.tv_nsec = attr->mtimensec;
367 inode->i_ctime.tv_sec = attr->ctime;
368 inode->i_ctime.tv_nsec = attr->ctimensec;
369 if (S_ISREG(inode->i_mode)) {
370 fuse_init_common(inode);
371 fuse_init_file_inode(inode);
372 } else if (S_ISDIR(inode->i_mode))
373 fuse_init_dir(inode);
374 else if (S_ISLNK(inode->i_mode))
375 fuse_init_symlink(inode);
376 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
377 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
378 fuse_init_common(inode);
379 init_special_inode(inode, inode->i_mode, attr->rdev);
380 } else
381 BUG();
382 }
383
384 struct fuse_inode_identifier {
385 u64 nodeid;
386 struct inode *backing_inode;
387 };
388
fuse_inode_eq(struct inode * inode,void * _nodeidp)389 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
390 {
391 struct fuse_inode_identifier *fii =
392 (struct fuse_inode_identifier *) _nodeidp;
393 struct fuse_inode *fi = get_fuse_inode(inode);
394
395 return fii->nodeid == fi->nodeid;
396 }
397
fuse_inode_backing_eq(struct inode * inode,void * _nodeidp)398 static int fuse_inode_backing_eq(struct inode *inode, void *_nodeidp)
399 {
400 struct fuse_inode_identifier *fii =
401 (struct fuse_inode_identifier *) _nodeidp;
402 struct fuse_inode *fi = get_fuse_inode(inode);
403
404 return fii->nodeid == fi->nodeid
405 #ifdef CONFIG_FUSE_BPF
406 && fii->backing_inode == fi->backing_inode
407 #endif
408 ;
409 }
410
fuse_inode_set(struct inode * inode,void * _nodeidp)411 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
412 {
413 struct fuse_inode_identifier *fii =
414 (struct fuse_inode_identifier *) _nodeidp;
415 struct fuse_inode *fi = get_fuse_inode(inode);
416
417 fi->nodeid = fii->nodeid;
418
419 return 0;
420 }
421
fuse_inode_backing_set(struct inode * inode,void * _nodeidp)422 static int fuse_inode_backing_set(struct inode *inode, void *_nodeidp)
423 {
424 struct fuse_inode_identifier *fii =
425 (struct fuse_inode_identifier *) _nodeidp;
426 struct fuse_inode *fi = get_fuse_inode(inode);
427
428 fi->nodeid = fii->nodeid;
429 #ifdef CONFIG_FUSE_BPF
430 fi->backing_inode = fii->backing_inode;
431 if (fi->backing_inode)
432 ihold(fi->backing_inode);
433 #endif
434
435 return 0;
436 }
437
fuse_iget_backing(struct super_block * sb,u64 nodeid,struct inode * backing_inode)438 struct inode *fuse_iget_backing(struct super_block *sb, u64 nodeid,
439 struct inode *backing_inode)
440 {
441 struct inode *inode;
442 struct fuse_inode *fi;
443 struct fuse_conn *fc = get_fuse_conn_super(sb);
444 struct fuse_inode_identifier fii = {
445 .nodeid = nodeid,
446 .backing_inode = backing_inode,
447 };
448 struct fuse_attr attr;
449 unsigned long hash = (unsigned long) backing_inode;
450
451 if (nodeid)
452 hash = nodeid;
453
454 fuse_fill_attr_from_inode(&attr, backing_inode);
455 inode = iget5_locked(sb, hash, fuse_inode_backing_eq,
456 fuse_inode_backing_set, &fii);
457 if (!inode)
458 return NULL;
459
460 if ((inode->i_state & I_NEW)) {
461 inode->i_flags |= S_NOATIME;
462 if (!fc->writeback_cache)
463 inode->i_flags |= S_NOCMTIME;
464 fuse_init_common(inode);
465 unlock_new_inode(inode);
466 }
467
468 fi = get_fuse_inode(inode);
469 fuse_init_inode(inode, &attr);
470 spin_lock(&fi->lock);
471 fi->nlookup++;
472 spin_unlock(&fi->lock);
473
474 return inode;
475 }
476
fuse_iget(struct super_block * sb,u64 nodeid,int generation,struct fuse_attr * attr,u64 attr_valid,u64 attr_version)477 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
478 int generation, struct fuse_attr *attr,
479 u64 attr_valid, u64 attr_version)
480 {
481 struct inode *inode;
482 struct fuse_inode *fi;
483 struct fuse_conn *fc = get_fuse_conn_super(sb);
484 struct fuse_inode_identifier fii = {
485 .nodeid = nodeid,
486 };
487
488 /*
489 * Auto mount points get their node id from the submount root, which is
490 * not a unique identifier within this filesystem.
491 *
492 * To avoid conflicts, do not place submount points into the inode hash
493 * table.
494 */
495 if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
496 S_ISDIR(attr->mode)) {
497 struct fuse_inode *fi;
498
499 inode = new_inode(sb);
500 if (!inode)
501 return NULL;
502
503 fuse_init_inode(inode, attr);
504 fi = get_fuse_inode(inode);
505 fi->nodeid = nodeid;
506 fi->submount_lookup = fuse_alloc_submount_lookup();
507 if (!fi->submount_lookup) {
508 iput(inode);
509 return NULL;
510 }
511 /* Sets nlookup = 1 on fi->submount_lookup->nlookup */
512 fuse_init_submount_lookup(fi->submount_lookup, nodeid);
513 inode->i_flags |= S_AUTOMOUNT;
514 goto done;
515 }
516
517 retry:
518 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &fii);
519 if (!inode)
520 return NULL;
521
522 if ((inode->i_state & I_NEW)) {
523 inode->i_flags |= S_NOATIME;
524 if (!fc->writeback_cache || !S_ISREG(attr->mode))
525 inode->i_flags |= S_NOCMTIME;
526 inode->i_generation = generation;
527 fuse_init_inode(inode, attr);
528 unlock_new_inode(inode);
529 } else if (fuse_stale_inode(inode, generation, attr)) {
530 /* nodeid was reused, any I/O on the old inode should fail */
531 fuse_make_bad(inode);
532 iput(inode);
533 goto retry;
534 }
535 fi = get_fuse_inode(inode);
536 spin_lock(&fi->lock);
537 fi->nlookup++;
538 spin_unlock(&fi->lock);
539 done:
540 fuse_change_attributes(inode, attr, attr_valid, attr_version);
541
542 return inode;
543 }
544
fuse_ilookup(struct fuse_conn * fc,u64 nodeid,struct fuse_mount ** fm)545 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
546 struct fuse_mount **fm)
547 {
548 struct fuse_mount *fm_iter;
549 struct inode *inode;
550 struct fuse_inode_identifier fii = {
551 .nodeid = nodeid,
552 };
553
554 WARN_ON(!rwsem_is_locked(&fc->killsb));
555 list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
556 if (!fm_iter->sb)
557 continue;
558
559 inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &fii);
560 if (inode) {
561 if (fm)
562 *fm = fm_iter;
563 return inode;
564 }
565 }
566
567 return NULL;
568 }
569
fuse_reverse_inval_inode(struct fuse_conn * fc,u64 nodeid,loff_t offset,loff_t len)570 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
571 loff_t offset, loff_t len)
572 {
573 struct fuse_inode *fi;
574 struct inode *inode;
575 pgoff_t pg_start;
576 pgoff_t pg_end;
577
578 inode = fuse_ilookup(fc, nodeid, NULL);
579 if (!inode)
580 return -ENOENT;
581
582 fi = get_fuse_inode(inode);
583 spin_lock(&fi->lock);
584 fi->attr_version = atomic64_inc_return(&fc->attr_version);
585 spin_unlock(&fi->lock);
586
587 fuse_invalidate_attr(inode);
588 forget_all_cached_acls(inode);
589 if (offset >= 0) {
590 pg_start = offset >> PAGE_SHIFT;
591 if (len <= 0)
592 pg_end = -1;
593 else
594 pg_end = (offset + len - 1) >> PAGE_SHIFT;
595 invalidate_inode_pages2_range(inode->i_mapping,
596 pg_start, pg_end);
597 }
598 iput(inode);
599 return 0;
600 }
601
fuse_lock_inode(struct inode * inode)602 bool fuse_lock_inode(struct inode *inode)
603 {
604 bool locked = false;
605
606 if (!get_fuse_conn(inode)->parallel_dirops) {
607 mutex_lock(&get_fuse_inode(inode)->mutex);
608 locked = true;
609 }
610
611 return locked;
612 }
613
fuse_unlock_inode(struct inode * inode,bool locked)614 void fuse_unlock_inode(struct inode *inode, bool locked)
615 {
616 if (locked)
617 mutex_unlock(&get_fuse_inode(inode)->mutex);
618 }
619
fuse_umount_begin(struct super_block * sb)620 static void fuse_umount_begin(struct super_block *sb)
621 {
622 struct fuse_conn *fc = get_fuse_conn_super(sb);
623
624 if (!fc->no_force_umount)
625 fuse_abort_conn(fc);
626 }
627
fuse_send_destroy(struct fuse_mount * fm)628 static void fuse_send_destroy(struct fuse_mount *fm)
629 {
630 if (fm->fc->conn_init) {
631 FUSE_ARGS(args);
632
633 args.opcode = FUSE_DESTROY;
634 args.force = true;
635 args.nocreds = true;
636 fuse_simple_request(fm, &args);
637 }
638 }
639
fuse_statfs(struct dentry * dentry,struct kstatfs * buf)640 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
641 {
642 struct super_block *sb = dentry->d_sb;
643 struct fuse_mount *fm = get_fuse_mount_super(sb);
644 FUSE_ARGS(args);
645 struct fuse_statfs_out outarg;
646 int err;
647 #ifdef CONFIG_FUSE_BPF
648 struct fuse_err_ret fer;
649 #endif
650
651 if (!fuse_allow_current_process(fm->fc)) {
652 buf->f_type = FUSE_SUPER_MAGIC;
653 return 0;
654 }
655
656 #ifdef CONFIG_FUSE_BPF
657 fer = fuse_bpf_backing(dentry->d_inode, struct fuse_statfs_out,
658 fuse_statfs_initialize, fuse_statfs_backing,
659 fuse_statfs_finalize,
660 dentry, buf);
661 if (fer.ret)
662 return PTR_ERR(fer.result);
663 #endif
664
665 memset(&outarg, 0, sizeof(outarg));
666 args.in_numargs = 0;
667 args.opcode = FUSE_STATFS;
668 args.nodeid = get_node_id(d_inode(dentry));
669 args.out_numargs = 1;
670 args.out_args[0].size = sizeof(outarg);
671 args.out_args[0].value = &outarg;
672 err = fuse_simple_request(fm, &args);
673 if (!err)
674 convert_fuse_statfs(buf, &outarg.st);
675 return err;
676 }
677
fuse_sync_bucket_alloc(void)678 static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void)
679 {
680 struct fuse_sync_bucket *bucket;
681
682 bucket = kzalloc(sizeof(*bucket), GFP_KERNEL | __GFP_NOFAIL);
683 if (bucket) {
684 init_waitqueue_head(&bucket->waitq);
685 /* Initial active count */
686 atomic_set(&bucket->count, 1);
687 }
688 return bucket;
689 }
690
fuse_sync_fs_writes(struct fuse_conn * fc)691 static void fuse_sync_fs_writes(struct fuse_conn *fc)
692 {
693 struct fuse_sync_bucket *bucket, *new_bucket;
694 int count;
695
696 new_bucket = fuse_sync_bucket_alloc();
697 spin_lock(&fc->lock);
698 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
699 count = atomic_read(&bucket->count);
700 WARN_ON(count < 1);
701 /* No outstanding writes? */
702 if (count == 1) {
703 spin_unlock(&fc->lock);
704 kfree(new_bucket);
705 return;
706 }
707
708 /*
709 * Completion of new bucket depends on completion of this bucket, so add
710 * one more count.
711 */
712 atomic_inc(&new_bucket->count);
713 rcu_assign_pointer(fc->curr_bucket, new_bucket);
714 spin_unlock(&fc->lock);
715 /*
716 * Drop initial active count. At this point if all writes in this and
717 * ancestor buckets complete, the count will go to zero and this task
718 * will be woken up.
719 */
720 atomic_dec(&bucket->count);
721
722 wait_event(bucket->waitq, atomic_read(&bucket->count) == 0);
723
724 /* Drop temp count on descendant bucket */
725 fuse_sync_bucket_dec(new_bucket);
726 kfree_rcu(bucket, rcu);
727 }
728
fuse_sync_fs(struct super_block * sb,int wait)729 static int fuse_sync_fs(struct super_block *sb, int wait)
730 {
731 struct fuse_mount *fm = get_fuse_mount_super(sb);
732 struct fuse_conn *fc = fm->fc;
733 struct fuse_syncfs_in inarg;
734 FUSE_ARGS(args);
735 int err;
736
737 /*
738 * Userspace cannot handle the wait == 0 case. Avoid a
739 * gratuitous roundtrip.
740 */
741 if (!wait)
742 return 0;
743
744 /* The filesystem is being unmounted. Nothing to do. */
745 if (!sb->s_root)
746 return 0;
747
748 if (!fc->sync_fs)
749 return 0;
750
751 fuse_sync_fs_writes(fc);
752
753 memset(&inarg, 0, sizeof(inarg));
754 args.in_numargs = 1;
755 args.in_args[0].size = sizeof(inarg);
756 args.in_args[0].value = &inarg;
757 args.opcode = FUSE_SYNCFS;
758 args.nodeid = get_node_id(sb->s_root->d_inode);
759 args.out_numargs = 0;
760
761 err = fuse_simple_request(fm, &args);
762 if (err == -ENOSYS) {
763 fc->sync_fs = 0;
764 err = 0;
765 }
766
767 return err;
768 }
769
770 enum {
771 OPT_SOURCE,
772 OPT_SUBTYPE,
773 OPT_FD,
774 OPT_ROOTMODE,
775 OPT_USER_ID,
776 OPT_GROUP_ID,
777 OPT_DEFAULT_PERMISSIONS,
778 OPT_ALLOW_OTHER,
779 OPT_MAX_READ,
780 OPT_BLKSIZE,
781 OPT_ROOT_BPF,
782 OPT_ROOT_DIR,
783 OPT_NO_DAEMON,
784 OPT_ERR
785 };
786
787 static const struct fs_parameter_spec fuse_fs_parameters[] = {
788 fsparam_string ("source", OPT_SOURCE),
789 fsparam_u32 ("fd", OPT_FD),
790 fsparam_u32oct ("rootmode", OPT_ROOTMODE),
791 fsparam_u32 ("user_id", OPT_USER_ID),
792 fsparam_u32 ("group_id", OPT_GROUP_ID),
793 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
794 fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
795 fsparam_u32 ("max_read", OPT_MAX_READ),
796 fsparam_u32 ("blksize", OPT_BLKSIZE),
797 fsparam_string ("subtype", OPT_SUBTYPE),
798 fsparam_u32 ("root_bpf", OPT_ROOT_BPF),
799 fsparam_u32 ("root_dir", OPT_ROOT_DIR),
800 fsparam_flag ("no_daemon", OPT_NO_DAEMON),
801 {}
802 };
803
fuse_parse_param(struct fs_context * fsc,struct fs_parameter * param)804 static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
805 {
806 struct fs_parse_result result;
807 struct fuse_fs_context *ctx = fsc->fs_private;
808 int opt;
809
810 if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
811 /*
812 * Ignore options coming from mount(MS_REMOUNT) for backward
813 * compatibility.
814 */
815 if (fsc->oldapi)
816 return 0;
817
818 return invalfc(fsc, "No changes allowed in reconfigure");
819 }
820
821 opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
822 if (opt < 0)
823 return opt;
824
825 switch (opt) {
826 case OPT_SOURCE:
827 if (fsc->source)
828 return invalfc(fsc, "Multiple sources specified");
829 fsc->source = param->string;
830 param->string = NULL;
831 break;
832
833 case OPT_SUBTYPE:
834 if (ctx->subtype)
835 return invalfc(fsc, "Multiple subtypes specified");
836 ctx->subtype = param->string;
837 param->string = NULL;
838 return 0;
839
840 case OPT_FD:
841 ctx->fd = result.uint_32;
842 ctx->fd_present = true;
843 break;
844
845 case OPT_ROOTMODE:
846 if (!fuse_valid_type(result.uint_32))
847 return invalfc(fsc, "Invalid rootmode");
848 ctx->rootmode = result.uint_32;
849 ctx->rootmode_present = true;
850 break;
851
852 case OPT_USER_ID:
853 ctx->user_id = make_kuid(fsc->user_ns, result.uint_32);
854 if (!uid_valid(ctx->user_id))
855 return invalfc(fsc, "Invalid user_id");
856 ctx->user_id_present = true;
857 break;
858
859 case OPT_GROUP_ID:
860 ctx->group_id = make_kgid(fsc->user_ns, result.uint_32);
861 if (!gid_valid(ctx->group_id))
862 return invalfc(fsc, "Invalid group_id");
863 ctx->group_id_present = true;
864 break;
865
866 case OPT_DEFAULT_PERMISSIONS:
867 ctx->default_permissions = true;
868 break;
869
870 case OPT_ALLOW_OTHER:
871 ctx->allow_other = true;
872 break;
873
874 case OPT_MAX_READ:
875 ctx->max_read = result.uint_32;
876 break;
877
878 case OPT_BLKSIZE:
879 if (!ctx->is_bdev)
880 return invalfc(fsc, "blksize only supported for fuseblk");
881 ctx->blksize = result.uint_32;
882 break;
883
884 case OPT_ROOT_BPF:
885 ctx->root_bpf = bpf_prog_get_type_dev(result.uint_32,
886 BPF_PROG_TYPE_FUSE, false);
887 if (IS_ERR(ctx->root_bpf)) {
888 ctx->root_bpf = NULL;
889 return invalfc(fsc, "Unable to open bpf program");
890 }
891 break;
892
893 case OPT_ROOT_DIR:
894 ctx->root_dir = fget(result.uint_32);
895 if (!ctx->root_dir)
896 return invalfc(fsc, "Unable to open root directory");
897 break;
898
899 case OPT_NO_DAEMON:
900 ctx->no_daemon = true;
901 ctx->fd_present = true;
902 break;
903
904 default:
905 return -EINVAL;
906 }
907
908 return 0;
909 }
910
fuse_free_fsc(struct fs_context * fsc)911 static void fuse_free_fsc(struct fs_context *fsc)
912 {
913 struct fuse_fs_context *ctx = fsc->fs_private;
914
915 if (ctx) {
916 if (ctx->root_dir)
917 fput(ctx->root_dir);
918 if (ctx->root_bpf)
919 bpf_prog_put(ctx->root_bpf);
920 kfree(ctx->subtype);
921 kfree(ctx);
922 }
923 }
924
fuse_show_options(struct seq_file * m,struct dentry * root)925 static int fuse_show_options(struct seq_file *m, struct dentry *root)
926 {
927 struct super_block *sb = root->d_sb;
928 struct fuse_conn *fc = get_fuse_conn_super(sb);
929
930 if (fc->legacy_opts_show) {
931 seq_printf(m, ",user_id=%u",
932 from_kuid_munged(fc->user_ns, fc->user_id));
933 seq_printf(m, ",group_id=%u",
934 from_kgid_munged(fc->user_ns, fc->group_id));
935 if (fc->default_permissions)
936 seq_puts(m, ",default_permissions");
937 if (fc->allow_other)
938 seq_puts(m, ",allow_other");
939 if (fc->max_read != ~0)
940 seq_printf(m, ",max_read=%u", fc->max_read);
941 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
942 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
943 }
944 #ifdef CONFIG_FUSE_DAX
945 if (fc->dax)
946 seq_puts(m, ",dax");
947 #endif
948
949 return 0;
950 }
951
fuse_iqueue_init(struct fuse_iqueue * fiq,const struct fuse_iqueue_ops * ops,void * priv)952 static void fuse_iqueue_init(struct fuse_iqueue *fiq,
953 const struct fuse_iqueue_ops *ops,
954 void *priv)
955 {
956 memset(fiq, 0, sizeof(struct fuse_iqueue));
957 spin_lock_init(&fiq->lock);
958 init_waitqueue_head(&fiq->waitq);
959 INIT_LIST_HEAD(&fiq->pending);
960 INIT_LIST_HEAD(&fiq->interrupts);
961 fiq->forget_list_tail = &fiq->forget_list_head;
962 fiq->connected = 1;
963 fiq->ops = ops;
964 fiq->priv = priv;
965 }
966
fuse_pqueue_init(struct fuse_pqueue * fpq)967 static void fuse_pqueue_init(struct fuse_pqueue *fpq)
968 {
969 unsigned int i;
970
971 spin_lock_init(&fpq->lock);
972 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
973 INIT_LIST_HEAD(&fpq->processing[i]);
974 INIT_LIST_HEAD(&fpq->io);
975 fpq->connected = 1;
976 }
977
fuse_conn_init(struct fuse_conn * fc,struct fuse_mount * fm,struct user_namespace * user_ns,const struct fuse_iqueue_ops * fiq_ops,void * fiq_priv)978 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
979 struct user_namespace *user_ns,
980 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
981 {
982 memset(fc, 0, sizeof(*fc));
983 spin_lock_init(&fc->lock);
984 spin_lock_init(&fc->bg_lock);
985 spin_lock_init(&fc->passthrough_req_lock);
986 init_rwsem(&fc->killsb);
987 refcount_set(&fc->count, 1);
988 atomic_set(&fc->dev_count, 1);
989 init_waitqueue_head(&fc->blocked_waitq);
990 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
991 INIT_LIST_HEAD(&fc->bg_queue);
992 INIT_LIST_HEAD(&fc->entry);
993 INIT_LIST_HEAD(&fc->devices);
994 idr_init(&fc->passthrough_req);
995 atomic_set(&fc->num_waiting, 0);
996 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
997 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
998 atomic64_set(&fc->khctr, 0);
999 fc->polled_files = RB_ROOT;
1000 fc->blocked = 0;
1001 fc->initialized = 0;
1002 fc->connected = 1;
1003 atomic64_set(&fc->attr_version, 1);
1004 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
1005 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
1006 fc->user_ns = get_user_ns(user_ns);
1007 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
1008 fc->max_pages_limit = FUSE_MAX_MAX_PAGES;
1009
1010 INIT_LIST_HEAD(&fc->mounts);
1011 list_add(&fm->fc_entry, &fc->mounts);
1012 fm->fc = fc;
1013 }
1014 EXPORT_SYMBOL_GPL(fuse_conn_init);
1015
fuse_conn_put(struct fuse_conn * fc)1016 void fuse_conn_put(struct fuse_conn *fc)
1017 {
1018 if (refcount_dec_and_test(&fc->count)) {
1019 struct fuse_iqueue *fiq = &fc->iq;
1020 struct fuse_sync_bucket *bucket;
1021
1022 if (IS_ENABLED(CONFIG_FUSE_DAX))
1023 fuse_dax_conn_free(fc);
1024 if (fiq->ops->release)
1025 fiq->ops->release(fiq);
1026 put_pid_ns(fc->pid_ns);
1027 put_user_ns(fc->user_ns);
1028 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
1029 if (bucket) {
1030 WARN_ON(atomic_read(&bucket->count) != 1);
1031 kfree(bucket);
1032 }
1033 fc->release(fc);
1034 }
1035 }
1036 EXPORT_SYMBOL_GPL(fuse_conn_put);
1037
fuse_conn_get(struct fuse_conn * fc)1038 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
1039 {
1040 refcount_inc(&fc->count);
1041 return fc;
1042 }
1043 EXPORT_SYMBOL_GPL(fuse_conn_get);
1044
fuse_get_root_inode(struct super_block * sb,unsigned int mode,struct bpf_prog * root_bpf,struct file * backing_fd)1045 static struct inode *fuse_get_root_inode(struct super_block *sb,
1046 unsigned int mode,
1047 struct bpf_prog *root_bpf,
1048 struct file *backing_fd)
1049 {
1050 struct fuse_attr attr;
1051 struct inode *inode;
1052
1053 memset(&attr, 0, sizeof(attr));
1054 attr.mode = mode;
1055 attr.ino = FUSE_ROOT_ID;
1056 attr.nlink = 1;
1057 inode = fuse_iget(sb, 1, 0, &attr, 0, 0);
1058 if (!inode)
1059 return NULL;
1060
1061 #ifdef CONFIG_FUSE_BPF
1062 get_fuse_inode(inode)->bpf = root_bpf;
1063 if (root_bpf)
1064 bpf_prog_inc(root_bpf);
1065
1066 if (backing_fd) {
1067 get_fuse_inode(inode)->backing_inode = backing_fd->f_inode;
1068 ihold(backing_fd->f_inode);
1069 }
1070 #endif
1071
1072 return inode;
1073 }
1074
1075 struct fuse_inode_handle {
1076 u64 nodeid;
1077 u32 generation;
1078 };
1079
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)1080 static struct dentry *fuse_get_dentry(struct super_block *sb,
1081 struct fuse_inode_handle *handle)
1082 {
1083 struct fuse_conn *fc = get_fuse_conn_super(sb);
1084 struct inode *inode;
1085 struct dentry *entry;
1086 int err = -ESTALE;
1087 struct fuse_inode_identifier fii = {
1088 .nodeid = handle->nodeid,
1089 };
1090
1091 if (handle->nodeid == 0)
1092 goto out_err;
1093
1094 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &fii);
1095 if (!inode) {
1096 struct fuse_entry_out outarg;
1097 const struct qstr name = QSTR_INIT(".", 1);
1098
1099 if (!fc->export_support)
1100 goto out_err;
1101
1102 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
1103 NULL, &inode);
1104 if (err && err != -ENOENT)
1105 goto out_err;
1106 if (err || !inode) {
1107 err = -ESTALE;
1108 goto out_err;
1109 }
1110 err = -EIO;
1111 if (get_node_id(inode) != handle->nodeid)
1112 goto out_iput;
1113 }
1114 err = -ESTALE;
1115 if (inode->i_generation != handle->generation)
1116 goto out_iput;
1117
1118 entry = d_obtain_alias(inode);
1119 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
1120 fuse_invalidate_entry_cache(entry);
1121
1122 return entry;
1123
1124 out_iput:
1125 iput(inode);
1126 out_err:
1127 return ERR_PTR(err);
1128 }
1129
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)1130 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1131 struct inode *parent)
1132 {
1133 int len = parent ? 6 : 3;
1134 u64 nodeid;
1135 u32 generation;
1136
1137 if (*max_len < len) {
1138 *max_len = len;
1139 return FILEID_INVALID;
1140 }
1141
1142 nodeid = get_fuse_inode(inode)->nodeid;
1143 generation = inode->i_generation;
1144
1145 fh[0] = (u32)(nodeid >> 32);
1146 fh[1] = (u32)(nodeid & 0xffffffff);
1147 fh[2] = generation;
1148
1149 if (parent) {
1150 nodeid = get_fuse_inode(parent)->nodeid;
1151 generation = parent->i_generation;
1152
1153 fh[3] = (u32)(nodeid >> 32);
1154 fh[4] = (u32)(nodeid & 0xffffffff);
1155 fh[5] = generation;
1156 }
1157
1158 *max_len = len;
1159 return parent ? 0x82 : 0x81;
1160 }
1161
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1162 static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
1163 struct fid *fid, int fh_len, int fh_type)
1164 {
1165 struct fuse_inode_handle handle;
1166
1167 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
1168 return NULL;
1169
1170 handle.nodeid = (u64) fid->raw[0] << 32;
1171 handle.nodeid |= (u64) fid->raw[1];
1172 handle.generation = fid->raw[2];
1173 return fuse_get_dentry(sb, &handle);
1174 }
1175
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1176 static struct dentry *fuse_fh_to_parent(struct super_block *sb,
1177 struct fid *fid, int fh_len, int fh_type)
1178 {
1179 struct fuse_inode_handle parent;
1180
1181 if (fh_type != 0x82 || fh_len < 6)
1182 return NULL;
1183
1184 parent.nodeid = (u64) fid->raw[3] << 32;
1185 parent.nodeid |= (u64) fid->raw[4];
1186 parent.generation = fid->raw[5];
1187 return fuse_get_dentry(sb, &parent);
1188 }
1189
fuse_get_parent(struct dentry * child)1190 static struct dentry *fuse_get_parent(struct dentry *child)
1191 {
1192 struct inode *child_inode = d_inode(child);
1193 struct fuse_conn *fc = get_fuse_conn(child_inode);
1194 struct inode *inode;
1195 struct dentry *parent;
1196 struct fuse_entry_out outarg;
1197 const struct qstr name = QSTR_INIT("..", 2);
1198 int err;
1199
1200 if (!fc->export_support)
1201 return ERR_PTR(-ESTALE);
1202
1203 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
1204 &name, &outarg, NULL, &inode);
1205 if (err) {
1206 if (err == -ENOENT)
1207 return ERR_PTR(-ESTALE);
1208 return ERR_PTR(err);
1209 }
1210
1211 parent = d_obtain_alias(inode);
1212 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
1213 fuse_invalidate_entry_cache(parent);
1214
1215 return parent;
1216 }
1217
1218 static const struct export_operations fuse_export_operations = {
1219 .fh_to_dentry = fuse_fh_to_dentry,
1220 .fh_to_parent = fuse_fh_to_parent,
1221 .encode_fh = fuse_encode_fh,
1222 .get_parent = fuse_get_parent,
1223 };
1224
1225 static const struct super_operations fuse_super_operations = {
1226 .alloc_inode = fuse_alloc_inode,
1227 #ifdef CONFIG_FUSE_BPF
1228 .destroy_inode = fuse_destroy_inode,
1229 #endif
1230 .free_inode = fuse_free_inode,
1231 .evict_inode = fuse_evict_inode,
1232 .write_inode = fuse_write_inode,
1233 .drop_inode = generic_delete_inode,
1234 .umount_begin = fuse_umount_begin,
1235 .statfs = fuse_statfs,
1236 .sync_fs = fuse_sync_fs,
1237 .show_options = fuse_show_options,
1238 };
1239
sanitize_global_limit(unsigned * limit)1240 static void sanitize_global_limit(unsigned *limit)
1241 {
1242 /*
1243 * The default maximum number of async requests is calculated to consume
1244 * 1/2^13 of the total memory, assuming 392 bytes per request.
1245 */
1246 if (*limit == 0)
1247 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
1248
1249 if (*limit >= 1 << 16)
1250 *limit = (1 << 16) - 1;
1251 }
1252
set_global_limit(const char * val,const struct kernel_param * kp)1253 static int set_global_limit(const char *val, const struct kernel_param *kp)
1254 {
1255 int rv;
1256
1257 rv = param_set_uint(val, kp);
1258 if (rv)
1259 return rv;
1260
1261 sanitize_global_limit((unsigned *)kp->arg);
1262
1263 return 0;
1264 }
1265
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)1266 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1267 {
1268 int cap_sys_admin = capable(CAP_SYS_ADMIN);
1269
1270 if (arg->minor < 13)
1271 return;
1272
1273 sanitize_global_limit(&max_user_bgreq);
1274 sanitize_global_limit(&max_user_congthresh);
1275
1276 spin_lock(&fc->bg_lock);
1277 if (arg->max_background) {
1278 fc->max_background = arg->max_background;
1279
1280 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1281 fc->max_background = max_user_bgreq;
1282 }
1283 if (arg->congestion_threshold) {
1284 fc->congestion_threshold = arg->congestion_threshold;
1285
1286 if (!cap_sys_admin &&
1287 fc->congestion_threshold > max_user_congthresh)
1288 fc->congestion_threshold = max_user_congthresh;
1289 }
1290 spin_unlock(&fc->bg_lock);
1291 }
1292
1293 struct fuse_init_args {
1294 struct fuse_args args;
1295 struct fuse_init_in in;
1296 struct fuse_init_out out;
1297 };
1298
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)1299 static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
1300 int error)
1301 {
1302 struct fuse_conn *fc = fm->fc;
1303 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1304 struct fuse_init_out *arg = &ia->out;
1305 bool ok = true;
1306
1307 if (error || arg->major != FUSE_KERNEL_VERSION)
1308 ok = false;
1309 else {
1310 unsigned long ra_pages;
1311
1312 process_init_limits(fc, arg);
1313
1314 if (arg->minor >= 6) {
1315 ra_pages = arg->max_readahead / PAGE_SIZE;
1316 if (arg->flags & FUSE_ASYNC_READ)
1317 fc->async_read = 1;
1318 if (!(arg->flags & FUSE_POSIX_LOCKS))
1319 fc->no_lock = 1;
1320 if (arg->minor >= 17) {
1321 if (!(arg->flags & FUSE_FLOCK_LOCKS))
1322 fc->no_flock = 1;
1323 } else {
1324 if (!(arg->flags & FUSE_POSIX_LOCKS))
1325 fc->no_flock = 1;
1326 }
1327 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1328 fc->atomic_o_trunc = 1;
1329 if (arg->minor >= 9) {
1330 /* LOOKUP has dependency on proto version */
1331 if (arg->flags & FUSE_EXPORT_SUPPORT)
1332 fc->export_support = 1;
1333 }
1334 if (arg->flags & FUSE_BIG_WRITES)
1335 fc->big_writes = 1;
1336 if (arg->flags & FUSE_DONT_MASK)
1337 fc->dont_mask = 1;
1338 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1339 fc->auto_inval_data = 1;
1340 else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
1341 fc->explicit_inval_data = 1;
1342 if (arg->flags & FUSE_DO_READDIRPLUS) {
1343 fc->do_readdirplus = 1;
1344 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1345 fc->readdirplus_auto = 1;
1346 }
1347 if (arg->flags & FUSE_ASYNC_DIO)
1348 fc->async_dio = 1;
1349 if (arg->flags & FUSE_WRITEBACK_CACHE)
1350 fc->writeback_cache = 1;
1351 if (arg->flags & FUSE_PARALLEL_DIROPS)
1352 fc->parallel_dirops = 1;
1353 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1354 fc->handle_killpriv = 1;
1355 if (arg->time_gran && arg->time_gran <= 1000000000)
1356 fm->sb->s_time_gran = arg->time_gran;
1357 if ((arg->flags & FUSE_POSIX_ACL)) {
1358 fc->default_permissions = 1;
1359 fc->posix_acl = 1;
1360 fm->sb->s_xattr = fuse_acl_xattr_handlers;
1361 }
1362 if (arg->flags & FUSE_CACHE_SYMLINKS)
1363 fc->cache_symlinks = 1;
1364 if (arg->flags & FUSE_ABORT_ERROR)
1365 fc->abort_err = 1;
1366 if (arg->flags & FUSE_MAX_PAGES) {
1367 fc->max_pages =
1368 min_t(unsigned int, fc->max_pages_limit,
1369 max_t(unsigned int, arg->max_pages, 1));
1370 }
1371 if (IS_ENABLED(CONFIG_FUSE_DAX) &&
1372 arg->flags & FUSE_MAP_ALIGNMENT &&
1373 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1374 ok = false;
1375 }
1376 if (arg->flags & FUSE_HANDLE_KILLPRIV_V2) {
1377 fc->handle_killpriv_v2 = 1;
1378 fm->sb->s_flags |= SB_NOSEC;
1379 }
1380 if (arg->flags & FUSE_PASSTHROUGH) {
1381 fc->passthrough = 1;
1382 /* Prevent further stacking */
1383 fm->sb->s_stack_depth =
1384 FILESYSTEM_MAX_STACK_DEPTH;
1385 }
1386 if (arg->flags & FUSE_SETXATTR_EXT)
1387 fc->setxattr_ext = 1;
1388 } else {
1389 ra_pages = fc->max_read / PAGE_SIZE;
1390 fc->no_lock = 1;
1391 fc->no_flock = 1;
1392 }
1393
1394 fm->sb->s_bdi->ra_pages =
1395 min(fm->sb->s_bdi->ra_pages, ra_pages);
1396 fc->minor = arg->minor;
1397 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1398 fc->max_write = max_t(unsigned, 4096, fc->max_write);
1399 fc->conn_init = 1;
1400 }
1401 kfree(ia);
1402
1403 if (!ok) {
1404 fc->conn_init = 0;
1405 fc->conn_error = 1;
1406 }
1407
1408 fuse_set_initialized(fc);
1409 wake_up_all(&fc->blocked_waitq);
1410 }
1411
fuse_send_init(struct fuse_mount * fm)1412 void fuse_send_init(struct fuse_mount *fm)
1413 {
1414 struct fuse_init_args *ia;
1415
1416 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1417
1418 ia->in.major = FUSE_KERNEL_VERSION;
1419 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1420 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1421 ia->in.flags |=
1422 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1423 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1424 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1425 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1426 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1427 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1428 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1429 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1430 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1431 FUSE_PASSTHROUGH |
1432 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT;
1433 #ifdef CONFIG_FUSE_DAX
1434 if (fm->fc->dax)
1435 ia->in.flags |= FUSE_MAP_ALIGNMENT;
1436 #endif
1437 if (fm->fc->auto_submounts)
1438 ia->in.flags |= FUSE_SUBMOUNTS;
1439
1440 ia->args.opcode = FUSE_INIT;
1441 ia->args.in_numargs = 1;
1442 ia->args.in_args[0].size = sizeof(ia->in);
1443 ia->args.in_args[0].value = &ia->in;
1444 ia->args.out_numargs = 1;
1445 /* Variable length argument used for backward compatibility
1446 with interface version < 7.5. Rest of init_out is zeroed
1447 by do_get_request(), so a short reply is not a problem */
1448 ia->args.out_argvar = true;
1449 ia->args.out_args[0].size = sizeof(ia->out);
1450 ia->args.out_args[0].value = &ia->out;
1451 ia->args.force = true;
1452 ia->args.nocreds = true;
1453 ia->args.end = process_init_reply;
1454
1455 if (unlikely(fm->fc->no_daemon) || fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1456 process_init_reply(fm, &ia->args, -ENOTCONN);
1457 }
1458 EXPORT_SYMBOL_GPL(fuse_send_init);
1459
free_fuse_passthrough(int id,void * p,void * data)1460 static int free_fuse_passthrough(int id, void *p, void *data)
1461 {
1462 struct fuse_passthrough *passthrough = (struct fuse_passthrough *)p;
1463
1464 fuse_passthrough_release(passthrough);
1465 kfree(p);
1466
1467 return 0;
1468 }
1469
fuse_free_conn(struct fuse_conn * fc)1470 void fuse_free_conn(struct fuse_conn *fc)
1471 {
1472 WARN_ON(!list_empty(&fc->devices));
1473 idr_for_each(&fc->passthrough_req, free_fuse_passthrough, NULL);
1474 idr_destroy(&fc->passthrough_req);
1475 kfree_rcu(fc, rcu);
1476 }
1477 EXPORT_SYMBOL_GPL(fuse_free_conn);
1478
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1479 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1480 {
1481 int err;
1482 char *suffix = "";
1483
1484 if (sb->s_bdev) {
1485 suffix = "-fuseblk";
1486 /*
1487 * sb->s_bdi points to blkdev's bdi however we want to redirect
1488 * it to our private bdi...
1489 */
1490 bdi_put(sb->s_bdi);
1491 sb->s_bdi = &noop_backing_dev_info;
1492 }
1493 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1494 MINOR(fc->dev), suffix);
1495 if (err)
1496 return err;
1497
1498 /* fuse does it's own writeback accounting */
1499 sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1500 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
1501
1502 /*
1503 * For a single fuse filesystem use max 1% of dirty +
1504 * writeback threshold.
1505 *
1506 * This gives about 1M of write buffer for memory maps on a
1507 * machine with 1G and 10% dirty_ratio, which should be more
1508 * than enough.
1509 *
1510 * Privileged users can raise it by writing to
1511 *
1512 * /sys/class/bdi/<bdi>/max_ratio
1513 */
1514 bdi_set_max_ratio(sb->s_bdi, 1);
1515
1516 return 0;
1517 }
1518
fuse_dev_alloc(void)1519 struct fuse_dev *fuse_dev_alloc(void)
1520 {
1521 struct fuse_dev *fud;
1522 struct list_head *pq;
1523
1524 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1525 if (!fud)
1526 return NULL;
1527
1528 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1529 if (!pq) {
1530 kfree(fud);
1531 return NULL;
1532 }
1533
1534 fud->pq.processing = pq;
1535 fuse_pqueue_init(&fud->pq);
1536
1537 return fud;
1538 }
1539 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1540
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1541 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1542 {
1543 fud->fc = fuse_conn_get(fc);
1544 spin_lock(&fc->lock);
1545 list_add_tail(&fud->entry, &fc->devices);
1546 spin_unlock(&fc->lock);
1547 }
1548 EXPORT_SYMBOL_GPL(fuse_dev_install);
1549
fuse_dev_alloc_install(struct fuse_conn * fc)1550 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1551 {
1552 struct fuse_dev *fud;
1553
1554 fud = fuse_dev_alloc();
1555 if (!fud)
1556 return NULL;
1557
1558 fuse_dev_install(fud, fc);
1559 return fud;
1560 }
1561 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1562
fuse_dev_free(struct fuse_dev * fud)1563 void fuse_dev_free(struct fuse_dev *fud)
1564 {
1565 struct fuse_conn *fc = fud->fc;
1566
1567 if (fc) {
1568 spin_lock(&fc->lock);
1569 list_del(&fud->entry);
1570 spin_unlock(&fc->lock);
1571
1572 fuse_conn_put(fc);
1573 }
1574 kfree(fud->pq.processing);
1575 kfree(fud);
1576 }
1577 EXPORT_SYMBOL_GPL(fuse_dev_free);
1578
fuse_sb_defaults(struct super_block * sb)1579 static void fuse_sb_defaults(struct super_block *sb)
1580 {
1581 sb->s_magic = FUSE_SUPER_MAGIC;
1582 sb->s_op = &fuse_super_operations;
1583 sb->s_xattr = fuse_xattr_handlers;
1584 sb->s_maxbytes = MAX_LFS_FILESIZE;
1585 sb->s_time_gran = 1;
1586 sb->s_export_op = &fuse_export_operations;
1587 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1588 if (sb->s_user_ns != &init_user_ns)
1589 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1590 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1591
1592 /*
1593 * If we are not in the initial user namespace posix
1594 * acls must be translated.
1595 */
1596 if (sb->s_user_ns != &init_user_ns)
1597 sb->s_xattr = fuse_no_acl_xattr_handlers;
1598 }
1599
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)1600 static int fuse_fill_super_submount(struct super_block *sb,
1601 struct fuse_inode *parent_fi)
1602 {
1603 struct fuse_mount *fm = get_fuse_mount_super(sb);
1604 struct super_block *parent_sb = parent_fi->inode.i_sb;
1605 struct fuse_attr root_attr;
1606 struct inode *root;
1607 struct fuse_submount_lookup *sl;
1608 struct fuse_inode *fi;
1609
1610 fuse_sb_defaults(sb);
1611 fm->sb = sb;
1612
1613 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1614 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1615
1616 sb->s_xattr = parent_sb->s_xattr;
1617 sb->s_time_gran = parent_sb->s_time_gran;
1618 sb->s_blocksize = parent_sb->s_blocksize;
1619 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1620 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1621 if (parent_sb->s_subtype && !sb->s_subtype)
1622 return -ENOMEM;
1623
1624 fuse_fill_attr_from_inode(&root_attr, &parent_fi->inode);
1625 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1626 /*
1627 * This inode is just a duplicate, so it is not looked up and
1628 * its nlookup should not be incremented. fuse_iget() does
1629 * that, though, so undo it here.
1630 */
1631 fi = get_fuse_inode(root);
1632 fi->nlookup--;
1633
1634 sb->s_d_op = &fuse_dentry_operations;
1635 sb->s_root = d_make_root(root);
1636 if (!sb->s_root)
1637 return -ENOMEM;
1638
1639 /*
1640 * Grab the parent's submount_lookup pointer and take a
1641 * reference on the shared nlookup from the parent. This is to
1642 * prevent the last forget for this nodeid from getting
1643 * triggered until all users have finished with it.
1644 */
1645 sl = parent_fi->submount_lookup;
1646 WARN_ON(!sl);
1647 if (sl) {
1648 refcount_inc(&sl->count);
1649 fi->submount_lookup = sl;
1650 }
1651
1652 return 0;
1653 }
1654
1655 /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1656 static int fuse_get_tree_submount(struct fs_context *fsc)
1657 {
1658 struct fuse_mount *fm;
1659 struct fuse_inode *mp_fi = fsc->fs_private;
1660 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1661 struct super_block *sb;
1662 int err;
1663
1664 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1665 if (!fm)
1666 return -ENOMEM;
1667
1668 fm->fc = fuse_conn_get(fc);
1669 fsc->s_fs_info = fm;
1670 sb = sget_fc(fsc, NULL, set_anon_super_fc);
1671 if (fsc->s_fs_info)
1672 fuse_mount_destroy(fm);
1673 if (IS_ERR(sb))
1674 return PTR_ERR(sb);
1675
1676 /* Initialize superblock, making @mp_fi its root */
1677 err = fuse_fill_super_submount(sb, mp_fi);
1678 if (err) {
1679 deactivate_locked_super(sb);
1680 return err;
1681 }
1682
1683 down_write(&fc->killsb);
1684 list_add_tail(&fm->fc_entry, &fc->mounts);
1685 up_write(&fc->killsb);
1686
1687 sb->s_flags |= SB_ACTIVE;
1688 fsc->root = dget(sb->s_root);
1689
1690 return 0;
1691 }
1692
1693 static const struct fs_context_operations fuse_context_submount_ops = {
1694 .get_tree = fuse_get_tree_submount,
1695 };
1696
fuse_init_fs_context_submount(struct fs_context * fsc)1697 int fuse_init_fs_context_submount(struct fs_context *fsc)
1698 {
1699 fsc->ops = &fuse_context_submount_ops;
1700 return 0;
1701 }
1702 EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1703
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1704 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1705 {
1706 struct fuse_dev *fud = NULL;
1707 struct fuse_mount *fm = get_fuse_mount_super(sb);
1708 struct fuse_conn *fc = fm->fc;
1709 struct inode *root;
1710 struct dentry *root_dentry;
1711 int err;
1712
1713 err = -EINVAL;
1714 if (sb->s_flags & SB_MANDLOCK)
1715 goto err;
1716
1717 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
1718 fuse_sb_defaults(sb);
1719
1720 if (ctx->is_bdev) {
1721 #ifdef CONFIG_BLOCK
1722 err = -EINVAL;
1723 if (!sb_set_blocksize(sb, ctx->blksize))
1724 goto err;
1725 #endif
1726 } else {
1727 sb->s_blocksize = PAGE_SIZE;
1728 sb->s_blocksize_bits = PAGE_SHIFT;
1729 }
1730
1731 sb->s_subtype = ctx->subtype;
1732 ctx->subtype = NULL;
1733 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1734 err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1735 if (err)
1736 goto err;
1737 }
1738
1739 if (ctx->fudptr) {
1740 err = -ENOMEM;
1741 fud = fuse_dev_alloc_install(fc);
1742 if (!fud)
1743 goto err_free_dax;
1744 }
1745
1746 fc->dev = sb->s_dev;
1747 fm->sb = sb;
1748 err = fuse_bdi_init(fc, sb);
1749 if (err)
1750 goto err_dev_free;
1751
1752 /* Handle umasking inside the fuse code */
1753 if (sb->s_flags & SB_POSIXACL)
1754 fc->dont_mask = 1;
1755 sb->s_flags |= SB_POSIXACL;
1756
1757 fc->default_permissions = ctx->default_permissions;
1758 fc->allow_other = ctx->allow_other;
1759 fc->user_id = ctx->user_id;
1760 fc->group_id = ctx->group_id;
1761 fc->legacy_opts_show = ctx->legacy_opts_show;
1762 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1763 fc->destroy = ctx->destroy;
1764 fc->no_control = ctx->no_control;
1765 fc->no_force_umount = ctx->no_force_umount;
1766 fc->no_daemon = ctx->no_daemon;
1767
1768 err = -ENOMEM;
1769 root = fuse_get_root_inode(sb, ctx->rootmode, ctx->root_bpf,
1770 ctx->root_dir);
1771 sb->s_d_op = &fuse_root_dentry_operations;
1772 root_dentry = d_make_root(root);
1773 if (!root_dentry)
1774 goto err_dev_free;
1775 fuse_init_dentry_root(root_dentry, ctx->root_dir);
1776 /* Root dentry doesn't have .d_revalidate */
1777 sb->s_d_op = &fuse_dentry_operations;
1778
1779 mutex_lock(&fuse_mutex);
1780 err = -EINVAL;
1781 if (ctx->fudptr && *ctx->fudptr)
1782 goto err_unlock;
1783
1784 err = fuse_ctl_add_conn(fc);
1785 if (err)
1786 goto err_unlock;
1787
1788 list_add_tail(&fc->entry, &fuse_conn_list);
1789 sb->s_root = root_dentry;
1790 if (ctx->fudptr)
1791 *ctx->fudptr = fud;
1792 mutex_unlock(&fuse_mutex);
1793 return 0;
1794
1795 err_unlock:
1796 mutex_unlock(&fuse_mutex);
1797 dput(root_dentry);
1798 err_dev_free:
1799 if (fud)
1800 fuse_dev_free(fud);
1801 err_free_dax:
1802 if (IS_ENABLED(CONFIG_FUSE_DAX))
1803 fuse_dax_conn_free(fc);
1804 err:
1805 return err;
1806 }
1807 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1808
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1809 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1810 {
1811 struct fuse_fs_context *ctx = fsc->fs_private;
1812 int err;
1813
1814 if (!ctx->no_daemon) {
1815 if (!ctx->file || !ctx->rootmode_present ||
1816 !ctx->user_id_present || !ctx->group_id_present)
1817 return -EINVAL;
1818
1819 /*
1820 * Require mount to happen from the same user namespace which
1821 * opened /dev/fuse to prevent potential attacks.
1822 */
1823 if ((ctx->file->f_op != &fuse_dev_operations) ||
1824 (ctx->file->f_cred->user_ns != sb->s_user_ns))
1825 return -EINVAL;
1826 ctx->fudptr = &ctx->file->private_data;
1827 }
1828
1829 err = fuse_fill_super_common(sb, ctx);
1830 if (err)
1831 return err;
1832 /* file->private_data shall be visible on all CPUs after this */
1833 smp_mb();
1834 fuse_send_init(get_fuse_mount_super(sb));
1835 return 0;
1836 }
1837
1838 /*
1839 * This is the path where user supplied an already initialized fuse dev. In
1840 * this case never create a new super if the old one is gone.
1841 */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)1842 static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1843 {
1844 return -ENOTCONN;
1845 }
1846
fuse_test_super(struct super_block * sb,struct fs_context * fsc)1847 static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
1848 {
1849
1850 return fsc->sget_key == get_fuse_conn_super(sb);
1851 }
1852
fuse_get_tree(struct fs_context * fsc)1853 static int fuse_get_tree(struct fs_context *fsc)
1854 {
1855 struct fuse_fs_context *ctx = fsc->fs_private;
1856 struct fuse_dev *fud;
1857 struct fuse_conn *fc;
1858 struct fuse_mount *fm;
1859 struct super_block *sb;
1860 int err;
1861
1862 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1863 if (!fc)
1864 return -ENOMEM;
1865
1866 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1867 if (!fm) {
1868 kfree(fc);
1869 return -ENOMEM;
1870 }
1871
1872 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
1873 fc->release = fuse_free_conn;
1874
1875 fsc->s_fs_info = fm;
1876
1877 if (ctx->fd_present)
1878 ctx->file = fget(ctx->fd);
1879
1880 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
1881 err = get_tree_bdev(fsc, fuse_fill_super);
1882 goto out;
1883 }
1884 /*
1885 * While block dev mount can be initialized with a dummy device fd
1886 * (found by device name), normal fuse mounts can't
1887 */
1888 err = -EINVAL;
1889 if (!ctx->file)
1890 goto out;
1891
1892 /*
1893 * Allow creating a fuse mount with an already initialized fuse
1894 * connection
1895 */
1896 fud = READ_ONCE(ctx->file->private_data);
1897 if (ctx->file->f_op == &fuse_dev_operations && fud) {
1898 fsc->sget_key = fud->fc;
1899 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
1900 err = PTR_ERR_OR_ZERO(sb);
1901 if (!IS_ERR(sb))
1902 fsc->root = dget(sb->s_root);
1903 } else {
1904 err = get_tree_nodev(fsc, fuse_fill_super);
1905 }
1906 out:
1907 if (fsc->s_fs_info)
1908 fuse_mount_destroy(fm);
1909 if (ctx->file)
1910 fput(ctx->file);
1911 return err;
1912 }
1913
1914 static const struct fs_context_operations fuse_context_ops = {
1915 .free = fuse_free_fsc,
1916 .parse_param = fuse_parse_param,
1917 .reconfigure = fuse_reconfigure,
1918 .get_tree = fuse_get_tree,
1919 };
1920
1921 /*
1922 * Set up the filesystem mount context.
1923 */
fuse_init_fs_context(struct fs_context * fsc)1924 static int fuse_init_fs_context(struct fs_context *fsc)
1925 {
1926 struct fuse_fs_context *ctx;
1927
1928 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1929 if (!ctx)
1930 return -ENOMEM;
1931
1932 ctx->max_read = ~0;
1933 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1934 ctx->legacy_opts_show = true;
1935
1936 #ifdef CONFIG_BLOCK
1937 if (fsc->fs_type == &fuseblk_fs_type) {
1938 ctx->is_bdev = true;
1939 ctx->destroy = true;
1940 }
1941 #endif
1942
1943 fsc->fs_private = ctx;
1944 fsc->ops = &fuse_context_ops;
1945 return 0;
1946 }
1947
fuse_mount_remove(struct fuse_mount * fm)1948 bool fuse_mount_remove(struct fuse_mount *fm)
1949 {
1950 struct fuse_conn *fc = fm->fc;
1951 bool last = false;
1952
1953 down_write(&fc->killsb);
1954 list_del_init(&fm->fc_entry);
1955 if (list_empty(&fc->mounts))
1956 last = true;
1957 up_write(&fc->killsb);
1958
1959 return last;
1960 }
1961 EXPORT_SYMBOL_GPL(fuse_mount_remove);
1962
fuse_conn_destroy(struct fuse_mount * fm)1963 void fuse_conn_destroy(struct fuse_mount *fm)
1964 {
1965 struct fuse_conn *fc = fm->fc;
1966
1967 if (fc->destroy)
1968 fuse_send_destroy(fm);
1969
1970 fuse_abort_conn(fc);
1971 fuse_wait_aborted(fc);
1972
1973 if (!list_empty(&fc->entry)) {
1974 mutex_lock(&fuse_mutex);
1975 list_del(&fc->entry);
1976 fuse_ctl_remove_conn(fc);
1977 mutex_unlock(&fuse_mutex);
1978 }
1979 }
1980 EXPORT_SYMBOL_GPL(fuse_conn_destroy);
1981
fuse_sb_destroy(struct super_block * sb)1982 static void fuse_sb_destroy(struct super_block *sb)
1983 {
1984 struct fuse_mount *fm = get_fuse_mount_super(sb);
1985 bool last;
1986
1987 if (sb->s_root) {
1988 last = fuse_mount_remove(fm);
1989 if (last)
1990 fuse_conn_destroy(fm);
1991 }
1992 }
1993
fuse_mount_destroy(struct fuse_mount * fm)1994 void fuse_mount_destroy(struct fuse_mount *fm)
1995 {
1996 fuse_conn_put(fm->fc);
1997 kfree(fm);
1998 }
1999 EXPORT_SYMBOL(fuse_mount_destroy);
2000
fuse_kill_sb_anon(struct super_block * sb)2001 static void fuse_kill_sb_anon(struct super_block *sb)
2002 {
2003 fuse_sb_destroy(sb);
2004 kill_anon_super(sb);
2005 fuse_mount_destroy(get_fuse_mount_super(sb));
2006 }
2007
2008 static struct file_system_type fuse_fs_type = {
2009 .owner = THIS_MODULE,
2010 .name = "fuse",
2011 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
2012 .init_fs_context = fuse_init_fs_context,
2013 .parameters = fuse_fs_parameters,
2014 .kill_sb = fuse_kill_sb_anon,
2015 };
2016 MODULE_ALIAS_FS("fuse");
2017
2018 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)2019 static void fuse_kill_sb_blk(struct super_block *sb)
2020 {
2021 fuse_sb_destroy(sb);
2022 kill_block_super(sb);
2023 fuse_mount_destroy(get_fuse_mount_super(sb));
2024 }
2025
2026 static struct file_system_type fuseblk_fs_type = {
2027 .owner = THIS_MODULE,
2028 .name = "fuseblk",
2029 .init_fs_context = fuse_init_fs_context,
2030 .parameters = fuse_fs_parameters,
2031 .kill_sb = fuse_kill_sb_blk,
2032 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
2033 };
2034 MODULE_ALIAS_FS("fuseblk");
2035
register_fuseblk(void)2036 static inline int register_fuseblk(void)
2037 {
2038 return register_filesystem(&fuseblk_fs_type);
2039 }
2040
unregister_fuseblk(void)2041 static inline void unregister_fuseblk(void)
2042 {
2043 unregister_filesystem(&fuseblk_fs_type);
2044 }
2045 #else
register_fuseblk(void)2046 static inline int register_fuseblk(void)
2047 {
2048 return 0;
2049 }
2050
unregister_fuseblk(void)2051 static inline void unregister_fuseblk(void)
2052 {
2053 }
2054 #endif
2055
fuse_inode_init_once(void * foo)2056 static void fuse_inode_init_once(void *foo)
2057 {
2058 struct inode *inode = foo;
2059
2060 inode_init_once(inode);
2061 }
2062
fuse_fs_init(void)2063 static int __init fuse_fs_init(void)
2064 {
2065 int err;
2066
2067 fuse_inode_cachep = kmem_cache_create("fuse_inode",
2068 sizeof(struct fuse_inode), 0,
2069 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
2070 fuse_inode_init_once);
2071 err = -ENOMEM;
2072 if (!fuse_inode_cachep)
2073 goto out;
2074
2075 err = register_fuseblk();
2076 if (err)
2077 goto out2;
2078
2079 err = register_filesystem(&fuse_fs_type);
2080 if (err)
2081 goto out3;
2082
2083 return 0;
2084
2085 out3:
2086 unregister_fuseblk();
2087 out2:
2088 kmem_cache_destroy(fuse_inode_cachep);
2089 out:
2090 return err;
2091 }
2092
fuse_fs_cleanup(void)2093 static void fuse_fs_cleanup(void)
2094 {
2095 unregister_filesystem(&fuse_fs_type);
2096 unregister_fuseblk();
2097
2098 /*
2099 * Make sure all delayed rcu free inodes are flushed before we
2100 * destroy cache.
2101 */
2102 rcu_barrier();
2103 kmem_cache_destroy(fuse_inode_cachep);
2104 }
2105
2106 static struct kobject *fuse_kobj;
2107
fuse_bpf_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)2108 static ssize_t fuse_bpf_show(struct kobject *kobj,
2109 struct kobj_attribute *attr, char *buff)
2110 {
2111 return sysfs_emit(buff, "supported\n");
2112 }
2113
2114 static struct kobj_attribute fuse_bpf_attr =
2115 __ATTR_RO(fuse_bpf);
2116
2117 static struct attribute *bpf_features[] = {
2118 &fuse_bpf_attr.attr,
2119 NULL,
2120 };
2121
2122 static const struct attribute_group bpf_features_group = {
2123 .name = "features",
2124 .attrs = bpf_features,
2125 };
2126
2127 /*
2128 * TODO Remove this once fuse-bpf is upstreamed
2129 *
2130 * bpf_prog_type_fuse exports the bpf_prog_type_fuse 'constant', which cannot be
2131 * constant until the code is upstreamed
2132 */
bpf_prog_type_fuse_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)2133 static ssize_t bpf_prog_type_fuse_show(struct kobject *kobj,
2134 struct kobj_attribute *attr, char *buff)
2135 {
2136 return sysfs_emit(buff, "%d\n", BPF_PROG_TYPE_FUSE);
2137 }
2138
2139 static struct kobj_attribute bpf_prog_type_fuse_attr =
2140 __ATTR_RO(bpf_prog_type_fuse);
2141
2142 static struct attribute *bpf_attributes[] = {
2143 &bpf_prog_type_fuse_attr.attr,
2144 NULL,
2145 };
2146
2147 static const struct attribute_group bpf_attr_group = {
2148 .attrs = bpf_attributes,
2149 };
2150
2151 static const struct attribute_group *attribute_groups[] = {
2152 &bpf_features_group,
2153 &bpf_attr_group,
2154 NULL
2155 };
2156
2157 /* TODO remove to here */
2158
fuse_sysfs_init(void)2159 static int fuse_sysfs_init(void)
2160 {
2161 int err;
2162
2163 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
2164 if (!fuse_kobj) {
2165 err = -ENOMEM;
2166 goto out_err;
2167 }
2168
2169 err = sysfs_create_mount_point(fuse_kobj, "connections");
2170 if (err)
2171 goto out_fuse_unregister;
2172
2173 /* TODO Remove when BPF_PROG_TYPE_FUSE is upstreamed */
2174 err = sysfs_create_groups(fuse_kobj, attribute_groups);
2175 if (err)
2176 goto out_fuse_remove_mount_point;
2177
2178 return 0;
2179
2180 out_fuse_remove_mount_point:
2181 sysfs_remove_mount_point(fuse_kobj, "connections");
2182 out_fuse_unregister:
2183 kobject_put(fuse_kobj);
2184 out_err:
2185 return err;
2186 }
2187
fuse_sysfs_cleanup(void)2188 static void fuse_sysfs_cleanup(void)
2189 {
2190 sysfs_remove_groups(fuse_kobj, attribute_groups);
2191 sysfs_remove_mount_point(fuse_kobj, "connections");
2192 kobject_put(fuse_kobj);
2193 }
2194
fuse_init(void)2195 static int __init fuse_init(void)
2196 {
2197 int res;
2198
2199 pr_info("init (API version %i.%i)\n",
2200 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2201
2202 INIT_LIST_HEAD(&fuse_conn_list);
2203 res = fuse_fs_init();
2204 if (res)
2205 goto err;
2206
2207 res = fuse_dev_init();
2208 if (res)
2209 goto err_fs_cleanup;
2210
2211 res = fuse_sysfs_init();
2212 if (res)
2213 goto err_dev_cleanup;
2214
2215 res = fuse_ctl_init();
2216 if (res)
2217 goto err_sysfs_cleanup;
2218
2219 #ifdef CONFIG_FUSE_BPF
2220 res = fuse_bpf_init();
2221 if (res)
2222 goto err_ctl_cleanup;
2223 #endif
2224
2225 sanitize_global_limit(&max_user_bgreq);
2226 sanitize_global_limit(&max_user_congthresh);
2227
2228 return 0;
2229
2230 #ifdef CONFIG_FUSE_BPF
2231 err_ctl_cleanup:
2232 fuse_ctl_cleanup();
2233 #endif
2234 err_sysfs_cleanup:
2235 fuse_sysfs_cleanup();
2236 err_dev_cleanup:
2237 fuse_dev_cleanup();
2238 err_fs_cleanup:
2239 fuse_fs_cleanup();
2240 err:
2241 return res;
2242 }
2243
fuse_exit(void)2244 static void __exit fuse_exit(void)
2245 {
2246 pr_debug("exit\n");
2247
2248 fuse_ctl_cleanup();
2249 fuse_sysfs_cleanup();
2250 fuse_fs_cleanup();
2251 #ifdef CONFIG_FUSE_BPF
2252 fuse_bpf_cleanup();
2253 #endif
2254 fuse_dev_cleanup();
2255 }
2256
2257 module_init(fuse_init);
2258 module_exit(fuse_exit);
2259