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