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