xref: /linux/fs/fuse/dir.c (revision 7db28abbea0f7dc1ec4fdfdc149db5fbd9e4c994)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3   FUSE: Filesystem in Userspace
4   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
5 */
6 
7 #include "dev.h"
8 #include "fuse_i.h"
9 
10 #include <linux/pagemap.h>
11 #include <linux/file.h>
12 #include <linux/fs_context.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched.h>
15 #include <linux/namei.h>
16 #include <linux/slab.h>
17 #include <linux/xattr.h>
18 #include <linux/iversion.h>
19 #include <linux/posix_acl.h>
20 #include <linux/security.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 
24 static bool __read_mostly allow_sys_admin_access;
25 module_param(allow_sys_admin_access, bool, 0644);
26 MODULE_PARM_DESC(allow_sys_admin_access,
27 		 "Allow users with CAP_SYS_ADMIN in initial userns to bypass allow_other access check");
28 
29 struct dentry_bucket {
30 	struct rb_root tree;
31 	spinlock_t lock;
32 };
33 
34 #define FUSE_HASH_BITS	5
35 #define FUSE_HASH_SIZE	(1 << FUSE_HASH_BITS)
36 static struct dentry_bucket dentry_hash[FUSE_HASH_SIZE];
37 static struct delayed_work dentry_tree_work;
38 
39 /* Minimum invalidation work queue frequency */
40 #define FUSE_DENTRY_INVAL_FREQ_MIN 5
41 
42 unsigned __read_mostly inval_wq;
43 static int inval_wq_set(const char *val, const struct kernel_param *kp)
44 {
45 	unsigned int num;
46 	unsigned int old = inval_wq;
47 	int ret;
48 
49 	if (!val)
50 		return -EINVAL;
51 
52 	ret = kstrtouint(val, 0, &num);
53 	if (ret)
54 		return ret;
55 
56 	if ((num < FUSE_DENTRY_INVAL_FREQ_MIN) && (num != 0))
57 		return -EINVAL;
58 
59 	/* This should prevent overflow in secs_to_jiffies() */
60 	if (num > USHRT_MAX)
61 		return -EINVAL;
62 
63 	*((unsigned int *)kp->arg) = num;
64 
65 	if (num && !old)
66 		schedule_delayed_work(&dentry_tree_work,
67 				      secs_to_jiffies(num));
68 	else if (!num && old)
69 		cancel_delayed_work_sync(&dentry_tree_work);
70 
71 	return 0;
72 }
73 static const struct kernel_param_ops inval_wq_ops = {
74 	.set = inval_wq_set,
75 	.get = param_get_uint,
76 };
77 module_param_cb(inval_wq, &inval_wq_ops, &inval_wq, 0644);
78 __MODULE_PARM_TYPE(inval_wq, "uint");
79 MODULE_PARM_DESC(inval_wq,
80 		 "Dentries invalidation work queue period in secs (>= "
81 		 __stringify(FUSE_DENTRY_INVAL_FREQ_MIN) ").");
82 
83 static inline struct dentry_bucket *get_dentry_bucket(struct dentry *dentry)
84 {
85 	int i = hash_ptr(dentry, FUSE_HASH_BITS);
86 
87 	return &dentry_hash[i];
88 }
89 
90 static void fuse_advise_use_readdirplus(struct inode *dir)
91 {
92 	struct fuse_inode *fi = get_fuse_inode(dir);
93 
94 	set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
95 }
96 
97 struct fuse_dentry {
98 	u64 time;
99 	u64 epoch;
100 	union {
101 		struct rcu_head rcu;
102 		struct rb_node node;
103 	};
104 	struct dentry *dentry;
105 };
106 
107 static void __fuse_dentry_tree_del_node(struct fuse_dentry *fd,
108 					struct dentry_bucket *bucket)
109 {
110 	if (!RB_EMPTY_NODE(&fd->node)) {
111 		rb_erase(&fd->node, &bucket->tree);
112 		RB_CLEAR_NODE(&fd->node);
113 	}
114 }
115 
116 static void fuse_dentry_tree_del_node(struct dentry *dentry)
117 {
118 	struct fuse_dentry *fd = dentry->d_fsdata;
119 	struct dentry_bucket *bucket = get_dentry_bucket(dentry);
120 
121 	spin_lock(&bucket->lock);
122 	__fuse_dentry_tree_del_node(fd, bucket);
123 	spin_unlock(&bucket->lock);
124 }
125 
126 static void fuse_dentry_tree_add_node(struct dentry *dentry)
127 {
128 	struct fuse_dentry *fd = dentry->d_fsdata;
129 	struct dentry_bucket *bucket;
130 	struct fuse_dentry *cur;
131 	struct rb_node **p, *parent = NULL;
132 
133 	if (!inval_wq)
134 		return;
135 
136 	bucket = get_dentry_bucket(dentry);
137 
138 	spin_lock(&bucket->lock);
139 
140 	__fuse_dentry_tree_del_node(fd, bucket);
141 
142 	p = &bucket->tree.rb_node;
143 	while (*p) {
144 		parent = *p;
145 		cur = rb_entry(*p, struct fuse_dentry, node);
146 		if (fd->time < cur->time)
147 			p = &(*p)->rb_left;
148 		else
149 			p = &(*p)->rb_right;
150 	}
151 	rb_link_node(&fd->node, parent, p);
152 	rb_insert_color(&fd->node, &bucket->tree);
153 	spin_unlock(&bucket->lock);
154 }
155 
156 /*
157  * work queue which, when enabled, will periodically check for expired dentries
158  * in the dentries tree.
159  */
160 static void fuse_dentry_tree_work(struct work_struct *work)
161 {
162 	LIST_HEAD(dispose);
163 	struct fuse_dentry *fd;
164 	struct rb_node *node;
165 	int i;
166 
167 	for (i = 0; i < FUSE_HASH_SIZE; i++) {
168 		spin_lock(&dentry_hash[i].lock);
169 		node = rb_first(&dentry_hash[i].tree);
170 		while (node) {
171 			fd = rb_entry(node, struct fuse_dentry, node);
172 			if (!time_before64(fd->time, get_jiffies_64()))
173 				break;
174 
175 			rb_erase(&fd->node, &dentry_hash[i].tree);
176 			RB_CLEAR_NODE(&fd->node);
177 			spin_lock(&fd->dentry->d_lock);
178 			/* If dentry is still referenced, let next dput release it */
179 			fd->dentry->d_flags |= DCACHE_OP_DELETE;
180 			__move_to_shrink_list(fd->dentry, &dispose);
181 			spin_unlock(&fd->dentry->d_lock);
182 			if (need_resched()) {
183 				spin_unlock(&dentry_hash[i].lock);
184 				cond_resched();
185 				spin_lock(&dentry_hash[i].lock);
186 			}
187 			node = rb_first(&dentry_hash[i].tree);
188 		}
189 		spin_unlock(&dentry_hash[i].lock);
190 	}
191 	shrink_dentry_list(&dispose);
192 
193 	if (inval_wq)
194 		schedule_delayed_work(&dentry_tree_work,
195 				      secs_to_jiffies(inval_wq));
196 }
197 
198 void fuse_epoch_work(struct work_struct *work)
199 {
200 	struct fuse_conn *fc = container_of(work, struct fuse_conn,
201 					    epoch_work);
202 	struct fuse_mount *fm;
203 	struct inode *inode;
204 
205 	down_read(&fc->killsb);
206 
207 	inode = fuse_ilookup(fc, FUSE_ROOT_ID, &fm);
208 	if (inode) {
209 		iput(inode);
210 		/* Remove all possible active references to cached inodes */
211 		shrink_dcache_sb(fm->sb);
212 	} else
213 		pr_warn("Failed to get root inode");
214 
215 	up_read(&fc->killsb);
216 }
217 
218 void fuse_dentry_tree_init(void)
219 {
220 	int i;
221 
222 	for (i = 0; i < FUSE_HASH_SIZE; i++) {
223 		spin_lock_init(&dentry_hash[i].lock);
224 		dentry_hash[i].tree = RB_ROOT;
225 	}
226 	INIT_DELAYED_WORK(&dentry_tree_work, fuse_dentry_tree_work);
227 }
228 
229 void fuse_dentry_tree_cleanup(void)
230 {
231 	int i;
232 
233 	inval_wq = 0;
234 	cancel_delayed_work_sync(&dentry_tree_work);
235 
236 	for (i = 0; i < FUSE_HASH_SIZE; i++)
237 		WARN_ON_ONCE(!RB_EMPTY_ROOT(&dentry_hash[i].tree));
238 }
239 
240 void fuse_dentry_set_epoch(struct dentry *dentry, u64 epoch)
241 {
242 	struct fuse_dentry *fd = dentry->d_fsdata;
243 
244 	fd->epoch = epoch;
245 }
246 
247 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
248 {
249 	((struct fuse_dentry *) dentry->d_fsdata)->time = time;
250 }
251 
252 static inline u64 fuse_dentry_time(const struct dentry *entry)
253 {
254 	return ((struct fuse_dentry *) entry->d_fsdata)->time;
255 }
256 
257 static void fuse_dentry_settime(struct dentry *dentry, u64 time)
258 {
259 	struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
260 	bool delete = !time && fc->delete_stale;
261 	/*
262 	 * Mess with DCACHE_OP_DELETE because dput() will be faster without it.
263 	 * Don't care about races, either way it's just an optimization
264 	 */
265 	if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) ||
266 	    (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) {
267 		spin_lock(&dentry->d_lock);
268 		if (!delete)
269 			dentry->d_flags &= ~DCACHE_OP_DELETE;
270 		else
271 			dentry->d_flags |= DCACHE_OP_DELETE;
272 		spin_unlock(&dentry->d_lock);
273 	}
274 
275 	__fuse_dentry_settime(dentry, time);
276 	fuse_dentry_tree_add_node(dentry);
277 }
278 
279 /*
280  * FUSE caches dentries and attributes with separate timeout.  The
281  * time in jiffies until the dentry/attributes are valid is stored in
282  * dentry->d_fsdata and fuse_inode->i_time respectively.
283  */
284 
285 /*
286  * Calculate the time in jiffies until a dentry/attributes are valid
287  */
288 u64 fuse_time_to_jiffies(u64 sec, u32 nsec)
289 {
290 	if (sec || nsec) {
291 		struct timespec64 ts = {
292 			sec,
293 			min_t(u32, nsec, NSEC_PER_SEC - 1)
294 		};
295 
296 		return get_jiffies_64() + timespec64_to_jiffies(&ts);
297 	} else
298 		return 0;
299 }
300 
301 /*
302  * Set dentry and possibly attribute timeouts from the lookup/mk*
303  * replies
304  */
305 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
306 {
307 	fuse_dentry_settime(entry,
308 		fuse_time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
309 }
310 
311 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
312 {
313 	set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
314 }
315 
316 /*
317  * Mark the attributes as stale, so that at the next call to
318  * ->getattr() they will be fetched from userspace
319  */
320 void fuse_invalidate_attr(struct inode *inode)
321 {
322 	fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
323 }
324 
325 static void fuse_dir_changed(struct inode *dir)
326 {
327 	fuse_invalidate_attr_mask(dir, FUSE_STATX_MODDIR);
328 	inode_maybe_inc_iversion(dir, false);
329 }
330 
331 /*
332  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
333  * atime is not used.
334  */
335 void fuse_invalidate_atime(struct inode *inode)
336 {
337 	if (!IS_RDONLY(inode))
338 		fuse_invalidate_attr_mask(inode, STATX_ATIME);
339 }
340 
341 /*
342  * Just mark the entry as stale, so that a next attempt to look it up
343  * will result in a new lookup call to userspace
344  *
345  * This is called when a dentry is about to become negative and the
346  * timeout is unknown (unlink, rmdir, rename and in some cases
347  * lookup)
348  */
349 void fuse_invalidate_entry_cache(struct dentry *entry)
350 {
351 	fuse_dentry_settime(entry, 0);
352 }
353 
354 /*
355  * Same as fuse_invalidate_entry_cache(), but also try to remove the
356  * dentry from the hash
357  */
358 static void fuse_invalidate_entry(struct dentry *entry)
359 {
360 	d_invalidate(entry);
361 	fuse_invalidate_entry_cache(entry);
362 }
363 
364 static void fuse_lookup_init(struct fuse_args *args, u64 nodeid,
365 			     const struct qstr *name,
366 			     struct fuse_entry_out *outarg)
367 {
368 	memset(outarg, 0, sizeof(struct fuse_entry_out));
369 	args->opcode = FUSE_LOOKUP;
370 	args->nodeid = nodeid;
371 	args->in_numargs = 3;
372 	fuse_set_zero_arg0(args);
373 	args->in_args[1].size = name->len;
374 	args->in_args[1].value = name->name;
375 	args->in_args[2].size = 1;
376 	args->in_args[2].value = "";
377 	args->out_numargs = 1;
378 	args->out_args[0].size = sizeof(struct fuse_entry_out);
379 	args->out_args[0].value = outarg;
380 }
381 
382 /*
383  * Check whether the dentry is still valid
384  *
385  * If the entry validity timeout has expired and the dentry is
386  * positive, try to redo the lookup.  If the lookup results in a
387  * different inode, then let the VFS invalidate the dentry and redo
388  * the lookup once more.  If the lookup results in the same inode,
389  * then refresh the attributes, timeouts and mark the dentry valid.
390  */
391 static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name,
392 				  struct dentry *entry, unsigned int flags)
393 {
394 	struct inode *inode;
395 	struct fuse_mount *fm;
396 	struct fuse_conn *fc;
397 	struct fuse_inode *fi;
398 	struct fuse_dentry *fd = entry->d_fsdata;
399 	int ret;
400 
401 	fc = get_fuse_conn_super(dir->i_sb);
402 	if (fd->epoch < atomic_read(&fc->epoch))
403 		goto invalid;
404 
405 	inode = d_inode_rcu(entry);
406 	if (inode && fuse_is_bad(inode))
407 		goto invalid;
408 	else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
409 		 (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) {
410 		struct fuse_entry_out outarg;
411 		FUSE_ARGS(args);
412 		struct fuse_forget_link *forget;
413 		u64 attr_version;
414 
415 		/* For negative dentries, always do a fresh lookup */
416 		if (!inode)
417 			goto invalid;
418 
419 		ret = -ECHILD;
420 		if (flags & LOOKUP_RCU)
421 			goto out;
422 
423 		fm = get_fuse_mount(inode);
424 
425 		forget = fuse_alloc_forget();
426 		ret = -ENOMEM;
427 		if (!forget)
428 			goto out;
429 
430 		attr_version = fuse_get_attr_version(fm->fc);
431 
432 		fuse_lookup_init(&args, get_node_id(dir), name, &outarg);
433 		ret = fuse_simple_request(fm, &args);
434 		/* Zero nodeid is same as -ENOENT */
435 		if (!ret && !outarg.nodeid)
436 			ret = -ENOENT;
437 		if (!ret) {
438 			fi = get_fuse_inode(inode);
439 			if (outarg.nodeid != get_node_id(inode) ||
440 			    (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) {
441 				fuse_chan_queue_forget(fm->fc->chan, forget,
442 						  outarg.nodeid, 1);
443 				goto invalid;
444 			}
445 			spin_lock(&fi->lock);
446 			fi->nlookup++;
447 			spin_unlock(&fi->lock);
448 		}
449 		kfree(forget);
450 		if (ret == -ENOMEM || ret == -EINTR)
451 			goto out;
452 		if (ret || fuse_invalid_attr(&outarg.attr) ||
453 		    fuse_stale_inode(inode, outarg.generation, &outarg.attr))
454 			goto invalid;
455 
456 		forget_all_cached_acls(inode);
457 		fuse_change_attributes(inode, &outarg.attr, NULL,
458 				       ATTR_TIMEOUT(&outarg),
459 				       attr_version);
460 		fuse_change_entry_timeout(entry, &outarg);
461 	} else if (inode) {
462 		fi = get_fuse_inode(inode);
463 		if (flags & LOOKUP_RCU) {
464 			if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
465 				return -ECHILD;
466 		} else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
467 			fuse_advise_use_readdirplus(dir);
468 		}
469 	}
470 	ret = 1;
471 out:
472 	return ret;
473 
474 invalid:
475 	ret = 0;
476 	goto out;
477 }
478 
479 static int fuse_dentry_init(struct dentry *dentry)
480 {
481 	struct fuse_dentry *fd;
482 
483 	fd = kzalloc_obj(struct fuse_dentry,
484 			 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
485 	if (!fd)
486 		return -ENOMEM;
487 
488 	fd->dentry = dentry;
489 	RB_CLEAR_NODE(&fd->node);
490 	dentry->d_fsdata = fd;
491 	/*
492 	 * Initialising epoch to '0' ensures the dentry is invalid
493 	 * if compared to fc->epoch, which is initialized to '1'.
494 	 */
495 	fuse_dentry_set_epoch(dentry, 0);
496 
497 	return 0;
498 }
499 
500 static void fuse_dentry_release(struct dentry *dentry)
501 {
502 	struct fuse_dentry *fd = dentry->d_fsdata;
503 
504 	if (!RB_EMPTY_NODE(&fd->node))
505 		fuse_dentry_tree_del_node(dentry);
506 	kfree_rcu(fd, rcu);
507 }
508 
509 static int fuse_dentry_delete(const struct dentry *dentry)
510 {
511 	return time_before64(fuse_dentry_time(dentry), get_jiffies_64());
512 }
513 
514 /*
515  * Create a fuse_mount object with a new superblock (with path->dentry
516  * as the root), and return that mount so it can be auto-mounted on
517  * @path.
518  */
519 static struct vfsmount *fuse_dentry_automount(struct path *path)
520 {
521 	struct fs_context *fsc;
522 	struct vfsmount *mnt;
523 	struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry));
524 
525 	fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
526 	if (IS_ERR(fsc))
527 		return ERR_CAST(fsc);
528 
529 	/* Pass the FUSE inode of the mount for fuse_get_tree_submount() */
530 	fsc->fs_private = mp_fi;
531 
532 	/* Create the submount */
533 	mnt = fc_mount(fsc);
534 	put_fs_context(fsc);
535 	return mnt;
536 }
537 
538 const struct dentry_operations fuse_dentry_operations = {
539 	.d_revalidate	= fuse_dentry_revalidate,
540 	.d_delete	= fuse_dentry_delete,
541 	.d_init		= fuse_dentry_init,
542 	.d_release	= fuse_dentry_release,
543 	.d_automount	= fuse_dentry_automount,
544 };
545 
546 int fuse_valid_type(int m)
547 {
548 	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
549 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
550 }
551 
552 static bool fuse_valid_size(u64 size)
553 {
554 	return size <= LLONG_MAX;
555 }
556 
557 bool fuse_invalid_attr(struct fuse_attr *attr)
558 {
559 	return !fuse_valid_type(attr->mode) || !fuse_valid_size(attr->size);
560 }
561 
562 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
563 		     struct fuse_entry_out *outarg, struct inode **inode)
564 {
565 	struct fuse_mount *fm = get_fuse_mount_super(sb);
566 	FUSE_ARGS(args);
567 	struct fuse_forget_link *forget;
568 	u64 attr_version, evict_ctr;
569 	int err;
570 
571 	*inode = NULL;
572 	err = -ENAMETOOLONG;
573 	if (name->len > fm->fc->name_max)
574 		goto out;
575 
576 
577 	forget = fuse_alloc_forget();
578 	err = -ENOMEM;
579 	if (!forget)
580 		goto out;
581 
582 	attr_version = fuse_get_attr_version(fm->fc);
583 	evict_ctr = fuse_get_evict_ctr(fm->fc);
584 
585 	fuse_lookup_init(&args, nodeid, name, outarg);
586 	err = fuse_simple_request(fm, &args);
587 	/* Zero nodeid is same as -ENOENT, but with valid timeout */
588 	if (err || !outarg->nodeid)
589 		goto out_put_forget;
590 
591 	err = -EIO;
592 	if (fuse_invalid_attr(&outarg->attr))
593 		goto out_put_forget;
594 	if (outarg->nodeid == FUSE_ROOT_ID && outarg->generation != 0) {
595 		pr_warn_once("root generation should be zero\n");
596 		outarg->generation = 0;
597 	}
598 
599 	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
600 			   &outarg->attr, ATTR_TIMEOUT(outarg),
601 			   attr_version, evict_ctr);
602 	err = -ENOMEM;
603 	if (!*inode) {
604 		fuse_chan_queue_forget(fm->fc->chan, forget, outarg->nodeid, 1);
605 		goto out;
606 	}
607 	err = 0;
608 
609  out_put_forget:
610 	kfree(forget);
611  out:
612 	return err;
613 }
614 
615 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
616 				  unsigned int flags)
617 {
618 	struct fuse_entry_out outarg;
619 	struct fuse_conn *fc;
620 	struct inode *inode;
621 	struct dentry *newent;
622 	int err, epoch;
623 	bool outarg_valid = true;
624 	bool locked;
625 
626 	if (fuse_is_bad(dir))
627 		return ERR_PTR(-EIO);
628 
629 	fc = get_fuse_conn_super(dir->i_sb);
630 	epoch = atomic_read(&fc->epoch);
631 
632 	locked = fuse_lock_inode(dir);
633 	err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
634 			       &outarg, &inode);
635 	fuse_unlock_inode(dir, locked);
636 	if (err == -ENOENT) {
637 		outarg_valid = false;
638 		err = 0;
639 	}
640 	if (err)
641 		goto out_err;
642 
643 	err = -EIO;
644 	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
645 		goto out_iput;
646 
647 	newent = d_splice_alias(inode, entry);
648 	err = PTR_ERR(newent);
649 	if (IS_ERR(newent))
650 		goto out_err;
651 
652 	entry = newent ? newent : entry;
653 	fuse_dentry_set_epoch(entry, epoch);
654 	if (outarg_valid)
655 		fuse_change_entry_timeout(entry, &outarg);
656 	else
657 		fuse_invalidate_entry_cache(entry);
658 
659 	if (inode)
660 		fuse_advise_use_readdirplus(dir);
661 	return newent;
662 
663  out_iput:
664 	iput(inode);
665  out_err:
666 	return ERR_PTR(err);
667 }
668 
669 static int get_security_context(struct dentry *entry, umode_t mode,
670 				struct fuse_in_arg *ext)
671 {
672 	struct fuse_secctx *fctx;
673 	struct fuse_secctx_header *header;
674 	struct lsm_context lsmctx = { };
675 	void *ptr;
676 	u32 total_len = sizeof(*header);
677 	int err, nr_ctx = 0;
678 	const char *name = NULL;
679 	size_t namesize;
680 
681 	err = security_dentry_init_security(entry, mode, &entry->d_name,
682 					    &name, &lsmctx);
683 
684 	/* If no LSM is supporting this security hook ignore error */
685 	if (err && err != -EOPNOTSUPP)
686 		goto out_err;
687 
688 	if (lsmctx.len) {
689 		nr_ctx = 1;
690 		namesize = strlen(name) + 1;
691 		err = -EIO;
692 		if (WARN_ON(namesize > XATTR_NAME_MAX + 1 ||
693 		    lsmctx.len > S32_MAX))
694 			goto out_err;
695 		total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namesize +
696 					    lsmctx.len);
697 	}
698 
699 	err = -ENOMEM;
700 	header = ptr = kzalloc(total_len, GFP_KERNEL);
701 	if (!ptr)
702 		goto out_err;
703 
704 	header->nr_secctx = nr_ctx;
705 	header->size = total_len;
706 	ptr += sizeof(*header);
707 	if (nr_ctx) {
708 		fctx = ptr;
709 		fctx->size = lsmctx.len;
710 		ptr += sizeof(*fctx);
711 
712 		strscpy(ptr, name, namesize);
713 		ptr += namesize;
714 
715 		memcpy(ptr, lsmctx.context, lsmctx.len);
716 	}
717 	ext->size = total_len;
718 	ext->value = header;
719 	err = 0;
720 out_err:
721 	if (nr_ctx)
722 		security_release_secctx(&lsmctx);
723 	return err;
724 }
725 
726 static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
727 {
728 	void *p;
729 	u32 newlen = buf->size + bytes;
730 
731 	p = krealloc(buf->value, newlen, GFP_KERNEL);
732 	if (!p) {
733 		kfree(buf->value);
734 		buf->size = 0;
735 		buf->value = NULL;
736 		return NULL;
737 	}
738 
739 	memset(p + buf->size, 0, bytes);
740 	buf->value = p;
741 	buf->size = newlen;
742 
743 	return p + newlen - bytes;
744 }
745 
746 static u32 fuse_ext_size(size_t size)
747 {
748 	return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size);
749 }
750 
751 /*
752  * This adds just a single supplementary group that matches the parent's group.
753  */
754 static int get_create_supp_group(struct mnt_idmap *idmap,
755 				 struct inode *dir,
756 				 struct fuse_in_arg *ext)
757 {
758 	struct fuse_conn *fc = get_fuse_conn(dir);
759 	struct fuse_ext_header *xh;
760 	struct fuse_supp_groups *sg;
761 	kgid_t kgid = dir->i_gid;
762 	vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns, kgid);
763 	gid_t parent_gid = from_kgid(fc->user_ns, kgid);
764 
765 	u32 sg_len = fuse_ext_size(sizeof(*sg) + sizeof(sg->groups[0]));
766 
767 	if (parent_gid == (gid_t) -1 || vfsgid_eq_kgid(vfsgid, current_fsgid()) ||
768 	    !vfsgid_in_group_p(vfsgid))
769 		return 0;
770 
771 	xh = extend_arg(ext, sg_len);
772 	if (!xh)
773 		return -ENOMEM;
774 
775 	xh->size = sg_len;
776 	xh->type = FUSE_EXT_GROUPS;
777 
778 	sg = (struct fuse_supp_groups *) &xh[1];
779 	sg->nr_groups = 1;
780 	sg->groups[0] = parent_gid;
781 
782 	return 0;
783 }
784 
785 static int get_create_ext(struct mnt_idmap *idmap,
786 			  struct fuse_args *args,
787 			  struct inode *dir, struct dentry *dentry,
788 			  umode_t mode)
789 {
790 	struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb);
791 	struct fuse_in_arg ext = { .size = 0, .value = NULL };
792 	int err = 0;
793 
794 	if (fc->init_security)
795 		err = get_security_context(dentry, mode, &ext);
796 	if (!err && fc->create_supp_group)
797 		err = get_create_supp_group(idmap, dir, &ext);
798 
799 	if (!err && ext.size) {
800 		WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
801 		args->is_ext = true;
802 		args->ext_idx = args->in_numargs++;
803 		args->in_args[args->ext_idx] = ext;
804 	} else {
805 		kfree(ext.value);
806 	}
807 
808 	return err;
809 }
810 
811 static void free_ext_value(struct fuse_args *args)
812 {
813 	if (args->is_ext)
814 		kfree(args->in_args[args->ext_idx].value);
815 }
816 
817 /*
818  * Atomic create+open operation
819  *
820  * If the filesystem doesn't support this, then fall back to separate
821  * 'mknod' + 'open' requests.
822  */
823 static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
824 			    struct dentry *entry, struct file *file,
825 			    unsigned int flags, umode_t mode, u32 opcode)
826 {
827 	struct inode *inode;
828 	struct fuse_mount *fm = get_fuse_mount(dir);
829 	FUSE_ARGS(args);
830 	struct fuse_forget_link *forget;
831 	struct fuse_create_in inarg;
832 	struct fuse_open_out *outopenp;
833 	struct fuse_entry_out outentry;
834 	struct fuse_inode *fi;
835 	struct fuse_file *ff;
836 	int epoch, err;
837 	bool trunc = flags & O_TRUNC;
838 
839 	/* Userspace expects S_IFREG in create mode */
840 	BUG_ON((mode & S_IFMT) != S_IFREG);
841 
842 	epoch = atomic_read(&fm->fc->epoch);
843 	forget = fuse_alloc_forget();
844 	err = -ENOMEM;
845 	if (!forget)
846 		goto out_err;
847 
848 	ff = fuse_file_alloc(fm, true);
849 	if (!ff)
850 		goto out_put_forget_req;
851 
852 	if (!fm->fc->dont_mask)
853 		mode &= ~current_umask();
854 
855 	flags &= ~O_NOCTTY;
856 	memset(&inarg, 0, sizeof(inarg));
857 	memset(&outentry, 0, sizeof(outentry));
858 	inarg.flags = flags;
859 	inarg.mode = mode;
860 	inarg.umask = current_umask();
861 
862 	if (fm->fc->handle_killpriv_v2 && trunc &&
863 	    !(flags & O_EXCL) && !capable(CAP_FSETID)) {
864 		inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
865 	}
866 
867 	args.opcode = opcode;
868 	args.nodeid = get_node_id(dir);
869 	args.in_numargs = 2;
870 	args.in_args[0].size = sizeof(inarg);
871 	args.in_args[0].value = &inarg;
872 	args.in_args[1].size = entry->d_name.len + 1;
873 	args.in_args[1].value = entry->d_name.name;
874 	args.out_numargs = 2;
875 	args.out_args[0].size = sizeof(outentry);
876 	args.out_args[0].value = &outentry;
877 	/* Store outarg for fuse_finish_open() */
878 	outopenp = &ff->args->open_outarg;
879 	args.out_args[1].size = sizeof(*outopenp);
880 	args.out_args[1].value = outopenp;
881 
882 	err = get_create_ext(idmap, &args, dir, entry, mode);
883 	if (err)
884 		goto out_free_ff;
885 
886 	err = fuse_simple_idmap_request(idmap, fm, &args);
887 	free_ext_value(&args);
888 	if (err)
889 		goto out_free_ff;
890 
891 	err = -EIO;
892 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
893 	    fuse_invalid_attr(&outentry.attr))
894 		goto out_free_ff;
895 
896 	ff->fh = outopenp->fh;
897 	ff->nodeid = outentry.nodeid;
898 	ff->open_flags = outopenp->open_flags;
899 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
900 			  &outentry.attr, ATTR_TIMEOUT(&outentry), 0, 0);
901 	if (!inode) {
902 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
903 		fuse_sync_release(NULL, ff, flags);
904 		fuse_chan_queue_forget(fm->fc->chan, forget, outentry.nodeid, 1);
905 		err = -ENOMEM;
906 		goto out_err;
907 	}
908 	kfree(forget);
909 	d_instantiate(entry, inode);
910 	fuse_dentry_set_epoch(entry, epoch);
911 	fuse_change_entry_timeout(entry, &outentry);
912 	fuse_dir_changed(dir);
913 	err = generic_file_open(inode, file);
914 	if (!err) {
915 		file->private_data = ff;
916 		err = finish_open(file, entry, fuse_finish_open);
917 	}
918 	if (err) {
919 		fi = get_fuse_inode(inode);
920 		fuse_sync_release(fi, ff, flags);
921 	} else {
922 		if (fm->fc->atomic_o_trunc && trunc)
923 			truncate_pagecache(inode, 0);
924 		else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
925 			invalidate_inode_pages2(inode->i_mapping);
926 	}
927 	return err;
928 
929 out_free_ff:
930 	fuse_file_free(ff);
931 out_put_forget_req:
932 	kfree(forget);
933 out_err:
934 	return err;
935 }
936 
937 static int fuse_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
938 		      umode_t, dev_t);
939 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
940 			    struct file *file, unsigned flags,
941 			    umode_t mode)
942 {
943 	int err;
944 	struct mnt_idmap *idmap = file_mnt_idmap(file);
945 	struct fuse_conn *fc = get_fuse_conn(dir);
946 
947 	if (fuse_is_bad(dir))
948 		return -EIO;
949 
950 	if (d_in_lookup(entry)) {
951 		struct dentry *res = fuse_lookup(dir, entry, 0);
952 		if (res || d_really_is_positive(entry))
953 			return finish_no_open(file, res);
954 	}
955 
956 	if (!(flags & O_CREAT))
957 		return finish_no_open(file, NULL);
958 
959 	/* Only creates */
960 	file->f_mode |= FMODE_CREATED;
961 
962 	if (fc->no_create)
963 		goto mknod;
964 
965 	err = fuse_create_open(idmap, dir, entry, file, flags, mode, FUSE_CREATE);
966 	if (err == -ENOSYS) {
967 		fc->no_create = 1;
968 		goto mknod;
969 	} else if (err == -EEXIST)
970 		fuse_invalidate_entry(entry);
971 	return err;
972 
973 mknod:
974 	err = fuse_mknod(idmap, dir, entry, mode, 0);
975 	if (err)
976 		return err;
977 	return finish_no_open(file, NULL);
978 }
979 
980 /*
981  * Code shared between mknod, mkdir, symlink and link
982  */
983 static struct dentry *create_new_entry(struct mnt_idmap *idmap, struct fuse_mount *fm,
984 				       struct fuse_args *args, struct inode *dir,
985 				       struct dentry *entry, umode_t mode)
986 {
987 	struct fuse_entry_out outarg;
988 	struct inode *inode;
989 	struct dentry *d;
990 	struct fuse_forget_link *forget;
991 	int epoch, err;
992 
993 	if (fuse_is_bad(dir))
994 		return ERR_PTR(-EIO);
995 
996 	epoch = atomic_read(&fm->fc->epoch);
997 
998 	forget = fuse_alloc_forget();
999 	if (!forget)
1000 		return ERR_PTR(-ENOMEM);
1001 
1002 	memset(&outarg, 0, sizeof(outarg));
1003 	args->nodeid = get_node_id(dir);
1004 	args->out_numargs = 1;
1005 	args->out_args[0].size = sizeof(outarg);
1006 	args->out_args[0].value = &outarg;
1007 
1008 	if (args->opcode != FUSE_LINK) {
1009 		err = get_create_ext(idmap, args, dir, entry, mode);
1010 		if (err)
1011 			goto out_put_forget_req;
1012 	}
1013 
1014 	err = fuse_simple_idmap_request(idmap, fm, args);
1015 	free_ext_value(args);
1016 	if (err)
1017 		goto out_put_forget_req;
1018 
1019 	err = -EIO;
1020 	if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
1021 		goto out_put_forget_req;
1022 
1023 	if ((outarg.attr.mode ^ mode) & S_IFMT)
1024 		goto out_put_forget_req;
1025 
1026 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
1027 			  &outarg.attr, ATTR_TIMEOUT(&outarg), 0, 0);
1028 	if (!inode) {
1029 		fuse_chan_queue_forget(fm->fc->chan, forget, outarg.nodeid, 1);
1030 		return ERR_PTR(-ENOMEM);
1031 	}
1032 	kfree(forget);
1033 
1034 	d_drop(entry);
1035 	d = d_splice_alias(inode, entry);
1036 	if (IS_ERR(d))
1037 		return d;
1038 
1039 	if (d) {
1040 		fuse_dentry_set_epoch(d, epoch);
1041 		fuse_change_entry_timeout(d, &outarg);
1042 	} else {
1043 		fuse_dentry_set_epoch(entry, epoch);
1044 		fuse_change_entry_timeout(entry, &outarg);
1045 	}
1046 	fuse_dir_changed(dir);
1047 	return d;
1048 
1049  out_put_forget_req:
1050 	if (err == -EEXIST)
1051 		fuse_invalidate_entry(entry);
1052 	kfree(forget);
1053 	return ERR_PTR(err);
1054 }
1055 
1056 static int create_new_nondir(struct mnt_idmap *idmap, struct fuse_mount *fm,
1057 			     struct fuse_args *args, struct inode *dir,
1058 			     struct dentry *entry, umode_t mode)
1059 {
1060 	/*
1061 	 * Note that when creating anything other than a directory we
1062 	 * can be sure create_new_entry() will NOT return an alternate
1063 	 * dentry as d_splice_alias() only returns an alternate dentry
1064 	 * for directories.  So we don't need to check for that case
1065 	 * when passing back the result.
1066 	 */
1067 	WARN_ON_ONCE(S_ISDIR(mode));
1068 
1069 	return PTR_ERR(create_new_entry(idmap, fm, args, dir, entry, mode));
1070 }
1071 
1072 static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir,
1073 		      struct dentry *entry, umode_t mode, dev_t rdev)
1074 {
1075 	struct fuse_mknod_in inarg;
1076 	struct fuse_mount *fm = get_fuse_mount(dir);
1077 	FUSE_ARGS(args);
1078 
1079 	if (!fm->fc->dont_mask)
1080 		mode &= ~current_umask();
1081 
1082 	memset(&inarg, 0, sizeof(inarg));
1083 	inarg.mode = mode;
1084 	inarg.rdev = new_encode_dev(rdev);
1085 	inarg.umask = current_umask();
1086 	args.opcode = FUSE_MKNOD;
1087 	args.in_numargs = 2;
1088 	args.in_args[0].size = sizeof(inarg);
1089 	args.in_args[0].value = &inarg;
1090 	args.in_args[1].size = entry->d_name.len + 1;
1091 	args.in_args[1].value = entry->d_name.name;
1092 	return create_new_nondir(idmap, fm, &args, dir, entry, mode);
1093 }
1094 
1095 static int fuse_create(struct mnt_idmap *idmap, struct inode *dir,
1096 		       struct dentry *entry, umode_t mode)
1097 {
1098 	return fuse_mknod(idmap, dir, entry, mode, 0);
1099 }
1100 
1101 static int fuse_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1102 			struct file *file, umode_t mode)
1103 {
1104 	struct fuse_conn *fc = get_fuse_conn(dir);
1105 	int err;
1106 
1107 	if (fc->no_tmpfile)
1108 		return -EOPNOTSUPP;
1109 
1110 	err = fuse_create_open(idmap, dir, file->f_path.dentry, file,
1111 			       file->f_flags, mode, FUSE_TMPFILE);
1112 	if (err == -ENOSYS) {
1113 		fc->no_tmpfile = 1;
1114 		err = -EOPNOTSUPP;
1115 	}
1116 	return err;
1117 }
1118 
1119 static struct dentry *fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1120 				 struct dentry *entry, umode_t mode)
1121 {
1122 	struct fuse_mkdir_in inarg;
1123 	struct fuse_mount *fm = get_fuse_mount(dir);
1124 	FUSE_ARGS(args);
1125 
1126 	if (!fm->fc->dont_mask)
1127 		mode &= ~current_umask();
1128 
1129 	/*
1130 	 * vfs_mkdir() now passes S_IFDIR in @mode, but @mode is forwarded
1131 	 * verbatim to the userspace server which has only ever been given the
1132 	 * permission bits. Strip the type bit until the protocol is known to
1133 	 * cope with it.
1134 	 */
1135 	mode &= ~S_IFDIR;
1136 
1137 	memset(&inarg, 0, sizeof(inarg));
1138 	inarg.mode = mode;
1139 	inarg.umask = current_umask();
1140 	args.opcode = FUSE_MKDIR;
1141 	args.in_numargs = 2;
1142 	args.in_args[0].size = sizeof(inarg);
1143 	args.in_args[0].value = &inarg;
1144 	args.in_args[1].size = entry->d_name.len + 1;
1145 	args.in_args[1].value = entry->d_name.name;
1146 	return create_new_entry(idmap, fm, &args, dir, entry, S_IFDIR);
1147 }
1148 
1149 static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir,
1150 			struct dentry *entry, const char *link)
1151 {
1152 	struct fuse_mount *fm = get_fuse_mount(dir);
1153 	unsigned len = strlen(link) + 1;
1154 	FUSE_ARGS(args);
1155 
1156 	args.opcode = FUSE_SYMLINK;
1157 	args.in_numargs = 3;
1158 	fuse_set_zero_arg0(&args);
1159 	args.in_args[1].size = entry->d_name.len + 1;
1160 	args.in_args[1].value = entry->d_name.name;
1161 	args.in_args[2].size = len;
1162 	args.in_args[2].value = link;
1163 	return create_new_nondir(idmap, fm, &args, dir, entry, S_IFLNK);
1164 }
1165 
1166 void fuse_flush_time_update(struct inode *inode)
1167 {
1168 	int err = sync_inode_metadata(inode, 1);
1169 
1170 	mapping_set_error(inode->i_mapping, err);
1171 }
1172 
1173 static void fuse_update_ctime_in_cache(struct inode *inode)
1174 {
1175 	if (!IS_NOCMTIME(inode)) {
1176 		inode_set_ctime_current(inode);
1177 		mark_inode_dirty_sync(inode);
1178 		fuse_flush_time_update(inode);
1179 	}
1180 }
1181 
1182 void fuse_update_ctime(struct inode *inode)
1183 {
1184 	fuse_invalidate_attr_mask(inode, STATX_CTIME);
1185 	fuse_update_ctime_in_cache(inode);
1186 }
1187 
1188 static void fuse_entry_unlinked(struct dentry *entry)
1189 {
1190 	struct inode *inode = d_inode(entry);
1191 	struct fuse_conn *fc = get_fuse_conn(inode);
1192 	struct fuse_inode *fi = get_fuse_inode(inode);
1193 
1194 	spin_lock(&fi->lock);
1195 	fi->attr_version = atomic64_inc_return(&fc->attr_version);
1196 	/*
1197 	 * If i_nlink == 0 then unlink doesn't make sense, yet this can
1198 	 * happen if userspace filesystem is careless.  It would be
1199 	 * difficult to enforce correct nlink usage so just ignore this
1200 	 * condition here
1201 	 */
1202 	if (S_ISDIR(inode->i_mode))
1203 		clear_nlink(inode);
1204 	else if (inode->i_nlink > 0)
1205 		drop_nlink(inode);
1206 	spin_unlock(&fi->lock);
1207 	fuse_invalidate_entry_cache(entry);
1208 	fuse_update_ctime(inode);
1209 }
1210 
1211 static int fuse_unlink(struct inode *dir, struct dentry *entry)
1212 {
1213 	int err;
1214 	struct fuse_mount *fm = get_fuse_mount(dir);
1215 	FUSE_ARGS(args);
1216 
1217 	if (fuse_is_bad(dir))
1218 		return -EIO;
1219 
1220 	args.opcode = FUSE_UNLINK;
1221 	args.nodeid = get_node_id(dir);
1222 	args.in_numargs = 2;
1223 	fuse_set_zero_arg0(&args);
1224 	args.in_args[1].size = entry->d_name.len + 1;
1225 	args.in_args[1].value = entry->d_name.name;
1226 	err = fuse_simple_request(fm, &args);
1227 	if (!err) {
1228 		fuse_dir_changed(dir);
1229 		fuse_entry_unlinked(entry);
1230 	} else if (err == -EINTR || err == -ENOENT)
1231 		fuse_invalidate_entry(entry);
1232 	return err;
1233 }
1234 
1235 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
1236 {
1237 	int err;
1238 	struct fuse_mount *fm = get_fuse_mount(dir);
1239 	FUSE_ARGS(args);
1240 
1241 	if (fuse_is_bad(dir))
1242 		return -EIO;
1243 
1244 	args.opcode = FUSE_RMDIR;
1245 	args.nodeid = get_node_id(dir);
1246 	args.in_numargs = 2;
1247 	fuse_set_zero_arg0(&args);
1248 	args.in_args[1].size = entry->d_name.len + 1;
1249 	args.in_args[1].value = entry->d_name.name;
1250 	err = fuse_simple_request(fm, &args);
1251 	if (!err) {
1252 		fuse_dir_changed(dir);
1253 		fuse_entry_unlinked(entry);
1254 	} else if (err == -EINTR || err == -ENOENT)
1255 		fuse_invalidate_entry(entry);
1256 	return err;
1257 }
1258 
1259 static int fuse_rename_common(struct mnt_idmap *idmap, struct inode *olddir, struct dentry *oldent,
1260 			      struct inode *newdir, struct dentry *newent,
1261 			      unsigned int flags, int opcode, size_t argsize)
1262 {
1263 	int err;
1264 	struct fuse_rename2_in inarg;
1265 	struct fuse_mount *fm = get_fuse_mount(olddir);
1266 	FUSE_ARGS(args);
1267 
1268 	memset(&inarg, 0, argsize);
1269 	inarg.newdir = get_node_id(newdir);
1270 	inarg.flags = flags;
1271 	args.opcode = opcode;
1272 	args.nodeid = get_node_id(olddir);
1273 	args.in_numargs = 3;
1274 	args.in_args[0].size = argsize;
1275 	args.in_args[0].value = &inarg;
1276 	args.in_args[1].size = oldent->d_name.len + 1;
1277 	args.in_args[1].value = oldent->d_name.name;
1278 	args.in_args[2].size = newent->d_name.len + 1;
1279 	args.in_args[2].value = newent->d_name.name;
1280 	err = fuse_simple_idmap_request(idmap, fm, &args);
1281 	if (!err) {
1282 		/* ctime changes */
1283 		fuse_update_ctime(d_inode(oldent));
1284 
1285 		if (flags & RENAME_EXCHANGE)
1286 			fuse_update_ctime(d_inode(newent));
1287 
1288 		fuse_dir_changed(olddir);
1289 		if (olddir != newdir)
1290 			fuse_dir_changed(newdir);
1291 
1292 		/* newent will end up negative */
1293 		if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
1294 			fuse_entry_unlinked(newent);
1295 	} else if (err == -EINTR || err == -ENOENT) {
1296 		/* If request was interrupted, DEITY only knows if the
1297 		   rename actually took place.  If the invalidation
1298 		   fails (e.g. some process has CWD under the renamed
1299 		   directory), then there can be inconsistency between
1300 		   the dcache and the real filesystem.  Tough luck. */
1301 		fuse_invalidate_entry(oldent);
1302 		if (d_really_is_positive(newent))
1303 			fuse_invalidate_entry(newent);
1304 	}
1305 
1306 	return err;
1307 }
1308 
1309 static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
1310 			struct dentry *oldent, struct inode *newdir,
1311 			struct dentry *newent, unsigned int flags)
1312 {
1313 	struct fuse_conn *fc = get_fuse_conn(olddir);
1314 	int err;
1315 
1316 	if (fuse_is_bad(olddir))
1317 		return -EIO;
1318 
1319 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1320 		return -EINVAL;
1321 
1322 	if (flags) {
1323 		if (fc->no_rename2 || fc->minor < 23)
1324 			return -EINVAL;
1325 
1326 		err = fuse_rename_common((flags & RENAME_WHITEOUT) ? idmap : &invalid_mnt_idmap,
1327 					 olddir, oldent, newdir, newent, flags,
1328 					 FUSE_RENAME2,
1329 					 sizeof(struct fuse_rename2_in));
1330 		if (err == -ENOSYS) {
1331 			fc->no_rename2 = 1;
1332 			err = -EINVAL;
1333 		}
1334 	} else {
1335 		err = fuse_rename_common(&invalid_mnt_idmap, olddir, oldent, newdir, newent, 0,
1336 					 FUSE_RENAME,
1337 					 sizeof(struct fuse_rename_in));
1338 	}
1339 
1340 	return err;
1341 }
1342 
1343 static int fuse_link(struct dentry *entry, struct inode *newdir,
1344 		     struct dentry *newent)
1345 {
1346 	int err;
1347 	struct fuse_link_in inarg;
1348 	struct inode *inode = d_inode(entry);
1349 	struct fuse_mount *fm = get_fuse_mount(inode);
1350 	FUSE_ARGS(args);
1351 
1352 	if (fm->fc->no_link)
1353 		goto out;
1354 
1355 	memset(&inarg, 0, sizeof(inarg));
1356 	inarg.oldnodeid = get_node_id(inode);
1357 	args.opcode = FUSE_LINK;
1358 	args.in_numargs = 2;
1359 	args.in_args[0].size = sizeof(inarg);
1360 	args.in_args[0].value = &inarg;
1361 	args.in_args[1].size = newent->d_name.len + 1;
1362 	args.in_args[1].value = newent->d_name.name;
1363 	err = create_new_nondir(&invalid_mnt_idmap, fm, &args, newdir, newent, inode->i_mode);
1364 	if (!err)
1365 		fuse_update_ctime_in_cache(inode);
1366 	else if (err == -EINTR)
1367 		fuse_invalidate_attr(inode);
1368 
1369 	if (err == -ENOSYS)
1370 		fm->fc->no_link = 1;
1371 out:
1372 	if (fm->fc->no_link)
1373 		return -EPERM;
1374 
1375 	return err;
1376 }
1377 
1378 static void fuse_fillattr(struct mnt_idmap *idmap, struct inode *inode,
1379 			  struct fuse_attr *attr, struct kstat *stat)
1380 {
1381 	unsigned int blkbits;
1382 	struct fuse_conn *fc = get_fuse_conn(inode);
1383 	vfsuid_t vfsuid = make_vfsuid(idmap, fc->user_ns,
1384 				      make_kuid(fc->user_ns, attr->uid));
1385 	vfsgid_t vfsgid = make_vfsgid(idmap, fc->user_ns,
1386 				      make_kgid(fc->user_ns, attr->gid));
1387 
1388 	stat->dev = inode->i_sb->s_dev;
1389 	stat->ino = attr->ino;
1390 	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1391 	stat->nlink = attr->nlink;
1392 	stat->uid = vfsuid_into_kuid(vfsuid);
1393 	stat->gid = vfsgid_into_kgid(vfsgid);
1394 	stat->rdev = inode->i_rdev;
1395 	stat->atime.tv_sec = attr->atime;
1396 	stat->atime.tv_nsec = attr->atimensec;
1397 	stat->mtime.tv_sec = attr->mtime;
1398 	stat->mtime.tv_nsec = attr->mtimensec;
1399 	stat->ctime.tv_sec = attr->ctime;
1400 	stat->ctime.tv_nsec = attr->ctimensec;
1401 	stat->size = attr->size;
1402 	stat->blocks = attr->blocks;
1403 
1404 	if (attr->blksize != 0)
1405 		blkbits = ilog2(attr->blksize);
1406 	else
1407 		blkbits = inode->i_sb->s_blocksize_bits;
1408 
1409 	stat->blksize = 1 << blkbits;
1410 }
1411 
1412 static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr)
1413 {
1414 	memset(attr, 0, sizeof(*attr));
1415 	attr->ino = sx->ino;
1416 	attr->size = sx->size;
1417 	attr->blocks = sx->blocks;
1418 	attr->atime = sx->atime.tv_sec;
1419 	attr->mtime = sx->mtime.tv_sec;
1420 	attr->ctime = sx->ctime.tv_sec;
1421 	attr->atimensec = sx->atime.tv_nsec;
1422 	attr->mtimensec = sx->mtime.tv_nsec;
1423 	attr->ctimensec = sx->ctime.tv_nsec;
1424 	attr->mode = sx->mode;
1425 	attr->nlink = sx->nlink;
1426 	attr->uid = sx->uid;
1427 	attr->gid = sx->gid;
1428 	attr->rdev = new_encode_dev(MKDEV(sx->rdev_major, sx->rdev_minor));
1429 	attr->blksize = sx->blksize;
1430 }
1431 
1432 static int fuse_do_statx(struct mnt_idmap *idmap, struct inode *inode,
1433 			 struct file *file, struct kstat *stat)
1434 {
1435 	int err;
1436 	struct fuse_attr attr;
1437 	struct fuse_statx *sx;
1438 	struct fuse_statx_in inarg;
1439 	struct fuse_statx_out outarg;
1440 	struct fuse_mount *fm = get_fuse_mount(inode);
1441 	u64 attr_version = fuse_get_attr_version(fm->fc);
1442 	FUSE_ARGS(args);
1443 
1444 	memset(&inarg, 0, sizeof(inarg));
1445 	memset(&outarg, 0, sizeof(outarg));
1446 	/* Directories have separate file-handle space */
1447 	if (file && S_ISREG(inode->i_mode)) {
1448 		struct fuse_file *ff = file->private_data;
1449 
1450 		inarg.getattr_flags |= FUSE_GETATTR_FH;
1451 		inarg.fh = ff->fh;
1452 	}
1453 	/* For now leave sync hints as the default, request all stats. */
1454 	inarg.sx_flags = 0;
1455 	inarg.sx_mask = STATX_BASIC_STATS | STATX_BTIME;
1456 	args.opcode = FUSE_STATX;
1457 	args.nodeid = get_node_id(inode);
1458 	args.in_numargs = 1;
1459 	args.in_args[0].size = sizeof(inarg);
1460 	args.in_args[0].value = &inarg;
1461 	args.out_numargs = 1;
1462 	args.out_args[0].size = sizeof(outarg);
1463 	args.out_args[0].value = &outarg;
1464 	err = fuse_simple_request(fm, &args);
1465 	if (err)
1466 		return err;
1467 
1468 	sx = &outarg.stat;
1469 	if (((sx->mask & STATX_SIZE) && !fuse_valid_size(sx->size)) ||
1470 	    ((sx->mask & STATX_TYPE) && (!fuse_valid_type(sx->mode) ||
1471 					 inode_wrong_type(inode, sx->mode)))) {
1472 		fuse_make_bad(inode);
1473 		return -EIO;
1474 	}
1475 
1476 	fuse_statx_to_attr(&outarg.stat, &attr);
1477 	if ((sx->mask & STATX_BASIC_STATS) == STATX_BASIC_STATS) {
1478 		fuse_change_attributes(inode, &attr, &outarg.stat,
1479 				       ATTR_TIMEOUT(&outarg), attr_version);
1480 	}
1481 
1482 	if (stat) {
1483 		stat->result_mask = sx->mask & (STATX_BASIC_STATS | STATX_BTIME);
1484 		stat->btime.tv_sec = sx->btime.tv_sec;
1485 		stat->btime.tv_nsec = min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1);
1486 		fuse_fillattr(idmap, inode, &attr, stat);
1487 		stat->result_mask |= STATX_TYPE;
1488 	}
1489 
1490 	return 0;
1491 }
1492 
1493 static int fuse_do_getattr(struct mnt_idmap *idmap, struct inode *inode,
1494 			   struct kstat *stat, struct file *file)
1495 {
1496 	int err;
1497 	struct fuse_getattr_in inarg;
1498 	struct fuse_attr_out outarg;
1499 	struct fuse_mount *fm = get_fuse_mount(inode);
1500 	FUSE_ARGS(args);
1501 	u64 attr_version;
1502 
1503 	attr_version = fuse_get_attr_version(fm->fc);
1504 
1505 	memset(&inarg, 0, sizeof(inarg));
1506 	memset(&outarg, 0, sizeof(outarg));
1507 	/* Directories have separate file-handle space */
1508 	if (file && S_ISREG(inode->i_mode)) {
1509 		struct fuse_file *ff = file->private_data;
1510 
1511 		inarg.getattr_flags |= FUSE_GETATTR_FH;
1512 		inarg.fh = ff->fh;
1513 	}
1514 	args.opcode = FUSE_GETATTR;
1515 	args.nodeid = get_node_id(inode);
1516 	args.in_numargs = 1;
1517 	args.in_args[0].size = sizeof(inarg);
1518 	args.in_args[0].value = &inarg;
1519 	args.out_numargs = 1;
1520 	args.out_args[0].size = sizeof(outarg);
1521 	args.out_args[0].value = &outarg;
1522 	err = fuse_simple_request(fm, &args);
1523 	if (!err) {
1524 		if (fuse_invalid_attr(&outarg.attr) ||
1525 		    inode_wrong_type(inode, outarg.attr.mode)) {
1526 			fuse_make_bad(inode);
1527 			err = -EIO;
1528 		} else {
1529 			fuse_change_attributes(inode, &outarg.attr, NULL,
1530 					       ATTR_TIMEOUT(&outarg),
1531 					       attr_version);
1532 			if (stat)
1533 				fuse_fillattr(idmap, inode, &outarg.attr, stat);
1534 		}
1535 	}
1536 	return err;
1537 }
1538 
1539 static int fuse_update_get_attr(struct mnt_idmap *idmap, struct inode *inode,
1540 				struct file *file, struct kstat *stat,
1541 				u32 request_mask, unsigned int flags)
1542 {
1543 	struct fuse_inode *fi = get_fuse_inode(inode);
1544 	struct fuse_conn *fc = get_fuse_conn(inode);
1545 	int err = 0;
1546 	bool sync;
1547 	u32 inval_mask = READ_ONCE(fi->inval_mask);
1548 	u32 cache_mask = fuse_get_cache_mask(inode);
1549 
1550 
1551 	/* FUSE only supports basic stats and possibly btime */
1552 	request_mask &= STATX_BASIC_STATS | STATX_BTIME;
1553 retry:
1554 	if (fc->no_statx)
1555 		request_mask &= STATX_BASIC_STATS;
1556 
1557 	if (!request_mask)
1558 		sync = false;
1559 	else if (flags & AT_STATX_FORCE_SYNC)
1560 		sync = true;
1561 	else if (flags & AT_STATX_DONT_SYNC)
1562 		sync = false;
1563 	else if (request_mask & inval_mask & ~cache_mask)
1564 		sync = true;
1565 	else
1566 		sync = time_before64(fi->i_time, get_jiffies_64());
1567 
1568 	if (sync) {
1569 		forget_all_cached_acls(inode);
1570 		/* Try statx if BTIME is requested */
1571 		if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS)) {
1572 			err = fuse_do_statx(idmap, inode, file, stat);
1573 			if (err == -ENOSYS) {
1574 				fc->no_statx = 1;
1575 				err = 0;
1576 				goto retry;
1577 			}
1578 		} else {
1579 			err = fuse_do_getattr(idmap, inode, stat, file);
1580 		}
1581 	} else if (stat) {
1582 		generic_fillattr(idmap, request_mask, inode, stat);
1583 		stat->mode = fi->orig_i_mode;
1584 		stat->ino = fi->orig_ino;
1585 		stat->blksize = 1 << fi->cached_i_blkbits;
1586 		if (test_bit(FUSE_I_BTIME, &fi->state)) {
1587 			stat->btime = fi->i_btime;
1588 			stat->result_mask |= STATX_BTIME;
1589 		}
1590 	}
1591 
1592 	return err;
1593 }
1594 
1595 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask)
1596 {
1597 	return fuse_update_get_attr(&nop_mnt_idmap, inode, file, NULL, mask, 0);
1598 }
1599 
1600 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1601 			     u64 child_nodeid, struct qstr *name, u32 flags)
1602 {
1603 	int err = -ENOTDIR;
1604 	struct inode *parent;
1605 	struct dentry *dir;
1606 	struct dentry *entry;
1607 
1608 	parent = fuse_ilookup(fc, parent_nodeid, NULL);
1609 	if (!parent)
1610 		return -ENOENT;
1611 
1612 	inode_lock_nested(parent, I_MUTEX_PARENT);
1613 	if (!S_ISDIR(parent->i_mode))
1614 		goto unlock;
1615 
1616 	err = -ENOENT;
1617 	dir = d_find_alias(parent);
1618 	if (!dir)
1619 		goto unlock;
1620 
1621 	name->hash = full_name_hash(dir, name->name, name->len);
1622 	entry = d_lookup(dir, name);
1623 	dput(dir);
1624 	if (!entry)
1625 		goto unlock;
1626 
1627 	fuse_dir_changed(parent);
1628 	if (!(flags & FUSE_EXPIRE_ONLY))
1629 		d_invalidate(entry);
1630 	fuse_invalidate_entry_cache(entry);
1631 
1632 	if (child_nodeid != 0 && d_really_is_positive(entry)) {
1633 		inode_lock(d_inode(entry));
1634 		if (get_node_id(d_inode(entry)) != child_nodeid) {
1635 			err = -ENOENT;
1636 			goto badentry;
1637 		}
1638 		if (d_mountpoint(entry)) {
1639 			err = -EBUSY;
1640 			goto badentry;
1641 		}
1642 		if (d_is_dir(entry)) {
1643 			shrink_dcache_parent(entry);
1644 			if (!simple_empty(entry)) {
1645 				err = -ENOTEMPTY;
1646 				goto badentry;
1647 			}
1648 			d_inode(entry)->i_flags |= S_DEAD;
1649 		}
1650 		dont_mount(entry);
1651 		clear_nlink(d_inode(entry));
1652 		err = 0;
1653  badentry:
1654 		inode_unlock(d_inode(entry));
1655 		if (!err)
1656 			d_delete(entry);
1657 	} else {
1658 		err = 0;
1659 	}
1660 	dput(entry);
1661 
1662  unlock:
1663 	inode_unlock(parent);
1664 	iput(parent);
1665 	return err;
1666 }
1667 
1668 static inline bool fuse_permissible_uidgid(struct fuse_conn *fc)
1669 {
1670 	const struct cred *cred = current_cred();
1671 
1672 	return (uid_eq(cred->euid, fc->user_id) &&
1673 		uid_eq(cred->suid, fc->user_id) &&
1674 		uid_eq(cred->uid,  fc->user_id) &&
1675 		gid_eq(cred->egid, fc->group_id) &&
1676 		gid_eq(cred->sgid, fc->group_id) &&
1677 		gid_eq(cred->gid,  fc->group_id));
1678 }
1679 
1680 /*
1681  * Calling into a user-controlled filesystem gives the filesystem
1682  * daemon ptrace-like capabilities over the current process.  This
1683  * means, that the filesystem daemon is able to record the exact
1684  * filesystem operations performed, and can also control the behavior
1685  * of the requester process in otherwise impossible ways.  For example
1686  * it can delay the operation for arbitrary length of time allowing
1687  * DoS against the requester.
1688  *
1689  * For this reason only those processes can call into the filesystem,
1690  * for which the owner of the mount has ptrace privilege.  This
1691  * excludes processes started by other users, suid or sgid processes.
1692  */
1693 bool fuse_allow_current_process(struct fuse_conn *fc)
1694 {
1695 	bool allow;
1696 
1697 	if (fc->allow_other)
1698 		allow = current_in_userns(fc->user_ns);
1699 	else
1700 		allow = fuse_permissible_uidgid(fc);
1701 
1702 	if (!allow && allow_sys_admin_access && capable(CAP_SYS_ADMIN))
1703 		allow = true;
1704 
1705 	return allow;
1706 }
1707 
1708 static int fuse_access(struct inode *inode, int mask)
1709 {
1710 	struct fuse_mount *fm = get_fuse_mount(inode);
1711 	FUSE_ARGS(args);
1712 	struct fuse_access_in inarg;
1713 	int err;
1714 
1715 	BUG_ON(mask & MAY_NOT_BLOCK);
1716 
1717 	/*
1718 	 * We should not send FUSE_ACCESS to the userspace
1719 	 * when idmapped mounts are enabled as for this case
1720 	 * we have fc->default_permissions = 1 and access
1721 	 * permission checks are done on the kernel side.
1722 	 */
1723 	WARN_ON_ONCE(!(fm->sb->s_iflags & SB_I_NOIDMAP));
1724 
1725 	if (fm->fc->no_access)
1726 		return 0;
1727 
1728 	memset(&inarg, 0, sizeof(inarg));
1729 	inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1730 	args.opcode = FUSE_ACCESS;
1731 	args.nodeid = get_node_id(inode);
1732 	args.in_numargs = 1;
1733 	args.in_args[0].size = sizeof(inarg);
1734 	args.in_args[0].value = &inarg;
1735 	err = fuse_simple_request(fm, &args);
1736 	if (err == -ENOSYS) {
1737 		fm->fc->no_access = 1;
1738 		err = 0;
1739 	}
1740 	return err;
1741 }
1742 
1743 static int fuse_perm_getattr(struct inode *inode, int mask)
1744 {
1745 	if (mask & MAY_NOT_BLOCK)
1746 		return -ECHILD;
1747 
1748 	forget_all_cached_acls(inode);
1749 	return fuse_do_getattr(&nop_mnt_idmap, inode, NULL, NULL);
1750 }
1751 
1752 /*
1753  * Check permission.  The two basic access models of FUSE are:
1754  *
1755  * 1) Local access checking ('default_permissions' mount option) based
1756  * on file mode.  This is the plain old disk filesystem permission
1757  * model.
1758  *
1759  * 2) "Remote" access checking, where server is responsible for
1760  * checking permission in each inode operation.  An exception to this
1761  * is if ->permission() was invoked from sys_access() in which case an
1762  * access request is sent.  Execute permission is still checked
1763  * locally based on file mode.
1764  */
1765 static int fuse_permission(struct mnt_idmap *idmap,
1766 			   struct inode *inode, int mask)
1767 {
1768 	struct fuse_conn *fc = get_fuse_conn(inode);
1769 	bool refreshed = false;
1770 	int err = 0;
1771 
1772 	if (fuse_is_bad(inode))
1773 		return -EIO;
1774 
1775 	if (!fuse_allow_current_process(fc))
1776 		return -EACCES;
1777 
1778 	/*
1779 	 * If attributes are needed, refresh them before proceeding
1780 	 */
1781 	if (fc->default_permissions ||
1782 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1783 		struct fuse_inode *fi = get_fuse_inode(inode);
1784 		u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1785 
1786 		if (perm_mask & READ_ONCE(fi->inval_mask) ||
1787 		    time_before64(fi->i_time, get_jiffies_64())) {
1788 			refreshed = true;
1789 
1790 			err = fuse_perm_getattr(inode, mask);
1791 			if (err)
1792 				return err;
1793 		}
1794 	}
1795 
1796 	if (fc->default_permissions) {
1797 		err = generic_permission(idmap, inode, mask);
1798 
1799 		/* If permission is denied, try to refresh file
1800 		   attributes.  This is also needed, because the root
1801 		   node will at first have no permissions */
1802 		if (err == -EACCES && !refreshed) {
1803 			err = fuse_perm_getattr(inode, mask);
1804 			if (!err)
1805 				err = generic_permission(idmap,
1806 							 inode, mask);
1807 		}
1808 
1809 		/* Note: the opposite of the above test does not
1810 		   exist.  So if permissions are revoked this won't be
1811 		   noticed immediately, only after the attribute
1812 		   timeout has expired */
1813 	} else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1814 		err = fuse_access(inode, mask);
1815 	} else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1816 		if (!(inode->i_mode & S_IXUGO)) {
1817 			if (refreshed)
1818 				return -EACCES;
1819 
1820 			err = fuse_perm_getattr(inode, mask);
1821 			if (!err && !(inode->i_mode & S_IXUGO))
1822 				return -EACCES;
1823 		}
1824 	}
1825 	return err;
1826 }
1827 
1828 static int fuse_readlink_folio(struct inode *inode, struct folio *folio)
1829 {
1830 	struct fuse_mount *fm = get_fuse_mount(inode);
1831 	struct fuse_folio_desc desc = { .length = folio_size(folio) - 1 };
1832 	struct fuse_args_pages ap = {
1833 		.num_folios = 1,
1834 		.folios = &folio,
1835 		.descs = &desc,
1836 	};
1837 	char *link;
1838 	ssize_t res;
1839 
1840 	ap.args.opcode = FUSE_READLINK;
1841 	ap.args.nodeid = get_node_id(inode);
1842 	ap.args.out_pages = true;
1843 	ap.args.out_argvar = true;
1844 	ap.args.page_zeroing = true;
1845 	ap.args.out_numargs = 1;
1846 	ap.args.out_args[0].size = desc.length;
1847 	res = fuse_simple_request(fm, &ap.args);
1848 
1849 	fuse_invalidate_atime(inode);
1850 
1851 	if (res < 0)
1852 		return res;
1853 
1854 	if (WARN_ON(res >= PAGE_SIZE))
1855 		return -EIO;
1856 
1857 	link = folio_address(folio);
1858 	link[res] = '\0';
1859 
1860 	return 0;
1861 }
1862 
1863 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1864 				 struct delayed_call *callback)
1865 {
1866 	struct fuse_conn *fc = get_fuse_conn(inode);
1867 	struct folio *folio;
1868 	int err;
1869 
1870 	err = -EIO;
1871 	if (fuse_is_bad(inode))
1872 		goto out_err;
1873 
1874 	if (fc->cache_symlinks)
1875 		return page_get_link_raw(dentry, inode, callback);
1876 
1877 	err = -ECHILD;
1878 	if (!dentry)
1879 		goto out_err;
1880 
1881 	folio = folio_alloc(GFP_KERNEL, 0);
1882 	err = -ENOMEM;
1883 	if (!folio)
1884 		goto out_err;
1885 
1886 	err = fuse_readlink_folio(inode, folio);
1887 	if (err) {
1888 		folio_put(folio);
1889 		goto out_err;
1890 	}
1891 
1892 	set_delayed_call(callback, page_put_link, folio);
1893 
1894 	return folio_address(folio);
1895 
1896 out_err:
1897 	return ERR_PTR(err);
1898 }
1899 
1900 static int fuse_dir_open(struct inode *inode, struct file *file)
1901 {
1902 	struct fuse_mount *fm = get_fuse_mount(inode);
1903 	int err;
1904 
1905 	if (fuse_is_bad(inode))
1906 		return -EIO;
1907 
1908 	err = generic_file_open(inode, file);
1909 	if (err)
1910 		return err;
1911 
1912 	err = fuse_do_open(fm, get_node_id(inode), file, true);
1913 	if (!err) {
1914 		struct fuse_file *ff = file->private_data;
1915 
1916 		/*
1917 		 * Keep handling FOPEN_STREAM and FOPEN_NONSEEKABLE for
1918 		 * directories for backward compatibility, though it's unlikely
1919 		 * to be useful.
1920 		 */
1921 		if (ff->open_flags & (FOPEN_STREAM | FOPEN_NONSEEKABLE))
1922 			nonseekable_open(inode, file);
1923 		if (!(ff->open_flags & FOPEN_KEEP_CACHE))
1924 			invalidate_inode_pages2(inode->i_mapping);
1925 	}
1926 
1927 	return err;
1928 }
1929 
1930 static int fuse_dir_release(struct inode *inode, struct file *file)
1931 {
1932 	fuse_release_common(file, true);
1933 
1934 	return 0;
1935 }
1936 
1937 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1938 			  int datasync)
1939 {
1940 	struct inode *inode = file->f_mapping->host;
1941 	struct fuse_conn *fc = get_fuse_conn(inode);
1942 	int err;
1943 
1944 	if (fuse_is_bad(inode))
1945 		return -EIO;
1946 
1947 	if (fc->no_fsyncdir)
1948 		return 0;
1949 
1950 	inode_lock(inode);
1951 	err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1952 	if (err == -ENOSYS) {
1953 		fc->no_fsyncdir = 1;
1954 		err = 0;
1955 	}
1956 	inode_unlock(inode);
1957 
1958 	return err;
1959 }
1960 
1961 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1962 			    unsigned long arg)
1963 {
1964 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1965 
1966 	/* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1967 	if (fc->minor < 18)
1968 		return -ENOTTY;
1969 
1970 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1971 }
1972 
1973 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1974 				   unsigned long arg)
1975 {
1976 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1977 
1978 	if (fc->minor < 18)
1979 		return -ENOTTY;
1980 
1981 	return fuse_ioctl_common(file, cmd, arg,
1982 				 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1983 }
1984 
1985 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1986 {
1987 	/* Always update if mtime is explicitly set  */
1988 	if (ivalid & ATTR_MTIME_SET)
1989 		return true;
1990 
1991 	/* Or if kernel i_mtime is the official one */
1992 	if (trust_local_mtime)
1993 		return true;
1994 
1995 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1996 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1997 		return false;
1998 
1999 	/* In all other cases update */
2000 	return true;
2001 }
2002 
2003 static void iattr_to_fattr(struct mnt_idmap *idmap, struct fuse_conn *fc,
2004 			   struct iattr *iattr, struct fuse_setattr_in *arg,
2005 			   bool trust_local_cmtime)
2006 {
2007 	unsigned ivalid = iattr->ia_valid;
2008 
2009 	if (ivalid & ATTR_MODE)
2010 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
2011 
2012 	if (ivalid & ATTR_UID) {
2013 		kuid_t fsuid = from_vfsuid(idmap, fc->user_ns, iattr->ia_vfsuid);
2014 
2015 		arg->valid |= FATTR_UID;
2016 		arg->uid = from_kuid(fc->user_ns, fsuid);
2017 	}
2018 
2019 	if (ivalid & ATTR_GID) {
2020 		kgid_t fsgid = from_vfsgid(idmap, fc->user_ns, iattr->ia_vfsgid);
2021 
2022 		arg->valid |= FATTR_GID;
2023 		arg->gid = from_kgid(fc->user_ns, fsgid);
2024 	}
2025 
2026 	if (ivalid & ATTR_SIZE)
2027 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
2028 	if (ivalid & ATTR_ATIME) {
2029 		arg->valid |= FATTR_ATIME;
2030 		arg->atime = iattr->ia_atime.tv_sec;
2031 		arg->atimensec = iattr->ia_atime.tv_nsec;
2032 		if (!(ivalid & ATTR_ATIME_SET))
2033 			arg->valid |= FATTR_ATIME_NOW;
2034 	}
2035 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
2036 		arg->valid |= FATTR_MTIME;
2037 		arg->mtime = iattr->ia_mtime.tv_sec;
2038 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
2039 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
2040 			arg->valid |= FATTR_MTIME_NOW;
2041 	}
2042 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
2043 		arg->valid |= FATTR_CTIME;
2044 		arg->ctime = iattr->ia_ctime.tv_sec;
2045 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
2046 	}
2047 }
2048 
2049 /*
2050  * Prevent concurrent writepages on inode
2051  *
2052  * This is done by adding a negative bias to the inode write counter
2053  * and waiting for all pending writes to finish.
2054  */
2055 void fuse_set_nowrite(struct inode *inode)
2056 {
2057 	struct fuse_inode *fi = get_fuse_inode(inode);
2058 
2059 	BUG_ON(!inode_is_locked(inode));
2060 
2061 	spin_lock(&fi->lock);
2062 	BUG_ON(fi->writectr < 0);
2063 	fi->writectr += FUSE_NOWRITE;
2064 	spin_unlock(&fi->lock);
2065 	wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
2066 }
2067 
2068 /*
2069  * Allow writepages on inode
2070  *
2071  * Remove the bias from the writecounter and send any queued
2072  * writepages.
2073  */
2074 static void __fuse_release_nowrite(struct inode *inode)
2075 {
2076 	struct fuse_inode *fi = get_fuse_inode(inode);
2077 
2078 	BUG_ON(fi->writectr != FUSE_NOWRITE);
2079 	fi->writectr = 0;
2080 	fuse_flush_writepages(inode);
2081 }
2082 
2083 void fuse_release_nowrite(struct inode *inode)
2084 {
2085 	struct fuse_inode *fi = get_fuse_inode(inode);
2086 
2087 	spin_lock(&fi->lock);
2088 	__fuse_release_nowrite(inode);
2089 	spin_unlock(&fi->lock);
2090 }
2091 
2092 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
2093 			      struct inode *inode,
2094 			      struct fuse_setattr_in *inarg_p,
2095 			      struct fuse_attr_out *outarg_p)
2096 {
2097 	args->opcode = FUSE_SETATTR;
2098 	args->nodeid = get_node_id(inode);
2099 	args->in_numargs = 1;
2100 	args->in_args[0].size = sizeof(*inarg_p);
2101 	args->in_args[0].value = inarg_p;
2102 	args->out_numargs = 1;
2103 	args->out_args[0].size = sizeof(*outarg_p);
2104 	args->out_args[0].value = outarg_p;
2105 }
2106 
2107 /*
2108  * Flush inode->i_mtime to the server
2109  */
2110 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
2111 {
2112 	struct fuse_mount *fm = get_fuse_mount(inode);
2113 	FUSE_ARGS(args);
2114 	struct fuse_setattr_in inarg;
2115 	struct fuse_attr_out outarg;
2116 
2117 	memset(&inarg, 0, sizeof(inarg));
2118 	memset(&outarg, 0, sizeof(outarg));
2119 
2120 	inarg.valid = FATTR_MTIME;
2121 	inarg.mtime = inode_get_mtime_sec(inode);
2122 	inarg.mtimensec = inode_get_mtime_nsec(inode);
2123 	if (fm->fc->minor >= 23) {
2124 		inarg.valid |= FATTR_CTIME;
2125 		inarg.ctime = inode_get_ctime_sec(inode);
2126 		inarg.ctimensec = inode_get_ctime_nsec(inode);
2127 	}
2128 	if (ff) {
2129 		inarg.valid |= FATTR_FH;
2130 		inarg.fh = ff->fh;
2131 	}
2132 	fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
2133 
2134 	return fuse_simple_request(fm, &args);
2135 }
2136 
2137 /*
2138  * Set attributes, and at the same time refresh them.
2139  *
2140  * Truncation is slightly complicated, because the 'truncate' request
2141  * may fail, in which case we don't want to touch the mapping.
2142  * vmtruncate() doesn't allow for this case, so do the rlimit checking
2143  * and the actual truncation by hand.
2144  */
2145 int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2146 		    struct iattr *attr, struct file *file)
2147 {
2148 	struct inode *inode = d_inode(dentry);
2149 	struct fuse_mount *fm = get_fuse_mount(inode);
2150 	struct fuse_conn *fc = fm->fc;
2151 	struct fuse_inode *fi = get_fuse_inode(inode);
2152 	struct address_space *mapping = inode->i_mapping;
2153 	FUSE_ARGS(args);
2154 	struct fuse_setattr_in inarg;
2155 	struct fuse_attr_out outarg;
2156 	bool is_truncate = false;
2157 	bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
2158 	loff_t oldsize;
2159 	int err;
2160 	bool trust_local_cmtime = is_wb;
2161 	bool fault_blocked = false;
2162 	u64 attr_version;
2163 
2164 	if (!fc->default_permissions)
2165 		attr->ia_valid |= ATTR_FORCE;
2166 
2167 	err = setattr_prepare(idmap, dentry, attr);
2168 	if (err)
2169 		return err;
2170 
2171 	if (attr->ia_valid & ATTR_SIZE) {
2172 		if (WARN_ON(!S_ISREG(inode->i_mode)))
2173 			return -EIO;
2174 		is_truncate = true;
2175 	}
2176 
2177 	if (FUSE_IS_DAX(inode) && is_truncate) {
2178 		filemap_invalidate_lock(mapping);
2179 		fault_blocked = true;
2180 		err = fuse_dax_break_layouts(inode, 0, -1);
2181 		if (err)
2182 			goto unlock;
2183 	}
2184 
2185 	if (attr->ia_valid & ATTR_OPEN) {
2186 		/* This is coming from open(..., ... | O_TRUNC); */
2187 		WARN_ON(!(attr->ia_valid & ATTR_SIZE));
2188 		WARN_ON(attr->ia_size != 0);
2189 		if (fc->atomic_o_trunc) {
2190 			/*
2191 			 * No need to send request to userspace, since actual
2192 			 * truncation has already been done by OPEN.  But still
2193 			 * need to truncate page cache.
2194 			 */
2195 			i_size_write(inode, 0);
2196 			truncate_pagecache(inode, 0);
2197 			goto out;
2198 		}
2199 		file = NULL;
2200 	}
2201 
2202 	/* Flush dirty data/metadata before non-truncate SETATTR */
2203 	if (is_wb &&
2204 	    attr->ia_valid &
2205 			(ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
2206 			 ATTR_TIMES_SET)) {
2207 		err = write_inode_now(inode, true);
2208 		if (err)
2209 			goto unlock;
2210 
2211 		fuse_set_nowrite(inode);
2212 		fuse_release_nowrite(inode);
2213 	}
2214 
2215 	if (is_truncate) {
2216 		fuse_set_nowrite(inode);
2217 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2218 		if (trust_local_cmtime && attr->ia_size != inode->i_size)
2219 			attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
2220 	}
2221 
2222 	memset(&inarg, 0, sizeof(inarg));
2223 	memset(&outarg, 0, sizeof(outarg));
2224 	iattr_to_fattr(idmap, fc, attr, &inarg, trust_local_cmtime);
2225 	if (file) {
2226 		struct fuse_file *ff = file->private_data;
2227 		inarg.valid |= FATTR_FH;
2228 		inarg.fh = ff->fh;
2229 	}
2230 
2231 	/* Kill suid/sgid for non-directory chown unconditionally */
2232 	if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) &&
2233 	    attr->ia_valid & (ATTR_UID | ATTR_GID))
2234 		inarg.valid |= FATTR_KILL_SUIDGID;
2235 
2236 	if (attr->ia_valid & ATTR_SIZE) {
2237 		/* For mandatory locking in truncate */
2238 		inarg.valid |= FATTR_LOCKOWNER;
2239 		inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
2240 
2241 		/* Kill suid/sgid for truncate only if no CAP_FSETID */
2242 		if (fc->handle_killpriv_v2 && !capable(CAP_FSETID))
2243 			inarg.valid |= FATTR_KILL_SUIDGID;
2244 	}
2245 
2246 	attr_version = fuse_get_attr_version(fm->fc);
2247 	fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
2248 	err = fuse_simple_request(fm, &args);
2249 	if (err) {
2250 		if (err == -EINTR)
2251 			fuse_invalidate_attr(inode);
2252 		goto error;
2253 	}
2254 
2255 	if (fuse_invalid_attr(&outarg.attr) ||
2256 	    inode_wrong_type(inode, outarg.attr.mode)) {
2257 		fuse_make_bad(inode);
2258 		err = -EIO;
2259 		goto error;
2260 	}
2261 
2262 	spin_lock(&fi->lock);
2263 	/* the kernel maintains i_mtime locally */
2264 	if (trust_local_cmtime) {
2265 		if (attr->ia_valid & ATTR_MTIME)
2266 			inode_set_mtime_to_ts(inode, attr->ia_mtime);
2267 		if (attr->ia_valid & ATTR_CTIME)
2268 			inode_set_ctime_to_ts(inode, attr->ia_ctime);
2269 		/* FIXME: clear I_DIRTY_SYNC? */
2270 	}
2271 
2272 	if (fi->attr_version > attr_version) {
2273 		/*
2274 		 * Apply attributes, for example for fsnotify_change(), but set
2275 		 * attribute timeout to zero.
2276 		 */
2277 		outarg.attr_valid = outarg.attr_valid_nsec = 0;
2278 	}
2279 
2280 	fuse_change_attributes_common(inode, &outarg.attr, NULL,
2281 				      ATTR_TIMEOUT(&outarg),
2282 				      fuse_get_cache_mask(inode), 0);
2283 	oldsize = inode->i_size;
2284 	/* see the comment in fuse_change_attributes() */
2285 	if (!is_wb || is_truncate)
2286 		i_size_write(inode, outarg.attr.size);
2287 
2288 	if (is_truncate) {
2289 		/* NOTE: this may release/reacquire fi->lock */
2290 		__fuse_release_nowrite(inode);
2291 	}
2292 	spin_unlock(&fi->lock);
2293 
2294 	/*
2295 	 * Only call invalidate_inode_pages2() after removing
2296 	 * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock.
2297 	 */
2298 	if ((is_truncate || !is_wb) &&
2299 	    S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
2300 		if (outarg.attr.size > oldsize)
2301 			truncate_pagecache_range(inode, oldsize,
2302 						 outarg.attr.size - 1);
2303 		truncate_pagecache(inode, outarg.attr.size);
2304 		invalidate_inode_pages2(mapping);
2305 	}
2306 
2307 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2308 out:
2309 	if (fault_blocked)
2310 		filemap_invalidate_unlock(mapping);
2311 
2312 	return 0;
2313 
2314 error:
2315 	if (is_truncate)
2316 		fuse_release_nowrite(inode);
2317 
2318 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2319 
2320 unlock:
2321 	if (fault_blocked)
2322 		filemap_invalidate_unlock(mapping);
2323 	return err;
2324 }
2325 
2326 static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
2327 			struct iattr *attr)
2328 {
2329 	struct inode *inode = d_inode(entry);
2330 	struct fuse_conn *fc = get_fuse_conn(inode);
2331 	struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
2332 	int ret;
2333 
2334 	if (fuse_is_bad(inode))
2335 		return -EIO;
2336 
2337 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
2338 		return -EACCES;
2339 
2340 	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
2341 		attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
2342 				    ATTR_MODE);
2343 
2344 		/*
2345 		 * The only sane way to reliably kill suid/sgid is to do it in
2346 		 * the userspace filesystem
2347 		 *
2348 		 * This should be done on write(), truncate() and chown().
2349 		 */
2350 		if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
2351 			/*
2352 			 * ia_mode calculation may have used stale i_mode.
2353 			 * Refresh and recalculate.
2354 			 */
2355 			ret = fuse_do_getattr(idmap, inode, NULL, file);
2356 			if (ret)
2357 				return ret;
2358 
2359 			attr->ia_mode = inode->i_mode;
2360 			if (inode->i_mode & S_ISUID) {
2361 				attr->ia_valid |= ATTR_MODE;
2362 				attr->ia_mode &= ~S_ISUID;
2363 			}
2364 			if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2365 				attr->ia_valid |= ATTR_MODE;
2366 				attr->ia_mode &= ~S_ISGID;
2367 			}
2368 		}
2369 	}
2370 	if (!attr->ia_valid)
2371 		return 0;
2372 
2373 	ret = fuse_do_setattr(idmap, entry, attr, file);
2374 	if (!ret) {
2375 		/*
2376 		 * If filesystem supports acls it may have updated acl xattrs in
2377 		 * the filesystem, so forget cached acls for the inode.
2378 		 */
2379 		if (fc->posix_acl)
2380 			forget_all_cached_acls(inode);
2381 
2382 		/* Directory mode changed, may need to revalidate access */
2383 		if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
2384 			fuse_invalidate_entry_cache(entry);
2385 	}
2386 	return ret;
2387 }
2388 
2389 static int fuse_getattr(struct mnt_idmap *idmap,
2390 			const struct path *path, struct kstat *stat,
2391 			u32 request_mask, unsigned int flags)
2392 {
2393 	struct inode *inode = d_inode(path->dentry);
2394 	struct fuse_conn *fc = get_fuse_conn(inode);
2395 
2396 	if (fuse_is_bad(inode))
2397 		return -EIO;
2398 
2399 	if (!fuse_allow_current_process(fc)) {
2400 		if (!request_mask) {
2401 			/*
2402 			 * If user explicitly requested *nothing* then don't
2403 			 * error out, but return st_dev only.
2404 			 */
2405 			stat->result_mask = 0;
2406 			stat->dev = inode->i_sb->s_dev;
2407 			return 0;
2408 		}
2409 		return -EACCES;
2410 	}
2411 
2412 	return fuse_update_get_attr(idmap, inode, NULL, stat, request_mask, flags);
2413 }
2414 
2415 static const struct inode_operations fuse_dir_inode_operations = {
2416 	.lookup		= fuse_lookup,
2417 	.mkdir		= fuse_mkdir,
2418 	.symlink	= fuse_symlink,
2419 	.unlink		= fuse_unlink,
2420 	.rmdir		= fuse_rmdir,
2421 	.rename		= fuse_rename2,
2422 	.link		= fuse_link,
2423 	.setattr	= fuse_setattr,
2424 	.create		= fuse_create,
2425 	.atomic_open	= fuse_atomic_open,
2426 	.tmpfile	= fuse_tmpfile,
2427 	.mknod		= fuse_mknod,
2428 	.permission	= fuse_permission,
2429 	.getattr	= fuse_getattr,
2430 	.listxattr	= fuse_listxattr,
2431 	.get_inode_acl	= fuse_get_inode_acl,
2432 	.get_acl	= fuse_get_acl,
2433 	.set_acl	= fuse_set_acl,
2434 	.fileattr_get	= fuse_fileattr_get,
2435 	.fileattr_set	= fuse_fileattr_set,
2436 };
2437 
2438 static const struct file_operations fuse_dir_operations = {
2439 	.llseek		= generic_file_llseek,
2440 	.read		= generic_read_dir,
2441 	.iterate_shared	= fuse_readdir,
2442 	.open		= fuse_dir_open,
2443 	.release	= fuse_dir_release,
2444 	.fsync		= fuse_dir_fsync,
2445 	.unlocked_ioctl	= fuse_dir_ioctl,
2446 	.compat_ioctl	= fuse_dir_compat_ioctl,
2447 };
2448 
2449 static const struct inode_operations fuse_common_inode_operations = {
2450 	.setattr	= fuse_setattr,
2451 	.permission	= fuse_permission,
2452 	.getattr	= fuse_getattr,
2453 	.listxattr	= fuse_listxattr,
2454 	.get_inode_acl	= fuse_get_inode_acl,
2455 	.get_acl	= fuse_get_acl,
2456 	.set_acl	= fuse_set_acl,
2457 	.fileattr_get	= fuse_fileattr_get,
2458 	.fileattr_set	= fuse_fileattr_set,
2459 };
2460 
2461 static const struct inode_operations fuse_symlink_inode_operations = {
2462 	.setattr	= fuse_setattr,
2463 	.get_link	= fuse_get_link,
2464 	.getattr	= fuse_getattr,
2465 	.listxattr	= fuse_listxattr,
2466 };
2467 
2468 void fuse_init_common(struct inode *inode)
2469 {
2470 	inode->i_op = &fuse_common_inode_operations;
2471 }
2472 
2473 void fuse_init_dir(struct inode *inode)
2474 {
2475 	struct fuse_inode *fi = get_fuse_inode(inode);
2476 
2477 	inode->i_op = &fuse_dir_inode_operations;
2478 	inode->i_fop = &fuse_dir_operations;
2479 
2480 	spin_lock_init(&fi->rdc.lock);
2481 	fi->rdc.cached = false;
2482 	fi->rdc.size = 0;
2483 	fi->rdc.pos = 0;
2484 	fi->rdc.version = 0;
2485 }
2486 
2487 static int fuse_symlink_read_folio(struct file *null, struct folio *folio)
2488 {
2489 	int err = fuse_readlink_folio(folio->mapping->host, folio);
2490 
2491 	if (!err)
2492 		folio_mark_uptodate(folio);
2493 
2494 	folio_unlock(folio);
2495 
2496 	return err;
2497 }
2498 
2499 static const struct address_space_operations fuse_symlink_aops = {
2500 	.read_folio	= fuse_symlink_read_folio,
2501 };
2502 
2503 void fuse_init_symlink(struct inode *inode)
2504 {
2505 	inode->i_op = &fuse_symlink_inode_operations;
2506 	inode->i_data.a_ops = &fuse_symlink_aops;
2507 	inode_nohighmem(inode);
2508 }
2509