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->i_state & 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 = fc->blkbits;
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->i_state & 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_waitqueue_head(&fc->blocked_waitq);
981 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
982 INIT_LIST_HEAD(&fc->bg_queue);
983 INIT_LIST_HEAD(&fc->entry);
984 INIT_LIST_HEAD(&fc->devices);
985 atomic_set(&fc->num_waiting, 0);
986 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
987 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
988 atomic64_set(&fc->khctr, 0);
989 fc->polled_files = RB_ROOT;
990 fc->blocked = 0;
991 fc->initialized = 0;
992 fc->connected = 1;
993 atomic64_set(&fc->attr_version, 1);
994 atomic64_set(&fc->evict_ctr, 1);
995 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
996 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
997 fc->user_ns = get_user_ns(user_ns);
998 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
999 fc->max_pages_limit = fuse_max_pages_limit;
1000 fc->name_max = FUSE_NAME_LOW_MAX;
1001 fc->timeout.req_timeout = 0;
1002
1003 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1004 fuse_backing_files_init(fc);
1005
1006 INIT_LIST_HEAD(&fc->mounts);
1007 list_add(&fm->fc_entry, &fc->mounts);
1008 fm->fc = fc;
1009 }
1010 EXPORT_SYMBOL_GPL(fuse_conn_init);
1011
delayed_release(struct rcu_head * p)1012 static void delayed_release(struct rcu_head *p)
1013 {
1014 struct fuse_conn *fc = container_of(p, struct fuse_conn, rcu);
1015
1016 fuse_uring_destruct(fc);
1017
1018 put_user_ns(fc->user_ns);
1019 fc->release(fc);
1020 }
1021
fuse_conn_put(struct fuse_conn * fc)1022 void fuse_conn_put(struct fuse_conn *fc)
1023 {
1024 if (refcount_dec_and_test(&fc->count)) {
1025 struct fuse_iqueue *fiq = &fc->iq;
1026 struct fuse_sync_bucket *bucket;
1027
1028 if (IS_ENABLED(CONFIG_FUSE_DAX))
1029 fuse_dax_conn_free(fc);
1030 if (fc->timeout.req_timeout)
1031 cancel_delayed_work_sync(&fc->timeout.work);
1032 if (fiq->ops->release)
1033 fiq->ops->release(fiq);
1034 put_pid_ns(fc->pid_ns);
1035 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
1036 if (bucket) {
1037 WARN_ON(atomic_read(&bucket->count) != 1);
1038 kfree(bucket);
1039 }
1040 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1041 fuse_backing_files_free(fc);
1042 call_rcu(&fc->rcu, delayed_release);
1043 }
1044 }
1045 EXPORT_SYMBOL_GPL(fuse_conn_put);
1046
fuse_conn_get(struct fuse_conn * fc)1047 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
1048 {
1049 refcount_inc(&fc->count);
1050 return fc;
1051 }
1052 EXPORT_SYMBOL_GPL(fuse_conn_get);
1053
fuse_get_root_inode(struct super_block * sb,unsigned int mode)1054 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned int mode)
1055 {
1056 struct fuse_attr attr;
1057 memset(&attr, 0, sizeof(attr));
1058
1059 attr.mode = mode;
1060 attr.ino = FUSE_ROOT_ID;
1061 attr.nlink = 1;
1062 return fuse_iget(sb, FUSE_ROOT_ID, 0, &attr, 0, 0, 0);
1063 }
1064
1065 struct fuse_inode_handle {
1066 u64 nodeid;
1067 u32 generation;
1068 };
1069
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)1070 static struct dentry *fuse_get_dentry(struct super_block *sb,
1071 struct fuse_inode_handle *handle)
1072 {
1073 struct fuse_conn *fc = get_fuse_conn_super(sb);
1074 struct inode *inode;
1075 struct dentry *entry;
1076 int err = -ESTALE;
1077
1078 if (handle->nodeid == 0)
1079 goto out_err;
1080
1081 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
1082 if (!inode) {
1083 struct fuse_entry_out outarg;
1084 const struct qstr name = QSTR_INIT(".", 1);
1085
1086 if (!fc->export_support)
1087 goto out_err;
1088
1089 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
1090 &inode);
1091 if (err && err != -ENOENT)
1092 goto out_err;
1093 if (err || !inode) {
1094 err = -ESTALE;
1095 goto out_err;
1096 }
1097 err = -EIO;
1098 if (get_node_id(inode) != handle->nodeid)
1099 goto out_iput;
1100 }
1101 err = -ESTALE;
1102 if (inode->i_generation != handle->generation)
1103 goto out_iput;
1104
1105 entry = d_obtain_alias(inode);
1106 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
1107 fuse_invalidate_entry_cache(entry);
1108
1109 return entry;
1110
1111 out_iput:
1112 iput(inode);
1113 out_err:
1114 return ERR_PTR(err);
1115 }
1116
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)1117 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1118 struct inode *parent)
1119 {
1120 int len = parent ? 6 : 3;
1121 u64 nodeid;
1122 u32 generation;
1123
1124 if (*max_len < len) {
1125 *max_len = len;
1126 return FILEID_INVALID;
1127 }
1128
1129 nodeid = get_fuse_inode(inode)->nodeid;
1130 generation = inode->i_generation;
1131
1132 fh[0] = (u32)(nodeid >> 32);
1133 fh[1] = (u32)(nodeid & 0xffffffff);
1134 fh[2] = generation;
1135
1136 if (parent) {
1137 nodeid = get_fuse_inode(parent)->nodeid;
1138 generation = parent->i_generation;
1139
1140 fh[3] = (u32)(nodeid >> 32);
1141 fh[4] = (u32)(nodeid & 0xffffffff);
1142 fh[5] = generation;
1143 }
1144
1145 *max_len = len;
1146 return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN;
1147 }
1148
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1149 static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
1150 struct fid *fid, int fh_len, int fh_type)
1151 {
1152 struct fuse_inode_handle handle;
1153
1154 if ((fh_type != FILEID_INO64_GEN &&
1155 fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3)
1156 return NULL;
1157
1158 handle.nodeid = (u64) fid->raw[0] << 32;
1159 handle.nodeid |= (u64) fid->raw[1];
1160 handle.generation = fid->raw[2];
1161 return fuse_get_dentry(sb, &handle);
1162 }
1163
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1164 static struct dentry *fuse_fh_to_parent(struct super_block *sb,
1165 struct fid *fid, int fh_len, int fh_type)
1166 {
1167 struct fuse_inode_handle parent;
1168
1169 if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6)
1170 return NULL;
1171
1172 parent.nodeid = (u64) fid->raw[3] << 32;
1173 parent.nodeid |= (u64) fid->raw[4];
1174 parent.generation = fid->raw[5];
1175 return fuse_get_dentry(sb, &parent);
1176 }
1177
fuse_get_parent(struct dentry * child)1178 static struct dentry *fuse_get_parent(struct dentry *child)
1179 {
1180 struct inode *child_inode = d_inode(child);
1181 struct fuse_conn *fc = get_fuse_conn(child_inode);
1182 struct inode *inode;
1183 struct dentry *parent;
1184 struct fuse_entry_out outarg;
1185 int err;
1186
1187 if (!fc->export_support)
1188 return ERR_PTR(-ESTALE);
1189
1190 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
1191 &dotdot_name, &outarg, &inode);
1192 if (err) {
1193 if (err == -ENOENT)
1194 return ERR_PTR(-ESTALE);
1195 return ERR_PTR(err);
1196 }
1197
1198 parent = d_obtain_alias(inode);
1199 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
1200 fuse_invalidate_entry_cache(parent);
1201
1202 return parent;
1203 }
1204
1205 /* only for fid encoding; no support for file handle */
1206 static const struct export_operations fuse_export_fid_operations = {
1207 .encode_fh = fuse_encode_fh,
1208 };
1209
1210 static const struct export_operations fuse_export_operations = {
1211 .fh_to_dentry = fuse_fh_to_dentry,
1212 .fh_to_parent = fuse_fh_to_parent,
1213 .encode_fh = fuse_encode_fh,
1214 .get_parent = fuse_get_parent,
1215 };
1216
1217 static const struct super_operations fuse_super_operations = {
1218 .alloc_inode = fuse_alloc_inode,
1219 .free_inode = fuse_free_inode,
1220 .evict_inode = fuse_evict_inode,
1221 .write_inode = fuse_write_inode,
1222 .drop_inode = inode_just_drop,
1223 .umount_begin = fuse_umount_begin,
1224 .statfs = fuse_statfs,
1225 .sync_fs = fuse_sync_fs,
1226 .show_options = fuse_show_options,
1227 };
1228
sanitize_global_limit(unsigned int * limit)1229 static void sanitize_global_limit(unsigned int *limit)
1230 {
1231 /*
1232 * The default maximum number of async requests is calculated to consume
1233 * 1/2^13 of the total memory, assuming 392 bytes per request.
1234 */
1235 if (*limit == 0)
1236 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
1237
1238 if (*limit >= 1 << 16)
1239 *limit = (1 << 16) - 1;
1240 }
1241
set_global_limit(const char * val,const struct kernel_param * kp)1242 static int set_global_limit(const char *val, const struct kernel_param *kp)
1243 {
1244 int rv;
1245
1246 rv = param_set_uint(val, kp);
1247 if (rv)
1248 return rv;
1249
1250 sanitize_global_limit((unsigned int *)kp->arg);
1251
1252 return 0;
1253 }
1254
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)1255 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1256 {
1257 int cap_sys_admin = capable(CAP_SYS_ADMIN);
1258
1259 if (arg->minor < 13)
1260 return;
1261
1262 sanitize_global_limit(&max_user_bgreq);
1263 sanitize_global_limit(&max_user_congthresh);
1264
1265 spin_lock(&fc->bg_lock);
1266 if (arg->max_background) {
1267 fc->max_background = arg->max_background;
1268
1269 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1270 fc->max_background = max_user_bgreq;
1271 }
1272 if (arg->congestion_threshold) {
1273 fc->congestion_threshold = arg->congestion_threshold;
1274
1275 if (!cap_sys_admin &&
1276 fc->congestion_threshold > max_user_congthresh)
1277 fc->congestion_threshold = max_user_congthresh;
1278 }
1279 spin_unlock(&fc->bg_lock);
1280 }
1281
set_request_timeout(struct fuse_conn * fc,unsigned int timeout)1282 static void set_request_timeout(struct fuse_conn *fc, unsigned int timeout)
1283 {
1284 fc->timeout.req_timeout = secs_to_jiffies(timeout);
1285 INIT_DELAYED_WORK(&fc->timeout.work, fuse_check_timeout);
1286 queue_delayed_work(system_percpu_wq, &fc->timeout.work,
1287 fuse_timeout_timer_freq);
1288 }
1289
init_server_timeout(struct fuse_conn * fc,unsigned int timeout)1290 static void init_server_timeout(struct fuse_conn *fc, unsigned int timeout)
1291 {
1292 if (!timeout && !fuse_max_req_timeout && !fuse_default_req_timeout)
1293 return;
1294
1295 if (!timeout)
1296 timeout = fuse_default_req_timeout;
1297
1298 if (fuse_max_req_timeout) {
1299 if (timeout)
1300 timeout = min(fuse_max_req_timeout, timeout);
1301 else
1302 timeout = fuse_max_req_timeout;
1303 }
1304
1305 timeout = max(FUSE_TIMEOUT_TIMER_FREQ, timeout);
1306
1307 set_request_timeout(fc, timeout);
1308 }
1309
1310 struct fuse_init_args {
1311 struct fuse_args args;
1312 struct fuse_init_in in;
1313 struct fuse_init_out out;
1314 };
1315
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)1316 static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
1317 int error)
1318 {
1319 struct fuse_conn *fc = fm->fc;
1320 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1321 struct fuse_init_out *arg = &ia->out;
1322 bool ok = true;
1323
1324 if (error || arg->major != FUSE_KERNEL_VERSION)
1325 ok = false;
1326 else {
1327 unsigned long ra_pages;
1328 unsigned int timeout = 0;
1329
1330 process_init_limits(fc, arg);
1331
1332 if (arg->minor >= 6) {
1333 u64 flags = arg->flags;
1334
1335 if (flags & FUSE_INIT_EXT)
1336 flags |= (u64) arg->flags2 << 32;
1337
1338 ra_pages = arg->max_readahead / PAGE_SIZE;
1339 if (flags & FUSE_ASYNC_READ)
1340 fc->async_read = 1;
1341 if (!(flags & FUSE_POSIX_LOCKS))
1342 fc->no_lock = 1;
1343 if (arg->minor >= 17) {
1344 if (!(flags & FUSE_FLOCK_LOCKS))
1345 fc->no_flock = 1;
1346 } else {
1347 if (!(flags & FUSE_POSIX_LOCKS))
1348 fc->no_flock = 1;
1349 }
1350 if (flags & FUSE_ATOMIC_O_TRUNC)
1351 fc->atomic_o_trunc = 1;
1352 if (arg->minor >= 9) {
1353 /* LOOKUP has dependency on proto version */
1354 if (flags & FUSE_EXPORT_SUPPORT)
1355 fc->export_support = 1;
1356 }
1357 if (flags & FUSE_BIG_WRITES)
1358 fc->big_writes = 1;
1359 if (flags & FUSE_DONT_MASK)
1360 fc->dont_mask = 1;
1361 if (flags & FUSE_AUTO_INVAL_DATA)
1362 fc->auto_inval_data = 1;
1363 else if (flags & FUSE_EXPLICIT_INVAL_DATA)
1364 fc->explicit_inval_data = 1;
1365 if (flags & FUSE_DO_READDIRPLUS) {
1366 fc->do_readdirplus = 1;
1367 if (flags & FUSE_READDIRPLUS_AUTO)
1368 fc->readdirplus_auto = 1;
1369 }
1370 if (flags & FUSE_ASYNC_DIO)
1371 fc->async_dio = 1;
1372 if (flags & FUSE_WRITEBACK_CACHE)
1373 fc->writeback_cache = 1;
1374 if (flags & FUSE_PARALLEL_DIROPS)
1375 fc->parallel_dirops = 1;
1376 if (flags & FUSE_HANDLE_KILLPRIV)
1377 fc->handle_killpriv = 1;
1378 if (arg->time_gran && arg->time_gran <= 1000000000)
1379 fm->sb->s_time_gran = arg->time_gran;
1380 if ((flags & FUSE_POSIX_ACL)) {
1381 fc->default_permissions = 1;
1382 fc->posix_acl = 1;
1383 }
1384 if (flags & FUSE_CACHE_SYMLINKS)
1385 fc->cache_symlinks = 1;
1386 if (flags & FUSE_ABORT_ERROR)
1387 fc->abort_err = 1;
1388 if (flags & FUSE_MAX_PAGES) {
1389 fc->max_pages =
1390 min_t(unsigned int, fc->max_pages_limit,
1391 max_t(unsigned int, arg->max_pages, 1));
1392
1393 /*
1394 * PATH_MAX file names might need two pages for
1395 * ops like rename
1396 */
1397 if (fc->max_pages > 1)
1398 fc->name_max = FUSE_NAME_MAX;
1399 }
1400 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1401 if (flags & FUSE_MAP_ALIGNMENT &&
1402 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1403 ok = false;
1404 }
1405 if (flags & FUSE_HAS_INODE_DAX)
1406 fc->inode_dax = 1;
1407 }
1408 if (flags & FUSE_HANDLE_KILLPRIV_V2) {
1409 fc->handle_killpriv_v2 = 1;
1410 fm->sb->s_flags |= SB_NOSEC;
1411 }
1412 if (flags & FUSE_SETXATTR_EXT)
1413 fc->setxattr_ext = 1;
1414 if (flags & FUSE_SECURITY_CTX)
1415 fc->init_security = 1;
1416 if (flags & FUSE_CREATE_SUPP_GROUP)
1417 fc->create_supp_group = 1;
1418 if (flags & FUSE_DIRECT_IO_ALLOW_MMAP)
1419 fc->direct_io_allow_mmap = 1;
1420 /*
1421 * max_stack_depth is the max stack depth of FUSE fs,
1422 * so it has to be at least 1 to support passthrough
1423 * to backing files.
1424 *
1425 * with max_stack_depth > 1, the backing files can be
1426 * on a stacked fs (e.g. overlayfs) themselves and with
1427 * max_stack_depth == 1, FUSE fs can be stacked as the
1428 * underlying fs of a stacked fs (e.g. overlayfs).
1429 *
1430 * Also don't allow the combination of FUSE_PASSTHROUGH
1431 * and FUSE_WRITEBACK_CACHE, current design doesn't handle
1432 * them together.
1433 */
1434 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) &&
1435 (flags & FUSE_PASSTHROUGH) &&
1436 arg->max_stack_depth > 0 &&
1437 arg->max_stack_depth <= FILESYSTEM_MAX_STACK_DEPTH &&
1438 !(flags & FUSE_WRITEBACK_CACHE)) {
1439 fc->passthrough = 1;
1440 fc->max_stack_depth = arg->max_stack_depth;
1441 fm->sb->s_stack_depth = arg->max_stack_depth;
1442 }
1443 if (flags & FUSE_NO_EXPORT_SUPPORT)
1444 fm->sb->s_export_op = &fuse_export_fid_operations;
1445 if (flags & FUSE_ALLOW_IDMAP) {
1446 if (fc->default_permissions)
1447 fm->sb->s_iflags &= ~SB_I_NOIDMAP;
1448 else
1449 ok = false;
1450 }
1451 if (flags & FUSE_OVER_IO_URING && fuse_uring_enabled())
1452 fc->io_uring = 1;
1453
1454 if (flags & FUSE_REQUEST_TIMEOUT)
1455 timeout = arg->request_timeout;
1456 } else {
1457 ra_pages = fc->max_read / PAGE_SIZE;
1458 fc->no_lock = 1;
1459 fc->no_flock = 1;
1460 }
1461
1462 init_server_timeout(fc, timeout);
1463
1464 fm->sb->s_bdi->ra_pages =
1465 min(fm->sb->s_bdi->ra_pages, ra_pages);
1466 fc->minor = arg->minor;
1467 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1468 fc->max_write = max_t(unsigned, 4096, fc->max_write);
1469 fc->conn_init = 1;
1470 }
1471 kfree(ia);
1472
1473 if (!ok) {
1474 fc->conn_init = 0;
1475 fc->conn_error = 1;
1476 }
1477
1478 fuse_set_initialized(fc);
1479 wake_up_all(&fc->blocked_waitq);
1480 }
1481
fuse_new_init(struct fuse_mount * fm)1482 static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
1483 {
1484 struct fuse_init_args *ia;
1485 u64 flags;
1486
1487 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1488
1489 ia->in.major = FUSE_KERNEL_VERSION;
1490 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1491 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1492 flags =
1493 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1494 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1495 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1496 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1497 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1498 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1499 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1500 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1501 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1502 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
1503 FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
1504 FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
1505 FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
1506 FUSE_REQUEST_TIMEOUT;
1507 #ifdef CONFIG_FUSE_DAX
1508 if (fm->fc->dax)
1509 flags |= FUSE_MAP_ALIGNMENT;
1510 if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
1511 flags |= FUSE_HAS_INODE_DAX;
1512 #endif
1513 if (fm->fc->auto_submounts)
1514 flags |= FUSE_SUBMOUNTS;
1515 if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
1516 flags |= FUSE_PASSTHROUGH;
1517
1518 /*
1519 * This is just an information flag for fuse server. No need to check
1520 * the reply - server is either sending IORING_OP_URING_CMD or not.
1521 */
1522 if (fuse_uring_enabled())
1523 flags |= FUSE_OVER_IO_URING;
1524
1525 ia->in.flags = flags;
1526 ia->in.flags2 = flags >> 32;
1527
1528 ia->args.opcode = FUSE_INIT;
1529 ia->args.in_numargs = 1;
1530 ia->args.in_args[0].size = sizeof(ia->in);
1531 ia->args.in_args[0].value = &ia->in;
1532 ia->args.out_numargs = 1;
1533 /* Variable length argument used for backward compatibility
1534 with interface version < 7.5. Rest of init_out is zeroed
1535 by do_get_request(), so a short reply is not a problem */
1536 ia->args.out_argvar = true;
1537 ia->args.out_args[0].size = sizeof(ia->out);
1538 ia->args.out_args[0].value = &ia->out;
1539 ia->args.force = true;
1540 ia->args.nocreds = true;
1541
1542 return ia;
1543 }
1544
fuse_send_init(struct fuse_mount * fm)1545 int fuse_send_init(struct fuse_mount *fm)
1546 {
1547 struct fuse_init_args *ia = fuse_new_init(fm);
1548 int err;
1549
1550 if (fm->fc->sync_init) {
1551 err = fuse_simple_request(fm, &ia->args);
1552 /* Ignore size of init reply */
1553 if (err > 0)
1554 err = 0;
1555 } else {
1556 ia->args.end = process_init_reply;
1557 err = fuse_simple_background(fm, &ia->args, GFP_KERNEL);
1558 if (!err)
1559 return 0;
1560 }
1561 process_init_reply(fm, &ia->args, err);
1562 if (fm->fc->conn_error)
1563 return -ENOTCONN;
1564 return 0;
1565 }
1566 EXPORT_SYMBOL_GPL(fuse_send_init);
1567
fuse_free_conn(struct fuse_conn * fc)1568 void fuse_free_conn(struct fuse_conn *fc)
1569 {
1570 WARN_ON(!list_empty(&fc->devices));
1571 kfree(fc);
1572 }
1573 EXPORT_SYMBOL_GPL(fuse_free_conn);
1574
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1575 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1576 {
1577 int err;
1578 char *suffix = "";
1579
1580 if (sb->s_bdev) {
1581 suffix = "-fuseblk";
1582 /*
1583 * sb->s_bdi points to blkdev's bdi however we want to redirect
1584 * it to our private bdi...
1585 */
1586 bdi_put(sb->s_bdi);
1587 sb->s_bdi = &noop_backing_dev_info;
1588 }
1589 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1590 MINOR(fc->dev), suffix);
1591 if (err)
1592 return err;
1593
1594 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
1595
1596 /*
1597 * For a single fuse filesystem use max 1% of dirty +
1598 * writeback threshold.
1599 *
1600 * This gives about 1M of write buffer for memory maps on a
1601 * machine with 1G and 10% dirty_ratio, which should be more
1602 * than enough.
1603 *
1604 * Privileged users can raise it by writing to
1605 *
1606 * /sys/class/bdi/<bdi>/max_ratio
1607 */
1608 bdi_set_max_ratio(sb->s_bdi, 1);
1609
1610 return 0;
1611 }
1612
fuse_dev_alloc(void)1613 struct fuse_dev *fuse_dev_alloc(void)
1614 {
1615 struct fuse_dev *fud;
1616 struct list_head *pq;
1617
1618 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1619 if (!fud)
1620 return NULL;
1621
1622 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1623 if (!pq) {
1624 kfree(fud);
1625 return NULL;
1626 }
1627
1628 fud->pq.processing = pq;
1629 fuse_pqueue_init(&fud->pq);
1630
1631 return fud;
1632 }
1633 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1634
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1635 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1636 {
1637 fud->fc = fuse_conn_get(fc);
1638 spin_lock(&fc->lock);
1639 list_add_tail(&fud->entry, &fc->devices);
1640 spin_unlock(&fc->lock);
1641 }
1642 EXPORT_SYMBOL_GPL(fuse_dev_install);
1643
fuse_dev_alloc_install(struct fuse_conn * fc)1644 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1645 {
1646 struct fuse_dev *fud;
1647
1648 fud = fuse_dev_alloc();
1649 if (!fud)
1650 return NULL;
1651
1652 fuse_dev_install(fud, fc);
1653 return fud;
1654 }
1655 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1656
fuse_dev_free(struct fuse_dev * fud)1657 void fuse_dev_free(struct fuse_dev *fud)
1658 {
1659 struct fuse_conn *fc = fud->fc;
1660
1661 if (fc) {
1662 spin_lock(&fc->lock);
1663 list_del(&fud->entry);
1664 spin_unlock(&fc->lock);
1665
1666 fuse_conn_put(fc);
1667 }
1668 kfree(fud->pq.processing);
1669 kfree(fud);
1670 }
1671 EXPORT_SYMBOL_GPL(fuse_dev_free);
1672
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct fuse_inode * fi)1673 static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1674 const struct fuse_inode *fi)
1675 {
1676 struct timespec64 atime = inode_get_atime(&fi->inode);
1677 struct timespec64 mtime = inode_get_mtime(&fi->inode);
1678 struct timespec64 ctime = inode_get_ctime(&fi->inode);
1679
1680 *attr = (struct fuse_attr){
1681 .ino = fi->inode.i_ino,
1682 .size = fi->inode.i_size,
1683 .blocks = fi->inode.i_blocks,
1684 .atime = atime.tv_sec,
1685 .mtime = mtime.tv_sec,
1686 .ctime = ctime.tv_sec,
1687 .atimensec = atime.tv_nsec,
1688 .mtimensec = mtime.tv_nsec,
1689 .ctimensec = ctime.tv_nsec,
1690 .mode = fi->inode.i_mode,
1691 .nlink = fi->inode.i_nlink,
1692 .uid = __kuid_val(fi->inode.i_uid),
1693 .gid = __kgid_val(fi->inode.i_gid),
1694 .rdev = fi->inode.i_rdev,
1695 .blksize = 1u << fi->inode.i_blkbits,
1696 };
1697 }
1698
fuse_sb_defaults(struct super_block * sb)1699 static void fuse_sb_defaults(struct super_block *sb)
1700 {
1701 sb->s_magic = FUSE_SUPER_MAGIC;
1702 sb->s_op = &fuse_super_operations;
1703 sb->s_xattr = fuse_xattr_handlers;
1704 sb->s_maxbytes = MAX_LFS_FILESIZE;
1705 sb->s_time_gran = 1;
1706 sb->s_export_op = &fuse_export_operations;
1707 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1708 sb->s_iflags |= SB_I_NOIDMAP;
1709 if (sb->s_user_ns != &init_user_ns)
1710 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1711 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1712 }
1713
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)1714 static int fuse_fill_super_submount(struct super_block *sb,
1715 struct fuse_inode *parent_fi)
1716 {
1717 struct fuse_mount *fm = get_fuse_mount_super(sb);
1718 struct super_block *parent_sb = parent_fi->inode.i_sb;
1719 struct fuse_attr root_attr;
1720 struct inode *root;
1721 struct fuse_submount_lookup *sl;
1722 struct fuse_inode *fi;
1723
1724 fuse_sb_defaults(sb);
1725 fm->sb = sb;
1726
1727 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1728 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1729
1730 sb->s_xattr = parent_sb->s_xattr;
1731 sb->s_export_op = parent_sb->s_export_op;
1732 sb->s_time_gran = parent_sb->s_time_gran;
1733 sb->s_blocksize = parent_sb->s_blocksize;
1734 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1735 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1736 if (parent_sb->s_subtype && !sb->s_subtype)
1737 return -ENOMEM;
1738
1739 fuse_fill_attr_from_inode(&root_attr, parent_fi);
1740 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0,
1741 fuse_get_evict_ctr(fm->fc));
1742 /*
1743 * This inode is just a duplicate, so it is not looked up and
1744 * its nlookup should not be incremented. fuse_iget() does
1745 * that, though, so undo it here.
1746 */
1747 fi = get_fuse_inode(root);
1748 fi->nlookup--;
1749
1750 set_default_d_op(sb, &fuse_dentry_operations);
1751 sb->s_root = d_make_root(root);
1752 if (!sb->s_root)
1753 return -ENOMEM;
1754
1755 /*
1756 * Grab the parent's submount_lookup pointer and take a
1757 * reference on the shared nlookup from the parent. This is to
1758 * prevent the last forget for this nodeid from getting
1759 * triggered until all users have finished with it.
1760 */
1761 sl = parent_fi->submount_lookup;
1762 WARN_ON(!sl);
1763 if (sl) {
1764 refcount_inc(&sl->count);
1765 fi->submount_lookup = sl;
1766 }
1767
1768 return 0;
1769 }
1770
1771 /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1772 static int fuse_get_tree_submount(struct fs_context *fsc)
1773 {
1774 struct fuse_mount *fm;
1775 struct fuse_inode *mp_fi = fsc->fs_private;
1776 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1777 struct super_block *sb;
1778 int err;
1779
1780 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1781 if (!fm)
1782 return -ENOMEM;
1783
1784 fm->fc = fuse_conn_get(fc);
1785 fsc->s_fs_info = fm;
1786 sb = sget_fc(fsc, NULL, set_anon_super_fc);
1787 if (fsc->s_fs_info)
1788 fuse_mount_destroy(fm);
1789 if (IS_ERR(sb))
1790 return PTR_ERR(sb);
1791
1792 /* Initialize superblock, making @mp_fi its root */
1793 err = fuse_fill_super_submount(sb, mp_fi);
1794 if (err) {
1795 deactivate_locked_super(sb);
1796 return err;
1797 }
1798
1799 down_write(&fc->killsb);
1800 list_add_tail(&fm->fc_entry, &fc->mounts);
1801 up_write(&fc->killsb);
1802
1803 sb->s_flags |= SB_ACTIVE;
1804 fsc->root = dget(sb->s_root);
1805
1806 return 0;
1807 }
1808
1809 static const struct fs_context_operations fuse_context_submount_ops = {
1810 .get_tree = fuse_get_tree_submount,
1811 };
1812
fuse_init_fs_context_submount(struct fs_context * fsc)1813 int fuse_init_fs_context_submount(struct fs_context *fsc)
1814 {
1815 fsc->ops = &fuse_context_submount_ops;
1816 return 0;
1817 }
1818 EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1819
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1820 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1821 {
1822 struct fuse_dev *fud = NULL;
1823 struct fuse_mount *fm = get_fuse_mount_super(sb);
1824 struct fuse_conn *fc = fm->fc;
1825 struct inode *root;
1826 struct dentry *root_dentry;
1827 int err;
1828
1829 err = -EINVAL;
1830 if (sb->s_flags & SB_MANDLOCK)
1831 goto err;
1832
1833 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
1834 fuse_sb_defaults(sb);
1835
1836 if (ctx->is_bdev) {
1837 #ifdef CONFIG_BLOCK
1838 err = -EINVAL;
1839 if (!sb_set_blocksize(sb, ctx->blksize))
1840 goto err;
1841 /*
1842 * This is a workaround until fuse hooks into iomap for reads.
1843 * Use PAGE_SIZE for the blocksize else if the writeback cache
1844 * is enabled, buffered writes go through iomap and a read may
1845 * overwrite partially written data if blocksize < PAGE_SIZE
1846 */
1847 fc->blkbits = sb->s_blocksize_bits;
1848 if (ctx->blksize != PAGE_SIZE &&
1849 !sb_set_blocksize(sb, PAGE_SIZE))
1850 goto err;
1851 #endif
1852 fc->sync_fs = 1;
1853 } else {
1854 sb->s_blocksize = PAGE_SIZE;
1855 sb->s_blocksize_bits = PAGE_SHIFT;
1856 fc->blkbits = sb->s_blocksize_bits;
1857 }
1858
1859 sb->s_subtype = ctx->subtype;
1860 ctx->subtype = NULL;
1861 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1862 err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
1863 if (err)
1864 goto err;
1865 }
1866
1867 if (ctx->fudptr) {
1868 err = -ENOMEM;
1869 fud = fuse_dev_alloc_install(fc);
1870 if (!fud)
1871 goto err_free_dax;
1872 }
1873
1874 fc->dev = sb->s_dev;
1875 fm->sb = sb;
1876 err = fuse_bdi_init(fc, sb);
1877 if (err)
1878 goto err_dev_free;
1879
1880 /* Handle umasking inside the fuse code */
1881 if (sb->s_flags & SB_POSIXACL)
1882 fc->dont_mask = 1;
1883 sb->s_flags |= SB_POSIXACL;
1884
1885 fc->default_permissions = ctx->default_permissions;
1886 fc->allow_other = ctx->allow_other;
1887 fc->user_id = ctx->user_id;
1888 fc->group_id = ctx->group_id;
1889 fc->legacy_opts_show = ctx->legacy_opts_show;
1890 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1891 fc->destroy = ctx->destroy;
1892 fc->no_control = ctx->no_control;
1893 fc->no_force_umount = ctx->no_force_umount;
1894
1895 err = -ENOMEM;
1896 root = fuse_get_root_inode(sb, ctx->rootmode);
1897 set_default_d_op(sb, &fuse_dentry_operations);
1898 root_dentry = d_make_root(root);
1899 if (!root_dentry)
1900 goto err_dev_free;
1901
1902 mutex_lock(&fuse_mutex);
1903 err = -EINVAL;
1904 if (ctx->fudptr && *ctx->fudptr) {
1905 if (*ctx->fudptr == FUSE_DEV_SYNC_INIT)
1906 fc->sync_init = 1;
1907 else
1908 goto err_unlock;
1909 }
1910
1911 err = fuse_ctl_add_conn(fc);
1912 if (err)
1913 goto err_unlock;
1914
1915 list_add_tail(&fc->entry, &fuse_conn_list);
1916 sb->s_root = root_dentry;
1917 if (ctx->fudptr) {
1918 *ctx->fudptr = fud;
1919 wake_up_all(&fuse_dev_waitq);
1920 }
1921 mutex_unlock(&fuse_mutex);
1922 return 0;
1923
1924 err_unlock:
1925 mutex_unlock(&fuse_mutex);
1926 dput(root_dentry);
1927 err_dev_free:
1928 if (fud)
1929 fuse_dev_free(fud);
1930 err_free_dax:
1931 if (IS_ENABLED(CONFIG_FUSE_DAX))
1932 fuse_dax_conn_free(fc);
1933 err:
1934 return err;
1935 }
1936 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1937
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1938 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1939 {
1940 struct fuse_fs_context *ctx = fsc->fs_private;
1941 struct fuse_mount *fm;
1942 int err;
1943
1944 if (!ctx->file || !ctx->rootmode_present ||
1945 !ctx->user_id_present || !ctx->group_id_present)
1946 return -EINVAL;
1947
1948 /*
1949 * Require mount to happen from the same user namespace which
1950 * opened /dev/fuse to prevent potential attacks.
1951 */
1952 if ((ctx->file->f_op != &fuse_dev_operations) ||
1953 (ctx->file->f_cred->user_ns != sb->s_user_ns))
1954 return -EINVAL;
1955 ctx->fudptr = &ctx->file->private_data;
1956
1957 err = fuse_fill_super_common(sb, ctx);
1958 if (err)
1959 return err;
1960 /* file->private_data shall be visible on all CPUs after this */
1961 smp_mb();
1962
1963 fm = get_fuse_mount_super(sb);
1964
1965 return fuse_send_init(fm);
1966 }
1967
1968 /*
1969 * This is the path where user supplied an already initialized fuse dev. In
1970 * this case never create a new super if the old one is gone.
1971 */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)1972 static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1973 {
1974 return -ENOTCONN;
1975 }
1976
fuse_test_super(struct super_block * sb,struct fs_context * fsc)1977 static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
1978 {
1979
1980 return fsc->sget_key == get_fuse_conn_super(sb);
1981 }
1982
fuse_get_tree(struct fs_context * fsc)1983 static int fuse_get_tree(struct fs_context *fsc)
1984 {
1985 struct fuse_fs_context *ctx = fsc->fs_private;
1986 struct fuse_dev *fud;
1987 struct fuse_conn *fc;
1988 struct fuse_mount *fm;
1989 struct super_block *sb;
1990 int err;
1991
1992 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1993 if (!fc)
1994 return -ENOMEM;
1995
1996 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1997 if (!fm) {
1998 kfree(fc);
1999 return -ENOMEM;
2000 }
2001
2002 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
2003 fc->release = fuse_free_conn;
2004
2005 fsc->s_fs_info = fm;
2006
2007 if (ctx->fd_present)
2008 ctx->file = fget(ctx->fd);
2009
2010 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
2011 err = get_tree_bdev(fsc, fuse_fill_super);
2012 goto out;
2013 }
2014 /*
2015 * While block dev mount can be initialized with a dummy device fd
2016 * (found by device name), normal fuse mounts can't
2017 */
2018 err = -EINVAL;
2019 if (!ctx->file)
2020 goto out;
2021
2022 /*
2023 * Allow creating a fuse mount with an already initialized fuse
2024 * connection
2025 */
2026 fud = __fuse_get_dev(ctx->file);
2027 if (ctx->file->f_op == &fuse_dev_operations && fud) {
2028 fsc->sget_key = fud->fc;
2029 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
2030 err = PTR_ERR_OR_ZERO(sb);
2031 if (!IS_ERR(sb))
2032 fsc->root = dget(sb->s_root);
2033 } else {
2034 err = get_tree_nodev(fsc, fuse_fill_super);
2035 }
2036 out:
2037 if (fsc->s_fs_info)
2038 fuse_mount_destroy(fm);
2039 if (ctx->file)
2040 fput(ctx->file);
2041 return err;
2042 }
2043
2044 static const struct fs_context_operations fuse_context_ops = {
2045 .free = fuse_free_fsc,
2046 .parse_param = fuse_parse_param,
2047 .reconfigure = fuse_reconfigure,
2048 .get_tree = fuse_get_tree,
2049 };
2050
2051 /*
2052 * Set up the filesystem mount context.
2053 */
fuse_init_fs_context(struct fs_context * fsc)2054 static int fuse_init_fs_context(struct fs_context *fsc)
2055 {
2056 struct fuse_fs_context *ctx;
2057
2058 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
2059 if (!ctx)
2060 return -ENOMEM;
2061
2062 ctx->max_read = ~0;
2063 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
2064 ctx->legacy_opts_show = true;
2065
2066 #ifdef CONFIG_BLOCK
2067 if (fsc->fs_type == &fuseblk_fs_type) {
2068 ctx->is_bdev = true;
2069 ctx->destroy = true;
2070 }
2071 #endif
2072
2073 fsc->fs_private = ctx;
2074 fsc->ops = &fuse_context_ops;
2075 return 0;
2076 }
2077
fuse_mount_remove(struct fuse_mount * fm)2078 bool fuse_mount_remove(struct fuse_mount *fm)
2079 {
2080 struct fuse_conn *fc = fm->fc;
2081 bool last = false;
2082
2083 down_write(&fc->killsb);
2084 list_del_init(&fm->fc_entry);
2085 if (list_empty(&fc->mounts))
2086 last = true;
2087 up_write(&fc->killsb);
2088
2089 return last;
2090 }
2091 EXPORT_SYMBOL_GPL(fuse_mount_remove);
2092
fuse_conn_destroy(struct fuse_mount * fm)2093 void fuse_conn_destroy(struct fuse_mount *fm)
2094 {
2095 struct fuse_conn *fc = fm->fc;
2096
2097 if (fc->destroy)
2098 fuse_send_destroy(fm);
2099
2100 fuse_abort_conn(fc);
2101 fuse_wait_aborted(fc);
2102
2103 if (!list_empty(&fc->entry)) {
2104 mutex_lock(&fuse_mutex);
2105 list_del(&fc->entry);
2106 fuse_ctl_remove_conn(fc);
2107 mutex_unlock(&fuse_mutex);
2108 }
2109 }
2110 EXPORT_SYMBOL_GPL(fuse_conn_destroy);
2111
fuse_sb_destroy(struct super_block * sb)2112 static void fuse_sb_destroy(struct super_block *sb)
2113 {
2114 struct fuse_mount *fm = get_fuse_mount_super(sb);
2115 bool last;
2116
2117 if (sb->s_root) {
2118 last = fuse_mount_remove(fm);
2119 if (last)
2120 fuse_conn_destroy(fm);
2121 }
2122 }
2123
fuse_mount_destroy(struct fuse_mount * fm)2124 void fuse_mount_destroy(struct fuse_mount *fm)
2125 {
2126 fuse_conn_put(fm->fc);
2127 kfree_rcu(fm, rcu);
2128 }
2129 EXPORT_SYMBOL(fuse_mount_destroy);
2130
fuse_kill_sb_anon(struct super_block * sb)2131 static void fuse_kill_sb_anon(struct super_block *sb)
2132 {
2133 fuse_sb_destroy(sb);
2134 kill_anon_super(sb);
2135 fuse_mount_destroy(get_fuse_mount_super(sb));
2136 }
2137
2138 static struct file_system_type fuse_fs_type = {
2139 .owner = THIS_MODULE,
2140 .name = "fuse",
2141 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT | FS_ALLOW_IDMAP,
2142 .init_fs_context = fuse_init_fs_context,
2143 .parameters = fuse_fs_parameters,
2144 .kill_sb = fuse_kill_sb_anon,
2145 };
2146 MODULE_ALIAS_FS("fuse");
2147
2148 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)2149 static void fuse_kill_sb_blk(struct super_block *sb)
2150 {
2151 fuse_sb_destroy(sb);
2152 kill_block_super(sb);
2153 fuse_mount_destroy(get_fuse_mount_super(sb));
2154 }
2155
2156 static struct file_system_type fuseblk_fs_type = {
2157 .owner = THIS_MODULE,
2158 .name = "fuseblk",
2159 .init_fs_context = fuse_init_fs_context,
2160 .parameters = fuse_fs_parameters,
2161 .kill_sb = fuse_kill_sb_blk,
2162 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE | FS_ALLOW_IDMAP,
2163 };
2164 MODULE_ALIAS_FS("fuseblk");
2165
register_fuseblk(void)2166 static inline int register_fuseblk(void)
2167 {
2168 return register_filesystem(&fuseblk_fs_type);
2169 }
2170
unregister_fuseblk(void)2171 static inline void unregister_fuseblk(void)
2172 {
2173 unregister_filesystem(&fuseblk_fs_type);
2174 }
2175 #else
register_fuseblk(void)2176 static inline int register_fuseblk(void)
2177 {
2178 return 0;
2179 }
2180
unregister_fuseblk(void)2181 static inline void unregister_fuseblk(void)
2182 {
2183 }
2184 #endif
2185
fuse_inode_init_once(void * foo)2186 static void fuse_inode_init_once(void *foo)
2187 {
2188 struct inode *inode = foo;
2189
2190 inode_init_once(inode);
2191 }
2192
fuse_fs_init(void)2193 static int __init fuse_fs_init(void)
2194 {
2195 int err;
2196
2197 fuse_inode_cachep = kmem_cache_create("fuse_inode",
2198 sizeof(struct fuse_inode), 0,
2199 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
2200 fuse_inode_init_once);
2201 err = -ENOMEM;
2202 if (!fuse_inode_cachep)
2203 goto out;
2204
2205 err = register_fuseblk();
2206 if (err)
2207 goto out2;
2208
2209 err = register_filesystem(&fuse_fs_type);
2210 if (err)
2211 goto out3;
2212
2213 err = fuse_sysctl_register();
2214 if (err)
2215 goto out4;
2216
2217 return 0;
2218
2219 out4:
2220 unregister_filesystem(&fuse_fs_type);
2221 out3:
2222 unregister_fuseblk();
2223 out2:
2224 kmem_cache_destroy(fuse_inode_cachep);
2225 out:
2226 return err;
2227 }
2228
fuse_fs_cleanup(void)2229 static void fuse_fs_cleanup(void)
2230 {
2231 fuse_sysctl_unregister();
2232 unregister_filesystem(&fuse_fs_type);
2233 unregister_fuseblk();
2234
2235 /*
2236 * Make sure all delayed rcu free inodes are flushed before we
2237 * destroy cache.
2238 */
2239 rcu_barrier();
2240 kmem_cache_destroy(fuse_inode_cachep);
2241 }
2242
2243 static struct kobject *fuse_kobj;
2244
fuse_sysfs_init(void)2245 static int fuse_sysfs_init(void)
2246 {
2247 int err;
2248
2249 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
2250 if (!fuse_kobj) {
2251 err = -ENOMEM;
2252 goto out_err;
2253 }
2254
2255 err = sysfs_create_mount_point(fuse_kobj, "connections");
2256 if (err)
2257 goto out_fuse_unregister;
2258
2259 return 0;
2260
2261 out_fuse_unregister:
2262 kobject_put(fuse_kobj);
2263 out_err:
2264 return err;
2265 }
2266
fuse_sysfs_cleanup(void)2267 static void fuse_sysfs_cleanup(void)
2268 {
2269 sysfs_remove_mount_point(fuse_kobj, "connections");
2270 kobject_put(fuse_kobj);
2271 }
2272
fuse_init(void)2273 static int __init fuse_init(void)
2274 {
2275 int res;
2276
2277 pr_info("init (API version %i.%i)\n",
2278 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2279
2280 INIT_LIST_HEAD(&fuse_conn_list);
2281 res = fuse_fs_init();
2282 if (res)
2283 goto err;
2284
2285 res = fuse_dev_init();
2286 if (res)
2287 goto err_fs_cleanup;
2288
2289 res = fuse_sysfs_init();
2290 if (res)
2291 goto err_dev_cleanup;
2292
2293 res = fuse_ctl_init();
2294 if (res)
2295 goto err_sysfs_cleanup;
2296
2297 sanitize_global_limit(&max_user_bgreq);
2298 sanitize_global_limit(&max_user_congthresh);
2299
2300 return 0;
2301
2302 err_sysfs_cleanup:
2303 fuse_sysfs_cleanup();
2304 err_dev_cleanup:
2305 fuse_dev_cleanup();
2306 err_fs_cleanup:
2307 fuse_fs_cleanup();
2308 err:
2309 return res;
2310 }
2311
fuse_exit(void)2312 static void __exit fuse_exit(void)
2313 {
2314 pr_debug("exit\n");
2315
2316 fuse_ctl_cleanup();
2317 fuse_sysfs_cleanup();
2318 fuse_fs_cleanup();
2319 fuse_dev_cleanup();
2320 }
2321
2322 module_init(fuse_init);
2323 module_exit(fuse_exit);
2324