xref: /OK3568_Linux_fs/kernel/fs/fuse/inode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
717 	INIT_LIST_HEAD(&fc->mounts);
718 	list_add(&fm->fc_entry, &fc->mounts);
719 	fm->fc = fc;
720 	refcount_set(&fm->count, 1);
721 }
722 EXPORT_SYMBOL_GPL(fuse_conn_init);
723 
fuse_conn_put(struct fuse_conn * fc)724 void fuse_conn_put(struct fuse_conn *fc)
725 {
726 	if (refcount_dec_and_test(&fc->count)) {
727 		struct fuse_iqueue *fiq = &fc->iq;
728 
729 		if (IS_ENABLED(CONFIG_FUSE_DAX))
730 			fuse_dax_conn_free(fc);
731 		if (fiq->ops->release)
732 			fiq->ops->release(fiq);
733 		put_pid_ns(fc->pid_ns);
734 		put_user_ns(fc->user_ns);
735 		fc->release(fc);
736 	}
737 }
738 EXPORT_SYMBOL_GPL(fuse_conn_put);
739 
fuse_conn_get(struct fuse_conn * fc)740 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
741 {
742 	refcount_inc(&fc->count);
743 	return fc;
744 }
745 EXPORT_SYMBOL_GPL(fuse_conn_get);
746 
fuse_mount_put(struct fuse_mount * fm)747 void fuse_mount_put(struct fuse_mount *fm)
748 {
749 	if (refcount_dec_and_test(&fm->count)) {
750 		if (fm->fc)
751 			fuse_conn_put(fm->fc);
752 		kfree(fm);
753 	}
754 }
755 EXPORT_SYMBOL_GPL(fuse_mount_put);
756 
fuse_mount_get(struct fuse_mount * fm)757 struct fuse_mount *fuse_mount_get(struct fuse_mount *fm)
758 {
759 	refcount_inc(&fm->count);
760 	return fm;
761 }
762 EXPORT_SYMBOL_GPL(fuse_mount_get);
763 
fuse_get_root_inode(struct super_block * sb,unsigned mode)764 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
765 {
766 	struct fuse_attr attr;
767 	memset(&attr, 0, sizeof(attr));
768 
769 	attr.mode = mode;
770 	attr.ino = FUSE_ROOT_ID;
771 	attr.nlink = 1;
772 	return fuse_iget(sb, 1, 0, &attr, 0, 0);
773 }
774 
775 struct fuse_inode_handle {
776 	u64 nodeid;
777 	u32 generation;
778 };
779 
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)780 static struct dentry *fuse_get_dentry(struct super_block *sb,
781 				      struct fuse_inode_handle *handle)
782 {
783 	struct fuse_conn *fc = get_fuse_conn_super(sb);
784 	struct inode *inode;
785 	struct dentry *entry;
786 	int err = -ESTALE;
787 
788 	if (handle->nodeid == 0)
789 		goto out_err;
790 
791 	inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
792 	if (!inode) {
793 		struct fuse_entry_out outarg;
794 		const struct qstr name = QSTR_INIT(".", 1);
795 
796 		if (!fc->export_support)
797 			goto out_err;
798 
799 		err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
800 				       &inode);
801 		if (err && err != -ENOENT)
802 			goto out_err;
803 		if (err || !inode) {
804 			err = -ESTALE;
805 			goto out_err;
806 		}
807 		err = -EIO;
808 		if (get_node_id(inode) != handle->nodeid)
809 			goto out_iput;
810 	}
811 	err = -ESTALE;
812 	if (inode->i_generation != handle->generation)
813 		goto out_iput;
814 
815 	entry = d_obtain_alias(inode);
816 	if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
817 		fuse_invalidate_entry_cache(entry);
818 
819 	return entry;
820 
821  out_iput:
822 	iput(inode);
823  out_err:
824 	return ERR_PTR(err);
825 }
826 
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)827 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
828 			   struct inode *parent)
829 {
830 	int len = parent ? 6 : 3;
831 	u64 nodeid;
832 	u32 generation;
833 
834 	if (*max_len < len) {
835 		*max_len = len;
836 		return  FILEID_INVALID;
837 	}
838 
839 	nodeid = get_fuse_inode(inode)->nodeid;
840 	generation = inode->i_generation;
841 
842 	fh[0] = (u32)(nodeid >> 32);
843 	fh[1] = (u32)(nodeid & 0xffffffff);
844 	fh[2] = generation;
845 
846 	if (parent) {
847 		nodeid = get_fuse_inode(parent)->nodeid;
848 		generation = parent->i_generation;
849 
850 		fh[3] = (u32)(nodeid >> 32);
851 		fh[4] = (u32)(nodeid & 0xffffffff);
852 		fh[5] = generation;
853 	}
854 
855 	*max_len = len;
856 	return parent ? 0x82 : 0x81;
857 }
858 
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)859 static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
860 		struct fid *fid, int fh_len, int fh_type)
861 {
862 	struct fuse_inode_handle handle;
863 
864 	if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
865 		return NULL;
866 
867 	handle.nodeid = (u64) fid->raw[0] << 32;
868 	handle.nodeid |= (u64) fid->raw[1];
869 	handle.generation = fid->raw[2];
870 	return fuse_get_dentry(sb, &handle);
871 }
872 
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)873 static struct dentry *fuse_fh_to_parent(struct super_block *sb,
874 		struct fid *fid, int fh_len, int fh_type)
875 {
876 	struct fuse_inode_handle parent;
877 
878 	if (fh_type != 0x82 || fh_len < 6)
879 		return NULL;
880 
881 	parent.nodeid = (u64) fid->raw[3] << 32;
882 	parent.nodeid |= (u64) fid->raw[4];
883 	parent.generation = fid->raw[5];
884 	return fuse_get_dentry(sb, &parent);
885 }
886 
fuse_get_parent(struct dentry * child)887 static struct dentry *fuse_get_parent(struct dentry *child)
888 {
889 	struct inode *child_inode = d_inode(child);
890 	struct fuse_conn *fc = get_fuse_conn(child_inode);
891 	struct inode *inode;
892 	struct dentry *parent;
893 	struct fuse_entry_out outarg;
894 	const struct qstr name = QSTR_INIT("..", 2);
895 	int err;
896 
897 	if (!fc->export_support)
898 		return ERR_PTR(-ESTALE);
899 
900 	err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
901 			       &name, &outarg, &inode);
902 	if (err) {
903 		if (err == -ENOENT)
904 			return ERR_PTR(-ESTALE);
905 		return ERR_PTR(err);
906 	}
907 
908 	parent = d_obtain_alias(inode);
909 	if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
910 		fuse_invalidate_entry_cache(parent);
911 
912 	return parent;
913 }
914 
915 static const struct export_operations fuse_export_operations = {
916 	.fh_to_dentry	= fuse_fh_to_dentry,
917 	.fh_to_parent	= fuse_fh_to_parent,
918 	.encode_fh	= fuse_encode_fh,
919 	.get_parent	= fuse_get_parent,
920 };
921 
922 static const struct super_operations fuse_super_operations = {
923 	.alloc_inode    = fuse_alloc_inode,
924 	.free_inode     = fuse_free_inode,
925 	.evict_inode	= fuse_evict_inode,
926 	.write_inode	= fuse_write_inode,
927 	.drop_inode	= generic_delete_inode,
928 	.put_super	= fuse_put_super,
929 	.umount_begin	= fuse_umount_begin,
930 	.statfs		= fuse_statfs,
931 	.show_options	= fuse_show_options,
932 };
933 
sanitize_global_limit(unsigned * limit)934 static void sanitize_global_limit(unsigned *limit)
935 {
936 	/*
937 	 * The default maximum number of async requests is calculated to consume
938 	 * 1/2^13 of the total memory, assuming 392 bytes per request.
939 	 */
940 	if (*limit == 0)
941 		*limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
942 
943 	if (*limit >= 1 << 16)
944 		*limit = (1 << 16) - 1;
945 }
946 
set_global_limit(const char * val,const struct kernel_param * kp)947 static int set_global_limit(const char *val, const struct kernel_param *kp)
948 {
949 	int rv;
950 
951 	rv = param_set_uint(val, kp);
952 	if (rv)
953 		return rv;
954 
955 	sanitize_global_limit((unsigned *)kp->arg);
956 
957 	return 0;
958 }
959 
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)960 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
961 {
962 	int cap_sys_admin = capable(CAP_SYS_ADMIN);
963 
964 	if (arg->minor < 13)
965 		return;
966 
967 	sanitize_global_limit(&max_user_bgreq);
968 	sanitize_global_limit(&max_user_congthresh);
969 
970 	spin_lock(&fc->bg_lock);
971 	if (arg->max_background) {
972 		fc->max_background = arg->max_background;
973 
974 		if (!cap_sys_admin && fc->max_background > max_user_bgreq)
975 			fc->max_background = max_user_bgreq;
976 	}
977 	if (arg->congestion_threshold) {
978 		fc->congestion_threshold = arg->congestion_threshold;
979 
980 		if (!cap_sys_admin &&
981 		    fc->congestion_threshold > max_user_congthresh)
982 			fc->congestion_threshold = max_user_congthresh;
983 	}
984 	spin_unlock(&fc->bg_lock);
985 }
986 
987 struct fuse_init_args {
988 	struct fuse_args args;
989 	struct fuse_init_in in;
990 	struct fuse_init_out out;
991 };
992 
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)993 static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
994 			       int error)
995 {
996 	struct fuse_conn *fc = fm->fc;
997 	struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
998 	struct fuse_init_out *arg = &ia->out;
999 	bool ok = true;
1000 
1001 	if (error || arg->major != FUSE_KERNEL_VERSION)
1002 		ok = false;
1003 	else {
1004 		unsigned long ra_pages;
1005 
1006 		process_init_limits(fc, arg);
1007 
1008 		if (arg->minor >= 6) {
1009 			ra_pages = arg->max_readahead / PAGE_SIZE;
1010 			if (arg->flags & FUSE_ASYNC_READ)
1011 				fc->async_read = 1;
1012 			if (!(arg->flags & FUSE_POSIX_LOCKS))
1013 				fc->no_lock = 1;
1014 			if (arg->minor >= 17) {
1015 				if (!(arg->flags & FUSE_FLOCK_LOCKS))
1016 					fc->no_flock = 1;
1017 			} else {
1018 				if (!(arg->flags & FUSE_POSIX_LOCKS))
1019 					fc->no_flock = 1;
1020 			}
1021 			if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1022 				fc->atomic_o_trunc = 1;
1023 			if (arg->minor >= 9) {
1024 				/* LOOKUP has dependency on proto version */
1025 				if (arg->flags & FUSE_EXPORT_SUPPORT)
1026 					fc->export_support = 1;
1027 			}
1028 			if (arg->flags & FUSE_BIG_WRITES)
1029 				fc->big_writes = 1;
1030 			if (arg->flags & FUSE_DONT_MASK)
1031 				fc->dont_mask = 1;
1032 			if (arg->flags & FUSE_AUTO_INVAL_DATA)
1033 				fc->auto_inval_data = 1;
1034 			else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
1035 				fc->explicit_inval_data = 1;
1036 			if (arg->flags & FUSE_DO_READDIRPLUS) {
1037 				fc->do_readdirplus = 1;
1038 				if (arg->flags & FUSE_READDIRPLUS_AUTO)
1039 					fc->readdirplus_auto = 1;
1040 			}
1041 			if (arg->flags & FUSE_ASYNC_DIO)
1042 				fc->async_dio = 1;
1043 			if (arg->flags & FUSE_WRITEBACK_CACHE)
1044 				fc->writeback_cache = 1;
1045 			if (arg->flags & FUSE_PARALLEL_DIROPS)
1046 				fc->parallel_dirops = 1;
1047 			if (arg->flags & FUSE_HANDLE_KILLPRIV)
1048 				fc->handle_killpriv = 1;
1049 			if (arg->time_gran && arg->time_gran <= 1000000000)
1050 				fm->sb->s_time_gran = arg->time_gran;
1051 			if ((arg->flags & FUSE_POSIX_ACL)) {
1052 				fc->default_permissions = 1;
1053 				fc->posix_acl = 1;
1054 				fm->sb->s_xattr = fuse_acl_xattr_handlers;
1055 			}
1056 			if (arg->flags & FUSE_CACHE_SYMLINKS)
1057 				fc->cache_symlinks = 1;
1058 			if (arg->flags & FUSE_ABORT_ERROR)
1059 				fc->abort_err = 1;
1060 			if (arg->flags & FUSE_MAX_PAGES) {
1061 				fc->max_pages =
1062 					min_t(unsigned int, FUSE_MAX_MAX_PAGES,
1063 					max_t(unsigned int, arg->max_pages, 1));
1064 			}
1065 			if (IS_ENABLED(CONFIG_FUSE_DAX) &&
1066 			    arg->flags & FUSE_MAP_ALIGNMENT &&
1067 			    !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1068 				ok = false;
1069 			}
1070 			if (arg->flags & FUSE_PASSTHROUGH) {
1071 				fc->passthrough = 1;
1072 				/* Prevent further stacking */
1073 				fm->sb->s_stack_depth =
1074 					FILESYSTEM_MAX_STACK_DEPTH;
1075 			}
1076 		} else {
1077 			ra_pages = fc->max_read / PAGE_SIZE;
1078 			fc->no_lock = 1;
1079 			fc->no_flock = 1;
1080 		}
1081 
1082 		fm->sb->s_bdi->ra_pages =
1083 				min(fm->sb->s_bdi->ra_pages, ra_pages);
1084 		fc->minor = arg->minor;
1085 		fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1086 		fc->max_write = max_t(unsigned, 4096, fc->max_write);
1087 		fc->conn_init = 1;
1088 	}
1089 	kfree(ia);
1090 
1091 	if (!ok) {
1092 		fc->conn_init = 0;
1093 		fc->conn_error = 1;
1094 	}
1095 
1096 	fuse_set_initialized(fc);
1097 	wake_up_all(&fc->blocked_waitq);
1098 }
1099 
fuse_send_init(struct fuse_mount * fm)1100 void fuse_send_init(struct fuse_mount *fm)
1101 {
1102 	struct fuse_init_args *ia;
1103 
1104 	ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1105 
1106 	ia->in.major = FUSE_KERNEL_VERSION;
1107 	ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1108 	ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1109 	ia->in.flags |=
1110 		FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1111 		FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
1112 		FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
1113 		FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
1114 		FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
1115 		FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
1116 		FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1117 		FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1118 		FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1119 		FUSE_PASSTHROUGH;
1120 #ifdef CONFIG_FUSE_DAX
1121 	if (fm->fc->dax)
1122 		ia->in.flags |= FUSE_MAP_ALIGNMENT;
1123 #endif
1124 	if (fm->fc->auto_submounts)
1125 		ia->in.flags |= FUSE_SUBMOUNTS;
1126 
1127 	ia->args.opcode = FUSE_INIT;
1128 	ia->args.in_numargs = 1;
1129 	ia->args.in_args[0].size = sizeof(ia->in);
1130 	ia->args.in_args[0].value = &ia->in;
1131 	ia->args.out_numargs = 1;
1132 	/* Variable length argument used for backward compatibility
1133 	   with interface version < 7.5.  Rest of init_out is zeroed
1134 	   by do_get_request(), so a short reply is not a problem */
1135 	ia->args.out_argvar = true;
1136 	ia->args.out_args[0].size = sizeof(ia->out);
1137 	ia->args.out_args[0].value = &ia->out;
1138 	ia->args.force = true;
1139 	ia->args.nocreds = true;
1140 	ia->args.end = process_init_reply;
1141 
1142 	if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1143 		process_init_reply(fm, &ia->args, -ENOTCONN);
1144 }
1145 EXPORT_SYMBOL_GPL(fuse_send_init);
1146 
free_fuse_passthrough(int id,void * p,void * data)1147 static int free_fuse_passthrough(int id, void *p, void *data)
1148 {
1149 	struct fuse_passthrough *passthrough = (struct fuse_passthrough *)p;
1150 
1151 	fuse_passthrough_release(passthrough);
1152 	kfree(p);
1153 
1154 	return 0;
1155 }
1156 
fuse_free_conn(struct fuse_conn * fc)1157 void fuse_free_conn(struct fuse_conn *fc)
1158 {
1159 	WARN_ON(!list_empty(&fc->devices));
1160 	idr_for_each(&fc->passthrough_req, free_fuse_passthrough, NULL);
1161 	idr_destroy(&fc->passthrough_req);
1162 	kfree_rcu(fc, rcu);
1163 }
1164 EXPORT_SYMBOL_GPL(fuse_free_conn);
1165 
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1166 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1167 {
1168 	int err;
1169 	char *suffix = "";
1170 
1171 	if (sb->s_bdev) {
1172 		suffix = "-fuseblk";
1173 		/*
1174 		 * sb->s_bdi points to blkdev's bdi however we want to redirect
1175 		 * it to our private bdi...
1176 		 */
1177 		bdi_put(sb->s_bdi);
1178 		sb->s_bdi = &noop_backing_dev_info;
1179 	}
1180 	err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1181 				   MINOR(fc->dev), suffix);
1182 	if (err)
1183 		return err;
1184 
1185 	/* fuse does it's own writeback accounting */
1186 	sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1187 	sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
1188 
1189 	/*
1190 	 * For a single fuse filesystem use max 1% of dirty +
1191 	 * writeback threshold.
1192 	 *
1193 	 * This gives about 1M of write buffer for memory maps on a
1194 	 * machine with 1G and 10% dirty_ratio, which should be more
1195 	 * than enough.
1196 	 *
1197 	 * Privileged users can raise it by writing to
1198 	 *
1199 	 *    /sys/class/bdi/<bdi>/max_ratio
1200 	 */
1201 	bdi_set_max_ratio(sb->s_bdi, 1);
1202 
1203 	return 0;
1204 }
1205 
fuse_dev_alloc(void)1206 struct fuse_dev *fuse_dev_alloc(void)
1207 {
1208 	struct fuse_dev *fud;
1209 	struct list_head *pq;
1210 
1211 	fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1212 	if (!fud)
1213 		return NULL;
1214 
1215 	pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1216 	if (!pq) {
1217 		kfree(fud);
1218 		return NULL;
1219 	}
1220 
1221 	fud->pq.processing = pq;
1222 	fuse_pqueue_init(&fud->pq);
1223 
1224 	return fud;
1225 }
1226 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1227 
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)1228 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1229 {
1230 	fud->fc = fuse_conn_get(fc);
1231 	spin_lock(&fc->lock);
1232 	list_add_tail(&fud->entry, &fc->devices);
1233 	spin_unlock(&fc->lock);
1234 }
1235 EXPORT_SYMBOL_GPL(fuse_dev_install);
1236 
fuse_dev_alloc_install(struct fuse_conn * fc)1237 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1238 {
1239 	struct fuse_dev *fud;
1240 
1241 	fud = fuse_dev_alloc();
1242 	if (!fud)
1243 		return NULL;
1244 
1245 	fuse_dev_install(fud, fc);
1246 	return fud;
1247 }
1248 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1249 
fuse_dev_free(struct fuse_dev * fud)1250 void fuse_dev_free(struct fuse_dev *fud)
1251 {
1252 	struct fuse_conn *fc = fud->fc;
1253 
1254 	if (fc) {
1255 		spin_lock(&fc->lock);
1256 		list_del(&fud->entry);
1257 		spin_unlock(&fc->lock);
1258 
1259 		fuse_conn_put(fc);
1260 	}
1261 	kfree(fud->pq.processing);
1262 	kfree(fud);
1263 }
1264 EXPORT_SYMBOL_GPL(fuse_dev_free);
1265 
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct fuse_inode * fi)1266 static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1267 				      const struct fuse_inode *fi)
1268 {
1269 	*attr = (struct fuse_attr){
1270 		.ino		= fi->inode.i_ino,
1271 		.size		= fi->inode.i_size,
1272 		.blocks		= fi->inode.i_blocks,
1273 		.atime		= fi->inode.i_atime.tv_sec,
1274 		.mtime		= fi->inode.i_mtime.tv_sec,
1275 		.ctime		= fi->inode.i_ctime.tv_sec,
1276 		.atimensec	= fi->inode.i_atime.tv_nsec,
1277 		.mtimensec	= fi->inode.i_mtime.tv_nsec,
1278 		.ctimensec	= fi->inode.i_ctime.tv_nsec,
1279 		.mode		= fi->inode.i_mode,
1280 		.nlink		= fi->inode.i_nlink,
1281 		.uid		= fi->inode.i_uid.val,
1282 		.gid		= fi->inode.i_gid.val,
1283 		.rdev		= fi->inode.i_rdev,
1284 		.blksize	= 1u << fi->inode.i_blkbits,
1285 	};
1286 }
1287 
fuse_sb_defaults(struct super_block * sb)1288 static void fuse_sb_defaults(struct super_block *sb)
1289 {
1290 	sb->s_magic = FUSE_SUPER_MAGIC;
1291 	sb->s_op = &fuse_super_operations;
1292 	sb->s_xattr = fuse_xattr_handlers;
1293 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1294 	sb->s_time_gran = 1;
1295 	sb->s_export_op = &fuse_export_operations;
1296 	sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1297 	if (sb->s_user_ns != &init_user_ns)
1298 		sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1299 	sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1300 
1301 	/*
1302 	 * If we are not in the initial user namespace posix
1303 	 * acls must be translated.
1304 	 */
1305 	if (sb->s_user_ns != &init_user_ns)
1306 		sb->s_xattr = fuse_no_acl_xattr_handlers;
1307 }
1308 
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)1309 int fuse_fill_super_submount(struct super_block *sb,
1310 			     struct fuse_inode *parent_fi)
1311 {
1312 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1313 	struct super_block *parent_sb = parent_fi->inode.i_sb;
1314 	struct fuse_attr root_attr;
1315 	struct inode *root;
1316 
1317 	fuse_sb_defaults(sb);
1318 	fm->sb = sb;
1319 
1320 	WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1321 	sb->s_bdi = bdi_get(parent_sb->s_bdi);
1322 
1323 	sb->s_xattr = parent_sb->s_xattr;
1324 	sb->s_time_gran = parent_sb->s_time_gran;
1325 	sb->s_blocksize = parent_sb->s_blocksize;
1326 	sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1327 	sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1328 	if (parent_sb->s_subtype && !sb->s_subtype)
1329 		return -ENOMEM;
1330 
1331 	fuse_fill_attr_from_inode(&root_attr, parent_fi);
1332 	root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1333 	/*
1334 	 * This inode is just a duplicate, so it is not looked up and
1335 	 * its nlookup should not be incremented.  fuse_iget() does
1336 	 * that, though, so undo it here.
1337 	 */
1338 	get_fuse_inode(root)->nlookup--;
1339 	sb->s_d_op = &fuse_dentry_operations;
1340 	sb->s_root = d_make_root(root);
1341 	if (!sb->s_root)
1342 		return -ENOMEM;
1343 
1344 	return 0;
1345 }
1346 
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)1347 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1348 {
1349 	struct fuse_dev *fud = NULL;
1350 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1351 	struct fuse_conn *fc = fm->fc;
1352 	struct inode *root;
1353 	struct dentry *root_dentry;
1354 	int err;
1355 
1356 	err = -EINVAL;
1357 	if (sb->s_flags & SB_MANDLOCK)
1358 		goto err;
1359 
1360 	fuse_sb_defaults(sb);
1361 
1362 	if (ctx->is_bdev) {
1363 #ifdef CONFIG_BLOCK
1364 		err = -EINVAL;
1365 		if (!sb_set_blocksize(sb, ctx->blksize))
1366 			goto err;
1367 #endif
1368 	} else {
1369 		sb->s_blocksize = PAGE_SIZE;
1370 		sb->s_blocksize_bits = PAGE_SHIFT;
1371 	}
1372 
1373 	sb->s_subtype = ctx->subtype;
1374 	ctx->subtype = NULL;
1375 	if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1376 		err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1377 		if (err)
1378 			goto err;
1379 	}
1380 
1381 	if (ctx->fudptr) {
1382 		err = -ENOMEM;
1383 		fud = fuse_dev_alloc_install(fc);
1384 		if (!fud)
1385 			goto err_free_dax;
1386 	}
1387 
1388 	fc->dev = sb->s_dev;
1389 	fm->sb = sb;
1390 	err = fuse_bdi_init(fc, sb);
1391 	if (err)
1392 		goto err_dev_free;
1393 
1394 	/* Handle umasking inside the fuse code */
1395 	if (sb->s_flags & SB_POSIXACL)
1396 		fc->dont_mask = 1;
1397 	sb->s_flags |= SB_POSIXACL;
1398 
1399 	fc->default_permissions = ctx->default_permissions;
1400 	fc->allow_other = ctx->allow_other;
1401 	fc->user_id = ctx->user_id;
1402 	fc->group_id = ctx->group_id;
1403 	fc->legacy_opts_show = ctx->legacy_opts_show;
1404 	fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1405 	fc->destroy = ctx->destroy;
1406 	fc->no_control = ctx->no_control;
1407 	fc->no_force_umount = ctx->no_force_umount;
1408 
1409 	err = -ENOMEM;
1410 	root = fuse_get_root_inode(sb, ctx->rootmode);
1411 	sb->s_d_op = &fuse_root_dentry_operations;
1412 	root_dentry = d_make_root(root);
1413 	if (!root_dentry)
1414 		goto err_dev_free;
1415 	/* Root dentry doesn't have .d_revalidate */
1416 	sb->s_d_op = &fuse_dentry_operations;
1417 
1418 	mutex_lock(&fuse_mutex);
1419 	err = -EINVAL;
1420 	if (ctx->fudptr && *ctx->fudptr)
1421 		goto err_unlock;
1422 
1423 	err = fuse_ctl_add_conn(fc);
1424 	if (err)
1425 		goto err_unlock;
1426 
1427 	list_add_tail(&fc->entry, &fuse_conn_list);
1428 	sb->s_root = root_dentry;
1429 	if (ctx->fudptr)
1430 		*ctx->fudptr = fud;
1431 	mutex_unlock(&fuse_mutex);
1432 	return 0;
1433 
1434  err_unlock:
1435 	mutex_unlock(&fuse_mutex);
1436 	dput(root_dentry);
1437  err_dev_free:
1438 	if (fud)
1439 		fuse_dev_free(fud);
1440  err_free_dax:
1441 	if (IS_ENABLED(CONFIG_FUSE_DAX))
1442 		fuse_dax_conn_free(fc);
1443  err:
1444 	return err;
1445 }
1446 EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1447 
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)1448 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1449 {
1450 	struct fuse_fs_context *ctx = fsc->fs_private;
1451 	struct file *file;
1452 	int err;
1453 	struct fuse_conn *fc;
1454 	struct fuse_mount *fm;
1455 
1456 	err = -EINVAL;
1457 	file = fget(ctx->fd);
1458 	if (!file)
1459 		goto err;
1460 
1461 	/*
1462 	 * Require mount to happen from the same user namespace which
1463 	 * opened /dev/fuse to prevent potential attacks.
1464 	 */
1465 	if ((file->f_op != &fuse_dev_operations) ||
1466 	    (file->f_cred->user_ns != sb->s_user_ns))
1467 		goto err_fput;
1468 	ctx->fudptr = &file->private_data;
1469 
1470 	fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1471 	err = -ENOMEM;
1472 	if (!fc)
1473 		goto err_fput;
1474 
1475 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1476 	if (!fm) {
1477 		kfree(fc);
1478 		goto err_fput;
1479 	}
1480 
1481 	fuse_conn_init(fc, fm, sb->s_user_ns, &fuse_dev_fiq_ops, NULL);
1482 	fc->release = fuse_free_conn;
1483 
1484 	sb->s_fs_info = fm;
1485 
1486 	err = fuse_fill_super_common(sb, ctx);
1487 	if (err)
1488 		goto err_put_conn;
1489 	/*
1490 	 * atomic_dec_and_test() in fput() provides the necessary
1491 	 * memory barrier for file->private_data to be visible on all
1492 	 * CPUs after this
1493 	 */
1494 	fput(file);
1495 	fuse_send_init(get_fuse_mount_super(sb));
1496 	return 0;
1497 
1498  err_put_conn:
1499 	fuse_mount_put(fm);
1500 	sb->s_fs_info = NULL;
1501  err_fput:
1502 	fput(file);
1503  err:
1504 	return err;
1505 }
1506 
fuse_get_tree(struct fs_context * fc)1507 static int fuse_get_tree(struct fs_context *fc)
1508 {
1509 	struct fuse_fs_context *ctx = fc->fs_private;
1510 
1511 	if (!ctx->fd_present || !ctx->rootmode_present ||
1512 	    !ctx->user_id_present || !ctx->group_id_present)
1513 		return -EINVAL;
1514 
1515 #ifdef CONFIG_BLOCK
1516 	if (ctx->is_bdev)
1517 		return get_tree_bdev(fc, fuse_fill_super);
1518 #endif
1519 
1520 	return get_tree_nodev(fc, fuse_fill_super);
1521 }
1522 
1523 static const struct fs_context_operations fuse_context_ops = {
1524 	.free		= fuse_free_fc,
1525 	.parse_param	= fuse_parse_param,
1526 	.reconfigure	= fuse_reconfigure,
1527 	.get_tree	= fuse_get_tree,
1528 };
1529 
1530 /*
1531  * Set up the filesystem mount context.
1532  */
fuse_init_fs_context(struct fs_context * fc)1533 static int fuse_init_fs_context(struct fs_context *fc)
1534 {
1535 	struct fuse_fs_context *ctx;
1536 
1537 	ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1538 	if (!ctx)
1539 		return -ENOMEM;
1540 
1541 	ctx->max_read = ~0;
1542 	ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1543 	ctx->legacy_opts_show = true;
1544 
1545 #ifdef CONFIG_BLOCK
1546 	if (fc->fs_type == &fuseblk_fs_type) {
1547 		ctx->is_bdev = true;
1548 		ctx->destroy = true;
1549 	}
1550 #endif
1551 
1552 	fc->fs_private = ctx;
1553 	fc->ops = &fuse_context_ops;
1554 	return 0;
1555 }
1556 
fuse_mount_remove(struct fuse_mount * fm)1557 bool fuse_mount_remove(struct fuse_mount *fm)
1558 {
1559 	struct fuse_conn *fc = fm->fc;
1560 	bool last = false;
1561 
1562 	down_write(&fc->killsb);
1563 	list_del_init(&fm->fc_entry);
1564 	if (list_empty(&fc->mounts))
1565 		last = true;
1566 	up_write(&fc->killsb);
1567 
1568 	return last;
1569 }
1570 EXPORT_SYMBOL_GPL(fuse_mount_remove);
1571 
fuse_conn_destroy(struct fuse_mount * fm)1572 void fuse_conn_destroy(struct fuse_mount *fm)
1573 {
1574 	struct fuse_conn *fc = fm->fc;
1575 
1576 	if (fc->destroy)
1577 		fuse_send_destroy(fm);
1578 
1579 	fuse_abort_conn(fc);
1580 	fuse_wait_aborted(fc);
1581 
1582 	if (!list_empty(&fc->entry)) {
1583 		mutex_lock(&fuse_mutex);
1584 		list_del(&fc->entry);
1585 		fuse_ctl_remove_conn(fc);
1586 		mutex_unlock(&fuse_mutex);
1587 	}
1588 }
1589 EXPORT_SYMBOL_GPL(fuse_conn_destroy);
1590 
fuse_kill_sb_anon(struct super_block * sb)1591 static void fuse_kill_sb_anon(struct super_block *sb)
1592 {
1593 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1594 	bool last;
1595 
1596 	if (fm) {
1597 		last = fuse_mount_remove(fm);
1598 		if (last)
1599 			fuse_conn_destroy(fm);
1600 	}
1601 	kill_anon_super(sb);
1602 }
1603 
1604 static struct file_system_type fuse_fs_type = {
1605 	.owner		= THIS_MODULE,
1606 	.name		= "fuse",
1607 	.fs_flags	= FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
1608 	.init_fs_context = fuse_init_fs_context,
1609 	.parameters	= fuse_fs_parameters,
1610 	.kill_sb	= fuse_kill_sb_anon,
1611 };
1612 MODULE_ALIAS_FS("fuse");
1613 
1614 #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)1615 static void fuse_kill_sb_blk(struct super_block *sb)
1616 {
1617 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1618 	bool last;
1619 
1620 	if (fm) {
1621 		last = fuse_mount_remove(fm);
1622 		if (last)
1623 			fuse_conn_destroy(fm);
1624 	}
1625 	kill_block_super(sb);
1626 }
1627 
1628 static struct file_system_type fuseblk_fs_type = {
1629 	.owner		= THIS_MODULE,
1630 	.name		= "fuseblk",
1631 	.init_fs_context = fuse_init_fs_context,
1632 	.parameters	= fuse_fs_parameters,
1633 	.kill_sb	= fuse_kill_sb_blk,
1634 	.fs_flags	= FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
1635 };
1636 MODULE_ALIAS_FS("fuseblk");
1637 
register_fuseblk(void)1638 static inline int register_fuseblk(void)
1639 {
1640 	return register_filesystem(&fuseblk_fs_type);
1641 }
1642 
unregister_fuseblk(void)1643 static inline void unregister_fuseblk(void)
1644 {
1645 	unregister_filesystem(&fuseblk_fs_type);
1646 }
1647 #else
register_fuseblk(void)1648 static inline int register_fuseblk(void)
1649 {
1650 	return 0;
1651 }
1652 
unregister_fuseblk(void)1653 static inline void unregister_fuseblk(void)
1654 {
1655 }
1656 #endif
1657 
fuse_inode_init_once(void * foo)1658 static void fuse_inode_init_once(void *foo)
1659 {
1660 	struct inode *inode = foo;
1661 
1662 	inode_init_once(inode);
1663 }
1664 
fuse_fs_init(void)1665 static int __init fuse_fs_init(void)
1666 {
1667 	int err;
1668 
1669 	fuse_inode_cachep = kmem_cache_create("fuse_inode",
1670 			sizeof(struct fuse_inode), 0,
1671 			SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
1672 			fuse_inode_init_once);
1673 	err = -ENOMEM;
1674 	if (!fuse_inode_cachep)
1675 		goto out;
1676 
1677 	err = register_fuseblk();
1678 	if (err)
1679 		goto out2;
1680 
1681 	err = register_filesystem(&fuse_fs_type);
1682 	if (err)
1683 		goto out3;
1684 
1685 	return 0;
1686 
1687  out3:
1688 	unregister_fuseblk();
1689  out2:
1690 	kmem_cache_destroy(fuse_inode_cachep);
1691  out:
1692 	return err;
1693 }
1694 
fuse_fs_cleanup(void)1695 static void fuse_fs_cleanup(void)
1696 {
1697 	unregister_filesystem(&fuse_fs_type);
1698 	unregister_fuseblk();
1699 
1700 	/*
1701 	 * Make sure all delayed rcu free inodes are flushed before we
1702 	 * destroy cache.
1703 	 */
1704 	rcu_barrier();
1705 	kmem_cache_destroy(fuse_inode_cachep);
1706 }
1707 
1708 static struct kobject *fuse_kobj;
1709 
fuse_sysfs_init(void)1710 static int fuse_sysfs_init(void)
1711 {
1712 	int err;
1713 
1714 	fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
1715 	if (!fuse_kobj) {
1716 		err = -ENOMEM;
1717 		goto out_err;
1718 	}
1719 
1720 	err = sysfs_create_mount_point(fuse_kobj, "connections");
1721 	if (err)
1722 		goto out_fuse_unregister;
1723 
1724 	return 0;
1725 
1726  out_fuse_unregister:
1727 	kobject_put(fuse_kobj);
1728  out_err:
1729 	return err;
1730 }
1731 
fuse_sysfs_cleanup(void)1732 static void fuse_sysfs_cleanup(void)
1733 {
1734 	sysfs_remove_mount_point(fuse_kobj, "connections");
1735 	kobject_put(fuse_kobj);
1736 }
1737 
fuse_init(void)1738 static int __init fuse_init(void)
1739 {
1740 	int res;
1741 
1742 	pr_info("init (API version %i.%i)\n",
1743 		FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1744 
1745 	INIT_LIST_HEAD(&fuse_conn_list);
1746 	res = fuse_fs_init();
1747 	if (res)
1748 		goto err;
1749 
1750 	res = fuse_dev_init();
1751 	if (res)
1752 		goto err_fs_cleanup;
1753 
1754 	res = fuse_sysfs_init();
1755 	if (res)
1756 		goto err_dev_cleanup;
1757 
1758 	res = fuse_ctl_init();
1759 	if (res)
1760 		goto err_sysfs_cleanup;
1761 
1762 	sanitize_global_limit(&max_user_bgreq);
1763 	sanitize_global_limit(&max_user_congthresh);
1764 
1765 	return 0;
1766 
1767  err_sysfs_cleanup:
1768 	fuse_sysfs_cleanup();
1769  err_dev_cleanup:
1770 	fuse_dev_cleanup();
1771  err_fs_cleanup:
1772 	fuse_fs_cleanup();
1773  err:
1774 	return res;
1775 }
1776 
fuse_exit(void)1777 static void __exit fuse_exit(void)
1778 {
1779 	pr_debug("exit\n");
1780 
1781 	fuse_ctl_cleanup();
1782 	fuse_sysfs_cleanup();
1783 	fuse_fs_cleanup();
1784 	fuse_dev_cleanup();
1785 }
1786 
1787 module_init(fuse_init);
1788 module_exit(fuse_exit);
1789