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_obj(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_obj(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 bool is_new_inode = false;
474
475 /*
476 * Auto mount points get their node id from the submount root, which is
477 * not a unique identifier within this filesystem.
478 *
479 * To avoid conflicts, do not place submount points into the inode hash
480 * table.
481 */
482 if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
483 S_ISDIR(attr->mode)) {
484 struct fuse_inode *fi;
485
486 inode = new_inode(sb);
487 if (!inode)
488 return NULL;
489
490 fuse_init_inode(inode, attr, fc);
491 fi = get_fuse_inode(inode);
492 fi->nodeid = nodeid;
493 fi->submount_lookup = fuse_alloc_submount_lookup();
494 if (!fi->submount_lookup) {
495 iput(inode);
496 return NULL;
497 }
498 /* Sets nlookup = 1 on fi->submount_lookup->nlookup */
499 fuse_init_submount_lookup(fi->submount_lookup, nodeid);
500 inode->i_flags |= S_AUTOMOUNT;
501 goto done;
502 }
503
504 retry:
505 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
506 if (!inode)
507 return NULL;
508
509 is_new_inode = inode_state_read_once(inode) & I_NEW;
510 if (is_new_inode) {
511 inode->i_flags |= S_NOATIME;
512 if (!fc->writeback_cache || !S_ISREG(attr->mode))
513 inode->i_flags |= S_NOCMTIME;
514 inode->i_generation = generation;
515 fuse_init_inode(inode, attr, fc);
516 } else if (fuse_stale_inode(inode, generation, attr)) {
517 /* nodeid was reused, any I/O on the old inode should fail */
518 fuse_make_bad(inode);
519 if (inode != d_inode(sb->s_root)) {
520 remove_inode_hash(inode);
521 iput(inode);
522 goto retry;
523 }
524 }
525 fi = get_fuse_inode(inode);
526 spin_lock(&fi->lock);
527 fi->nlookup++;
528 spin_unlock(&fi->lock);
529 done:
530 fuse_change_attributes_i(inode, attr, NULL, attr_valid, attr_version,
531 evict_ctr);
532 if (is_new_inode)
533 unlock_new_inode(inode);
534 return inode;
535 }
536
fuse_ilookup(struct fuse_conn * fc,u64 nodeid,struct fuse_mount ** fm)537 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
538 struct fuse_mount **fm)
539 {
540 struct fuse_mount *fm_iter;
541 struct inode *inode;
542
543 WARN_ON(!rwsem_is_locked(&fc->killsb));
544 list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
545 if (!fm_iter->sb)
546 continue;
547
548 inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
549 if (inode) {
550 if (fm)
551 *fm = fm_iter;
552 return inode;
553 }
554 }
555
556 return NULL;
557 }
558
fuse_reverse_inval_inode(struct fuse_conn * fc,u64 nodeid,loff_t offset,loff_t len)559 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
560 loff_t offset, loff_t len)
561 {
562 struct fuse_inode *fi;
563 struct inode *inode;
564 pgoff_t pg_start;
565 pgoff_t pg_end;
566
567 inode = fuse_ilookup(fc, nodeid, NULL);
568 if (!inode)
569 return -ENOENT;
570
571 fi = get_fuse_inode(inode);
572 spin_lock(&fi->lock);
573 fi->attr_version = atomic64_inc_return(&fc->attr_version);
574 spin_unlock(&fi->lock);
575
576 fuse_invalidate_attr(inode);
577 forget_all_cached_acls(inode);
578 if (offset >= 0) {
579 pg_start = offset >> PAGE_SHIFT;
580 if (len <= 0)
581 pg_end = -1;
582 else
583 pg_end = (offset + len - 1) >> PAGE_SHIFT;
584 invalidate_inode_pages2_range(inode->i_mapping,
585 pg_start, pg_end);
586 }
587 iput(inode);
588 return 0;
589 }
590
fuse_try_prune_one_inode(struct fuse_conn * fc,u64 nodeid)591 void fuse_try_prune_one_inode(struct fuse_conn *fc, u64 nodeid)
592 {
593 struct inode *inode;
594
595 inode = fuse_ilookup(fc, nodeid, NULL);
596 if (!inode)
597 return;
598 d_prune_aliases(inode);
599 iput(inode);
600 }
601
fuse_lock_inode(struct inode * inode)602 bool fuse_lock_inode(struct inode *inode)
603 {
604 bool locked = false;
605
606 if (!get_fuse_conn(inode)->parallel_dirops) {
607 mutex_lock(&get_fuse_inode(inode)->mutex);
608 locked = true;
609 }
610
611 return locked;
612 }
613
fuse_unlock_inode(struct inode * inode,bool locked)614 void fuse_unlock_inode(struct inode *inode, bool locked)
615 {
616 if (locked)
617 mutex_unlock(&get_fuse_inode(inode)->mutex);
618 }
619
fuse_umount_begin(struct super_block * sb)620 static void fuse_umount_begin(struct super_block *sb)
621 {
622 struct fuse_conn *fc = get_fuse_conn_super(sb);
623
624 if (fc->no_force_umount)
625 return;
626
627 fuse_abort_conn(fc);
628
629 // Only retire block-device-based superblocks.
630 if (sb->s_bdev != NULL)
631 retire_super(sb);
632 }
633
fuse_send_destroy(struct fuse_mount * fm)634 static void fuse_send_destroy(struct fuse_mount *fm)
635 {
636 if (fm->fc->conn_init) {
637 FUSE_ARGS(args);
638
639 args.opcode = FUSE_DESTROY;
640 args.force = true;
641 args.nocreds = true;
642 fuse_simple_request(fm, &args);
643 }
644 }
645
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)646 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
647 {
648 stbuf->f_type = FUSE_SUPER_MAGIC;
649 stbuf->f_bsize = attr->bsize;
650 stbuf->f_frsize = attr->frsize;
651 stbuf->f_blocks = attr->blocks;
652 stbuf->f_bfree = attr->bfree;
653 stbuf->f_bavail = attr->bavail;
654 stbuf->f_files = attr->files;
655 stbuf->f_ffree = attr->ffree;
656 stbuf->f_namelen = attr->namelen;
657 /* fsid is left zero */
658 }
659
fuse_statfs(struct dentry * dentry,struct kstatfs * buf)660 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
661 {
662 struct super_block *sb = dentry->d_sb;
663 struct fuse_mount *fm = get_fuse_mount_super(sb);
664 FUSE_ARGS(args);
665 struct fuse_statfs_out outarg;
666 int err;
667
668 if (!fuse_allow_current_process(fm->fc)) {
669 buf->f_type = FUSE_SUPER_MAGIC;
670 return 0;
671 }
672
673 memset(&outarg, 0, sizeof(outarg));
674 args.in_numargs = 0;
675 args.opcode = FUSE_STATFS;
676 args.nodeid = get_node_id(d_inode(dentry));
677 args.out_numargs = 1;
678 args.out_args[0].size = sizeof(outarg);
679 args.out_args[0].value = &outarg;
680 err = fuse_simple_request(fm, &args);
681 if (!err)
682 convert_fuse_statfs(buf, &outarg.st);
683 return err;
684 }
685
fuse_sync_bucket_alloc(void)686 static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void)
687 {
688 struct fuse_sync_bucket *bucket;
689
690 bucket = kzalloc_obj(*bucket, GFP_KERNEL | __GFP_NOFAIL);
691 if (bucket) {
692 init_waitqueue_head(&bucket->waitq);
693 /* Initial active count */
694 atomic_set(&bucket->count, 1);
695 }
696 return bucket;
697 }
698
fuse_sync_fs_writes(struct fuse_conn * fc)699 static void fuse_sync_fs_writes(struct fuse_conn *fc)
700 {
701 struct fuse_sync_bucket *bucket, *new_bucket;
702 int count;
703
704 new_bucket = fuse_sync_bucket_alloc();
705 spin_lock(&fc->lock);
706 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
707 count = atomic_read(&bucket->count);
708 WARN_ON(count < 1);
709 /* No outstanding writes? */
710 if (count == 1) {
711 spin_unlock(&fc->lock);
712 kfree(new_bucket);
713 return;
714 }
715
716 /*
717 * Completion of new bucket depends on completion of this bucket, so add
718 * one more count.
719 */
720 atomic_inc(&new_bucket->count);
721 rcu_assign_pointer(fc->curr_bucket, new_bucket);
722 spin_unlock(&fc->lock);
723 /*
724 * Drop initial active count. At this point if all writes in this and
725 * ancestor buckets complete, the count will go to zero and this task
726 * will be woken up.
727 */
728 atomic_dec(&bucket->count);
729
730 wait_event(bucket->waitq, atomic_read(&bucket->count) == 0);
731
732 /* Drop temp count on descendant bucket */
733 fuse_sync_bucket_dec(new_bucket);
734 kfree_rcu(bucket, rcu);
735 }
736
fuse_sync_fs(struct super_block * sb,int wait)737 static int fuse_sync_fs(struct super_block *sb, int wait)
738 {
739 struct fuse_mount *fm = get_fuse_mount_super(sb);
740 struct fuse_conn *fc = fm->fc;
741 struct fuse_syncfs_in inarg;
742 FUSE_ARGS(args);
743 int err;
744
745 /*
746 * Userspace cannot handle the wait == 0 case. Avoid a
747 * gratuitous roundtrip.
748 */
749 if (!wait)
750 return 0;
751
752 /* The filesystem is being unmounted. Nothing to do. */
753 if (!sb->s_root)
754 return 0;
755
756 if (!fc->sync_fs)
757 return 0;
758
759 fuse_sync_fs_writes(fc);
760
761 memset(&inarg, 0, sizeof(inarg));
762 args.in_numargs = 1;
763 args.in_args[0].size = sizeof(inarg);
764 args.in_args[0].value = &inarg;
765 args.opcode = FUSE_SYNCFS;
766 args.nodeid = get_node_id(sb->s_root->d_inode);
767 args.out_numargs = 0;
768
769 err = fuse_simple_request(fm, &args);
770 if (err == -ENOSYS) {
771 fc->sync_fs = 0;
772 err = 0;
773 }
774
775 return err;
776 }
777
778 enum {
779 OPT_SOURCE,
780 OPT_SUBTYPE,
781 OPT_FD,
782 OPT_ROOTMODE,
783 OPT_USER_ID,
784 OPT_GROUP_ID,
785 OPT_DEFAULT_PERMISSIONS,
786 OPT_ALLOW_OTHER,
787 OPT_MAX_READ,
788 OPT_BLKSIZE,
789 OPT_ERR
790 };
791
792 static const struct fs_parameter_spec fuse_fs_parameters[] = {
793 fsparam_string ("source", OPT_SOURCE),
794 fsparam_fd ("fd", OPT_FD),
795 fsparam_u32oct ("rootmode", OPT_ROOTMODE),
796 fsparam_uid ("user_id", OPT_USER_ID),
797 fsparam_gid ("group_id", OPT_GROUP_ID),
798 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
799 fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
800 fsparam_u32 ("max_read", OPT_MAX_READ),
801 fsparam_u32 ("blksize", OPT_BLKSIZE),
802 fsparam_string ("subtype", OPT_SUBTYPE),
803 {}
804 };
805
fuse_opt_fd(struct fs_context * fsc,struct file * file)806 static int fuse_opt_fd(struct fs_context *fsc, struct file *file)
807 {
808 struct fuse_fs_context *ctx = fsc->fs_private;
809
810 if (file->f_op != &fuse_dev_operations)
811 return invalfc(fsc, "fd is not a fuse device");
812 /*
813 * Require mount to happen from the same user namespace which
814 * opened /dev/fuse to prevent potential attacks.
815 */
816 if (file->f_cred->user_ns != fsc->user_ns)
817 return invalfc(fsc, "wrong user namespace for fuse device");
818
819 ctx->fud = file->private_data;
820 refcount_inc(&ctx->fud->ref);
821
822 return 0;
823 }
824
fuse_parse_param(struct fs_context * fsc,struct fs_parameter * param)825 static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
826 {
827 struct fs_parse_result result;
828 struct fuse_fs_context *ctx = fsc->fs_private;
829 int opt;
830 kuid_t kuid;
831 kgid_t kgid;
832
833 if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
834 /*
835 * Ignore options coming from mount(MS_REMOUNT) for backward
836 * compatibility.
837 */
838 if (fsc->oldapi)
839 return 0;
840
841 return invalfc(fsc, "No changes allowed in reconfigure");
842 }
843
844 opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
845 if (opt < 0)
846 return opt;
847
848 switch (opt) {
849 case OPT_SOURCE:
850 if (fsc->source)
851 return invalfc(fsc, "Multiple sources specified");
852 fsc->source = param->string;
853 param->string = NULL;
854 break;
855
856 case OPT_SUBTYPE:
857 if (ctx->subtype)
858 return invalfc(fsc, "Multiple subtypes specified");
859 ctx->subtype = param->string;
860 param->string = NULL;
861 return 0;
862
863 case OPT_FD:
864 if (param->type == fs_value_is_file) {
865 return fuse_opt_fd(fsc, param->file);
866 } else {
867 struct file *file __free(fput) = fget(result.uint_32);
868 if (!file)
869 return -EBADF;
870
871 return fuse_opt_fd(fsc, file);
872 }
873
874 case OPT_ROOTMODE:
875 if (!fuse_valid_type(result.uint_32))
876 return invalfc(fsc, "Invalid rootmode");
877 ctx->rootmode = result.uint_32;
878 ctx->rootmode_present = true;
879 break;
880
881 case OPT_USER_ID:
882 kuid = result.uid;
883 /*
884 * The requested uid must be representable in the
885 * filesystem's idmapping.
886 */
887 if (!kuid_has_mapping(fsc->user_ns, kuid))
888 return invalfc(fsc, "Invalid user_id");
889 ctx->user_id = kuid;
890 ctx->user_id_present = true;
891 break;
892
893 case OPT_GROUP_ID:
894 kgid = result.gid;
895 /*
896 * The requested gid must be representable in the
897 * filesystem's idmapping.
898 */
899 if (!kgid_has_mapping(fsc->user_ns, kgid))
900 return invalfc(fsc, "Invalid group_id");
901 ctx->group_id = kgid;
902 ctx->group_id_present = true;
903 break;
904
905 case OPT_DEFAULT_PERMISSIONS:
906 ctx->default_permissions = true;
907 break;
908
909 case OPT_ALLOW_OTHER:
910 ctx->allow_other = true;
911 break;
912
913 case OPT_MAX_READ:
914 ctx->max_read = result.uint_32;
915 break;
916
917 case OPT_BLKSIZE:
918 if (!ctx->is_bdev)
919 return invalfc(fsc, "blksize only supported for fuseblk");
920 ctx->blksize = result.uint_32;
921 break;
922
923 default:
924 return -EINVAL;
925 }
926
927 return 0;
928 }
929
fuse_free_fsc(struct fs_context * fsc)930 static void fuse_free_fsc(struct fs_context *fsc)
931 {
932 struct fuse_fs_context *ctx = fsc->fs_private;
933
934 if (ctx) {
935 if (ctx->fud)
936 fuse_dev_put(ctx->fud);
937 kfree(ctx->subtype);
938 kfree(ctx);
939 }
940 }
941
fuse_show_options(struct seq_file * m,struct dentry * root)942 static int fuse_show_options(struct seq_file *m, struct dentry *root)
943 {
944 struct super_block *sb = root->d_sb;
945 struct fuse_conn *fc = get_fuse_conn_super(sb);
946
947 if (fc->legacy_opts_show) {
948 seq_printf(m, ",user_id=%u",
949 from_kuid_munged(fc->user_ns, fc->user_id));
950 seq_printf(m, ",group_id=%u",
951 from_kgid_munged(fc->user_ns, fc->group_id));
952 if (fc->default_permissions)
953 seq_puts(m, ",default_permissions");
954 if (fc->allow_other)
955 seq_puts(m, ",allow_other");
956 if (fc->max_read != ~0)
957 seq_printf(m, ",max_read=%u", fc->max_read);
958 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
959 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
960 }
961 #ifdef CONFIG_FUSE_DAX
962 if (fc->dax_mode == FUSE_DAX_ALWAYS)
963 seq_puts(m, ",dax=always");
964 else if (fc->dax_mode == FUSE_DAX_NEVER)
965 seq_puts(m, ",dax=never");
966 else if (fc->dax_mode == FUSE_DAX_INODE_USER)
967 seq_puts(m, ",dax=inode");
968 #endif
969
970 return 0;
971 }
972
fuse_iqueue_init(struct fuse_iqueue * fiq,const struct fuse_iqueue_ops * ops,void * priv)973 static void fuse_iqueue_init(struct fuse_iqueue *fiq,
974 const struct fuse_iqueue_ops *ops,
975 void *priv)
976 {
977 memset(fiq, 0, sizeof(struct fuse_iqueue));
978 spin_lock_init(&fiq->lock);
979 init_waitqueue_head(&fiq->waitq);
980 INIT_LIST_HEAD(&fiq->pending);
981 INIT_LIST_HEAD(&fiq->interrupts);
982 fiq->forget_list_tail = &fiq->forget_list_head;
983 fiq->connected = 1;
984 fiq->ops = ops;
985 fiq->priv = priv;
986 }
987
fuse_pqueue_init(struct fuse_pqueue * fpq)988 void fuse_pqueue_init(struct fuse_pqueue *fpq)
989 {
990 unsigned int i;
991
992 spin_lock_init(&fpq->lock);
993 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
994 INIT_LIST_HEAD(&fpq->processing[i]);
995 INIT_LIST_HEAD(&fpq->io);
996 fpq->connected = 1;
997 }
998
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)999 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1000 struct user_namespace *user_ns,
1001 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
1002 {
1003 memset(fc, 0, sizeof(*fc));
1004 spin_lock_init(&fc->lock);
1005 spin_lock_init(&fc->bg_lock);
1006 init_rwsem(&fc->killsb);
1007 refcount_set(&fc->count, 1);
1008 atomic_set(&fc->epoch, 1);
1009 INIT_WORK(&fc->epoch_work, fuse_epoch_work);
1010 init_waitqueue_head(&fc->blocked_waitq);
1011 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
1012 INIT_LIST_HEAD(&fc->bg_queue);
1013 INIT_LIST_HEAD(&fc->entry);
1014 INIT_LIST_HEAD(&fc->devices);
1015 atomic_set(&fc->num_waiting, 0);
1016 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
1017 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
1018 atomic64_set(&fc->khctr, 0);
1019 fc->polled_files = RB_ROOT;
1020 fc->blocked = 0;
1021 fc->initialized = 0;
1022 fc->connected = 1;
1023 atomic64_set(&fc->attr_version, 1);
1024 atomic64_set(&fc->evict_ctr, 1);
1025 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
1026 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
1027 fc->user_ns = get_user_ns(user_ns);
1028 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
1029 fc->max_pages_limit = fuse_max_pages_limit;
1030 fc->name_max = FUSE_NAME_LOW_MAX;
1031 fc->timeout.req_timeout = 0;
1032
1033 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1034 fuse_backing_files_init(fc);
1035
1036 INIT_LIST_HEAD(&fc->mounts);
1037 list_add(&fm->fc_entry, &fc->mounts);
1038 fm->fc = fc;
1039 }
1040 EXPORT_SYMBOL_GPL(fuse_conn_init);
1041
delayed_release(struct rcu_head * p)1042 static void delayed_release(struct rcu_head *p)
1043 {
1044 struct fuse_conn *fc = container_of(p, struct fuse_conn, rcu);
1045
1046 fuse_uring_destruct(fc);
1047
1048 put_user_ns(fc->user_ns);
1049 fc->release(fc);
1050 }
1051
fuse_conn_put(struct fuse_conn * fc)1052 void fuse_conn_put(struct fuse_conn *fc)
1053 {
1054 struct fuse_iqueue *fiq = &fc->iq;
1055 struct fuse_sync_bucket *bucket;
1056
1057 if (!refcount_dec_and_test(&fc->count))
1058 return;
1059
1060 if (IS_ENABLED(CONFIG_FUSE_DAX))
1061 fuse_dax_conn_free(fc);
1062 if (fc->timeout.req_timeout)
1063 cancel_delayed_work_sync(&fc->timeout.work);
1064 cancel_work_sync(&fc->epoch_work);
1065 if (fiq->ops->release)
1066 fiq->ops->release(fiq);
1067 put_pid_ns(fc->pid_ns);
1068 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
1069 if (bucket) {
1070 WARN_ON(atomic_read(&bucket->count) != 1);
1071 kfree(bucket);
1072 }
1073 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1074 fuse_backing_files_free(fc);
1075 call_rcu(&fc->rcu, delayed_release);
1076 }
1077 EXPORT_SYMBOL_GPL(fuse_conn_put);
1078
fuse_conn_get(struct fuse_conn * fc)1079 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
1080 {
1081 refcount_inc(&fc->count);
1082 return fc;
1083 }
1084 EXPORT_SYMBOL_GPL(fuse_conn_get);
1085
fuse_get_root_inode(struct super_block * sb,unsigned int mode)1086 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned int mode)
1087 {
1088 struct fuse_attr attr;
1089 memset(&attr, 0, sizeof(attr));
1090
1091 attr.mode = mode;
1092 attr.ino = FUSE_ROOT_ID;
1093 attr.nlink = 1;
1094 return fuse_iget(sb, FUSE_ROOT_ID, 0, &attr, 0, 0, 0);
1095 }
1096
1097 struct fuse_inode_handle {
1098 u64 nodeid;
1099 u32 generation;
1100 };
1101
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)1102 static struct dentry *fuse_get_dentry(struct super_block *sb,
1103 struct fuse_inode_handle *handle)
1104 {
1105 struct fuse_conn *fc = get_fuse_conn_super(sb);
1106 struct inode *inode;
1107 struct dentry *entry;
1108 int err = -ESTALE;
1109
1110 if (handle->nodeid == 0)
1111 goto out_err;
1112
1113 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
1114 if (!inode) {
1115 struct fuse_entry_out outarg;
1116 const struct qstr name = QSTR_INIT(".", 1);
1117
1118 if (!fc->export_support)
1119 goto out_err;
1120
1121 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
1122 &inode);
1123 if (err && err != -ENOENT)
1124 goto out_err;
1125 if (err || !inode) {
1126 err = -ESTALE;
1127 goto out_err;
1128 }
1129 err = -EIO;
1130 if (get_node_id(inode) != handle->nodeid)
1131 goto out_iput;
1132 }
1133 err = -ESTALE;
1134 if (inode->i_generation != handle->generation)
1135 goto out_iput;
1136
1137 entry = d_obtain_alias(inode);
1138 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
1139 fuse_invalidate_entry_cache(entry);
1140
1141 return entry;
1142
1143 out_iput:
1144 iput(inode);
1145 out_err:
1146 return ERR_PTR(err);
1147 }
1148
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)1149 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1150 struct inode *parent)
1151 {
1152 int len = parent ? 6 : 3;
1153 u64 nodeid;
1154 u32 generation;
1155
1156 if (*max_len < len) {
1157 *max_len = len;
1158 return FILEID_INVALID;
1159 }
1160
1161 nodeid = get_fuse_inode(inode)->nodeid;
1162 generation = inode->i_generation;
1163
1164 fh[0] = (u32)(nodeid >> 32);
1165 fh[1] = (u32)(nodeid & 0xffffffff);
1166 fh[2] = generation;
1167
1168 if (parent) {
1169 nodeid = get_fuse_inode(parent)->nodeid;
1170 generation = parent->i_generation;
1171
1172 fh[3] = (u32)(nodeid >> 32);
1173 fh[4] = (u32)(nodeid & 0xffffffff);
1174 fh[5] = generation;
1175 }
1176
1177 *max_len = len;
1178 return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN;
1179 }
1180
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1181 static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
1182 struct fid *fid, int fh_len, int fh_type)
1183 {
1184 struct fuse_inode_handle handle;
1185
1186 if ((fh_type != FILEID_INO64_GEN &&
1187 fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3)
1188 return NULL;
1189
1190 handle.nodeid = (u64) fid->raw[0] << 32;
1191 handle.nodeid |= (u64) fid->raw[1];
1192 handle.generation = fid->raw[2];
1193 return fuse_get_dentry(sb, &handle);
1194 }
1195
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1196 static struct dentry *fuse_fh_to_parent(struct super_block *sb,
1197 struct fid *fid, int fh_len, int fh_type)
1198 {
1199 struct fuse_inode_handle parent;
1200
1201 if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6)
1202 return NULL;
1203
1204 parent.nodeid = (u64) fid->raw[3] << 32;
1205 parent.nodeid |= (u64) fid->raw[4];
1206 parent.generation = fid->raw[5];
1207 return fuse_get_dentry(sb, &parent);
1208 }
1209
fuse_get_parent(struct dentry * child)1210 static struct dentry *fuse_get_parent(struct dentry *child)
1211 {
1212 struct inode *child_inode = d_inode(child);
1213 struct fuse_conn *fc = get_fuse_conn(child_inode);
1214 struct inode *inode;
1215 struct dentry *parent;
1216 struct fuse_entry_out outarg;
1217 int err;
1218
1219 if (!fc->export_support)
1220 return ERR_PTR(-ESTALE);
1221
1222 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
1223 &dotdot_name, &outarg, &inode);
1224 if (err) {
1225 if (err == -ENOENT)
1226 return ERR_PTR(-ESTALE);
1227 return ERR_PTR(err);
1228 }
1229
1230 parent = d_obtain_alias(inode);
1231 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
1232 fuse_invalidate_entry_cache(parent);
1233
1234 return parent;
1235 }
1236
1237 /* only for fid encoding; no support for file handle */
1238 static const struct export_operations fuse_export_fid_operations = {
1239 .encode_fh = fuse_encode_fh,
1240 };
1241
1242 static const struct export_operations fuse_export_operations = {
1243 .fh_to_dentry = fuse_fh_to_dentry,
1244 .fh_to_parent = fuse_fh_to_parent,
1245 .encode_fh = fuse_encode_fh,
1246 .get_parent = fuse_get_parent,
1247 };
1248
1249 static const struct super_operations fuse_super_operations = {
1250 .alloc_inode = fuse_alloc_inode,
1251 .free_inode = fuse_free_inode,
1252 .evict_inode = fuse_evict_inode,
1253 .write_inode = fuse_write_inode,
1254 .drop_inode = inode_just_drop,
1255 .umount_begin = fuse_umount_begin,
1256 .statfs = fuse_statfs,
1257 .sync_fs = fuse_sync_fs,
1258 .show_options = fuse_show_options,
1259 };
1260
sanitize_global_limit(unsigned int * limit)1261 static void sanitize_global_limit(unsigned int *limit)
1262 {
1263 /*
1264 * The default maximum number of async requests is calculated to consume
1265 * 1/2^13 of the total memory, assuming 392 bytes per request.
1266 */
1267 if (*limit == 0)
1268 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
1269
1270 if (*limit >= 1 << 16)
1271 *limit = (1 << 16) - 1;
1272 }
1273
set_global_limit(const char * val,const struct kernel_param * kp)1274 static int set_global_limit(const char *val, const struct kernel_param *kp)
1275 {
1276 int rv;
1277
1278 rv = param_set_uint(val, kp);
1279 if (rv)
1280 return rv;
1281
1282 sanitize_global_limit((unsigned int *)kp->arg);
1283
1284 return 0;
1285 }
1286
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)1287 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1288 {
1289 int cap_sys_admin = capable(CAP_SYS_ADMIN);
1290
1291 if (arg->minor < 13)
1292 return;
1293
1294 sanitize_global_limit(&max_user_bgreq);
1295 sanitize_global_limit(&max_user_congthresh);
1296
1297 spin_lock(&fc->bg_lock);
1298 if (arg->max_background) {
1299 fc->max_background = arg->max_background;
1300
1301 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1302 fc->max_background = max_user_bgreq;
1303 }
1304 if (arg->congestion_threshold) {
1305 fc->congestion_threshold = arg->congestion_threshold;
1306
1307 if (!cap_sys_admin &&
1308 fc->congestion_threshold > max_user_congthresh)
1309 fc->congestion_threshold = max_user_congthresh;
1310 }
1311 spin_unlock(&fc->bg_lock);
1312 }
1313
set_request_timeout(struct fuse_conn * fc,unsigned int timeout)1314 static void set_request_timeout(struct fuse_conn *fc, unsigned int timeout)
1315 {
1316 fc->timeout.req_timeout = secs_to_jiffies(timeout);
1317 INIT_DELAYED_WORK(&fc->timeout.work, fuse_check_timeout);
1318 queue_delayed_work(system_percpu_wq, &fc->timeout.work,
1319 fuse_timeout_timer_freq);
1320 }
1321
init_server_timeout(struct fuse_conn * fc,unsigned int timeout)1322 static void init_server_timeout(struct fuse_conn *fc, unsigned int timeout)
1323 {
1324 if (!timeout && !fuse_max_req_timeout && !fuse_default_req_timeout)
1325 return;
1326
1327 if (!timeout)
1328 timeout = fuse_default_req_timeout;
1329
1330 if (fuse_max_req_timeout) {
1331 if (timeout)
1332 timeout = min(fuse_max_req_timeout, timeout);
1333 else
1334 timeout = fuse_max_req_timeout;
1335 }
1336
1337 timeout = max(FUSE_TIMEOUT_TIMER_FREQ, timeout);
1338
1339 set_request_timeout(fc, timeout);
1340 }
1341
1342 struct fuse_init_args {
1343 struct fuse_args args;
1344 struct fuse_init_in in;
1345 struct fuse_init_out out;
1346 };
1347
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)1348 static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
1349 int error)
1350 {
1351 struct fuse_conn *fc = fm->fc;
1352 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1353 struct fuse_init_out *arg = &ia->out;
1354 bool ok = true;
1355
1356 if (error || arg->major != FUSE_KERNEL_VERSION)
1357 ok = false;
1358 else {
1359 unsigned long ra_pages;
1360 unsigned int timeout = 0;
1361
1362 process_init_limits(fc, arg);
1363
1364 if (arg->minor >= 6) {
1365 u64 flags = arg->flags;
1366
1367 if (flags & FUSE_INIT_EXT)
1368 flags |= (u64) arg->flags2 << 32;
1369
1370 ra_pages = arg->max_readahead / PAGE_SIZE;
1371 if (flags & FUSE_ASYNC_READ)
1372 fc->async_read = 1;
1373 if (!(flags & FUSE_POSIX_LOCKS))
1374 fc->no_lock = 1;
1375 if (arg->minor >= 17) {
1376 if (!(flags & FUSE_FLOCK_LOCKS))
1377 fc->no_flock = 1;
1378 } else {
1379 if (!(flags & FUSE_POSIX_LOCKS))
1380 fc->no_flock = 1;
1381 }
1382 if (flags & FUSE_ATOMIC_O_TRUNC)
1383 fc->atomic_o_trunc = 1;
1384 if (arg->minor >= 9) {
1385 /* LOOKUP has dependency on proto version */
1386 if (flags & FUSE_EXPORT_SUPPORT)
1387 fc->export_support = 1;
1388 }
1389 if (flags & FUSE_BIG_WRITES)
1390 fc->big_writes = 1;
1391 if (flags & FUSE_DONT_MASK)
1392 fc->dont_mask = 1;
1393 if (flags & FUSE_AUTO_INVAL_DATA)
1394 fc->auto_inval_data = 1;
1395 else if (flags & FUSE_EXPLICIT_INVAL_DATA)
1396 fc->explicit_inval_data = 1;
1397 if (flags & FUSE_DO_READDIRPLUS) {
1398 fc->do_readdirplus = 1;
1399 if (flags & FUSE_READDIRPLUS_AUTO)
1400 fc->readdirplus_auto = 1;
1401 }
1402 if (flags & FUSE_ASYNC_DIO)
1403 fc->async_dio = 1;
1404 if (flags & FUSE_WRITEBACK_CACHE)
1405 fc->writeback_cache = 1;
1406 if (flags & FUSE_PARALLEL_DIROPS)
1407 fc->parallel_dirops = 1;
1408 if (flags & FUSE_HANDLE_KILLPRIV)
1409 fc->handle_killpriv = 1;
1410 if (arg->time_gran && arg->time_gran <= 1000000000)
1411 fm->sb->s_time_gran = arg->time_gran;
1412 if ((flags & FUSE_POSIX_ACL)) {
1413 fc->default_permissions = 1;
1414 fc->posix_acl = 1;
1415 }
1416 if (flags & FUSE_CACHE_SYMLINKS)
1417 fc->cache_symlinks = 1;
1418 if (flags & FUSE_ABORT_ERROR)
1419 fc->abort_err = 1;
1420 if (flags & FUSE_MAX_PAGES) {
1421 fc->max_pages =
1422 min_t(unsigned int, fc->max_pages_limit,
1423 max_t(unsigned int, arg->max_pages, 1));
1424
1425 /*
1426 * PATH_MAX file names might need two pages for
1427 * ops like rename
1428 */
1429 if (fc->max_pages > 1)
1430 fc->name_max = FUSE_NAME_MAX;
1431 }
1432 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1433 if (flags & FUSE_MAP_ALIGNMENT &&
1434 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1435 ok = false;
1436 }
1437 if (flags & FUSE_HAS_INODE_DAX)
1438 fc->inode_dax = 1;
1439 }
1440 if (flags & FUSE_HANDLE_KILLPRIV_V2) {
1441 fc->handle_killpriv_v2 = 1;
1442 fm->sb->s_flags |= SB_NOSEC;
1443 }
1444 if (flags & FUSE_SETXATTR_EXT)
1445 fc->setxattr_ext = 1;
1446 if (flags & FUSE_SECURITY_CTX)
1447 fc->init_security = 1;
1448 if (flags & FUSE_CREATE_SUPP_GROUP)
1449 fc->create_supp_group = 1;
1450 if (flags & FUSE_DIRECT_IO_ALLOW_MMAP)
1451 fc->direct_io_allow_mmap = 1;
1452 /*
1453 * max_stack_depth is the max stack depth of FUSE fs,
1454 * so it has to be at least 1 to support passthrough
1455 * to backing files.
1456 *
1457 * with max_stack_depth > 1, the backing files can be
1458 * on a stacked fs (e.g. overlayfs) themselves and with
1459 * max_stack_depth == 1, FUSE fs can be stacked as the
1460 * underlying fs of a stacked fs (e.g. overlayfs).
1461 *
1462 * Also don't allow the combination of FUSE_PASSTHROUGH
1463 * and FUSE_WRITEBACK_CACHE, current design doesn't handle
1464 * them together.
1465 */
1466 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) &&
1467 (flags & FUSE_PASSTHROUGH) &&
1468 arg->max_stack_depth > 0 &&
1469 arg->max_stack_depth <= FILESYSTEM_MAX_STACK_DEPTH &&
1470 !(flags & FUSE_WRITEBACK_CACHE)) {
1471 fc->passthrough = 1;
1472 fc->max_stack_depth = arg->max_stack_depth;
1473 fm->sb->s_stack_depth = arg->max_stack_depth;
1474 }
1475 if (flags & FUSE_NO_EXPORT_SUPPORT)
1476 fm->sb->s_export_op = &fuse_export_fid_operations;
1477 if (flags & FUSE_ALLOW_IDMAP) {
1478 if (fc->default_permissions)
1479 fm->sb->s_iflags &= ~SB_I_NOIDMAP;
1480 else
1481 ok = false;
1482 }
1483 if (flags & FUSE_OVER_IO_URING && fuse_uring_enabled())
1484 fc->io_uring = 1;
1485
1486 if (flags & FUSE_REQUEST_TIMEOUT)
1487 timeout = arg->request_timeout;
1488 } else {
1489 ra_pages = fc->max_read / PAGE_SIZE;
1490 fc->no_lock = 1;
1491 fc->no_flock = 1;
1492 }
1493
1494 init_server_timeout(fc, timeout);
1495
1496 fm->sb->s_bdi->ra_pages =
1497 min(fm->sb->s_bdi->ra_pages, ra_pages);
1498 fc->minor = arg->minor;
1499 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1500 fc->max_write = max_t(unsigned, 4096, fc->max_write);
1501 fc->conn_init = 1;
1502 }
1503 kfree(ia);
1504
1505 if (!ok) {
1506 fc->conn_init = 0;
1507 fc->conn_error = 1;
1508 }
1509
1510 fuse_set_initialized(fc);
1511 wake_up_all(&fc->blocked_waitq);
1512 }
1513
fuse_new_init(struct fuse_mount * fm)1514 static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
1515 {
1516 struct fuse_init_args *ia;
1517 u64 flags;
1518
1519 ia = kzalloc_obj(*ia, GFP_KERNEL | __GFP_NOFAIL);
1520
1521 ia->in.major = FUSE_KERNEL_VERSION;
1522 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1523 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1524 flags =
1525 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1526 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1527 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1528 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1529 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1530 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1531 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1532 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1533 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1534 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
1535 FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
1536 FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
1537 FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
1538 FUSE_REQUEST_TIMEOUT;
1539 #ifdef CONFIG_FUSE_DAX
1540 if (fm->fc->dax)
1541 flags |= FUSE_MAP_ALIGNMENT;
1542 if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
1543 flags |= FUSE_HAS_INODE_DAX;
1544 #endif
1545 if (fm->fc->auto_submounts)
1546 flags |= FUSE_SUBMOUNTS;
1547 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1548 flags |= FUSE_PASSTHROUGH;
1549
1550 /*
1551 * This is just an information flag for fuse server. No need to check
1552 * the reply - server is either sending IORING_OP_URING_CMD or not.
1553 */
1554 if (fuse_uring_enabled())
1555 flags |= FUSE_OVER_IO_URING;
1556
1557 ia->in.flags = flags;
1558 ia->in.flags2 = flags >> 32;
1559
1560 ia->args.opcode = FUSE_INIT;
1561 ia->args.in_numargs = 1;
1562 ia->args.in_args[0].size = sizeof(ia->in);
1563 ia->args.in_args[0].value = &ia->in;
1564 ia->args.out_numargs = 1;
1565 /* Variable length argument used for backward compatibility
1566 with interface version < 7.5. Rest of init_out is zeroed
1567 by do_get_request(), so a short reply is not a problem */
1568 ia->args.out_argvar = true;
1569 ia->args.out_args[0].size = sizeof(ia->out);
1570 ia->args.out_args[0].value = &ia->out;
1571 ia->args.force = true;
1572 ia->args.nocreds = true;
1573
1574 return ia;
1575 }
1576
fuse_send_init(struct fuse_mount * fm)1577 int fuse_send_init(struct fuse_mount *fm)
1578 {
1579 struct fuse_init_args *ia = fuse_new_init(fm);
1580 int err;
1581
1582 if (fm->fc->sync_init) {
1583 ia->args.abort_on_kill = true;
1584 err = fuse_simple_request(fm, &ia->args);
1585 /* Ignore size of init reply */
1586 if (err > 0)
1587 err = 0;
1588 } else {
1589 ia->args.end = process_init_reply;
1590 err = fuse_simple_background(fm, &ia->args, GFP_KERNEL);
1591 if (!err)
1592 return 0;
1593 }
1594 process_init_reply(fm, &ia->args, err);
1595 if (fm->fc->conn_error)
1596 return -ENOTCONN;
1597 return 0;
1598 }
1599 EXPORT_SYMBOL_GPL(fuse_send_init);
1600
fuse_free_conn(struct fuse_conn * fc)1601 void fuse_free_conn(struct fuse_conn *fc)
1602 {
1603 WARN_ON(!list_empty(&fc->devices));
1604 kfree(fc);
1605 }
1606 EXPORT_SYMBOL_GPL(fuse_free_conn);
1607
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1608 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1609 {
1610 int err;
1611 char *suffix = "";
1612
1613 if (sb->s_bdev) {
1614 suffix = "-fuseblk";
1615 /*
1616 * sb->s_bdi points to blkdev's bdi however we want to redirect
1617 * it to our private bdi...
1618 */
1619 bdi_put(sb->s_bdi);
1620 sb->s_bdi = &noop_backing_dev_info;
1621 }
1622 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1623 MINOR(fc->dev), suffix);
1624 if (err)
1625 return err;
1626
1627 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
1628
1629 /*
1630 * For a single fuse filesystem use max 1% of dirty +
1631 * writeback threshold.
1632 *
1633 * This gives about 1M of write buffer for memory maps on a
1634 * machine with 1G and 10% dirty_ratio, which should be more
1635 * than enough.
1636 *
1637 * Privileged users can raise it by writing to
1638 *
1639 * /sys/class/bdi/<bdi>/max_ratio
1640 */
1641 bdi_set_max_ratio(sb->s_bdi, 1);
1642
1643 return 0;
1644 }
1645
fuse_dev_alloc(void)1646 struct fuse_dev *fuse_dev_alloc(void)
1647 {
1648 struct fuse_dev *fud;
1649 struct list_head *pq;
1650
1651 fud = kzalloc_obj(struct fuse_dev);
1652 if (!fud)
1653 return NULL;
1654
1655 refcount_set(&fud->ref, 1);
1656 pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
1657 if (!pq) {
1658 kfree(fud);
1659 return NULL;
1660 }
1661
1662 fud->pq.processing = pq;
1663 fuse_pqueue_init(&fud->pq);
1664
1665 return fud;
1666 }
1667 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1668
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1669 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1670 {
1671 struct fuse_conn *old_fc;
1672
1673 spin_lock(&fc->lock);
1674 /*
1675 * Pairs with:
1676 * - xchg() in fuse_dev_release()
1677 * - smp_load_acquire() in fuse_dev_fc_get()
1678 */
1679 old_fc = cmpxchg(&fud->fc, NULL, fc);
1680 if (old_fc) {
1681 /*
1682 * failed to set fud->fc because
1683 * - it was already set to a different fc
1684 * - it was set to disconneted
1685 */
1686 fc->connected = 0;
1687 } else {
1688 list_add_tail(&fud->entry, &fc->devices);
1689 fuse_conn_get(fc);
1690 }
1691 spin_unlock(&fc->lock);
1692 }
1693 EXPORT_SYMBOL_GPL(fuse_dev_install);
1694
fuse_dev_alloc_install(struct fuse_conn * fc)1695 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1696 {
1697 struct fuse_dev *fud;
1698
1699 fud = fuse_dev_alloc();
1700 if (!fud)
1701 return NULL;
1702
1703 fuse_dev_install(fud, fc);
1704 return fud;
1705 }
1706 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1707
fuse_dev_put(struct fuse_dev * fud)1708 void fuse_dev_put(struct fuse_dev *fud)
1709 {
1710 struct fuse_conn *fc;
1711
1712 if (!refcount_dec_and_test(&fud->ref))
1713 return;
1714
1715 fc = fuse_dev_fc_get(fud);
1716 if (fc && fc != FUSE_DEV_FC_DISCONNECTED) {
1717 /* This is the virtiofs case (fuse_dev_release() not called) */
1718 spin_lock(&fc->lock);
1719 list_del(&fud->entry);
1720 spin_unlock(&fc->lock);
1721
1722 fuse_conn_put(fc);
1723 }
1724 kfree(fud->pq.processing);
1725 kfree(fud);
1726 }
1727 EXPORT_SYMBOL_GPL(fuse_dev_put);
1728
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct fuse_inode * fi)1729 static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1730 const struct fuse_inode *fi)
1731 {
1732 struct timespec64 atime = inode_get_atime(&fi->inode);
1733 struct timespec64 mtime = inode_get_mtime(&fi->inode);
1734 struct timespec64 ctime = inode_get_ctime(&fi->inode);
1735
1736 *attr = (struct fuse_attr){
1737 .ino = fi->inode.i_ino,
1738 .size = fi->inode.i_size,
1739 .blocks = fi->inode.i_blocks,
1740 .atime = atime.tv_sec,
1741 .mtime = mtime.tv_sec,
1742 .ctime = ctime.tv_sec,
1743 .atimensec = atime.tv_nsec,
1744 .mtimensec = mtime.tv_nsec,
1745 .ctimensec = ctime.tv_nsec,
1746 .mode = fi->inode.i_mode,
1747 .nlink = fi->inode.i_nlink,
1748 .uid = __kuid_val(fi->inode.i_uid),
1749 .gid = __kgid_val(fi->inode.i_gid),
1750 .rdev = fi->inode.i_rdev,
1751 .blksize = 1u << fi->inode.i_blkbits,
1752 };
1753 }
1754
fuse_sb_defaults(struct super_block * sb)1755 static void fuse_sb_defaults(struct super_block *sb)
1756 {
1757 sb->s_magic = FUSE_SUPER_MAGIC;
1758 sb->s_op = &fuse_super_operations;
1759 sb->s_xattr = fuse_xattr_handlers;
1760 sb->s_maxbytes = MAX_LFS_FILESIZE;
1761 sb->s_time_gran = 1;
1762 sb->s_export_op = &fuse_export_operations;
1763 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1764 sb->s_iflags |= SB_I_NOIDMAP;
1765 sb->s_iflags |= SB_I_NO_DATA_INTEGRITY;
1766 if (sb->s_user_ns != &init_user_ns)
1767 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1768 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1769 }
1770
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)1771 static int fuse_fill_super_submount(struct super_block *sb,
1772 struct fuse_inode *parent_fi)
1773 {
1774 struct fuse_mount *fm = get_fuse_mount_super(sb);
1775 struct super_block *parent_sb = parent_fi->inode.i_sb;
1776 struct fuse_attr root_attr;
1777 struct inode *root;
1778 struct fuse_submount_lookup *sl;
1779 struct fuse_inode *fi;
1780
1781 fuse_sb_defaults(sb);
1782 fm->sb = sb;
1783
1784 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1785 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1786
1787 sb->s_xattr = parent_sb->s_xattr;
1788 sb->s_export_op = parent_sb->s_export_op;
1789 sb->s_time_gran = parent_sb->s_time_gran;
1790 sb->s_blocksize = parent_sb->s_blocksize;
1791 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1792 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1793 if (parent_sb->s_subtype && !sb->s_subtype)
1794 return -ENOMEM;
1795
1796 fuse_fill_attr_from_inode(&root_attr, parent_fi);
1797 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0,
1798 fuse_get_evict_ctr(fm->fc));
1799 /*
1800 * This inode is just a duplicate, so it is not looked up and
1801 * its nlookup should not be incremented. fuse_iget() does
1802 * that, though, so undo it here.
1803 */
1804 fi = get_fuse_inode(root);
1805 fi->nlookup--;
1806
1807 set_default_d_op(sb, &fuse_dentry_operations);
1808 sb->s_root = d_make_root(root);
1809 if (!sb->s_root)
1810 return -ENOMEM;
1811
1812 /*
1813 * Grab the parent's submount_lookup pointer and take a
1814 * reference on the shared nlookup from the parent. This is to
1815 * prevent the last forget for this nodeid from getting
1816 * triggered until all users have finished with it.
1817 */
1818 sl = parent_fi->submount_lookup;
1819 WARN_ON(!sl);
1820 if (sl) {
1821 refcount_inc(&sl->count);
1822 fi->submount_lookup = sl;
1823 }
1824
1825 return 0;
1826 }
1827
1828 /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1829 static int fuse_get_tree_submount(struct fs_context *fsc)
1830 {
1831 struct fuse_mount *fm;
1832 struct fuse_inode *mp_fi = fsc->fs_private;
1833 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1834 struct super_block *sb;
1835 int err;
1836
1837 fm = kzalloc_obj(struct fuse_mount);
1838 if (!fm)
1839 return -ENOMEM;
1840
1841 fm->fc = fuse_conn_get(fc);
1842 fsc->s_fs_info = fm;
1843 sb = sget_fc(fsc, NULL, set_anon_super_fc);
1844 if (fsc->s_fs_info)
1845 fuse_mount_destroy(fm);
1846 if (IS_ERR(sb))
1847 return PTR_ERR(sb);
1848
1849 /* Initialize superblock, making @mp_fi its root */
1850 err = fuse_fill_super_submount(sb, mp_fi);
1851 if (err) {
1852 deactivate_locked_super(sb);
1853 return err;
1854 }
1855
1856 down_write(&fc->killsb);
1857 list_add_tail(&fm->fc_entry, &fc->mounts);
1858 up_write(&fc->killsb);
1859
1860 sb->s_flags |= SB_ACTIVE;
1861 fsc->root = dget(sb->s_root);
1862
1863 return 0;
1864 }
1865
1866 static const struct fs_context_operations fuse_context_submount_ops = {
1867 .get_tree = fuse_get_tree_submount,
1868 };
1869
fuse_init_fs_context_submount(struct fs_context * fsc)1870 int fuse_init_fs_context_submount(struct fs_context *fsc)
1871 {
1872 fsc->ops = &fuse_context_submount_ops;
1873 return 0;
1874 }
1875 EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1876
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1877 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1878 {
1879 struct fuse_dev *fud = ctx->fud;
1880 struct fuse_mount *fm = get_fuse_mount_super(sb);
1881 struct fuse_conn *fc = fm->fc;
1882 struct inode *root;
1883 struct dentry *root_dentry;
1884 int err;
1885
1886 err = -EINVAL;
1887 if (sb->s_flags & SB_MANDLOCK)
1888 goto err;
1889
1890 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
1891 fuse_sb_defaults(sb);
1892
1893 if (ctx->is_bdev) {
1894 #ifdef CONFIG_BLOCK
1895 err = -EINVAL;
1896 if (!sb_set_blocksize(sb, ctx->blksize))
1897 goto err;
1898 #endif
1899 fc->sync_fs = 1;
1900 } else {
1901 sb->s_blocksize = PAGE_SIZE;
1902 sb->s_blocksize_bits = PAGE_SHIFT;
1903 }
1904
1905 sb->s_subtype = ctx->subtype;
1906 ctx->subtype = NULL;
1907 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1908 err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
1909 if (err)
1910 goto err;
1911 }
1912
1913 fc->dev = sb->s_dev;
1914 fm->sb = sb;
1915 err = fuse_bdi_init(fc, sb);
1916 if (err)
1917 goto err_free_dax;
1918
1919 /* Handle umasking inside the fuse code */
1920 if (sb->s_flags & SB_POSIXACL)
1921 fc->dont_mask = 1;
1922 sb->s_flags |= SB_POSIXACL;
1923
1924 fc->default_permissions = ctx->default_permissions;
1925 fc->allow_other = ctx->allow_other;
1926 fc->user_id = ctx->user_id;
1927 fc->group_id = ctx->group_id;
1928 fc->legacy_opts_show = ctx->legacy_opts_show;
1929 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1930 fc->destroy = ctx->destroy;
1931 fc->no_control = ctx->no_control;
1932 fc->no_force_umount = ctx->no_force_umount;
1933
1934 err = -ENOMEM;
1935 root = fuse_get_root_inode(sb, ctx->rootmode);
1936 set_default_d_op(sb, &fuse_dentry_operations);
1937 root_dentry = d_make_root(root);
1938 if (!root_dentry)
1939 goto err_free_dax;
1940
1941 mutex_lock(&fuse_mutex);
1942 err = -EINVAL;
1943 if (fud) {
1944 if (fuse_dev_fc_get(fud))
1945 goto err_unlock;
1946 if (fud->sync_init)
1947 fc->sync_init = 1;
1948 }
1949
1950 err = fuse_ctl_add_conn(fc);
1951 if (err)
1952 goto err_unlock;
1953
1954 list_add_tail(&fc->entry, &fuse_conn_list);
1955 sb->s_root = root_dentry;
1956 if (fud) {
1957 fuse_dev_install(fud, fc);
1958 wake_up_all(&fuse_dev_waitq);
1959 }
1960 mutex_unlock(&fuse_mutex);
1961 return 0;
1962
1963 err_unlock:
1964 mutex_unlock(&fuse_mutex);
1965 dput(root_dentry);
1966 err_free_dax:
1967 if (IS_ENABLED(CONFIG_FUSE_DAX))
1968 fuse_dax_conn_free(fc);
1969 err:
1970 return err;
1971 }
1972 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1973
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1974 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1975 {
1976 struct fuse_fs_context *ctx = fsc->fs_private;
1977 struct fuse_mount *fm;
1978 int err;
1979
1980 if (!ctx->fud || !ctx->rootmode_present ||
1981 !ctx->user_id_present || !ctx->group_id_present)
1982 return -EINVAL;
1983
1984 err = fuse_fill_super_common(sb, ctx);
1985 if (err)
1986 return err;
1987
1988 fm = get_fuse_mount_super(sb);
1989
1990 return fuse_send_init(fm);
1991 }
1992
1993 /*
1994 * This is the path where user supplied an already initialized fuse dev. In
1995 * this case never create a new super if the old one is gone.
1996 */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)1997 static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1998 {
1999 return -ENOTCONN;
2000 }
2001
fuse_test_super(struct super_block * sb,struct fs_context * fsc)2002 static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
2003 {
2004 struct fuse_dev *fud = fsc->sget_key;
2005
2006 return fuse_dev_fc_get(fud) == get_fuse_conn_super(sb);
2007 }
2008
fuse_get_tree(struct fs_context * fsc)2009 static int fuse_get_tree(struct fs_context *fsc)
2010 {
2011 struct fuse_fs_context *ctx = fsc->fs_private;
2012 struct fuse_conn *fc;
2013 struct fuse_mount *fm;
2014 struct super_block *sb;
2015 int err;
2016
2017 fc = kmalloc_obj(*fc);
2018 if (!fc)
2019 return -ENOMEM;
2020
2021 fm = kzalloc_obj(*fm);
2022 if (!fm) {
2023 kfree(fc);
2024 return -ENOMEM;
2025 }
2026
2027 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
2028 fc->release = fuse_free_conn;
2029
2030 fsc->s_fs_info = fm;
2031
2032 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
2033 err = get_tree_bdev(fsc, fuse_fill_super);
2034 goto out;
2035 }
2036 /*
2037 * While block dev mount can be initialized with a dummy device fd
2038 * (found by device name), normal fuse mounts can't
2039 */
2040 err = -EINVAL;
2041 if (!ctx->fud)
2042 goto out;
2043
2044 /*
2045 * Allow creating a fuse mount with an already initialized fuse
2046 * connection
2047 */
2048 if (fuse_dev_fc_get(ctx->fud)) {
2049 fsc->sget_key = ctx->fud;
2050 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
2051 err = PTR_ERR_OR_ZERO(sb);
2052 if (!IS_ERR(sb))
2053 fsc->root = dget(sb->s_root);
2054 } else {
2055 err = get_tree_nodev(fsc, fuse_fill_super);
2056 }
2057 out:
2058 if (fsc->s_fs_info)
2059 fuse_mount_destroy(fm);
2060 return err;
2061 }
2062
2063 static const struct fs_context_operations fuse_context_ops = {
2064 .free = fuse_free_fsc,
2065 .parse_param = fuse_parse_param,
2066 .reconfigure = fuse_reconfigure,
2067 .get_tree = fuse_get_tree,
2068 };
2069
2070 /*
2071 * Set up the filesystem mount context.
2072 */
fuse_init_fs_context(struct fs_context * fsc)2073 static int fuse_init_fs_context(struct fs_context *fsc)
2074 {
2075 struct fuse_fs_context *ctx;
2076
2077 ctx = kzalloc_obj(struct fuse_fs_context);
2078 if (!ctx)
2079 return -ENOMEM;
2080
2081 ctx->max_read = ~0;
2082 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
2083 ctx->legacy_opts_show = true;
2084
2085 #ifdef CONFIG_BLOCK
2086 if (fsc->fs_type == &fuseblk_fs_type) {
2087 ctx->is_bdev = true;
2088 ctx->destroy = true;
2089 }
2090 #endif
2091
2092 fsc->fs_private = ctx;
2093 fsc->ops = &fuse_context_ops;
2094 return 0;
2095 }
2096
fuse_mount_remove(struct fuse_mount * fm)2097 bool fuse_mount_remove(struct fuse_mount *fm)
2098 {
2099 struct fuse_conn *fc = fm->fc;
2100 bool last = false;
2101
2102 down_write(&fc->killsb);
2103 list_del_init(&fm->fc_entry);
2104 if (list_empty(&fc->mounts))
2105 last = true;
2106 up_write(&fc->killsb);
2107
2108 return last;
2109 }
2110 EXPORT_SYMBOL_GPL(fuse_mount_remove);
2111
fuse_conn_destroy(struct fuse_mount * fm)2112 void fuse_conn_destroy(struct fuse_mount *fm)
2113 {
2114 struct fuse_conn *fc = fm->fc;
2115
2116 if (fc->destroy)
2117 fuse_send_destroy(fm);
2118
2119 fuse_abort_conn(fc);
2120 fuse_wait_aborted(fc);
2121
2122 if (!list_empty(&fc->entry)) {
2123 mutex_lock(&fuse_mutex);
2124 list_del(&fc->entry);
2125 fuse_ctl_remove_conn(fc);
2126 mutex_unlock(&fuse_mutex);
2127 }
2128 }
2129 EXPORT_SYMBOL_GPL(fuse_conn_destroy);
2130
fuse_sb_destroy(struct super_block * sb)2131 static void fuse_sb_destroy(struct super_block *sb)
2132 {
2133 struct fuse_mount *fm = get_fuse_mount_super(sb);
2134 bool last;
2135
2136 if (sb->s_root) {
2137 last = fuse_mount_remove(fm);
2138 if (last)
2139 fuse_conn_destroy(fm);
2140 }
2141 }
2142
fuse_mount_destroy(struct fuse_mount * fm)2143 void fuse_mount_destroy(struct fuse_mount *fm)
2144 {
2145 fuse_conn_put(fm->fc);
2146 kfree_rcu(fm, rcu);
2147 }
2148 EXPORT_SYMBOL(fuse_mount_destroy);
2149
fuse_kill_sb_anon(struct super_block * sb)2150 static void fuse_kill_sb_anon(struct super_block *sb)
2151 {
2152 fuse_sb_destroy(sb);
2153 kill_anon_super(sb);
2154 fuse_mount_destroy(get_fuse_mount_super(sb));
2155 }
2156
2157 static struct file_system_type fuse_fs_type = {
2158 .owner = THIS_MODULE,
2159 .name = "fuse",
2160 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT | FS_ALLOW_IDMAP,
2161 .init_fs_context = fuse_init_fs_context,
2162 .parameters = fuse_fs_parameters,
2163 .kill_sb = fuse_kill_sb_anon,
2164 };
2165 MODULE_ALIAS_FS("fuse");
2166
2167 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)2168 static void fuse_kill_sb_blk(struct super_block *sb)
2169 {
2170 fuse_sb_destroy(sb);
2171 kill_block_super(sb);
2172 fuse_mount_destroy(get_fuse_mount_super(sb));
2173 }
2174
2175 static struct file_system_type fuseblk_fs_type = {
2176 .owner = THIS_MODULE,
2177 .name = "fuseblk",
2178 .init_fs_context = fuse_init_fs_context,
2179 .parameters = fuse_fs_parameters,
2180 .kill_sb = fuse_kill_sb_blk,
2181 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE | FS_ALLOW_IDMAP,
2182 };
2183 MODULE_ALIAS_FS("fuseblk");
2184
register_fuseblk(void)2185 static inline int register_fuseblk(void)
2186 {
2187 return register_filesystem(&fuseblk_fs_type);
2188 }
2189
unregister_fuseblk(void)2190 static inline void unregister_fuseblk(void)
2191 {
2192 unregister_filesystem(&fuseblk_fs_type);
2193 }
2194 #else
register_fuseblk(void)2195 static inline int register_fuseblk(void)
2196 {
2197 return 0;
2198 }
2199
unregister_fuseblk(void)2200 static inline void unregister_fuseblk(void)
2201 {
2202 }
2203 #endif
2204
fuse_inode_init_once(void * foo)2205 static void fuse_inode_init_once(void *foo)
2206 {
2207 struct inode *inode = foo;
2208
2209 inode_init_once(inode);
2210 }
2211
fuse_fs_init(void)2212 static int __init fuse_fs_init(void)
2213 {
2214 int err;
2215
2216 fuse_inode_cachep = kmem_cache_create("fuse_inode",
2217 sizeof(struct fuse_inode), 0,
2218 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
2219 fuse_inode_init_once);
2220 err = -ENOMEM;
2221 if (!fuse_inode_cachep)
2222 goto out;
2223
2224 err = register_fuseblk();
2225 if (err)
2226 goto out2;
2227
2228 err = register_filesystem(&fuse_fs_type);
2229 if (err)
2230 goto out3;
2231
2232 err = fuse_sysctl_register();
2233 if (err)
2234 goto out4;
2235
2236 return 0;
2237
2238 out4:
2239 unregister_filesystem(&fuse_fs_type);
2240 out3:
2241 unregister_fuseblk();
2242 out2:
2243 kmem_cache_destroy(fuse_inode_cachep);
2244 out:
2245 return err;
2246 }
2247
fuse_fs_cleanup(void)2248 static void fuse_fs_cleanup(void)
2249 {
2250 fuse_sysctl_unregister();
2251 unregister_filesystem(&fuse_fs_type);
2252 unregister_fuseblk();
2253
2254 /*
2255 * Make sure all delayed rcu free inodes are flushed before we
2256 * destroy cache.
2257 */
2258 rcu_barrier();
2259 kmem_cache_destroy(fuse_inode_cachep);
2260 }
2261
2262 static struct kobject *fuse_kobj;
2263
fuse_sysfs_init(void)2264 static int fuse_sysfs_init(void)
2265 {
2266 int err;
2267
2268 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
2269 if (!fuse_kobj) {
2270 err = -ENOMEM;
2271 goto out_err;
2272 }
2273
2274 err = sysfs_create_mount_point(fuse_kobj, "connections");
2275 if (err)
2276 goto out_fuse_unregister;
2277
2278 return 0;
2279
2280 out_fuse_unregister:
2281 kobject_put(fuse_kobj);
2282 out_err:
2283 return err;
2284 }
2285
fuse_sysfs_cleanup(void)2286 static void fuse_sysfs_cleanup(void)
2287 {
2288 sysfs_remove_mount_point(fuse_kobj, "connections");
2289 kobject_put(fuse_kobj);
2290 }
2291
fuse_init(void)2292 static int __init fuse_init(void)
2293 {
2294 int res;
2295
2296 pr_info("init (API version %i.%i)\n",
2297 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2298
2299 INIT_LIST_HEAD(&fuse_conn_list);
2300 res = fuse_fs_init();
2301 if (res)
2302 goto err;
2303
2304 res = fuse_dev_init();
2305 if (res)
2306 goto err_fs_cleanup;
2307
2308 res = fuse_sysfs_init();
2309 if (res)
2310 goto err_dev_cleanup;
2311
2312 res = fuse_ctl_init();
2313 if (res)
2314 goto err_sysfs_cleanup;
2315
2316 fuse_dentry_tree_init();
2317
2318 sanitize_global_limit(&max_user_bgreq);
2319 sanitize_global_limit(&max_user_congthresh);
2320
2321 return 0;
2322
2323 err_sysfs_cleanup:
2324 fuse_sysfs_cleanup();
2325 err_dev_cleanup:
2326 fuse_dev_cleanup();
2327 err_fs_cleanup:
2328 fuse_fs_cleanup();
2329 err:
2330 return res;
2331 }
2332
fuse_exit(void)2333 static void __exit fuse_exit(void)
2334 {
2335 pr_debug("exit\n");
2336
2337 fuse_dentry_tree_cleanup();
2338 fuse_ctl_cleanup();
2339 fuse_sysfs_cleanup();
2340 fuse_fs_cleanup();
2341 fuse_dev_cleanup();
2342 }
2343
2344 module_init(fuse_init);
2345 module_exit(fuse_exit);
2346