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