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 u64 flags = arg->flags | (u64) arg->flags2 << 32;
1316
1317 ra_pages = arg->max_readahead / PAGE_SIZE;
1318 if (flags & FUSE_ASYNC_READ)
1319 fc->async_read = 1;
1320 if (!(flags & FUSE_POSIX_LOCKS))
1321 fc->no_lock = 1;
1322 if (arg->minor >= 17) {
1323 if (!(flags & FUSE_FLOCK_LOCKS))
1324 fc->no_flock = 1;
1325 } else {
1326 if (!(flags & FUSE_POSIX_LOCKS))
1327 fc->no_flock = 1;
1328 }
1329 if (flags & FUSE_ATOMIC_O_TRUNC)
1330 fc->atomic_o_trunc = 1;
1331 if (arg->minor >= 9) {
1332 /* LOOKUP has dependency on proto version */
1333 if (flags & FUSE_EXPORT_SUPPORT)
1334 fc->export_support = 1;
1335 }
1336 if (flags & FUSE_BIG_WRITES)
1337 fc->big_writes = 1;
1338 if (flags & FUSE_DONT_MASK)
1339 fc->dont_mask = 1;
1340 if (flags & FUSE_AUTO_INVAL_DATA)
1341 fc->auto_inval_data = 1;
1342 else if (flags & FUSE_EXPLICIT_INVAL_DATA)
1343 fc->explicit_inval_data = 1;
1344 if (flags & FUSE_DO_READDIRPLUS) {
1345 fc->do_readdirplus = 1;
1346 if (flags & FUSE_READDIRPLUS_AUTO)
1347 fc->readdirplus_auto = 1;
1348 }
1349 if (flags & FUSE_ASYNC_DIO)
1350 fc->async_dio = 1;
1351 if (flags & FUSE_WRITEBACK_CACHE)
1352 fc->writeback_cache = 1;
1353 if (flags & FUSE_PARALLEL_DIROPS)
1354 fc->parallel_dirops = 1;
1355 if (flags & FUSE_HANDLE_KILLPRIV)
1356 fc->handle_killpriv = 1;
1357 if (arg->time_gran && arg->time_gran <= 1000000000)
1358 fm->sb->s_time_gran = arg->time_gran;
1359 if ((flags & FUSE_POSIX_ACL)) {
1360 fc->default_permissions = 1;
1361 fc->posix_acl = 1;
1362 fm->sb->s_xattr = fuse_acl_xattr_handlers;
1363 }
1364 if (flags & FUSE_CACHE_SYMLINKS)
1365 fc->cache_symlinks = 1;
1366 if (flags & FUSE_ABORT_ERROR)
1367 fc->abort_err = 1;
1368 if (flags & FUSE_MAX_PAGES) {
1369 fc->max_pages =
1370 min_t(unsigned int, fc->max_pages_limit,
1371 max_t(unsigned int, arg->max_pages, 1));
1372 }
1373 if (IS_ENABLED(CONFIG_FUSE_DAX) &&
1374 flags & FUSE_MAP_ALIGNMENT &&
1375 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1376 ok = false;
1377 }
1378 if (flags & FUSE_HANDLE_KILLPRIV_V2) {
1379 fc->handle_killpriv_v2 = 1;
1380 fm->sb->s_flags |= SB_NOSEC;
1381 }
1382 if (flags & FUSE_PASSTHROUGH) {
1383 fc->passthrough = 1;
1384 /* Prevent further stacking */
1385 fm->sb->s_stack_depth =
1386 FILESYSTEM_MAX_STACK_DEPTH;
1387 }
1388 if (flags & FUSE_SETXATTR_EXT)
1389 fc->setxattr_ext = 1;
1390 if (flags & FUSE_SECURITY_CTX)
1391 fc->init_security = 1;
1392 } else {
1393 ra_pages = fc->max_read / PAGE_SIZE;
1394 fc->no_lock = 1;
1395 fc->no_flock = 1;
1396 }
1397
1398 fm->sb->s_bdi->ra_pages =
1399 min(fm->sb->s_bdi->ra_pages, ra_pages);
1400 fc->minor = arg->minor;
1401 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1402 fc->max_write = max_t(unsigned, 4096, fc->max_write);
1403 fc->conn_init = 1;
1404 }
1405 kfree(ia);
1406
1407 if (!ok) {
1408 fc->conn_init = 0;
1409 fc->conn_error = 1;
1410 }
1411
1412 fuse_set_initialized(fc);
1413 wake_up_all(&fc->blocked_waitq);
1414 }
1415
fuse_send_init(struct fuse_mount * fm)1416 void fuse_send_init(struct fuse_mount *fm)
1417 {
1418 struct fuse_init_args *ia;
1419 u64 flags;
1420
1421 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1422
1423 ia->in.major = FUSE_KERNEL_VERSION;
1424 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1425 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1426 flags =
1427 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1428 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1429 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1430 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1431 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1432 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1433 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1434 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1435 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1436 FUSE_PASSTHROUGH |
1437 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
1438 FUSE_SECURITY_CTX;
1439 #ifdef CONFIG_FUSE_DAX
1440 if (fm->fc->dax)
1441 flags |= FUSE_MAP_ALIGNMENT;
1442 #endif
1443 if (fm->fc->auto_submounts)
1444 flags |= FUSE_SUBMOUNTS;
1445
1446 ia->in.flags = flags;
1447 ia->in.flags2 = flags >> 32;
1448
1449 ia->args.opcode = FUSE_INIT;
1450 ia->args.in_numargs = 1;
1451 ia->args.in_args[0].size = sizeof(ia->in);
1452 ia->args.in_args[0].value = &ia->in;
1453 ia->args.out_numargs = 1;
1454 /* Variable length argument used for backward compatibility
1455 with interface version < 7.5. Rest of init_out is zeroed
1456 by do_get_request(), so a short reply is not a problem */
1457 ia->args.out_argvar = true;
1458 ia->args.out_args[0].size = sizeof(ia->out);
1459 ia->args.out_args[0].value = &ia->out;
1460 ia->args.force = true;
1461 ia->args.nocreds = true;
1462 ia->args.end = process_init_reply;
1463
1464 if (unlikely(fm->fc->no_daemon) || fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1465 process_init_reply(fm, &ia->args, -ENOTCONN);
1466 }
1467 EXPORT_SYMBOL_GPL(fuse_send_init);
1468
free_fuse_passthrough(int id,void * p,void * data)1469 static int free_fuse_passthrough(int id, void *p, void *data)
1470 {
1471 struct fuse_passthrough *passthrough = (struct fuse_passthrough *)p;
1472
1473 fuse_passthrough_release(passthrough);
1474 kfree(p);
1475
1476 return 0;
1477 }
1478
fuse_free_conn(struct fuse_conn * fc)1479 void fuse_free_conn(struct fuse_conn *fc)
1480 {
1481 WARN_ON(!list_empty(&fc->devices));
1482 idr_for_each(&fc->passthrough_req, free_fuse_passthrough, NULL);
1483 idr_destroy(&fc->passthrough_req);
1484 kfree_rcu(fc, rcu);
1485 }
1486 EXPORT_SYMBOL_GPL(fuse_free_conn);
1487
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1488 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1489 {
1490 int err;
1491 char *suffix = "";
1492
1493 if (sb->s_bdev) {
1494 suffix = "-fuseblk";
1495 /*
1496 * sb->s_bdi points to blkdev's bdi however we want to redirect
1497 * it to our private bdi...
1498 */
1499 bdi_put(sb->s_bdi);
1500 sb->s_bdi = &noop_backing_dev_info;
1501 }
1502 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1503 MINOR(fc->dev), suffix);
1504 if (err)
1505 return err;
1506
1507 /* fuse does it's own writeback accounting */
1508 sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1509 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
1510
1511 /*
1512 * For a single fuse filesystem use max 1% of dirty +
1513 * writeback threshold.
1514 *
1515 * This gives about 1M of write buffer for memory maps on a
1516 * machine with 1G and 10% dirty_ratio, which should be more
1517 * than enough.
1518 *
1519 * Privileged users can raise it by writing to
1520 *
1521 * /sys/class/bdi/<bdi>/max_ratio
1522 */
1523 bdi_set_max_ratio(sb->s_bdi, 1);
1524
1525 return 0;
1526 }
1527
fuse_dev_alloc(void)1528 struct fuse_dev *fuse_dev_alloc(void)
1529 {
1530 struct fuse_dev *fud;
1531 struct list_head *pq;
1532
1533 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1534 if (!fud)
1535 return NULL;
1536
1537 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1538 if (!pq) {
1539 kfree(fud);
1540 return NULL;
1541 }
1542
1543 fud->pq.processing = pq;
1544 fuse_pqueue_init(&fud->pq);
1545
1546 return fud;
1547 }
1548 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1549
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1550 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1551 {
1552 fud->fc = fuse_conn_get(fc);
1553 spin_lock(&fc->lock);
1554 list_add_tail(&fud->entry, &fc->devices);
1555 spin_unlock(&fc->lock);
1556 }
1557 EXPORT_SYMBOL_GPL(fuse_dev_install);
1558
fuse_dev_alloc_install(struct fuse_conn * fc)1559 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1560 {
1561 struct fuse_dev *fud;
1562
1563 fud = fuse_dev_alloc();
1564 if (!fud)
1565 return NULL;
1566
1567 fuse_dev_install(fud, fc);
1568 return fud;
1569 }
1570 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1571
fuse_dev_free(struct fuse_dev * fud)1572 void fuse_dev_free(struct fuse_dev *fud)
1573 {
1574 struct fuse_conn *fc = fud->fc;
1575
1576 if (fc) {
1577 spin_lock(&fc->lock);
1578 list_del(&fud->entry);
1579 spin_unlock(&fc->lock);
1580
1581 fuse_conn_put(fc);
1582 }
1583 kfree(fud->pq.processing);
1584 kfree(fud);
1585 }
1586 EXPORT_SYMBOL_GPL(fuse_dev_free);
1587
fuse_sb_defaults(struct super_block * sb)1588 static void fuse_sb_defaults(struct super_block *sb)
1589 {
1590 sb->s_magic = FUSE_SUPER_MAGIC;
1591 sb->s_op = &fuse_super_operations;
1592 sb->s_xattr = fuse_xattr_handlers;
1593 sb->s_maxbytes = MAX_LFS_FILESIZE;
1594 sb->s_time_gran = 1;
1595 sb->s_export_op = &fuse_export_operations;
1596 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1597 if (sb->s_user_ns != &init_user_ns)
1598 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1599 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1600
1601 /*
1602 * If we are not in the initial user namespace posix
1603 * acls must be translated.
1604 */
1605 if (sb->s_user_ns != &init_user_ns)
1606 sb->s_xattr = fuse_no_acl_xattr_handlers;
1607 }
1608
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)1609 static int fuse_fill_super_submount(struct super_block *sb,
1610 struct fuse_inode *parent_fi)
1611 {
1612 struct fuse_mount *fm = get_fuse_mount_super(sb);
1613 struct super_block *parent_sb = parent_fi->inode.i_sb;
1614 struct fuse_attr root_attr;
1615 struct inode *root;
1616 struct fuse_submount_lookup *sl;
1617 struct fuse_inode *fi;
1618
1619 fuse_sb_defaults(sb);
1620 fm->sb = sb;
1621
1622 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1623 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1624
1625 sb->s_xattr = parent_sb->s_xattr;
1626 sb->s_time_gran = parent_sb->s_time_gran;
1627 sb->s_blocksize = parent_sb->s_blocksize;
1628 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1629 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1630 if (parent_sb->s_subtype && !sb->s_subtype)
1631 return -ENOMEM;
1632
1633 fuse_fill_attr_from_inode(&root_attr, &parent_fi->inode);
1634 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1635 /*
1636 * This inode is just a duplicate, so it is not looked up and
1637 * its nlookup should not be incremented. fuse_iget() does
1638 * that, though, so undo it here.
1639 */
1640 fi = get_fuse_inode(root);
1641 fi->nlookup--;
1642
1643 sb->s_d_op = &fuse_dentry_operations;
1644 sb->s_root = d_make_root(root);
1645 if (!sb->s_root)
1646 return -ENOMEM;
1647
1648 /*
1649 * Grab the parent's submount_lookup pointer and take a
1650 * reference on the shared nlookup from the parent. This is to
1651 * prevent the last forget for this nodeid from getting
1652 * triggered until all users have finished with it.
1653 */
1654 sl = parent_fi->submount_lookup;
1655 WARN_ON(!sl);
1656 if (sl) {
1657 refcount_inc(&sl->count);
1658 fi->submount_lookup = sl;
1659 }
1660
1661 return 0;
1662 }
1663
1664 /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1665 static int fuse_get_tree_submount(struct fs_context *fsc)
1666 {
1667 struct fuse_mount *fm;
1668 struct fuse_inode *mp_fi = fsc->fs_private;
1669 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1670 struct super_block *sb;
1671 int err;
1672
1673 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1674 if (!fm)
1675 return -ENOMEM;
1676
1677 fm->fc = fuse_conn_get(fc);
1678 fsc->s_fs_info = fm;
1679 sb = sget_fc(fsc, NULL, set_anon_super_fc);
1680 if (fsc->s_fs_info)
1681 fuse_mount_destroy(fm);
1682 if (IS_ERR(sb))
1683 return PTR_ERR(sb);
1684
1685 /* Initialize superblock, making @mp_fi its root */
1686 err = fuse_fill_super_submount(sb, mp_fi);
1687 if (err) {
1688 deactivate_locked_super(sb);
1689 return err;
1690 }
1691
1692 down_write(&fc->killsb);
1693 list_add_tail(&fm->fc_entry, &fc->mounts);
1694 up_write(&fc->killsb);
1695
1696 sb->s_flags |= SB_ACTIVE;
1697 fsc->root = dget(sb->s_root);
1698
1699 return 0;
1700 }
1701
1702 static const struct fs_context_operations fuse_context_submount_ops = {
1703 .get_tree = fuse_get_tree_submount,
1704 };
1705
fuse_init_fs_context_submount(struct fs_context * fsc)1706 int fuse_init_fs_context_submount(struct fs_context *fsc)
1707 {
1708 fsc->ops = &fuse_context_submount_ops;
1709 return 0;
1710 }
1711 EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1712
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1713 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1714 {
1715 struct fuse_dev *fud = NULL;
1716 struct fuse_mount *fm = get_fuse_mount_super(sb);
1717 struct fuse_conn *fc = fm->fc;
1718 struct inode *root;
1719 struct dentry *root_dentry;
1720 int err;
1721
1722 err = -EINVAL;
1723 if (sb->s_flags & SB_MANDLOCK)
1724 goto err;
1725
1726 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
1727 fuse_sb_defaults(sb);
1728
1729 if (ctx->is_bdev) {
1730 #ifdef CONFIG_BLOCK
1731 err = -EINVAL;
1732 if (!sb_set_blocksize(sb, ctx->blksize))
1733 goto err;
1734 #endif
1735 } else {
1736 sb->s_blocksize = PAGE_SIZE;
1737 sb->s_blocksize_bits = PAGE_SHIFT;
1738 }
1739
1740 sb->s_subtype = ctx->subtype;
1741 ctx->subtype = NULL;
1742 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1743 err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1744 if (err)
1745 goto err;
1746 }
1747
1748 if (ctx->fudptr) {
1749 err = -ENOMEM;
1750 fud = fuse_dev_alloc_install(fc);
1751 if (!fud)
1752 goto err_free_dax;
1753 }
1754
1755 fc->dev = sb->s_dev;
1756 fm->sb = sb;
1757 err = fuse_bdi_init(fc, sb);
1758 if (err)
1759 goto err_dev_free;
1760
1761 /* Handle umasking inside the fuse code */
1762 if (sb->s_flags & SB_POSIXACL)
1763 fc->dont_mask = 1;
1764 sb->s_flags |= SB_POSIXACL;
1765
1766 fc->default_permissions = ctx->default_permissions;
1767 fc->allow_other = ctx->allow_other;
1768 fc->user_id = ctx->user_id;
1769 fc->group_id = ctx->group_id;
1770 fc->legacy_opts_show = ctx->legacy_opts_show;
1771 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1772 fc->destroy = ctx->destroy;
1773 fc->no_control = ctx->no_control;
1774 fc->no_force_umount = ctx->no_force_umount;
1775 fc->no_daemon = ctx->no_daemon;
1776
1777 err = -ENOMEM;
1778 root = fuse_get_root_inode(sb, ctx->rootmode, ctx->root_bpf,
1779 ctx->root_dir);
1780 sb->s_d_op = &fuse_root_dentry_operations;
1781 root_dentry = d_make_root(root);
1782 if (!root_dentry)
1783 goto err_dev_free;
1784 fuse_init_dentry_root(root_dentry, ctx->root_dir);
1785 /* Root dentry doesn't have .d_revalidate */
1786 sb->s_d_op = &fuse_dentry_operations;
1787
1788 mutex_lock(&fuse_mutex);
1789 err = -EINVAL;
1790 if (ctx->fudptr && *ctx->fudptr)
1791 goto err_unlock;
1792
1793 err = fuse_ctl_add_conn(fc);
1794 if (err)
1795 goto err_unlock;
1796
1797 list_add_tail(&fc->entry, &fuse_conn_list);
1798 sb->s_root = root_dentry;
1799 if (ctx->fudptr)
1800 *ctx->fudptr = fud;
1801 mutex_unlock(&fuse_mutex);
1802 return 0;
1803
1804 err_unlock:
1805 mutex_unlock(&fuse_mutex);
1806 dput(root_dentry);
1807 err_dev_free:
1808 if (fud)
1809 fuse_dev_free(fud);
1810 err_free_dax:
1811 if (IS_ENABLED(CONFIG_FUSE_DAX))
1812 fuse_dax_conn_free(fc);
1813 err:
1814 return err;
1815 }
1816 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1817
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1818 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1819 {
1820 struct fuse_fs_context *ctx = fsc->fs_private;
1821 int err;
1822
1823 if (!ctx->no_daemon) {
1824 if (!ctx->file || !ctx->rootmode_present ||
1825 !ctx->user_id_present || !ctx->group_id_present)
1826 return -EINVAL;
1827
1828 /*
1829 * Require mount to happen from the same user namespace which
1830 * opened /dev/fuse to prevent potential attacks.
1831 */
1832 if ((ctx->file->f_op != &fuse_dev_operations) ||
1833 (ctx->file->f_cred->user_ns != sb->s_user_ns))
1834 return -EINVAL;
1835 ctx->fudptr = &ctx->file->private_data;
1836 }
1837
1838 err = fuse_fill_super_common(sb, ctx);
1839 if (err)
1840 return err;
1841 /* file->private_data shall be visible on all CPUs after this */
1842 smp_mb();
1843 fuse_send_init(get_fuse_mount_super(sb));
1844 return 0;
1845 }
1846
1847 /*
1848 * This is the path where user supplied an already initialized fuse dev. In
1849 * this case never create a new super if the old one is gone.
1850 */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)1851 static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1852 {
1853 return -ENOTCONN;
1854 }
1855
fuse_test_super(struct super_block * sb,struct fs_context * fsc)1856 static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
1857 {
1858
1859 return fsc->sget_key == get_fuse_conn_super(sb);
1860 }
1861
fuse_get_tree(struct fs_context * fsc)1862 static int fuse_get_tree(struct fs_context *fsc)
1863 {
1864 struct fuse_fs_context *ctx = fsc->fs_private;
1865 struct fuse_dev *fud;
1866 struct fuse_conn *fc;
1867 struct fuse_mount *fm;
1868 struct super_block *sb;
1869 int err;
1870
1871 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1872 if (!fc)
1873 return -ENOMEM;
1874
1875 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1876 if (!fm) {
1877 kfree(fc);
1878 return -ENOMEM;
1879 }
1880
1881 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
1882 fc->release = fuse_free_conn;
1883
1884 fsc->s_fs_info = fm;
1885
1886 if (ctx->fd_present)
1887 ctx->file = fget(ctx->fd);
1888
1889 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
1890 err = get_tree_bdev(fsc, fuse_fill_super);
1891 goto out;
1892 }
1893 /*
1894 * While block dev mount can be initialized with a dummy device fd
1895 * (found by device name), normal fuse mounts can't
1896 */
1897 err = -EINVAL;
1898 if (!ctx->file)
1899 goto out;
1900
1901 /*
1902 * Allow creating a fuse mount with an already initialized fuse
1903 * connection
1904 */
1905 fud = READ_ONCE(ctx->file->private_data);
1906 if (ctx->file->f_op == &fuse_dev_operations && fud) {
1907 fsc->sget_key = fud->fc;
1908 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
1909 err = PTR_ERR_OR_ZERO(sb);
1910 if (!IS_ERR(sb))
1911 fsc->root = dget(sb->s_root);
1912 } else {
1913 err = get_tree_nodev(fsc, fuse_fill_super);
1914 }
1915 out:
1916 if (fsc->s_fs_info)
1917 fuse_mount_destroy(fm);
1918 if (ctx->file)
1919 fput(ctx->file);
1920 return err;
1921 }
1922
1923 static const struct fs_context_operations fuse_context_ops = {
1924 .free = fuse_free_fsc,
1925 .parse_param = fuse_parse_param,
1926 .reconfigure = fuse_reconfigure,
1927 .get_tree = fuse_get_tree,
1928 };
1929
1930 /*
1931 * Set up the filesystem mount context.
1932 */
fuse_init_fs_context(struct fs_context * fsc)1933 static int fuse_init_fs_context(struct fs_context *fsc)
1934 {
1935 struct fuse_fs_context *ctx;
1936
1937 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1938 if (!ctx)
1939 return -ENOMEM;
1940
1941 ctx->max_read = ~0;
1942 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1943 ctx->legacy_opts_show = true;
1944
1945 #ifdef CONFIG_BLOCK
1946 if (fsc->fs_type == &fuseblk_fs_type) {
1947 ctx->is_bdev = true;
1948 ctx->destroy = true;
1949 }
1950 #endif
1951
1952 fsc->fs_private = ctx;
1953 fsc->ops = &fuse_context_ops;
1954 return 0;
1955 }
1956
fuse_mount_remove(struct fuse_mount * fm)1957 bool fuse_mount_remove(struct fuse_mount *fm)
1958 {
1959 struct fuse_conn *fc = fm->fc;
1960 bool last = false;
1961
1962 down_write(&fc->killsb);
1963 list_del_init(&fm->fc_entry);
1964 if (list_empty(&fc->mounts))
1965 last = true;
1966 up_write(&fc->killsb);
1967
1968 return last;
1969 }
1970 EXPORT_SYMBOL_GPL(fuse_mount_remove);
1971
fuse_conn_destroy(struct fuse_mount * fm)1972 void fuse_conn_destroy(struct fuse_mount *fm)
1973 {
1974 struct fuse_conn *fc = fm->fc;
1975
1976 if (fc->destroy)
1977 fuse_send_destroy(fm);
1978
1979 fuse_abort_conn(fc);
1980 fuse_wait_aborted(fc);
1981
1982 if (!list_empty(&fc->entry)) {
1983 mutex_lock(&fuse_mutex);
1984 list_del(&fc->entry);
1985 fuse_ctl_remove_conn(fc);
1986 mutex_unlock(&fuse_mutex);
1987 }
1988 }
1989 EXPORT_SYMBOL_GPL(fuse_conn_destroy);
1990
fuse_sb_destroy(struct super_block * sb)1991 static void fuse_sb_destroy(struct super_block *sb)
1992 {
1993 struct fuse_mount *fm = get_fuse_mount_super(sb);
1994 bool last;
1995
1996 if (sb->s_root) {
1997 last = fuse_mount_remove(fm);
1998 if (last)
1999 fuse_conn_destroy(fm);
2000 }
2001 }
2002
fuse_mount_destroy(struct fuse_mount * fm)2003 void fuse_mount_destroy(struct fuse_mount *fm)
2004 {
2005 fuse_conn_put(fm->fc);
2006 kfree(fm);
2007 }
2008 EXPORT_SYMBOL(fuse_mount_destroy);
2009
fuse_kill_sb_anon(struct super_block * sb)2010 static void fuse_kill_sb_anon(struct super_block *sb)
2011 {
2012 fuse_sb_destroy(sb);
2013 kill_anon_super(sb);
2014 fuse_mount_destroy(get_fuse_mount_super(sb));
2015 }
2016
2017 static struct file_system_type fuse_fs_type = {
2018 .owner = THIS_MODULE,
2019 .name = "fuse",
2020 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
2021 .init_fs_context = fuse_init_fs_context,
2022 .parameters = fuse_fs_parameters,
2023 .kill_sb = fuse_kill_sb_anon,
2024 };
2025 MODULE_ALIAS_FS("fuse");
2026
2027 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)2028 static void fuse_kill_sb_blk(struct super_block *sb)
2029 {
2030 fuse_sb_destroy(sb);
2031 kill_block_super(sb);
2032 fuse_mount_destroy(get_fuse_mount_super(sb));
2033 }
2034
2035 static struct file_system_type fuseblk_fs_type = {
2036 .owner = THIS_MODULE,
2037 .name = "fuseblk",
2038 .init_fs_context = fuse_init_fs_context,
2039 .parameters = fuse_fs_parameters,
2040 .kill_sb = fuse_kill_sb_blk,
2041 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
2042 };
2043 MODULE_ALIAS_FS("fuseblk");
2044
register_fuseblk(void)2045 static inline int register_fuseblk(void)
2046 {
2047 return register_filesystem(&fuseblk_fs_type);
2048 }
2049
unregister_fuseblk(void)2050 static inline void unregister_fuseblk(void)
2051 {
2052 unregister_filesystem(&fuseblk_fs_type);
2053 }
2054 #else
register_fuseblk(void)2055 static inline int register_fuseblk(void)
2056 {
2057 return 0;
2058 }
2059
unregister_fuseblk(void)2060 static inline void unregister_fuseblk(void)
2061 {
2062 }
2063 #endif
2064
fuse_inode_init_once(void * foo)2065 static void fuse_inode_init_once(void *foo)
2066 {
2067 struct inode *inode = foo;
2068
2069 inode_init_once(inode);
2070 }
2071
fuse_fs_init(void)2072 static int __init fuse_fs_init(void)
2073 {
2074 int err;
2075
2076 fuse_inode_cachep = kmem_cache_create("fuse_inode",
2077 sizeof(struct fuse_inode), 0,
2078 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
2079 fuse_inode_init_once);
2080 err = -ENOMEM;
2081 if (!fuse_inode_cachep)
2082 goto out;
2083
2084 err = register_fuseblk();
2085 if (err)
2086 goto out2;
2087
2088 err = register_filesystem(&fuse_fs_type);
2089 if (err)
2090 goto out3;
2091
2092 return 0;
2093
2094 out3:
2095 unregister_fuseblk();
2096 out2:
2097 kmem_cache_destroy(fuse_inode_cachep);
2098 out:
2099 return err;
2100 }
2101
fuse_fs_cleanup(void)2102 static void fuse_fs_cleanup(void)
2103 {
2104 unregister_filesystem(&fuse_fs_type);
2105 unregister_fuseblk();
2106
2107 /*
2108 * Make sure all delayed rcu free inodes are flushed before we
2109 * destroy cache.
2110 */
2111 rcu_barrier();
2112 kmem_cache_destroy(fuse_inode_cachep);
2113 }
2114
2115 static struct kobject *fuse_kobj;
2116
fuse_bpf_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)2117 static ssize_t fuse_bpf_show(struct kobject *kobj,
2118 struct kobj_attribute *attr, char *buff)
2119 {
2120 return sysfs_emit(buff, "supported\n");
2121 }
2122
2123 static struct kobj_attribute fuse_bpf_attr =
2124 __ATTR_RO(fuse_bpf);
2125
2126 static struct attribute *bpf_features[] = {
2127 &fuse_bpf_attr.attr,
2128 NULL,
2129 };
2130
2131 static const struct attribute_group bpf_features_group = {
2132 .name = "features",
2133 .attrs = bpf_features,
2134 };
2135
2136 /*
2137 * TODO Remove this once fuse-bpf is upstreamed
2138 *
2139 * bpf_prog_type_fuse exports the bpf_prog_type_fuse 'constant', which cannot be
2140 * constant until the code is upstreamed
2141 */
bpf_prog_type_fuse_show(struct kobject * kobj,struct kobj_attribute * attr,char * buff)2142 static ssize_t bpf_prog_type_fuse_show(struct kobject *kobj,
2143 struct kobj_attribute *attr, char *buff)
2144 {
2145 return sysfs_emit(buff, "%d\n", BPF_PROG_TYPE_FUSE);
2146 }
2147
2148 static struct kobj_attribute bpf_prog_type_fuse_attr =
2149 __ATTR_RO(bpf_prog_type_fuse);
2150
2151 static struct attribute *bpf_attributes[] = {
2152 &bpf_prog_type_fuse_attr.attr,
2153 NULL,
2154 };
2155
2156 static const struct attribute_group bpf_attr_group = {
2157 .attrs = bpf_attributes,
2158 };
2159
2160 static const struct attribute_group *attribute_groups[] = {
2161 &bpf_features_group,
2162 &bpf_attr_group,
2163 NULL
2164 };
2165
2166 /* TODO remove to here */
2167
fuse_sysfs_init(void)2168 static int fuse_sysfs_init(void)
2169 {
2170 int err;
2171
2172 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
2173 if (!fuse_kobj) {
2174 err = -ENOMEM;
2175 goto out_err;
2176 }
2177
2178 err = sysfs_create_mount_point(fuse_kobj, "connections");
2179 if (err)
2180 goto out_fuse_unregister;
2181
2182 /* TODO Remove when BPF_PROG_TYPE_FUSE is upstreamed */
2183 err = sysfs_create_groups(fuse_kobj, attribute_groups);
2184 if (err)
2185 goto out_fuse_remove_mount_point;
2186
2187 return 0;
2188
2189 out_fuse_remove_mount_point:
2190 sysfs_remove_mount_point(fuse_kobj, "connections");
2191 out_fuse_unregister:
2192 kobject_put(fuse_kobj);
2193 out_err:
2194 return err;
2195 }
2196
fuse_sysfs_cleanup(void)2197 static void fuse_sysfs_cleanup(void)
2198 {
2199 sysfs_remove_groups(fuse_kobj, attribute_groups);
2200 sysfs_remove_mount_point(fuse_kobj, "connections");
2201 kobject_put(fuse_kobj);
2202 }
2203
fuse_init(void)2204 static int __init fuse_init(void)
2205 {
2206 int res;
2207
2208 pr_info("init (API version %i.%i)\n",
2209 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2210
2211 INIT_LIST_HEAD(&fuse_conn_list);
2212 res = fuse_fs_init();
2213 if (res)
2214 goto err;
2215
2216 res = fuse_dev_init();
2217 if (res)
2218 goto err_fs_cleanup;
2219
2220 res = fuse_sysfs_init();
2221 if (res)
2222 goto err_dev_cleanup;
2223
2224 res = fuse_ctl_init();
2225 if (res)
2226 goto err_sysfs_cleanup;
2227
2228 #ifdef CONFIG_FUSE_BPF
2229 res = fuse_bpf_init();
2230 if (res)
2231 goto err_ctl_cleanup;
2232 #endif
2233
2234 sanitize_global_limit(&max_user_bgreq);
2235 sanitize_global_limit(&max_user_congthresh);
2236
2237 return 0;
2238
2239 #ifdef CONFIG_FUSE_BPF
2240 err_ctl_cleanup:
2241 fuse_ctl_cleanup();
2242 #endif
2243 err_sysfs_cleanup:
2244 fuse_sysfs_cleanup();
2245 err_dev_cleanup:
2246 fuse_dev_cleanup();
2247 err_fs_cleanup:
2248 fuse_fs_cleanup();
2249 err:
2250 return res;
2251 }
2252
fuse_exit(void)2253 static void __exit fuse_exit(void)
2254 {
2255 pr_debug("exit\n");
2256
2257 fuse_ctl_cleanup();
2258 fuse_sysfs_cleanup();
2259 fuse_fs_cleanup();
2260 #ifdef CONFIG_FUSE_BPF
2261 fuse_bpf_cleanup();
2262 #endif
2263 fuse_dev_cleanup();
2264 }
2265
2266 module_init(fuse_init);
2267 module_exit(fuse_exit);
2268