xref: /openbmc/linux/fs/fuse/dir.c (revision 83268fa6)
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/sched.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 #include <linux/xattr.h>
17 #include <linux/iversion.h>
18 #include <linux/posix_acl.h>
19 
20 static void fuse_advise_use_readdirplus(struct inode *dir)
21 {
22 	struct fuse_inode *fi = get_fuse_inode(dir);
23 
24 	set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
25 }
26 
27 union fuse_dentry {
28 	u64 time;
29 	struct rcu_head rcu;
30 };
31 
32 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
33 {
34 	((union fuse_dentry *) entry->d_fsdata)->time = time;
35 }
36 
37 static inline u64 fuse_dentry_time(struct dentry *entry)
38 {
39 	return ((union fuse_dentry *) entry->d_fsdata)->time;
40 }
41 
42 /*
43  * FUSE caches dentries and attributes with separate timeout.  The
44  * time in jiffies until the dentry/attributes are valid is stored in
45  * dentry->d_fsdata and fuse_inode->i_time respectively.
46  */
47 
48 /*
49  * Calculate the time in jiffies until a dentry/attributes are valid
50  */
51 static u64 time_to_jiffies(u64 sec, u32 nsec)
52 {
53 	if (sec || nsec) {
54 		struct timespec64 ts = {
55 			sec,
56 			min_t(u32, nsec, NSEC_PER_SEC - 1)
57 		};
58 
59 		return get_jiffies_64() + timespec64_to_jiffies(&ts);
60 	} else
61 		return 0;
62 }
63 
64 /*
65  * Set dentry and possibly attribute timeouts from the lookup/mk*
66  * replies
67  */
68 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
69 {
70 	fuse_dentry_settime(entry,
71 		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
72 }
73 
74 static u64 attr_timeout(struct fuse_attr_out *o)
75 {
76 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
77 }
78 
79 u64 entry_attr_timeout(struct fuse_entry_out *o)
80 {
81 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
82 }
83 
84 static void fuse_invalidate_attr_mask(struct inode *inode, u32 mask)
85 {
86 	set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask);
87 }
88 
89 /*
90  * Mark the attributes as stale, so that at the next call to
91  * ->getattr() they will be fetched from userspace
92  */
93 void fuse_invalidate_attr(struct inode *inode)
94 {
95 	fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS);
96 }
97 
98 static void fuse_dir_changed(struct inode *dir)
99 {
100 	fuse_invalidate_attr(dir);
101 	inode_maybe_inc_iversion(dir, false);
102 }
103 
104 /**
105  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
106  * atime is not used.
107  */
108 void fuse_invalidate_atime(struct inode *inode)
109 {
110 	if (!IS_RDONLY(inode))
111 		fuse_invalidate_attr_mask(inode, STATX_ATIME);
112 }
113 
114 /*
115  * Just mark the entry as stale, so that a next attempt to look it up
116  * will result in a new lookup call to userspace
117  *
118  * This is called when a dentry is about to become negative and the
119  * timeout is unknown (unlink, rmdir, rename and in some cases
120  * lookup)
121  */
122 void fuse_invalidate_entry_cache(struct dentry *entry)
123 {
124 	fuse_dentry_settime(entry, 0);
125 }
126 
127 /*
128  * Same as fuse_invalidate_entry_cache(), but also try to remove the
129  * dentry from the hash
130  */
131 static void fuse_invalidate_entry(struct dentry *entry)
132 {
133 	d_invalidate(entry);
134 	fuse_invalidate_entry_cache(entry);
135 }
136 
137 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
138 			     u64 nodeid, const struct qstr *name,
139 			     struct fuse_entry_out *outarg)
140 {
141 	memset(outarg, 0, sizeof(struct fuse_entry_out));
142 	args->in.h.opcode = FUSE_LOOKUP;
143 	args->in.h.nodeid = nodeid;
144 	args->in.numargs = 1;
145 	args->in.args[0].size = name->len + 1;
146 	args->in.args[0].value = name->name;
147 	args->out.numargs = 1;
148 	args->out.args[0].size = sizeof(struct fuse_entry_out);
149 	args->out.args[0].value = outarg;
150 }
151 
152 u64 fuse_get_attr_version(struct fuse_conn *fc)
153 {
154 	u64 curr_version;
155 
156 	/*
157 	 * The spin lock isn't actually needed on 64bit archs, but we
158 	 * don't yet care too much about such optimizations.
159 	 */
160 	spin_lock(&fc->lock);
161 	curr_version = fc->attr_version;
162 	spin_unlock(&fc->lock);
163 
164 	return curr_version;
165 }
166 
167 /*
168  * Check whether the dentry is still valid
169  *
170  * If the entry validity timeout has expired and the dentry is
171  * positive, try to redo the lookup.  If the lookup results in a
172  * different inode, then let the VFS invalidate the dentry and redo
173  * the lookup once more.  If the lookup results in the same inode,
174  * then refresh the attributes, timeouts and mark the dentry valid.
175  */
176 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
177 {
178 	struct inode *inode;
179 	struct dentry *parent;
180 	struct fuse_conn *fc;
181 	struct fuse_inode *fi;
182 	int ret;
183 
184 	inode = d_inode_rcu(entry);
185 	if (inode && is_bad_inode(inode))
186 		goto invalid;
187 	else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
188 		 (flags & LOOKUP_REVAL)) {
189 		struct fuse_entry_out outarg;
190 		FUSE_ARGS(args);
191 		struct fuse_forget_link *forget;
192 		u64 attr_version;
193 
194 		/* For negative dentries, always do a fresh lookup */
195 		if (!inode)
196 			goto invalid;
197 
198 		ret = -ECHILD;
199 		if (flags & LOOKUP_RCU)
200 			goto out;
201 
202 		fc = get_fuse_conn(inode);
203 
204 		forget = fuse_alloc_forget();
205 		ret = -ENOMEM;
206 		if (!forget)
207 			goto out;
208 
209 		attr_version = fuse_get_attr_version(fc);
210 
211 		parent = dget_parent(entry);
212 		fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
213 				 &entry->d_name, &outarg);
214 		ret = fuse_simple_request(fc, &args);
215 		dput(parent);
216 		/* Zero nodeid is same as -ENOENT */
217 		if (!ret && !outarg.nodeid)
218 			ret = -ENOENT;
219 		if (!ret) {
220 			fi = get_fuse_inode(inode);
221 			if (outarg.nodeid != get_node_id(inode)) {
222 				fuse_queue_forget(fc, forget, outarg.nodeid, 1);
223 				goto invalid;
224 			}
225 			spin_lock(&fc->lock);
226 			fi->nlookup++;
227 			spin_unlock(&fc->lock);
228 		}
229 		kfree(forget);
230 		if (ret == -ENOMEM)
231 			goto out;
232 		if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
233 			goto invalid;
234 
235 		forget_all_cached_acls(inode);
236 		fuse_change_attributes(inode, &outarg.attr,
237 				       entry_attr_timeout(&outarg),
238 				       attr_version);
239 		fuse_change_entry_timeout(entry, &outarg);
240 	} else if (inode) {
241 		fi = get_fuse_inode(inode);
242 		if (flags & LOOKUP_RCU) {
243 			if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
244 				return -ECHILD;
245 		} else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
246 			parent = dget_parent(entry);
247 			fuse_advise_use_readdirplus(d_inode(parent));
248 			dput(parent);
249 		}
250 	}
251 	ret = 1;
252 out:
253 	return ret;
254 
255 invalid:
256 	ret = 0;
257 	goto out;
258 }
259 
260 static int fuse_dentry_init(struct dentry *dentry)
261 {
262 	dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), GFP_KERNEL);
263 
264 	return dentry->d_fsdata ? 0 : -ENOMEM;
265 }
266 static void fuse_dentry_release(struct dentry *dentry)
267 {
268 	union fuse_dentry *fd = dentry->d_fsdata;
269 
270 	kfree_rcu(fd, rcu);
271 }
272 
273 const struct dentry_operations fuse_dentry_operations = {
274 	.d_revalidate	= fuse_dentry_revalidate,
275 	.d_init		= fuse_dentry_init,
276 	.d_release	= fuse_dentry_release,
277 };
278 
279 const struct dentry_operations fuse_root_dentry_operations = {
280 	.d_init		= fuse_dentry_init,
281 	.d_release	= fuse_dentry_release,
282 };
283 
284 int fuse_valid_type(int m)
285 {
286 	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
287 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
288 }
289 
290 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
291 		     struct fuse_entry_out *outarg, struct inode **inode)
292 {
293 	struct fuse_conn *fc = get_fuse_conn_super(sb);
294 	FUSE_ARGS(args);
295 	struct fuse_forget_link *forget;
296 	u64 attr_version;
297 	int err;
298 
299 	*inode = NULL;
300 	err = -ENAMETOOLONG;
301 	if (name->len > FUSE_NAME_MAX)
302 		goto out;
303 
304 
305 	forget = fuse_alloc_forget();
306 	err = -ENOMEM;
307 	if (!forget)
308 		goto out;
309 
310 	attr_version = fuse_get_attr_version(fc);
311 
312 	fuse_lookup_init(fc, &args, nodeid, name, outarg);
313 	err = fuse_simple_request(fc, &args);
314 	/* Zero nodeid is same as -ENOENT, but with valid timeout */
315 	if (err || !outarg->nodeid)
316 		goto out_put_forget;
317 
318 	err = -EIO;
319 	if (!outarg->nodeid)
320 		goto out_put_forget;
321 	if (!fuse_valid_type(outarg->attr.mode))
322 		goto out_put_forget;
323 
324 	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
325 			   &outarg->attr, entry_attr_timeout(outarg),
326 			   attr_version);
327 	err = -ENOMEM;
328 	if (!*inode) {
329 		fuse_queue_forget(fc, forget, outarg->nodeid, 1);
330 		goto out;
331 	}
332 	err = 0;
333 
334  out_put_forget:
335 	kfree(forget);
336  out:
337 	return err;
338 }
339 
340 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
341 				  unsigned int flags)
342 {
343 	int err;
344 	struct fuse_entry_out outarg;
345 	struct inode *inode;
346 	struct dentry *newent;
347 	bool outarg_valid = true;
348 	bool locked;
349 
350 	locked = fuse_lock_inode(dir);
351 	err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
352 			       &outarg, &inode);
353 	fuse_unlock_inode(dir, locked);
354 	if (err == -ENOENT) {
355 		outarg_valid = false;
356 		err = 0;
357 	}
358 	if (err)
359 		goto out_err;
360 
361 	err = -EIO;
362 	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
363 		goto out_iput;
364 
365 	newent = d_splice_alias(inode, entry);
366 	err = PTR_ERR(newent);
367 	if (IS_ERR(newent))
368 		goto out_err;
369 
370 	entry = newent ? newent : entry;
371 	if (outarg_valid)
372 		fuse_change_entry_timeout(entry, &outarg);
373 	else
374 		fuse_invalidate_entry_cache(entry);
375 
376 	fuse_advise_use_readdirplus(dir);
377 	return newent;
378 
379  out_iput:
380 	iput(inode);
381  out_err:
382 	return ERR_PTR(err);
383 }
384 
385 /*
386  * Atomic create+open operation
387  *
388  * If the filesystem doesn't support this, then fall back to separate
389  * 'mknod' + 'open' requests.
390  */
391 static int fuse_create_open(struct inode *dir, struct dentry *entry,
392 			    struct file *file, unsigned flags,
393 			    umode_t mode)
394 {
395 	int err;
396 	struct inode *inode;
397 	struct fuse_conn *fc = get_fuse_conn(dir);
398 	FUSE_ARGS(args);
399 	struct fuse_forget_link *forget;
400 	struct fuse_create_in inarg;
401 	struct fuse_open_out outopen;
402 	struct fuse_entry_out outentry;
403 	struct fuse_file *ff;
404 
405 	/* Userspace expects S_IFREG in create mode */
406 	BUG_ON((mode & S_IFMT) != S_IFREG);
407 
408 	forget = fuse_alloc_forget();
409 	err = -ENOMEM;
410 	if (!forget)
411 		goto out_err;
412 
413 	err = -ENOMEM;
414 	ff = fuse_file_alloc(fc);
415 	if (!ff)
416 		goto out_put_forget_req;
417 
418 	if (!fc->dont_mask)
419 		mode &= ~current_umask();
420 
421 	flags &= ~O_NOCTTY;
422 	memset(&inarg, 0, sizeof(inarg));
423 	memset(&outentry, 0, sizeof(outentry));
424 	inarg.flags = flags;
425 	inarg.mode = mode;
426 	inarg.umask = current_umask();
427 	args.in.h.opcode = FUSE_CREATE;
428 	args.in.h.nodeid = get_node_id(dir);
429 	args.in.numargs = 2;
430 	args.in.args[0].size = sizeof(inarg);
431 	args.in.args[0].value = &inarg;
432 	args.in.args[1].size = entry->d_name.len + 1;
433 	args.in.args[1].value = entry->d_name.name;
434 	args.out.numargs = 2;
435 	args.out.args[0].size = sizeof(outentry);
436 	args.out.args[0].value = &outentry;
437 	args.out.args[1].size = sizeof(outopen);
438 	args.out.args[1].value = &outopen;
439 	err = fuse_simple_request(fc, &args);
440 	if (err)
441 		goto out_free_ff;
442 
443 	err = -EIO;
444 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
445 		goto out_free_ff;
446 
447 	ff->fh = outopen.fh;
448 	ff->nodeid = outentry.nodeid;
449 	ff->open_flags = outopen.open_flags;
450 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
451 			  &outentry.attr, entry_attr_timeout(&outentry), 0);
452 	if (!inode) {
453 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
454 		fuse_sync_release(ff, flags);
455 		fuse_queue_forget(fc, forget, outentry.nodeid, 1);
456 		err = -ENOMEM;
457 		goto out_err;
458 	}
459 	kfree(forget);
460 	d_instantiate(entry, inode);
461 	fuse_change_entry_timeout(entry, &outentry);
462 	fuse_dir_changed(dir);
463 	err = finish_open(file, entry, generic_file_open);
464 	if (err) {
465 		fuse_sync_release(ff, flags);
466 	} else {
467 		file->private_data = ff;
468 		fuse_finish_open(inode, file);
469 	}
470 	return err;
471 
472 out_free_ff:
473 	fuse_file_free(ff);
474 out_put_forget_req:
475 	kfree(forget);
476 out_err:
477 	return err;
478 }
479 
480 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
481 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
482 			    struct file *file, unsigned flags,
483 			    umode_t mode)
484 {
485 	int err;
486 	struct fuse_conn *fc = get_fuse_conn(dir);
487 	struct dentry *res = NULL;
488 
489 	if (d_in_lookup(entry)) {
490 		res = fuse_lookup(dir, entry, 0);
491 		if (IS_ERR(res))
492 			return PTR_ERR(res);
493 
494 		if (res)
495 			entry = res;
496 	}
497 
498 	if (!(flags & O_CREAT) || d_really_is_positive(entry))
499 		goto no_open;
500 
501 	/* Only creates */
502 	file->f_mode |= FMODE_CREATED;
503 
504 	if (fc->no_create)
505 		goto mknod;
506 
507 	err = fuse_create_open(dir, entry, file, flags, mode);
508 	if (err == -ENOSYS) {
509 		fc->no_create = 1;
510 		goto mknod;
511 	}
512 out_dput:
513 	dput(res);
514 	return err;
515 
516 mknod:
517 	err = fuse_mknod(dir, entry, mode, 0);
518 	if (err)
519 		goto out_dput;
520 no_open:
521 	return finish_no_open(file, res);
522 }
523 
524 /*
525  * Code shared between mknod, mkdir, symlink and link
526  */
527 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
528 			    struct inode *dir, struct dentry *entry,
529 			    umode_t mode)
530 {
531 	struct fuse_entry_out outarg;
532 	struct inode *inode;
533 	struct dentry *d;
534 	int err;
535 	struct fuse_forget_link *forget;
536 
537 	forget = fuse_alloc_forget();
538 	if (!forget)
539 		return -ENOMEM;
540 
541 	memset(&outarg, 0, sizeof(outarg));
542 	args->in.h.nodeid = get_node_id(dir);
543 	args->out.numargs = 1;
544 	args->out.args[0].size = sizeof(outarg);
545 	args->out.args[0].value = &outarg;
546 	err = fuse_simple_request(fc, args);
547 	if (err)
548 		goto out_put_forget_req;
549 
550 	err = -EIO;
551 	if (invalid_nodeid(outarg.nodeid))
552 		goto out_put_forget_req;
553 
554 	if ((outarg.attr.mode ^ mode) & S_IFMT)
555 		goto out_put_forget_req;
556 
557 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
558 			  &outarg.attr, entry_attr_timeout(&outarg), 0);
559 	if (!inode) {
560 		fuse_queue_forget(fc, forget, outarg.nodeid, 1);
561 		return -ENOMEM;
562 	}
563 	kfree(forget);
564 
565 	d_drop(entry);
566 	d = d_splice_alias(inode, entry);
567 	if (IS_ERR(d))
568 		return PTR_ERR(d);
569 
570 	if (d) {
571 		fuse_change_entry_timeout(d, &outarg);
572 		dput(d);
573 	} else {
574 		fuse_change_entry_timeout(entry, &outarg);
575 	}
576 	fuse_dir_changed(dir);
577 	return 0;
578 
579  out_put_forget_req:
580 	kfree(forget);
581 	return err;
582 }
583 
584 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
585 		      dev_t rdev)
586 {
587 	struct fuse_mknod_in inarg;
588 	struct fuse_conn *fc = get_fuse_conn(dir);
589 	FUSE_ARGS(args);
590 
591 	if (!fc->dont_mask)
592 		mode &= ~current_umask();
593 
594 	memset(&inarg, 0, sizeof(inarg));
595 	inarg.mode = mode;
596 	inarg.rdev = new_encode_dev(rdev);
597 	inarg.umask = current_umask();
598 	args.in.h.opcode = FUSE_MKNOD;
599 	args.in.numargs = 2;
600 	args.in.args[0].size = sizeof(inarg);
601 	args.in.args[0].value = &inarg;
602 	args.in.args[1].size = entry->d_name.len + 1;
603 	args.in.args[1].value = entry->d_name.name;
604 	return create_new_entry(fc, &args, dir, entry, mode);
605 }
606 
607 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
608 		       bool excl)
609 {
610 	return fuse_mknod(dir, entry, mode, 0);
611 }
612 
613 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
614 {
615 	struct fuse_mkdir_in inarg;
616 	struct fuse_conn *fc = get_fuse_conn(dir);
617 	FUSE_ARGS(args);
618 
619 	if (!fc->dont_mask)
620 		mode &= ~current_umask();
621 
622 	memset(&inarg, 0, sizeof(inarg));
623 	inarg.mode = mode;
624 	inarg.umask = current_umask();
625 	args.in.h.opcode = FUSE_MKDIR;
626 	args.in.numargs = 2;
627 	args.in.args[0].size = sizeof(inarg);
628 	args.in.args[0].value = &inarg;
629 	args.in.args[1].size = entry->d_name.len + 1;
630 	args.in.args[1].value = entry->d_name.name;
631 	return create_new_entry(fc, &args, dir, entry, S_IFDIR);
632 }
633 
634 static int fuse_symlink(struct inode *dir, struct dentry *entry,
635 			const char *link)
636 {
637 	struct fuse_conn *fc = get_fuse_conn(dir);
638 	unsigned len = strlen(link) + 1;
639 	FUSE_ARGS(args);
640 
641 	args.in.h.opcode = FUSE_SYMLINK;
642 	args.in.numargs = 2;
643 	args.in.args[0].size = entry->d_name.len + 1;
644 	args.in.args[0].value = entry->d_name.name;
645 	args.in.args[1].size = len;
646 	args.in.args[1].value = link;
647 	return create_new_entry(fc, &args, dir, entry, S_IFLNK);
648 }
649 
650 void fuse_update_ctime(struct inode *inode)
651 {
652 	if (!IS_NOCMTIME(inode)) {
653 		inode->i_ctime = current_time(inode);
654 		mark_inode_dirty_sync(inode);
655 	}
656 }
657 
658 static int fuse_unlink(struct inode *dir, struct dentry *entry)
659 {
660 	int err;
661 	struct fuse_conn *fc = get_fuse_conn(dir);
662 	FUSE_ARGS(args);
663 
664 	args.in.h.opcode = FUSE_UNLINK;
665 	args.in.h.nodeid = get_node_id(dir);
666 	args.in.numargs = 1;
667 	args.in.args[0].size = entry->d_name.len + 1;
668 	args.in.args[0].value = entry->d_name.name;
669 	err = fuse_simple_request(fc, &args);
670 	if (!err) {
671 		struct inode *inode = d_inode(entry);
672 		struct fuse_inode *fi = get_fuse_inode(inode);
673 
674 		spin_lock(&fc->lock);
675 		fi->attr_version = ++fc->attr_version;
676 		/*
677 		 * If i_nlink == 0 then unlink doesn't make sense, yet this can
678 		 * happen if userspace filesystem is careless.  It would be
679 		 * difficult to enforce correct nlink usage so just ignore this
680 		 * condition here
681 		 */
682 		if (inode->i_nlink > 0)
683 			drop_nlink(inode);
684 		spin_unlock(&fc->lock);
685 		fuse_invalidate_attr(inode);
686 		fuse_dir_changed(dir);
687 		fuse_invalidate_entry_cache(entry);
688 		fuse_update_ctime(inode);
689 	} else if (err == -EINTR)
690 		fuse_invalidate_entry(entry);
691 	return err;
692 }
693 
694 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
695 {
696 	int err;
697 	struct fuse_conn *fc = get_fuse_conn(dir);
698 	FUSE_ARGS(args);
699 
700 	args.in.h.opcode = FUSE_RMDIR;
701 	args.in.h.nodeid = get_node_id(dir);
702 	args.in.numargs = 1;
703 	args.in.args[0].size = entry->d_name.len + 1;
704 	args.in.args[0].value = entry->d_name.name;
705 	err = fuse_simple_request(fc, &args);
706 	if (!err) {
707 		clear_nlink(d_inode(entry));
708 		fuse_dir_changed(dir);
709 		fuse_invalidate_entry_cache(entry);
710 	} else if (err == -EINTR)
711 		fuse_invalidate_entry(entry);
712 	return err;
713 }
714 
715 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
716 			      struct inode *newdir, struct dentry *newent,
717 			      unsigned int flags, int opcode, size_t argsize)
718 {
719 	int err;
720 	struct fuse_rename2_in inarg;
721 	struct fuse_conn *fc = get_fuse_conn(olddir);
722 	FUSE_ARGS(args);
723 
724 	memset(&inarg, 0, argsize);
725 	inarg.newdir = get_node_id(newdir);
726 	inarg.flags = flags;
727 	args.in.h.opcode = opcode;
728 	args.in.h.nodeid = get_node_id(olddir);
729 	args.in.numargs = 3;
730 	args.in.args[0].size = argsize;
731 	args.in.args[0].value = &inarg;
732 	args.in.args[1].size = oldent->d_name.len + 1;
733 	args.in.args[1].value = oldent->d_name.name;
734 	args.in.args[2].size = newent->d_name.len + 1;
735 	args.in.args[2].value = newent->d_name.name;
736 	err = fuse_simple_request(fc, &args);
737 	if (!err) {
738 		/* ctime changes */
739 		fuse_invalidate_attr(d_inode(oldent));
740 		fuse_update_ctime(d_inode(oldent));
741 
742 		if (flags & RENAME_EXCHANGE) {
743 			fuse_invalidate_attr(d_inode(newent));
744 			fuse_update_ctime(d_inode(newent));
745 		}
746 
747 		fuse_dir_changed(olddir);
748 		if (olddir != newdir)
749 			fuse_dir_changed(newdir);
750 
751 		/* newent will end up negative */
752 		if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
753 			fuse_invalidate_attr(d_inode(newent));
754 			fuse_invalidate_entry_cache(newent);
755 			fuse_update_ctime(d_inode(newent));
756 		}
757 	} else if (err == -EINTR) {
758 		/* If request was interrupted, DEITY only knows if the
759 		   rename actually took place.  If the invalidation
760 		   fails (e.g. some process has CWD under the renamed
761 		   directory), then there can be inconsistency between
762 		   the dcache and the real filesystem.  Tough luck. */
763 		fuse_invalidate_entry(oldent);
764 		if (d_really_is_positive(newent))
765 			fuse_invalidate_entry(newent);
766 	}
767 
768 	return err;
769 }
770 
771 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
772 			struct inode *newdir, struct dentry *newent,
773 			unsigned int flags)
774 {
775 	struct fuse_conn *fc = get_fuse_conn(olddir);
776 	int err;
777 
778 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
779 		return -EINVAL;
780 
781 	if (flags) {
782 		if (fc->no_rename2 || fc->minor < 23)
783 			return -EINVAL;
784 
785 		err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
786 					 FUSE_RENAME2,
787 					 sizeof(struct fuse_rename2_in));
788 		if (err == -ENOSYS) {
789 			fc->no_rename2 = 1;
790 			err = -EINVAL;
791 		}
792 	} else {
793 		err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
794 					 FUSE_RENAME,
795 					 sizeof(struct fuse_rename_in));
796 	}
797 
798 	return err;
799 }
800 
801 static int fuse_link(struct dentry *entry, struct inode *newdir,
802 		     struct dentry *newent)
803 {
804 	int err;
805 	struct fuse_link_in inarg;
806 	struct inode *inode = d_inode(entry);
807 	struct fuse_conn *fc = get_fuse_conn(inode);
808 	FUSE_ARGS(args);
809 
810 	memset(&inarg, 0, sizeof(inarg));
811 	inarg.oldnodeid = get_node_id(inode);
812 	args.in.h.opcode = FUSE_LINK;
813 	args.in.numargs = 2;
814 	args.in.args[0].size = sizeof(inarg);
815 	args.in.args[0].value = &inarg;
816 	args.in.args[1].size = newent->d_name.len + 1;
817 	args.in.args[1].value = newent->d_name.name;
818 	err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
819 	/* Contrary to "normal" filesystems it can happen that link
820 	   makes two "logical" inodes point to the same "physical"
821 	   inode.  We invalidate the attributes of the old one, so it
822 	   will reflect changes in the backing inode (link count,
823 	   etc.)
824 	*/
825 	if (!err) {
826 		struct fuse_inode *fi = get_fuse_inode(inode);
827 
828 		spin_lock(&fc->lock);
829 		fi->attr_version = ++fc->attr_version;
830 		inc_nlink(inode);
831 		spin_unlock(&fc->lock);
832 		fuse_invalidate_attr(inode);
833 		fuse_update_ctime(inode);
834 	} else if (err == -EINTR) {
835 		fuse_invalidate_attr(inode);
836 	}
837 	return err;
838 }
839 
840 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
841 			  struct kstat *stat)
842 {
843 	unsigned int blkbits;
844 	struct fuse_conn *fc = get_fuse_conn(inode);
845 
846 	/* see the comment in fuse_change_attributes() */
847 	if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
848 		attr->size = i_size_read(inode);
849 		attr->mtime = inode->i_mtime.tv_sec;
850 		attr->mtimensec = inode->i_mtime.tv_nsec;
851 		attr->ctime = inode->i_ctime.tv_sec;
852 		attr->ctimensec = inode->i_ctime.tv_nsec;
853 	}
854 
855 	stat->dev = inode->i_sb->s_dev;
856 	stat->ino = attr->ino;
857 	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
858 	stat->nlink = attr->nlink;
859 	stat->uid = make_kuid(fc->user_ns, attr->uid);
860 	stat->gid = make_kgid(fc->user_ns, attr->gid);
861 	stat->rdev = inode->i_rdev;
862 	stat->atime.tv_sec = attr->atime;
863 	stat->atime.tv_nsec = attr->atimensec;
864 	stat->mtime.tv_sec = attr->mtime;
865 	stat->mtime.tv_nsec = attr->mtimensec;
866 	stat->ctime.tv_sec = attr->ctime;
867 	stat->ctime.tv_nsec = attr->ctimensec;
868 	stat->size = attr->size;
869 	stat->blocks = attr->blocks;
870 
871 	if (attr->blksize != 0)
872 		blkbits = ilog2(attr->blksize);
873 	else
874 		blkbits = inode->i_sb->s_blocksize_bits;
875 
876 	stat->blksize = 1 << blkbits;
877 }
878 
879 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
880 			   struct file *file)
881 {
882 	int err;
883 	struct fuse_getattr_in inarg;
884 	struct fuse_attr_out outarg;
885 	struct fuse_conn *fc = get_fuse_conn(inode);
886 	FUSE_ARGS(args);
887 	u64 attr_version;
888 
889 	attr_version = fuse_get_attr_version(fc);
890 
891 	memset(&inarg, 0, sizeof(inarg));
892 	memset(&outarg, 0, sizeof(outarg));
893 	/* Directories have separate file-handle space */
894 	if (file && S_ISREG(inode->i_mode)) {
895 		struct fuse_file *ff = file->private_data;
896 
897 		inarg.getattr_flags |= FUSE_GETATTR_FH;
898 		inarg.fh = ff->fh;
899 	}
900 	args.in.h.opcode = FUSE_GETATTR;
901 	args.in.h.nodeid = get_node_id(inode);
902 	args.in.numargs = 1;
903 	args.in.args[0].size = sizeof(inarg);
904 	args.in.args[0].value = &inarg;
905 	args.out.numargs = 1;
906 	args.out.args[0].size = sizeof(outarg);
907 	args.out.args[0].value = &outarg;
908 	err = fuse_simple_request(fc, &args);
909 	if (!err) {
910 		if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
911 			make_bad_inode(inode);
912 			err = -EIO;
913 		} else {
914 			fuse_change_attributes(inode, &outarg.attr,
915 					       attr_timeout(&outarg),
916 					       attr_version);
917 			if (stat)
918 				fuse_fillattr(inode, &outarg.attr, stat);
919 		}
920 	}
921 	return err;
922 }
923 
924 static int fuse_update_get_attr(struct inode *inode, struct file *file,
925 				struct kstat *stat, u32 request_mask,
926 				unsigned int flags)
927 {
928 	struct fuse_inode *fi = get_fuse_inode(inode);
929 	int err = 0;
930 	bool sync;
931 
932 	if (flags & AT_STATX_FORCE_SYNC)
933 		sync = true;
934 	else if (flags & AT_STATX_DONT_SYNC)
935 		sync = false;
936 	else if (request_mask & READ_ONCE(fi->inval_mask))
937 		sync = true;
938 	else
939 		sync = time_before64(fi->i_time, get_jiffies_64());
940 
941 	if (sync) {
942 		forget_all_cached_acls(inode);
943 		err = fuse_do_getattr(inode, stat, file);
944 	} else if (stat) {
945 		generic_fillattr(inode, stat);
946 		stat->mode = fi->orig_i_mode;
947 		stat->ino = fi->orig_ino;
948 	}
949 
950 	return err;
951 }
952 
953 int fuse_update_attributes(struct inode *inode, struct file *file)
954 {
955 	/* Do *not* need to get atime for internal purposes */
956 	return fuse_update_get_attr(inode, file, NULL,
957 				    STATX_BASIC_STATS & ~STATX_ATIME, 0);
958 }
959 
960 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
961 			     u64 child_nodeid, struct qstr *name)
962 {
963 	int err = -ENOTDIR;
964 	struct inode *parent;
965 	struct dentry *dir;
966 	struct dentry *entry;
967 
968 	parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
969 	if (!parent)
970 		return -ENOENT;
971 
972 	inode_lock(parent);
973 	if (!S_ISDIR(parent->i_mode))
974 		goto unlock;
975 
976 	err = -ENOENT;
977 	dir = d_find_alias(parent);
978 	if (!dir)
979 		goto unlock;
980 
981 	name->hash = full_name_hash(dir, name->name, name->len);
982 	entry = d_lookup(dir, name);
983 	dput(dir);
984 	if (!entry)
985 		goto unlock;
986 
987 	fuse_dir_changed(parent);
988 	fuse_invalidate_entry(entry);
989 
990 	if (child_nodeid != 0 && d_really_is_positive(entry)) {
991 		inode_lock(d_inode(entry));
992 		if (get_node_id(d_inode(entry)) != child_nodeid) {
993 			err = -ENOENT;
994 			goto badentry;
995 		}
996 		if (d_mountpoint(entry)) {
997 			err = -EBUSY;
998 			goto badentry;
999 		}
1000 		if (d_is_dir(entry)) {
1001 			shrink_dcache_parent(entry);
1002 			if (!simple_empty(entry)) {
1003 				err = -ENOTEMPTY;
1004 				goto badentry;
1005 			}
1006 			d_inode(entry)->i_flags |= S_DEAD;
1007 		}
1008 		dont_mount(entry);
1009 		clear_nlink(d_inode(entry));
1010 		err = 0;
1011  badentry:
1012 		inode_unlock(d_inode(entry));
1013 		if (!err)
1014 			d_delete(entry);
1015 	} else {
1016 		err = 0;
1017 	}
1018 	dput(entry);
1019 
1020  unlock:
1021 	inode_unlock(parent);
1022 	iput(parent);
1023 	return err;
1024 }
1025 
1026 /*
1027  * Calling into a user-controlled filesystem gives the filesystem
1028  * daemon ptrace-like capabilities over the current process.  This
1029  * means, that the filesystem daemon is able to record the exact
1030  * filesystem operations performed, and can also control the behavior
1031  * of the requester process in otherwise impossible ways.  For example
1032  * it can delay the operation for arbitrary length of time allowing
1033  * DoS against the requester.
1034  *
1035  * For this reason only those processes can call into the filesystem,
1036  * for which the owner of the mount has ptrace privilege.  This
1037  * excludes processes started by other users, suid or sgid processes.
1038  */
1039 int fuse_allow_current_process(struct fuse_conn *fc)
1040 {
1041 	const struct cred *cred;
1042 
1043 	if (fc->allow_other)
1044 		return current_in_userns(fc->user_ns);
1045 
1046 	cred = current_cred();
1047 	if (uid_eq(cred->euid, fc->user_id) &&
1048 	    uid_eq(cred->suid, fc->user_id) &&
1049 	    uid_eq(cred->uid,  fc->user_id) &&
1050 	    gid_eq(cred->egid, fc->group_id) &&
1051 	    gid_eq(cred->sgid, fc->group_id) &&
1052 	    gid_eq(cred->gid,  fc->group_id))
1053 		return 1;
1054 
1055 	return 0;
1056 }
1057 
1058 static int fuse_access(struct inode *inode, int mask)
1059 {
1060 	struct fuse_conn *fc = get_fuse_conn(inode);
1061 	FUSE_ARGS(args);
1062 	struct fuse_access_in inarg;
1063 	int err;
1064 
1065 	BUG_ON(mask & MAY_NOT_BLOCK);
1066 
1067 	if (fc->no_access)
1068 		return 0;
1069 
1070 	memset(&inarg, 0, sizeof(inarg));
1071 	inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1072 	args.in.h.opcode = FUSE_ACCESS;
1073 	args.in.h.nodeid = get_node_id(inode);
1074 	args.in.numargs = 1;
1075 	args.in.args[0].size = sizeof(inarg);
1076 	args.in.args[0].value = &inarg;
1077 	err = fuse_simple_request(fc, &args);
1078 	if (err == -ENOSYS) {
1079 		fc->no_access = 1;
1080 		err = 0;
1081 	}
1082 	return err;
1083 }
1084 
1085 static int fuse_perm_getattr(struct inode *inode, int mask)
1086 {
1087 	if (mask & MAY_NOT_BLOCK)
1088 		return -ECHILD;
1089 
1090 	forget_all_cached_acls(inode);
1091 	return fuse_do_getattr(inode, NULL, NULL);
1092 }
1093 
1094 /*
1095  * Check permission.  The two basic access models of FUSE are:
1096  *
1097  * 1) Local access checking ('default_permissions' mount option) based
1098  * on file mode.  This is the plain old disk filesystem permission
1099  * modell.
1100  *
1101  * 2) "Remote" access checking, where server is responsible for
1102  * checking permission in each inode operation.  An exception to this
1103  * is if ->permission() was invoked from sys_access() in which case an
1104  * access request is sent.  Execute permission is still checked
1105  * locally based on file mode.
1106  */
1107 static int fuse_permission(struct inode *inode, int mask)
1108 {
1109 	struct fuse_conn *fc = get_fuse_conn(inode);
1110 	bool refreshed = false;
1111 	int err = 0;
1112 
1113 	if (!fuse_allow_current_process(fc))
1114 		return -EACCES;
1115 
1116 	/*
1117 	 * If attributes are needed, refresh them before proceeding
1118 	 */
1119 	if (fc->default_permissions ||
1120 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1121 		struct fuse_inode *fi = get_fuse_inode(inode);
1122 
1123 		if (time_before64(fi->i_time, get_jiffies_64())) {
1124 			refreshed = true;
1125 
1126 			err = fuse_perm_getattr(inode, mask);
1127 			if (err)
1128 				return err;
1129 		}
1130 	}
1131 
1132 	if (fc->default_permissions) {
1133 		err = generic_permission(inode, mask);
1134 
1135 		/* If permission is denied, try to refresh file
1136 		   attributes.  This is also needed, because the root
1137 		   node will at first have no permissions */
1138 		if (err == -EACCES && !refreshed) {
1139 			err = fuse_perm_getattr(inode, mask);
1140 			if (!err)
1141 				err = generic_permission(inode, mask);
1142 		}
1143 
1144 		/* Note: the opposite of the above test does not
1145 		   exist.  So if permissions are revoked this won't be
1146 		   noticed immediately, only after the attribute
1147 		   timeout has expired */
1148 	} else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1149 		err = fuse_access(inode, mask);
1150 	} else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1151 		if (!(inode->i_mode & S_IXUGO)) {
1152 			if (refreshed)
1153 				return -EACCES;
1154 
1155 			err = fuse_perm_getattr(inode, mask);
1156 			if (!err && !(inode->i_mode & S_IXUGO))
1157 				return -EACCES;
1158 		}
1159 	}
1160 	return err;
1161 }
1162 
1163 static int fuse_readlink_page(struct inode *inode, struct page *page)
1164 {
1165 	struct fuse_conn *fc = get_fuse_conn(inode);
1166 	struct fuse_req *req;
1167 	int err;
1168 
1169 	req = fuse_get_req(fc, 1);
1170 	if (IS_ERR(req))
1171 		return PTR_ERR(req);
1172 
1173 	req->out.page_zeroing = 1;
1174 	req->out.argpages = 1;
1175 	req->num_pages = 1;
1176 	req->pages[0] = page;
1177 	req->page_descs[0].length = PAGE_SIZE - 1;
1178 	req->in.h.opcode = FUSE_READLINK;
1179 	req->in.h.nodeid = get_node_id(inode);
1180 	req->out.argvar = 1;
1181 	req->out.numargs = 1;
1182 	req->out.args[0].size = PAGE_SIZE - 1;
1183 	fuse_request_send(fc, req);
1184 	err = req->out.h.error;
1185 
1186 	if (!err) {
1187 		char *link = page_address(page);
1188 		size_t len = req->out.args[0].size;
1189 
1190 		BUG_ON(len >= PAGE_SIZE);
1191 		link[len] = '\0';
1192 	}
1193 
1194 	fuse_put_request(fc, req);
1195 	fuse_invalidate_atime(inode);
1196 
1197 	return err;
1198 }
1199 
1200 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1201 				 struct delayed_call *callback)
1202 {
1203 	struct fuse_conn *fc = get_fuse_conn(inode);
1204 	struct page *page;
1205 	int err;
1206 
1207 	err = -EIO;
1208 	if (is_bad_inode(inode))
1209 		goto out_err;
1210 
1211 	if (fc->cache_symlinks)
1212 		return page_get_link(dentry, inode, callback);
1213 
1214 	err = -ECHILD;
1215 	if (!dentry)
1216 		goto out_err;
1217 
1218 	page = alloc_page(GFP_KERNEL);
1219 	err = -ENOMEM;
1220 	if (!page)
1221 		goto out_err;
1222 
1223 	err = fuse_readlink_page(inode, page);
1224 	if (err) {
1225 		__free_page(page);
1226 		goto out_err;
1227 	}
1228 
1229 	set_delayed_call(callback, page_put_link, page);
1230 
1231 	return page_address(page);
1232 
1233 out_err:
1234 	return ERR_PTR(err);
1235 }
1236 
1237 static int fuse_dir_open(struct inode *inode, struct file *file)
1238 {
1239 	return fuse_open_common(inode, file, true);
1240 }
1241 
1242 static int fuse_dir_release(struct inode *inode, struct file *file)
1243 {
1244 	fuse_release_common(file, FUSE_RELEASEDIR);
1245 
1246 	return 0;
1247 }
1248 
1249 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1250 			  int datasync)
1251 {
1252 	return fuse_fsync_common(file, start, end, datasync, 1);
1253 }
1254 
1255 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1256 			    unsigned long arg)
1257 {
1258 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1259 
1260 	/* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1261 	if (fc->minor < 18)
1262 		return -ENOTTY;
1263 
1264 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1265 }
1266 
1267 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1268 				   unsigned long arg)
1269 {
1270 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1271 
1272 	if (fc->minor < 18)
1273 		return -ENOTTY;
1274 
1275 	return fuse_ioctl_common(file, cmd, arg,
1276 				 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1277 }
1278 
1279 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1280 {
1281 	/* Always update if mtime is explicitly set  */
1282 	if (ivalid & ATTR_MTIME_SET)
1283 		return true;
1284 
1285 	/* Or if kernel i_mtime is the official one */
1286 	if (trust_local_mtime)
1287 		return true;
1288 
1289 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1290 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1291 		return false;
1292 
1293 	/* In all other cases update */
1294 	return true;
1295 }
1296 
1297 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1298 			   struct fuse_setattr_in *arg, bool trust_local_cmtime)
1299 {
1300 	unsigned ivalid = iattr->ia_valid;
1301 
1302 	if (ivalid & ATTR_MODE)
1303 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1304 	if (ivalid & ATTR_UID)
1305 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1306 	if (ivalid & ATTR_GID)
1307 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1308 	if (ivalid & ATTR_SIZE)
1309 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1310 	if (ivalid & ATTR_ATIME) {
1311 		arg->valid |= FATTR_ATIME;
1312 		arg->atime = iattr->ia_atime.tv_sec;
1313 		arg->atimensec = iattr->ia_atime.tv_nsec;
1314 		if (!(ivalid & ATTR_ATIME_SET))
1315 			arg->valid |= FATTR_ATIME_NOW;
1316 	}
1317 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1318 		arg->valid |= FATTR_MTIME;
1319 		arg->mtime = iattr->ia_mtime.tv_sec;
1320 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1321 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1322 			arg->valid |= FATTR_MTIME_NOW;
1323 	}
1324 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1325 		arg->valid |= FATTR_CTIME;
1326 		arg->ctime = iattr->ia_ctime.tv_sec;
1327 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1328 	}
1329 }
1330 
1331 /*
1332  * Prevent concurrent writepages on inode
1333  *
1334  * This is done by adding a negative bias to the inode write counter
1335  * and waiting for all pending writes to finish.
1336  */
1337 void fuse_set_nowrite(struct inode *inode)
1338 {
1339 	struct fuse_conn *fc = get_fuse_conn(inode);
1340 	struct fuse_inode *fi = get_fuse_inode(inode);
1341 
1342 	BUG_ON(!inode_is_locked(inode));
1343 
1344 	spin_lock(&fc->lock);
1345 	BUG_ON(fi->writectr < 0);
1346 	fi->writectr += FUSE_NOWRITE;
1347 	spin_unlock(&fc->lock);
1348 	wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1349 }
1350 
1351 /*
1352  * Allow writepages on inode
1353  *
1354  * Remove the bias from the writecounter and send any queued
1355  * writepages.
1356  */
1357 static void __fuse_release_nowrite(struct inode *inode)
1358 {
1359 	struct fuse_inode *fi = get_fuse_inode(inode);
1360 
1361 	BUG_ON(fi->writectr != FUSE_NOWRITE);
1362 	fi->writectr = 0;
1363 	fuse_flush_writepages(inode);
1364 }
1365 
1366 void fuse_release_nowrite(struct inode *inode)
1367 {
1368 	struct fuse_conn *fc = get_fuse_conn(inode);
1369 
1370 	spin_lock(&fc->lock);
1371 	__fuse_release_nowrite(inode);
1372 	spin_unlock(&fc->lock);
1373 }
1374 
1375 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1376 			      struct inode *inode,
1377 			      struct fuse_setattr_in *inarg_p,
1378 			      struct fuse_attr_out *outarg_p)
1379 {
1380 	args->in.h.opcode = FUSE_SETATTR;
1381 	args->in.h.nodeid = get_node_id(inode);
1382 	args->in.numargs = 1;
1383 	args->in.args[0].size = sizeof(*inarg_p);
1384 	args->in.args[0].value = inarg_p;
1385 	args->out.numargs = 1;
1386 	args->out.args[0].size = sizeof(*outarg_p);
1387 	args->out.args[0].value = outarg_p;
1388 }
1389 
1390 /*
1391  * Flush inode->i_mtime to the server
1392  */
1393 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1394 {
1395 	struct fuse_conn *fc = get_fuse_conn(inode);
1396 	FUSE_ARGS(args);
1397 	struct fuse_setattr_in inarg;
1398 	struct fuse_attr_out outarg;
1399 
1400 	memset(&inarg, 0, sizeof(inarg));
1401 	memset(&outarg, 0, sizeof(outarg));
1402 
1403 	inarg.valid = FATTR_MTIME;
1404 	inarg.mtime = inode->i_mtime.tv_sec;
1405 	inarg.mtimensec = inode->i_mtime.tv_nsec;
1406 	if (fc->minor >= 23) {
1407 		inarg.valid |= FATTR_CTIME;
1408 		inarg.ctime = inode->i_ctime.tv_sec;
1409 		inarg.ctimensec = inode->i_ctime.tv_nsec;
1410 	}
1411 	if (ff) {
1412 		inarg.valid |= FATTR_FH;
1413 		inarg.fh = ff->fh;
1414 	}
1415 	fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1416 
1417 	return fuse_simple_request(fc, &args);
1418 }
1419 
1420 /*
1421  * Set attributes, and at the same time refresh them.
1422  *
1423  * Truncation is slightly complicated, because the 'truncate' request
1424  * may fail, in which case we don't want to touch the mapping.
1425  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1426  * and the actual truncation by hand.
1427  */
1428 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1429 		    struct file *file)
1430 {
1431 	struct inode *inode = d_inode(dentry);
1432 	struct fuse_conn *fc = get_fuse_conn(inode);
1433 	struct fuse_inode *fi = get_fuse_inode(inode);
1434 	FUSE_ARGS(args);
1435 	struct fuse_setattr_in inarg;
1436 	struct fuse_attr_out outarg;
1437 	bool is_truncate = false;
1438 	bool is_wb = fc->writeback_cache;
1439 	loff_t oldsize;
1440 	int err;
1441 	bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1442 
1443 	if (!fc->default_permissions)
1444 		attr->ia_valid |= ATTR_FORCE;
1445 
1446 	err = setattr_prepare(dentry, attr);
1447 	if (err)
1448 		return err;
1449 
1450 	if (attr->ia_valid & ATTR_OPEN) {
1451 		/* This is coming from open(..., ... | O_TRUNC); */
1452 		WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1453 		WARN_ON(attr->ia_size != 0);
1454 		if (fc->atomic_o_trunc) {
1455 			/*
1456 			 * No need to send request to userspace, since actual
1457 			 * truncation has already been done by OPEN.  But still
1458 			 * need to truncate page cache.
1459 			 */
1460 			i_size_write(inode, 0);
1461 			truncate_pagecache(inode, 0);
1462 			return 0;
1463 		}
1464 		file = NULL;
1465 	}
1466 
1467 	if (attr->ia_valid & ATTR_SIZE) {
1468 		if (WARN_ON(!S_ISREG(inode->i_mode)))
1469 			return -EIO;
1470 		is_truncate = true;
1471 	}
1472 
1473 	if (is_truncate) {
1474 		fuse_set_nowrite(inode);
1475 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1476 		if (trust_local_cmtime && attr->ia_size != inode->i_size)
1477 			attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1478 	}
1479 
1480 	memset(&inarg, 0, sizeof(inarg));
1481 	memset(&outarg, 0, sizeof(outarg));
1482 	iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1483 	if (file) {
1484 		struct fuse_file *ff = file->private_data;
1485 		inarg.valid |= FATTR_FH;
1486 		inarg.fh = ff->fh;
1487 	}
1488 	if (attr->ia_valid & ATTR_SIZE) {
1489 		/* For mandatory locking in truncate */
1490 		inarg.valid |= FATTR_LOCKOWNER;
1491 		inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1492 	}
1493 	fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1494 	err = fuse_simple_request(fc, &args);
1495 	if (err) {
1496 		if (err == -EINTR)
1497 			fuse_invalidate_attr(inode);
1498 		goto error;
1499 	}
1500 
1501 	if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1502 		make_bad_inode(inode);
1503 		err = -EIO;
1504 		goto error;
1505 	}
1506 
1507 	spin_lock(&fc->lock);
1508 	/* the kernel maintains i_mtime locally */
1509 	if (trust_local_cmtime) {
1510 		if (attr->ia_valid & ATTR_MTIME)
1511 			inode->i_mtime = attr->ia_mtime;
1512 		if (attr->ia_valid & ATTR_CTIME)
1513 			inode->i_ctime = attr->ia_ctime;
1514 		/* FIXME: clear I_DIRTY_SYNC? */
1515 	}
1516 
1517 	fuse_change_attributes_common(inode, &outarg.attr,
1518 				      attr_timeout(&outarg));
1519 	oldsize = inode->i_size;
1520 	/* see the comment in fuse_change_attributes() */
1521 	if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1522 		i_size_write(inode, outarg.attr.size);
1523 
1524 	if (is_truncate) {
1525 		/* NOTE: this may release/reacquire fc->lock */
1526 		__fuse_release_nowrite(inode);
1527 	}
1528 	spin_unlock(&fc->lock);
1529 
1530 	/*
1531 	 * Only call invalidate_inode_pages2() after removing
1532 	 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1533 	 */
1534 	if ((is_truncate || !is_wb) &&
1535 	    S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1536 		truncate_pagecache(inode, outarg.attr.size);
1537 		invalidate_inode_pages2(inode->i_mapping);
1538 	}
1539 
1540 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1541 	return 0;
1542 
1543 error:
1544 	if (is_truncate)
1545 		fuse_release_nowrite(inode);
1546 
1547 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1548 	return err;
1549 }
1550 
1551 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1552 {
1553 	struct inode *inode = d_inode(entry);
1554 	struct fuse_conn *fc = get_fuse_conn(inode);
1555 	struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1556 	int ret;
1557 
1558 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
1559 		return -EACCES;
1560 
1561 	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1562 		attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1563 				    ATTR_MODE);
1564 
1565 		/*
1566 		 * The only sane way to reliably kill suid/sgid is to do it in
1567 		 * the userspace filesystem
1568 		 *
1569 		 * This should be done on write(), truncate() and chown().
1570 		 */
1571 		if (!fc->handle_killpriv) {
1572 			/*
1573 			 * ia_mode calculation may have used stale i_mode.
1574 			 * Refresh and recalculate.
1575 			 */
1576 			ret = fuse_do_getattr(inode, NULL, file);
1577 			if (ret)
1578 				return ret;
1579 
1580 			attr->ia_mode = inode->i_mode;
1581 			if (inode->i_mode & S_ISUID) {
1582 				attr->ia_valid |= ATTR_MODE;
1583 				attr->ia_mode &= ~S_ISUID;
1584 			}
1585 			if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1586 				attr->ia_valid |= ATTR_MODE;
1587 				attr->ia_mode &= ~S_ISGID;
1588 			}
1589 		}
1590 	}
1591 	if (!attr->ia_valid)
1592 		return 0;
1593 
1594 	ret = fuse_do_setattr(entry, attr, file);
1595 	if (!ret) {
1596 		/*
1597 		 * If filesystem supports acls it may have updated acl xattrs in
1598 		 * the filesystem, so forget cached acls for the inode.
1599 		 */
1600 		if (fc->posix_acl)
1601 			forget_all_cached_acls(inode);
1602 
1603 		/* Directory mode changed, may need to revalidate access */
1604 		if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1605 			fuse_invalidate_entry_cache(entry);
1606 	}
1607 	return ret;
1608 }
1609 
1610 static int fuse_getattr(const struct path *path, struct kstat *stat,
1611 			u32 request_mask, unsigned int flags)
1612 {
1613 	struct inode *inode = d_inode(path->dentry);
1614 	struct fuse_conn *fc = get_fuse_conn(inode);
1615 
1616 	if (!fuse_allow_current_process(fc))
1617 		return -EACCES;
1618 
1619 	return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1620 }
1621 
1622 static const struct inode_operations fuse_dir_inode_operations = {
1623 	.lookup		= fuse_lookup,
1624 	.mkdir		= fuse_mkdir,
1625 	.symlink	= fuse_symlink,
1626 	.unlink		= fuse_unlink,
1627 	.rmdir		= fuse_rmdir,
1628 	.rename		= fuse_rename2,
1629 	.link		= fuse_link,
1630 	.setattr	= fuse_setattr,
1631 	.create		= fuse_create,
1632 	.atomic_open	= fuse_atomic_open,
1633 	.mknod		= fuse_mknod,
1634 	.permission	= fuse_permission,
1635 	.getattr	= fuse_getattr,
1636 	.listxattr	= fuse_listxattr,
1637 	.get_acl	= fuse_get_acl,
1638 	.set_acl	= fuse_set_acl,
1639 };
1640 
1641 static const struct file_operations fuse_dir_operations = {
1642 	.llseek		= generic_file_llseek,
1643 	.read		= generic_read_dir,
1644 	.iterate_shared	= fuse_readdir,
1645 	.open		= fuse_dir_open,
1646 	.release	= fuse_dir_release,
1647 	.fsync		= fuse_dir_fsync,
1648 	.unlocked_ioctl	= fuse_dir_ioctl,
1649 	.compat_ioctl	= fuse_dir_compat_ioctl,
1650 };
1651 
1652 static const struct inode_operations fuse_common_inode_operations = {
1653 	.setattr	= fuse_setattr,
1654 	.permission	= fuse_permission,
1655 	.getattr	= fuse_getattr,
1656 	.listxattr	= fuse_listxattr,
1657 	.get_acl	= fuse_get_acl,
1658 	.set_acl	= fuse_set_acl,
1659 };
1660 
1661 static const struct inode_operations fuse_symlink_inode_operations = {
1662 	.setattr	= fuse_setattr,
1663 	.get_link	= fuse_get_link,
1664 	.getattr	= fuse_getattr,
1665 	.listxattr	= fuse_listxattr,
1666 };
1667 
1668 void fuse_init_common(struct inode *inode)
1669 {
1670 	inode->i_op = &fuse_common_inode_operations;
1671 }
1672 
1673 void fuse_init_dir(struct inode *inode)
1674 {
1675 	struct fuse_inode *fi = get_fuse_inode(inode);
1676 
1677 	inode->i_op = &fuse_dir_inode_operations;
1678 	inode->i_fop = &fuse_dir_operations;
1679 
1680 	spin_lock_init(&fi->rdc.lock);
1681 	fi->rdc.cached = false;
1682 	fi->rdc.size = 0;
1683 	fi->rdc.pos = 0;
1684 	fi->rdc.version = 0;
1685 }
1686 
1687 static int fuse_symlink_readpage(struct file *null, struct page *page)
1688 {
1689 	int err = fuse_readlink_page(page->mapping->host, page);
1690 
1691 	if (!err)
1692 		SetPageUptodate(page);
1693 
1694 	unlock_page(page);
1695 
1696 	return err;
1697 }
1698 
1699 static const struct address_space_operations fuse_symlink_aops = {
1700 	.readpage	= fuse_symlink_readpage,
1701 };
1702 
1703 void fuse_init_symlink(struct inode *inode)
1704 {
1705 	inode->i_op = &fuse_symlink_inode_operations;
1706 	inode->i_data.a_ops = &fuse_symlink_aops;
1707 	inode_nohighmem(inode);
1708 }
1709