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